1/******************************************************************************
2 *
3 *  Copyright (C) 2014 Google, Inc.
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18
19#include <stdlib.h>
20#include <string.h>
21
22#include "osi/include/allocator.h"
23#include "osi/include/allocation_tracker.h"
24
25static const allocator_id_t alloc_allocator_id = 42;
26
27char *osi_strdup(const char *str) {
28  size_t size = strlen(str) + 1;  // + 1 for the null terminator
29  size_t real_size = allocation_tracker_resize_for_canary(size);
30
31  char *new_string = allocation_tracker_notify_alloc(
32    alloc_allocator_id,
33    malloc(real_size),
34    size);
35  if (!new_string)
36    return NULL;
37
38  memcpy(new_string, str, size);
39  return new_string;
40}
41
42void *osi_malloc(size_t size) {
43  size_t real_size = allocation_tracker_resize_for_canary(size);
44  return allocation_tracker_notify_alloc(
45    alloc_allocator_id,
46    malloc(real_size),
47    size);
48}
49
50void *osi_calloc(size_t size) {
51  size_t real_size = allocation_tracker_resize_for_canary(size);
52  return allocation_tracker_notify_alloc(
53    alloc_allocator_id,
54    calloc(1, real_size),
55    size);
56}
57
58void osi_free(void *ptr) {
59  free(allocation_tracker_notify_free(alloc_allocator_id, ptr));
60}
61
62const allocator_t allocator_malloc = {
63  osi_malloc,
64  osi_free
65};
66
67const allocator_t allocator_calloc = {
68  osi_calloc,
69  osi_free
70};
71