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