Zach Johnson | dcbfea8 | 2014-08-15 16:39:33 -0700 | [diff] [blame] | 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 | |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include "allocator.h" |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 22 | #include "allocation_tracker.h" |
| 23 | |
Zach Johnson | 4ed68b4 | 2014-08-29 17:08:44 -0700 | [diff] [blame] | 24 | static const allocator_id_t alloc_allocator_id = 42; |
| 25 | |
Zach Johnson | 0812fe3 | 2014-08-26 20:15:19 -0700 | [diff] [blame] | 26 | char *osi_strdup(const char *str) { |
Sharvil Nanavati | c0745da | 2014-11-13 01:04:19 -0800 | [diff] [blame^] | 27 | 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 Johnson | 4ed68b4 | 2014-08-29 17:08:44 -0700 | [diff] [blame] | 31 | alloc_allocator_id, |
Sharvil Nanavati | c0745da | 2014-11-13 01:04:19 -0800 | [diff] [blame^] | 32 | malloc(real_size), |
| 33 | size); |
| 34 | if (!new_string) |
| 35 | return NULL; |
| 36 | |
| 37 | memcpy(new_string, str, size); |
| 38 | return new_string; |
Zach Johnson | 0812fe3 | 2014-08-26 20:15:19 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 41 | void *osi_malloc(size_t size) { |
Zach Johnson | f947fdd | 2014-08-28 13:30:17 -0700 | [diff] [blame] | 42 | size_t real_size = allocation_tracker_resize_for_canary(size); |
Zach Johnson | 4ed68b4 | 2014-08-29 17:08:44 -0700 | [diff] [blame] | 43 | return allocation_tracker_notify_alloc( |
| 44 | alloc_allocator_id, |
| 45 | malloc(real_size), |
Sharvil Nanavati | c0745da | 2014-11-13 01:04:19 -0800 | [diff] [blame^] | 46 | size); |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void *osi_calloc(size_t size) { |
Zach Johnson | f947fdd | 2014-08-28 13:30:17 -0700 | [diff] [blame] | 50 | size_t real_size = allocation_tracker_resize_for_canary(size); |
Zach Johnson | 4ed68b4 | 2014-08-29 17:08:44 -0700 | [diff] [blame] | 51 | return allocation_tracker_notify_alloc( |
| 52 | alloc_allocator_id, |
| 53 | calloc(1, real_size), |
Sharvil Nanavati | c0745da | 2014-11-13 01:04:19 -0800 | [diff] [blame^] | 54 | size); |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void osi_free(void *ptr) { |
Zach Johnson | 4ed68b4 | 2014-08-29 17:08:44 -0700 | [diff] [blame] | 58 | free(allocation_tracker_notify_free(alloc_allocator_id, ptr)); |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 59 | } |
Zach Johnson | dcbfea8 | 2014-08-15 16:39:33 -0700 | [diff] [blame] | 60 | |
| 61 | const allocator_t allocator_malloc = { |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 62 | osi_malloc, |
| 63 | osi_free |
| 64 | }; |
| 65 | |
| 66 | const allocator_t allocator_calloc = { |
| 67 | osi_calloc, |
| 68 | osi_free |
Zach Johnson | dcbfea8 | 2014-08-15 16:39:33 -0700 | [diff] [blame] | 69 | }; |