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 #include <stdlib.h>
19 #include <string.h>
20
21 #include "bt_common.h"
22 #include "osi/allocator.h"
23
24 extern void *pvPortZalloc(size_t size);
25 extern void vPortFree(void *pv);
26
27
28 #if HEAP_MEMORY_DEBUG
29
30 #define OSI_MEM_DBG_INFO_MAX 1024*3
31 typedef struct {
32 void *p;
33 int size;
34 const char *func;
35 int line;
36 } osi_mem_dbg_info_t;
37
38 static uint32_t mem_dbg_count = 0;
39 static osi_mem_dbg_info_t mem_dbg_info[OSI_MEM_DBG_INFO_MAX];
40 static uint32_t mem_dbg_current_size = 0;
41 static uint32_t mem_dbg_max_size = 0;
42
43 #define OSI_MEM_DBG_MAX_SECTION_NUM 5
44 typedef struct {
45 bool used;
46 uint32_t max_size;
47 } osi_mem_dbg_max_size_section_t;
48 static osi_mem_dbg_max_size_section_t mem_dbg_max_size_section[OSI_MEM_DBG_MAX_SECTION_NUM];
49
osi_mem_dbg_init(void)50 void osi_mem_dbg_init(void)
51 {
52 int i;
53
54 for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) {
55 mem_dbg_info[i].p = NULL;
56 mem_dbg_info[i].size = 0;
57 mem_dbg_info[i].func = NULL;
58 mem_dbg_info[i].line = 0;
59 }
60 mem_dbg_count = 0;
61 mem_dbg_current_size = 0;
62 mem_dbg_max_size = 0;
63
64 for (i = 0; i < OSI_MEM_DBG_MAX_SECTION_NUM; i++){
65 mem_dbg_max_size_section[i].used = false;
66 mem_dbg_max_size_section[i].max_size = 0;
67 }
68 }
69
osi_mem_dbg_record(void * p,int size,const char * func,int line)70 void osi_mem_dbg_record(void *p, int size, const char *func, int line)
71 {
72 int i;
73
74 if (!p || size == 0) {
75 OSI_TRACE_ERROR("%s invalid !!\n", __func__);
76 return;
77 }
78
79 for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) {
80 if (mem_dbg_info[i].p == NULL) {
81 mem_dbg_info[i].p = p;
82 mem_dbg_info[i].size = size;
83 mem_dbg_info[i].func = func;
84 mem_dbg_info[i].line = line;
85 mem_dbg_count++;
86 break;
87 }
88 }
89
90 if (i >= OSI_MEM_DBG_INFO_MAX) {
91 OSI_TRACE_ERROR("%s full %s %d !!\n", __func__, func, line);
92 }
93
94 mem_dbg_current_size += size;
95 if(mem_dbg_max_size < mem_dbg_current_size) {
96 mem_dbg_max_size = mem_dbg_current_size;
97 }
98
99 for (i = 0; i < OSI_MEM_DBG_MAX_SECTION_NUM; i++){
100 if (mem_dbg_max_size_section[i].used) {
101 if(mem_dbg_max_size_section[i].max_size < mem_dbg_current_size) {
102 mem_dbg_max_size_section[i].max_size = mem_dbg_current_size;
103 }
104 }
105 }
106 }
107
osi_mem_dbg_clean(void * p,const char * func,int line)108 void osi_mem_dbg_clean(void *p, const char *func, int line)
109 {
110 int i;
111
112 if (!p) {
113 OSI_TRACE_ERROR("%s invalid\n", __func__);
114 return;
115 }
116
117 for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) {
118 if (mem_dbg_info[i].p == p) {
119 mem_dbg_current_size -= mem_dbg_info[i].size;
120 mem_dbg_info[i].p = NULL;
121 mem_dbg_info[i].size = 0;
122 mem_dbg_info[i].func = NULL;
123 mem_dbg_info[i].line = 0;
124 mem_dbg_count--;
125 break;
126 }
127 }
128
129 if (i >= OSI_MEM_DBG_INFO_MAX) {
130 OSI_TRACE_ERROR("%s full %s %d !!\n", __func__, func, line);
131 }
132 }
133
osi_mem_dbg_show(void)134 void osi_mem_dbg_show(void)
135 {
136 int i;
137
138 for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) {
139 if (mem_dbg_info[i].p || mem_dbg_info[i].size != 0 ) {
140 OSI_TRACE_ERROR("--> p %p, s %d, f %s, l %d\n", mem_dbg_info[i].p, mem_dbg_info[i].size, mem_dbg_info[i].func, mem_dbg_info[i].line);
141 }
142 }
143 OSI_TRACE_ERROR("--> count %d\n", mem_dbg_count);
144 OSI_TRACE_ERROR("--> size %dB\n--> max size %dB\n", mem_dbg_current_size, mem_dbg_max_size);
145 }
146
osi_mem_dbg_get_max_size(void)147 uint32_t osi_mem_dbg_get_max_size(void)
148 {
149 return mem_dbg_max_size;
150 }
151
osi_mem_dbg_get_current_size(void)152 uint32_t osi_mem_dbg_get_current_size(void)
153 {
154 return mem_dbg_current_size;
155 }
156
osi_men_dbg_set_section_start(uint8_t index)157 void osi_men_dbg_set_section_start(uint8_t index)
158 {
159 if (index >= OSI_MEM_DBG_MAX_SECTION_NUM) {
160 OSI_TRACE_ERROR("Then range of index should be between 0 and %d, current index is %d.\n",
161 OSI_MEM_DBG_MAX_SECTION_NUM - 1, index);
162 return;
163 }
164
165 if (mem_dbg_max_size_section[index].used) {
166 OSI_TRACE_WARNING("This index(%d) has been started, restart it.\n", index);
167 }
168
169 mem_dbg_max_size_section[index].used = true;
170 mem_dbg_max_size_section[index].max_size = mem_dbg_current_size;
171 }
172
osi_men_dbg_set_section_end(uint8_t index)173 void osi_men_dbg_set_section_end(uint8_t index)
174 {
175 if (index >= OSI_MEM_DBG_MAX_SECTION_NUM) {
176 OSI_TRACE_ERROR("Then range of index should be between 0 and %d, current index is %d.\n",
177 OSI_MEM_DBG_MAX_SECTION_NUM - 1, index);
178 return;
179 }
180
181 if (!mem_dbg_max_size_section[index].used) {
182 OSI_TRACE_ERROR("This index(%d) has not been started.\n", index);
183 return;
184 }
185
186 mem_dbg_max_size_section[index].used = false;
187 }
188
osi_mem_dbg_get_max_size_section(uint8_t index)189 uint32_t osi_mem_dbg_get_max_size_section(uint8_t index)
190 {
191 if (index >= OSI_MEM_DBG_MAX_SECTION_NUM){
192 OSI_TRACE_ERROR("Then range of index should be between 0 and %d, current index is %d.\n",
193 OSI_MEM_DBG_MAX_SECTION_NUM - 1, index);
194 return 0;
195 }
196
197 return mem_dbg_max_size_section[index].max_size;
198 }
199 #endif
200
osi_strdup(const char * str)201 char *osi_strdup(const char *str)
202 {
203 size_t size = strlen(str) + 1; // + 1 for the null terminator
204 char *new_string = (char *)osi_calloc(size);
205
206 if (!new_string) {
207 return NULL;
208 }
209
210 memcpy(new_string, str, size);
211 return new_string;
212 }
213
osi_malloc_func(size_t size)214 void *osi_malloc_func(size_t size)
215 {
216 #if HEAP_MEMORY_DEBUG
217 void *p;
218 #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
219 p = heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
220 #else
221 p = malloc(size);
222 #endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
223 osi_mem_dbg_record(p, size, __func__, __LINE__);
224 return p;
225 #else
226 #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
227 return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
228 #else
229 return malloc(size);
230 #endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
231 #endif /* #if HEAP_MEMORY_DEBUG */
232 }
233
osi_calloc_func(size_t size)234 void *osi_calloc_func(size_t size)
235 {
236 #if HEAP_MEMORY_DEBUG
237 void *p;
238 #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
239 p = heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
240 #else
241 p = calloc(1, size);
242 #endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
243 osi_mem_dbg_record(p, size, __func__, __LINE__);
244 return p;
245 #else
246 #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
247 return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
248 #else
249 return calloc(1, size);
250 #endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
251 #endif /* #if HEAP_MEMORY_DEBUG */
252 }
253
osi_free_func(void * ptr)254 void osi_free_func(void *ptr)
255 {
256 #if HEAP_MEMORY_DEBUG
257 osi_mem_dbg_clean(ptr, __func__, __LINE__);
258 #endif
259 free(ptr);
260 }
261