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 #include <assert.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include "osi/include/allocator.h"
23 #include "osi/include/allocation_tracker.h"
24 
25 static const allocator_id_t alloc_allocator_id = 42;
26 
osi_strdup(const char * str)27 char *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   void *ptr = malloc(real_size);
31   assert(ptr);
32 
33   char *new_string = allocation_tracker_notify_alloc(
34       alloc_allocator_id,
35       ptr,
36       size);
37   if (!new_string)
38     return NULL;
39 
40   memcpy(new_string, str, size);
41   return new_string;
42 }
43 
osi_strndup(const char * str,size_t len)44 char *osi_strndup(const char *str, size_t len) {
45   size_t size = strlen(str);
46   if (len < size)
47     size = len;
48 
49   size_t real_size = allocation_tracker_resize_for_canary(size + 1);
50   void *ptr = malloc(real_size);
51   assert(ptr);
52 
53   char *new_string = allocation_tracker_notify_alloc(
54       alloc_allocator_id,
55       ptr,
56       size + 1);
57   if (!new_string)
58     return NULL;
59 
60   memcpy(new_string, str, size);
61   new_string[size] = '\0';
62   return new_string;
63 }
64 
osi_malloc(size_t size)65 void *osi_malloc(size_t size) {
66   size_t real_size = allocation_tracker_resize_for_canary(size);
67   void *ptr = malloc(real_size);
68   assert(ptr);
69   return allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size);
70 }
71 
osi_calloc(size_t size)72 void *osi_calloc(size_t size) {
73   size_t real_size = allocation_tracker_resize_for_canary(size);
74   void *ptr = calloc(1, real_size);
75   assert(ptr);
76   return allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size);
77 }
78 
osi_free(void * ptr)79 void osi_free(void *ptr) {
80   free(allocation_tracker_notify_free(alloc_allocator_id, ptr));
81 }
82 
osi_free_and_reset(void ** p_ptr)83 void osi_free_and_reset(void **p_ptr)
84 {
85   assert(p_ptr != NULL);
86   osi_free(*p_ptr);
87   *p_ptr = NULL;
88 }
89 
90 const allocator_t allocator_calloc = {
91   osi_calloc,
92   osi_free
93 };
94 
95 const allocator_t allocator_malloc = {
96   osi_malloc,
97   osi_free
98 };
99