blob: 1c0449e148d86674fbfcf6c2fdd517d972020fcd [file] [log] [blame]
Zach Johnsondcbfea82014-08-15 16:39:33 -07001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2014 Google, Inc.
Zach Johnsondcbfea82014-08-15 16:39:33 -07004 *
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 ******************************************************************************/
Jack Hef2af1c42016-12-13 01:59:12 -080018#include <base/logging.h>
Zach Johnsondcbfea82014-08-15 16:39:33 -070019#include <stdlib.h>
Etan Cohen3e59b5b2015-03-31 17:15:53 -070020#include <string.h>
Zach Johnsondcbfea82014-08-15 16:39:33 -070021
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070022#include "osi/include/allocation_tracker.h"
Myles Watsonb55040c2016-10-19 13:15:34 -070023#include "osi/include/allocator.h"
Zach Johnson3b72a142014-08-25 16:44:56 -070024
Zach Johnson4ed68b42014-08-29 17:08:44 -070025static const allocator_id_t alloc_allocator_id = 42;
26
Myles Watsonb55040c2016-10-19 13:15:34 -070027char* osi_strdup(const char* str) {
Sharvil Nanavatic0745da2014-11-13 01:04:19 -080028 size_t size = strlen(str) + 1; // + 1 for the null terminator
29 size_t real_size = allocation_tracker_resize_for_canary(size);
Myles Watsonb55040c2016-10-19 13:15:34 -070030 void* ptr = malloc(real_size);
Jack Hef2af1c42016-12-13 01:59:12 -080031 CHECK(ptr);
Sharvil Nanavatic0745da2014-11-13 01:04:19 -080032
Myles Watsonb55040c2016-10-19 13:15:34 -070033 char* new_string = static_cast<char*>(
34 allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size));
35 if (!new_string) return NULL;
Sharvil Nanavatic0745da2014-11-13 01:04:19 -080036
37 memcpy(new_string, str, size);
38 return new_string;
Zach Johnson0812fe32014-08-26 20:15:19 -070039}
40
Myles Watsonb55040c2016-10-19 13:15:34 -070041char* osi_strndup(const char* str, size_t len) {
Miao Chou98a96412015-07-08 14:50:32 -070042 size_t size = strlen(str);
Myles Watsonb55040c2016-10-19 13:15:34 -070043 if (len < size) size = len;
Miao Chou98a96412015-07-08 14:50:32 -070044
45 size_t real_size = allocation_tracker_resize_for_canary(size + 1);
Myles Watsonb55040c2016-10-19 13:15:34 -070046 void* ptr = malloc(real_size);
Jack Hef2af1c42016-12-13 01:59:12 -080047 CHECK(ptr);
Miao Chou98a96412015-07-08 14:50:32 -070048
Myles Watsonb55040c2016-10-19 13:15:34 -070049 char* new_string = static_cast<char*>(
50 allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size + 1));
51 if (!new_string) return NULL;
Miao Chou98a96412015-07-08 14:50:32 -070052
53 memcpy(new_string, str, size);
54 new_string[size] = '\0';
55 return new_string;
56}
57
Myles Watsonb55040c2016-10-19 13:15:34 -070058void* osi_malloc(size_t size) {
Zach Johnsonf947fdd2014-08-28 13:30:17 -070059 size_t real_size = allocation_tracker_resize_for_canary(size);
Myles Watsonb55040c2016-10-19 13:15:34 -070060 void* ptr = malloc(real_size);
Jack Hef2af1c42016-12-13 01:59:12 -080061 CHECK(ptr);
Andre Eisenbachc02acb72016-01-11 12:24:14 -080062 return allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size);
Zach Johnson3b72a142014-08-25 16:44:56 -070063}
64
Myles Watsonb55040c2016-10-19 13:15:34 -070065void* osi_calloc(size_t size) {
Zach Johnsonf947fdd2014-08-28 13:30:17 -070066 size_t real_size = allocation_tracker_resize_for_canary(size);
Myles Watsonb55040c2016-10-19 13:15:34 -070067 void* ptr = calloc(1, real_size);
Jack Hef2af1c42016-12-13 01:59:12 -080068 CHECK(ptr);
Andre Eisenbachc02acb72016-01-11 12:24:14 -080069 return allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size);
Zach Johnson3b72a142014-08-25 16:44:56 -070070}
71
Myles Watsonb55040c2016-10-19 13:15:34 -070072void osi_free(void* ptr) {
Zach Johnson4ed68b42014-08-29 17:08:44 -070073 free(allocation_tracker_notify_free(alloc_allocator_id, ptr));
Zach Johnson3b72a142014-08-25 16:44:56 -070074}
Zach Johnsondcbfea82014-08-15 16:39:33 -070075
Myles Watsonb55040c2016-10-19 13:15:34 -070076void osi_free_and_reset(void** p_ptr) {
Jack Hef2af1c42016-12-13 01:59:12 -080077 CHECK(p_ptr != NULL);
Pavlin Radoslavov20524d32016-02-02 18:12:08 -080078 osi_free(*p_ptr);
79 *p_ptr = NULL;
80}
81
Myles Watsonb55040c2016-10-19 13:15:34 -070082const allocator_t allocator_calloc = {osi_calloc, osi_free};
Pavlin Radoslavov258c2532015-09-27 20:59:05 -070083
Myles Watsonb55040c2016-10-19 13:15:34 -070084const allocator_t allocator_malloc = {osi_malloc, osi_free};