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 | ******************************************************************************/ |
Pavlin Radoslavov | 258c253 | 2015-09-27 20:59:05 -0700 | [diff] [blame] | 18 | #include <assert.h> |
Zach Johnson | dcbfea8 | 2014-08-15 16:39:33 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
Etan Cohen | 3e59b5b | 2015-03-31 17:15:53 -0700 | [diff] [blame] | 20 | #include <string.h> |
Zach Johnson | dcbfea8 | 2014-08-15 16:39:33 -0700 | [diff] [blame] | 21 | |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 22 | #include "osi/include/allocator.h" |
| 23 | #include "osi/include/allocation_tracker.h" |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 24 | |
Zach Johnson | 4ed68b4 | 2014-08-29 17:08:44 -0700 | [diff] [blame] | 25 | static const allocator_id_t alloc_allocator_id = 42; |
| 26 | |
Zach Johnson | 0812fe3 | 2014-08-26 20:15:19 -0700 | [diff] [blame] | 27 | char *osi_strdup(const char *str) { |
Sharvil Nanavati | c0745da | 2014-11-13 01:04:19 -0800 | [diff] [blame] | 28 | size_t size = strlen(str) + 1; // + 1 for the null terminator |
| 29 | size_t real_size = allocation_tracker_resize_for_canary(size); |
Andre Eisenbach | c02acb7 | 2016-01-11 12:24:14 -0800 | [diff] [blame] | 30 | void *ptr = malloc(real_size); |
| 31 | assert(ptr); |
Sharvil Nanavati | c0745da | 2014-11-13 01:04:19 -0800 | [diff] [blame] | 32 | |
| 33 | char *new_string = allocation_tracker_notify_alloc( |
Miao Chou | 98a9641 | 2015-07-08 14:50:32 -0700 | [diff] [blame] | 34 | alloc_allocator_id, |
Andre Eisenbach | c02acb7 | 2016-01-11 12:24:14 -0800 | [diff] [blame] | 35 | ptr, |
Miao Chou | 98a9641 | 2015-07-08 14:50:32 -0700 | [diff] [blame] | 36 | size); |
Sharvil Nanavati | c0745da | 2014-11-13 01:04:19 -0800 | [diff] [blame] | 37 | if (!new_string) |
| 38 | return NULL; |
| 39 | |
| 40 | memcpy(new_string, str, size); |
| 41 | return new_string; |
Zach Johnson | 0812fe3 | 2014-08-26 20:15:19 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Miao Chou | 98a9641 | 2015-07-08 14:50:32 -0700 | [diff] [blame] | 44 | char *osi_strndup(const char *str, size_t len) { |
| 45 | size_t size = strlen(str); |
| 46 | if (len < size) |
| 47 | size = len; |
| 48 | |
| 49 | size_t real_size = allocation_tracker_resize_for_canary(size + 1); |
Andre Eisenbach | c02acb7 | 2016-01-11 12:24:14 -0800 | [diff] [blame] | 50 | void *ptr = malloc(real_size); |
| 51 | assert(ptr); |
Miao Chou | 98a9641 | 2015-07-08 14:50:32 -0700 | [diff] [blame] | 52 | |
| 53 | char *new_string = allocation_tracker_notify_alloc( |
| 54 | alloc_allocator_id, |
Andre Eisenbach | c02acb7 | 2016-01-11 12:24:14 -0800 | [diff] [blame] | 55 | ptr, |
Miao Chou | 98a9641 | 2015-07-08 14:50:32 -0700 | [diff] [blame] | 56 | size + 1); |
| 57 | if (!new_string) |
| 58 | return NULL; |
| 59 | |
| 60 | memcpy(new_string, str, size); |
| 61 | new_string[size] = '\0'; |
| 62 | return new_string; |
| 63 | } |
| 64 | |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 65 | void *osi_malloc(size_t size) { |
Zach Johnson | f947fdd | 2014-08-28 13:30:17 -0700 | [diff] [blame] | 66 | size_t real_size = allocation_tracker_resize_for_canary(size); |
Andre Eisenbach | c02acb7 | 2016-01-11 12:24:14 -0800 | [diff] [blame] | 67 | void *ptr = malloc(real_size); |
| 68 | assert(ptr); |
| 69 | return allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size); |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void *osi_calloc(size_t size) { |
Zach Johnson | f947fdd | 2014-08-28 13:30:17 -0700 | [diff] [blame] | 73 | size_t real_size = allocation_tracker_resize_for_canary(size); |
Andre Eisenbach | c02acb7 | 2016-01-11 12:24:14 -0800 | [diff] [blame] | 74 | void *ptr = calloc(1, real_size); |
| 75 | assert(ptr); |
| 76 | return allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size); |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void osi_free(void *ptr) { |
Zach Johnson | 4ed68b4 | 2014-08-29 17:08:44 -0700 | [diff] [blame] | 80 | free(allocation_tracker_notify_free(alloc_allocator_id, ptr)); |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 81 | } |
Zach Johnson | dcbfea8 | 2014-08-15 16:39:33 -0700 | [diff] [blame] | 82 | |
Pavlin Radoslavov | 20524d3 | 2016-02-02 18:12:08 -0800 | [diff] [blame^] | 83 | void osi_free_and_reset(void **p_ptr) |
| 84 | { |
| 85 | assert(p_ptr != NULL); |
| 86 | osi_free(*p_ptr); |
| 87 | *p_ptr = NULL; |
| 88 | } |
| 89 | |
Pavlin Radoslavov | 258c253 | 2015-09-27 20:59:05 -0700 | [diff] [blame] | 90 | const allocator_t allocator_calloc = { |
| 91 | osi_calloc, |
| 92 | osi_free |
| 93 | }; |
| 94 | |
Zach Johnson | dcbfea8 | 2014-08-15 16:39:33 -0700 | [diff] [blame] | 95 | const allocator_t allocator_malloc = { |
Zach Johnson | 3b72a14 | 2014-08-25 16:44:56 -0700 | [diff] [blame] | 96 | osi_malloc, |
| 97 | osi_free |
| 98 | }; |
| 99 | |
Pavlin Radoslavov | 258c253 | 2015-09-27 20:59:05 -0700 | [diff] [blame] | 100 | // |
| 101 | // TODO: Temporary buffer-allocation wrappers: should be removed |
| 102 | // |
| 103 | #define MAGIC_NUMBER 0xDDBADDBA |
| 104 | typedef struct _buffer_hdr |
| 105 | { |
| 106 | uint16_t size; |
| 107 | uint32_t magic_number; |
| 108 | } BUFFER_HDR_T; |
| 109 | |
| 110 | void *osi_getbuf(uint16_t size) |
| 111 | { |
| 112 | BUFFER_HDR_T *header = osi_malloc(size + sizeof(BUFFER_HDR_T)); |
| 113 | header->size = size; |
| 114 | header->magic_number = MAGIC_NUMBER; |
| 115 | return header + 1; |
| 116 | } |
| 117 | |
| 118 | void osi_freebuf(void *p_buf) |
| 119 | { |
Pavlin Radoslavov | 20524d3 | 2016-02-02 18:12:08 -0800 | [diff] [blame^] | 120 | if (p_buf == NULL) |
| 121 | return; |
| 122 | |
Pavlin Radoslavov | 258c253 | 2015-09-27 20:59:05 -0700 | [diff] [blame] | 123 | BUFFER_HDR_T *header = (BUFFER_HDR_T *)p_buf - 1; |
| 124 | assert(header->magic_number == MAGIC_NUMBER); |
| 125 | osi_free(header); |
| 126 | } |
| 127 | |
Pavlin Radoslavov | 20524d3 | 2016-02-02 18:12:08 -0800 | [diff] [blame^] | 128 | void osi_freebuf_and_reset(void **p_ptr) |
| 129 | { |
| 130 | assert(p_ptr != NULL); |
| 131 | osi_freebuf(*p_ptr); |
| 132 | *p_ptr = NULL; |
| 133 | } |
| 134 | |
Pavlin Radoslavov | 258c253 | 2015-09-27 20:59:05 -0700 | [diff] [blame] | 135 | uint16_t osi_get_buf_size(void *p_buf) |
| 136 | { |
| 137 | BUFFER_HDR_T *header = (BUFFER_HDR_T *)p_buf - 1; |
| 138 | assert(header->magic_number == MAGIC_NUMBER); |
| 139 | return header->size; |
| 140 | } |