1 /******************************************************************************
3 * Copyright (C) 2014 Google, Inc.
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
21 #include "osi/include/allocator.h"
22 #include "osi/include/allocation_tracker.h"
24 static const allocator_id_t alloc_allocator_id = 42;
26 char *osi_strdup(const char *str) {
27 size_t size = strlen(str) + 1; // + 1 for the null terminator
28 size_t real_size = allocation_tracker_resize_for_canary(size);
30 char *new_string = allocation_tracker_notify_alloc(
37 memcpy(new_string, str, size);
41 char *osi_strndup(const char *str, size_t len) {
42 size_t size = strlen(str);
46 size_t real_size = allocation_tracker_resize_for_canary(size + 1);
48 char *new_string = allocation_tracker_notify_alloc(
55 memcpy(new_string, str, size);
56 new_string[size] = '\0';
60 void *osi_malloc(size_t size) {
61 size_t real_size = allocation_tracker_resize_for_canary(size);
62 return allocation_tracker_notify_alloc(
68 void *osi_calloc(size_t size) {
69 size_t real_size = allocation_tracker_resize_for_canary(size);
70 return allocation_tracker_notify_alloc(
76 void osi_free(void *ptr) {
77 free(allocation_tracker_notify_free(alloc_allocator_id, ptr));
80 const allocator_t allocator_malloc = {
85 const allocator_t allocator_calloc = {