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