blob: 27080c7176a0f6e99bb2b0e088daa9fdebe8b402 [file] [log] [blame]
Zach Johnsondcbfea82014-08-15 16:39:33 -07001/******************************************************************************
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
19#include <stdlib.h>
20
21#include "allocator.h"
Zach Johnson3b72a142014-08-25 16:44:56 -070022#include "allocation_tracker.h"
23
Zach Johnson4ed68b42014-08-29 17:08:44 -070024static const allocator_id_t alloc_allocator_id = 42;
25
Zach Johnson0812fe32014-08-26 20:15:19 -070026char *osi_strdup(const char *str) {
Sharvil Nanavatic0745da2014-11-13 01:04:19 -080027 size_t size = strlen(str) + 1; // + 1 for the null terminator
28 size_t real_size = allocation_tracker_resize_for_canary(size);
29
30 char *new_string = allocation_tracker_notify_alloc(
Zach Johnson4ed68b42014-08-29 17:08:44 -070031 alloc_allocator_id,
Sharvil Nanavatic0745da2014-11-13 01:04:19 -080032 malloc(real_size),
33 size);
34 if (!new_string)
35 return NULL;
36
37 memcpy(new_string, str, size);
38 return new_string;
Zach Johnson0812fe32014-08-26 20:15:19 -070039}
40
Zach Johnson3b72a142014-08-25 16:44:56 -070041void *osi_malloc(size_t size) {
Zach Johnsonf947fdd2014-08-28 13:30:17 -070042 size_t real_size = allocation_tracker_resize_for_canary(size);
Zach Johnson4ed68b42014-08-29 17:08:44 -070043 return allocation_tracker_notify_alloc(
44 alloc_allocator_id,
45 malloc(real_size),
Sharvil Nanavatic0745da2014-11-13 01:04:19 -080046 size);
Zach Johnson3b72a142014-08-25 16:44:56 -070047}
48
49void *osi_calloc(size_t size) {
Zach Johnsonf947fdd2014-08-28 13:30:17 -070050 size_t real_size = allocation_tracker_resize_for_canary(size);
Zach Johnson4ed68b42014-08-29 17:08:44 -070051 return allocation_tracker_notify_alloc(
52 alloc_allocator_id,
53 calloc(1, real_size),
Sharvil Nanavatic0745da2014-11-13 01:04:19 -080054 size);
Zach Johnson3b72a142014-08-25 16:44:56 -070055}
56
57void osi_free(void *ptr) {
Zach Johnson4ed68b42014-08-29 17:08:44 -070058 free(allocation_tracker_notify_free(alloc_allocator_id, ptr));
Zach Johnson3b72a142014-08-25 16:44:56 -070059}
Zach Johnsondcbfea82014-08-15 16:39:33 -070060
61const allocator_t allocator_malloc = {
Zach Johnson3b72a142014-08-25 16:44:56 -070062 osi_malloc,
63 osi_free
64};
65
66const allocator_t allocator_calloc = {
67 osi_calloc,
68 osi_free
Zach Johnsondcbfea82014-08-15 16:39:33 -070069};