blob: ed29f9c0fd65ba1e45207254df743f1c221893a0 [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 ******************************************************************************/
Pavlin Radoslavov258c2532015-09-27 20:59:05 -070018#include <assert.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/allocator.h"
23#include "osi/include/allocation_tracker.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
Zach Johnson0812fe32014-08-26 20:15:19 -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);
Andre Eisenbachc02acb72016-01-11 12:24:14 -080030 void *ptr = malloc(real_size);
31 assert(ptr);
Sharvil Nanavatic0745da2014-11-13 01:04:19 -080032
33 char *new_string = allocation_tracker_notify_alloc(
Miao Chou98a96412015-07-08 14:50:32 -070034 alloc_allocator_id,
Andre Eisenbachc02acb72016-01-11 12:24:14 -080035 ptr,
Miao Chou98a96412015-07-08 14:50:32 -070036 size);
Sharvil Nanavatic0745da2014-11-13 01:04:19 -080037 if (!new_string)
38 return NULL;
39
40 memcpy(new_string, str, size);
41 return new_string;
Zach Johnson0812fe32014-08-26 20:15:19 -070042}
43
Miao Chou98a96412015-07-08 14:50:32 -070044char *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 Eisenbachc02acb72016-01-11 12:24:14 -080050 void *ptr = malloc(real_size);
51 assert(ptr);
Miao Chou98a96412015-07-08 14:50:32 -070052
53 char *new_string = allocation_tracker_notify_alloc(
54 alloc_allocator_id,
Andre Eisenbachc02acb72016-01-11 12:24:14 -080055 ptr,
Miao Chou98a96412015-07-08 14:50:32 -070056 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 Johnson3b72a142014-08-25 16:44:56 -070065void *osi_malloc(size_t size) {
Zach Johnsonf947fdd2014-08-28 13:30:17 -070066 size_t real_size = allocation_tracker_resize_for_canary(size);
Andre Eisenbachc02acb72016-01-11 12:24:14 -080067 void *ptr = malloc(real_size);
68 assert(ptr);
69 return allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size);
Zach Johnson3b72a142014-08-25 16:44:56 -070070}
71
72void *osi_calloc(size_t size) {
Zach Johnsonf947fdd2014-08-28 13:30:17 -070073 size_t real_size = allocation_tracker_resize_for_canary(size);
Andre Eisenbachc02acb72016-01-11 12:24:14 -080074 void *ptr = calloc(1, real_size);
75 assert(ptr);
76 return allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size);
Zach Johnson3b72a142014-08-25 16:44:56 -070077}
78
79void osi_free(void *ptr) {
Zach Johnson4ed68b42014-08-29 17:08:44 -070080 free(allocation_tracker_notify_free(alloc_allocator_id, ptr));
Zach Johnson3b72a142014-08-25 16:44:56 -070081}
Zach Johnsondcbfea82014-08-15 16:39:33 -070082
Pavlin Radoslavov20524d32016-02-02 18:12:08 -080083void 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 Radoslavov258c2532015-09-27 20:59:05 -070090const allocator_t allocator_calloc = {
91 osi_calloc,
92 osi_free
93};
94
Zach Johnsondcbfea82014-08-15 16:39:33 -070095const allocator_t allocator_malloc = {
Zach Johnson3b72a142014-08-25 16:44:56 -070096 osi_malloc,
97 osi_free
98};
99
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700100//
101// TODO: Temporary buffer-allocation wrappers: should be removed
102//
103#define MAGIC_NUMBER 0xDDBADDBA
104typedef struct _buffer_hdr
105{
106 uint16_t size;
107 uint32_t magic_number;
108} BUFFER_HDR_T;
109
110void *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
118void osi_freebuf(void *p_buf)
119{
Pavlin Radoslavov20524d32016-02-02 18:12:08 -0800120 if (p_buf == NULL)
121 return;
122
Pavlin Radoslavov258c2532015-09-27 20:59:05 -0700123 BUFFER_HDR_T *header = (BUFFER_HDR_T *)p_buf - 1;
124 assert(header->magic_number == MAGIC_NUMBER);
125 osi_free(header);
126}
127
Pavlin Radoslavov20524d32016-02-02 18:12:08 -0800128void 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 Radoslavov258c2532015-09-27 20:59:05 -0700135uint16_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}