xref: /btstack/src/btstack_memory.c (revision 2cc827d4920e09c7219a79fceb0577d37efd4025)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 
39 #define BTSTACK_FILE__ "btstack_memory.c"
40 
41 
42 /*
43  *  btstack_memory.c
44  *
45  *  @brief BTstack memory management via configurable memory pools
46  *
47  *  @note code generated by tool/btstack_memory_generator.py
48  *  @note returnes buffers are initialized with 0
49  *
50  */
51 
52 #include "btstack_memory.h"
53 #include "btstack_memory_pool.h"
54 #include "btstack_debug.h"
55 
56 #include <stdlib.h>
57 
58 #ifdef HAVE_MALLOC
59 typedef struct btstack_memory_buffer {
60     struct btstack_memory_buffer * next;
61     struct btstack_memory_buffer * prev;
62 } btstack_memory_buffer_t;
63 
64 typedef struct {
65     btstack_memory_buffer_t tracking;
66     void * pointer;
67 } test_buffer_t;
68 
69 static btstack_memory_buffer_t * btstack_memory_malloc_buffers;
70 static uint32_t btstack_memory_malloc_counter;
71 
72 static void btstack_memory_tracking_add(btstack_memory_buffer_t * buffer){
73     btstack_assert(buffer != NULL);
74     if (btstack_memory_malloc_buffers != NULL) {
75         // let current first item prev point to new first item
76         btstack_memory_malloc_buffers->prev = buffer;
77     }
78     buffer->prev = NULL;
79     buffer->next = btstack_memory_malloc_buffers;
80     btstack_memory_malloc_buffers = buffer;
81 
82     btstack_memory_malloc_counter++;
83 }
84 
85 static void btstack_memory_tracking_remove(btstack_memory_buffer_t * buffer){
86     btstack_assert(buffer != NULL);
87     if (buffer->prev == NULL){
88         // first item
89         btstack_memory_malloc_buffers = buffer->next;
90     } else {
91         buffer->prev->next = buffer->next;
92     }
93     if (buffer->next != NULL){
94         buffer->next->prev = buffer->prev;
95     }
96 
97     btstack_memory_malloc_counter--;
98 }
99 #endif
100 
101 void btstack_memory_deinit(void){
102 #ifdef HAVE_MALLOC
103     while (btstack_memory_malloc_buffers != NULL){
104         btstack_memory_buffer_t * buffer = btstack_memory_malloc_buffers;
105         btstack_memory_malloc_buffers = buffer->next;
106         free(buffer);
107     }
108     btstack_assert(btstack_memory_malloc_counter == 0);
109 #endif
110 }
111 
112 
113 // MARK: hci_connection_t
114 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_HCI_CONNECTIONS)
115     #if defined(MAX_NO_HCI_CONNECTIONS)
116         #error "Deprecated MAX_NO_HCI_CONNECTIONS defined instead of MAX_NR_HCI_CONNECTIONS. Please update your btstack_config.h to use MAX_NR_HCI_CONNECTIONS."
117     #else
118         #define MAX_NR_HCI_CONNECTIONS 0
119     #endif
120 #endif
121 
122 #ifdef MAX_NR_HCI_CONNECTIONS
123 #if MAX_NR_HCI_CONNECTIONS > 0
124 static hci_connection_t hci_connection_storage[MAX_NR_HCI_CONNECTIONS];
125 static btstack_memory_pool_t hci_connection_pool;
126 hci_connection_t * btstack_memory_hci_connection_get(void){
127     void * buffer = btstack_memory_pool_get(&hci_connection_pool);
128     if (buffer){
129         memset(buffer, 0, sizeof(hci_connection_t));
130     }
131     return (hci_connection_t *) buffer;
132 }
133 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
134     btstack_memory_pool_free(&hci_connection_pool, hci_connection);
135 }
136 #else
137 hci_connection_t * btstack_memory_hci_connection_get(void){
138     return NULL;
139 }
140 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
141     UNUSED(hci_connection);
142 };
143 #endif
144 #elif defined(HAVE_MALLOC)
145 
146 typedef struct {
147     btstack_memory_buffer_t tracking;
148     hci_connection_t data;
149 } btstack_memory_hci_connection_t;
150 
151 hci_connection_t * btstack_memory_hci_connection_get(void){
152     btstack_memory_hci_connection_t * buffer = (btstack_memory_hci_connection_t *) malloc(sizeof(btstack_memory_hci_connection_t));
153     if (buffer){
154         memset(buffer, 0, sizeof(btstack_memory_hci_connection_t));
155         btstack_memory_tracking_add(&buffer->tracking);
156         return &buffer->data;
157     } else {
158         return NULL;
159     }
160 }
161 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
162     // reconstruct buffer start
163     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) hci_connection)[-1];
164     btstack_memory_tracking_remove(buffer);
165     free(buffer);
166 }
167 #endif
168 
169 
170 
171 // MARK: l2cap_service_t
172 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_L2CAP_SERVICES)
173     #if defined(MAX_NO_L2CAP_SERVICES)
174         #error "Deprecated MAX_NO_L2CAP_SERVICES defined instead of MAX_NR_L2CAP_SERVICES. Please update your btstack_config.h to use MAX_NR_L2CAP_SERVICES."
175     #else
176         #define MAX_NR_L2CAP_SERVICES 0
177     #endif
178 #endif
179 
180 #ifdef MAX_NR_L2CAP_SERVICES
181 #if MAX_NR_L2CAP_SERVICES > 0
182 static l2cap_service_t l2cap_service_storage[MAX_NR_L2CAP_SERVICES];
183 static btstack_memory_pool_t l2cap_service_pool;
184 l2cap_service_t * btstack_memory_l2cap_service_get(void){
185     void * buffer = btstack_memory_pool_get(&l2cap_service_pool);
186     if (buffer){
187         memset(buffer, 0, sizeof(l2cap_service_t));
188     }
189     return (l2cap_service_t *) buffer;
190 }
191 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
192     btstack_memory_pool_free(&l2cap_service_pool, l2cap_service);
193 }
194 #else
195 l2cap_service_t * btstack_memory_l2cap_service_get(void){
196     return NULL;
197 }
198 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
199     UNUSED(l2cap_service);
200 };
201 #endif
202 #elif defined(HAVE_MALLOC)
203 
204 typedef struct {
205     btstack_memory_buffer_t tracking;
206     l2cap_service_t data;
207 } btstack_memory_l2cap_service_t;
208 
209 l2cap_service_t * btstack_memory_l2cap_service_get(void){
210     btstack_memory_l2cap_service_t * buffer = (btstack_memory_l2cap_service_t *) malloc(sizeof(btstack_memory_l2cap_service_t));
211     if (buffer){
212         memset(buffer, 0, sizeof(btstack_memory_l2cap_service_t));
213         btstack_memory_tracking_add(&buffer->tracking);
214         return &buffer->data;
215     } else {
216         return NULL;
217     }
218 }
219 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
220     // reconstruct buffer start
221     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) l2cap_service)[-1];
222     btstack_memory_tracking_remove(buffer);
223     free(buffer);
224 }
225 #endif
226 
227 
228 // MARK: l2cap_channel_t
229 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_L2CAP_CHANNELS)
230     #if defined(MAX_NO_L2CAP_CHANNELS)
231         #error "Deprecated MAX_NO_L2CAP_CHANNELS defined instead of MAX_NR_L2CAP_CHANNELS. Please update your btstack_config.h to use MAX_NR_L2CAP_CHANNELS."
232     #else
233         #define MAX_NR_L2CAP_CHANNELS 0
234     #endif
235 #endif
236 
237 #ifdef MAX_NR_L2CAP_CHANNELS
238 #if MAX_NR_L2CAP_CHANNELS > 0
239 static l2cap_channel_t l2cap_channel_storage[MAX_NR_L2CAP_CHANNELS];
240 static btstack_memory_pool_t l2cap_channel_pool;
241 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
242     void * buffer = btstack_memory_pool_get(&l2cap_channel_pool);
243     if (buffer){
244         memset(buffer, 0, sizeof(l2cap_channel_t));
245     }
246     return (l2cap_channel_t *) buffer;
247 }
248 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
249     btstack_memory_pool_free(&l2cap_channel_pool, l2cap_channel);
250 }
251 #else
252 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
253     return NULL;
254 }
255 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
256     UNUSED(l2cap_channel);
257 };
258 #endif
259 #elif defined(HAVE_MALLOC)
260 
261 typedef struct {
262     btstack_memory_buffer_t tracking;
263     l2cap_channel_t data;
264 } btstack_memory_l2cap_channel_t;
265 
266 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
267     btstack_memory_l2cap_channel_t * buffer = (btstack_memory_l2cap_channel_t *) malloc(sizeof(btstack_memory_l2cap_channel_t));
268     if (buffer){
269         memset(buffer, 0, sizeof(btstack_memory_l2cap_channel_t));
270         btstack_memory_tracking_add(&buffer->tracking);
271         return &buffer->data;
272     } else {
273         return NULL;
274     }
275 }
276 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
277     // reconstruct buffer start
278     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) l2cap_channel)[-1];
279     btstack_memory_tracking_remove(buffer);
280     free(buffer);
281 }
282 #endif
283 
284 
285 #ifdef ENABLE_CLASSIC
286 
287 // MARK: rfcomm_multiplexer_t
288 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_RFCOMM_MULTIPLEXERS)
289     #if defined(MAX_NO_RFCOMM_MULTIPLEXERS)
290         #error "Deprecated MAX_NO_RFCOMM_MULTIPLEXERS defined instead of MAX_NR_RFCOMM_MULTIPLEXERS. Please update your btstack_config.h to use MAX_NR_RFCOMM_MULTIPLEXERS."
291     #else
292         #define MAX_NR_RFCOMM_MULTIPLEXERS 0
293     #endif
294 #endif
295 
296 #ifdef MAX_NR_RFCOMM_MULTIPLEXERS
297 #if MAX_NR_RFCOMM_MULTIPLEXERS > 0
298 static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NR_RFCOMM_MULTIPLEXERS];
299 static btstack_memory_pool_t rfcomm_multiplexer_pool;
300 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
301     void * buffer = btstack_memory_pool_get(&rfcomm_multiplexer_pool);
302     if (buffer){
303         memset(buffer, 0, sizeof(rfcomm_multiplexer_t));
304     }
305     return (rfcomm_multiplexer_t *) buffer;
306 }
307 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
308     btstack_memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer);
309 }
310 #else
311 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
312     return NULL;
313 }
314 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
315     UNUSED(rfcomm_multiplexer);
316 };
317 #endif
318 #elif defined(HAVE_MALLOC)
319 
320 typedef struct {
321     btstack_memory_buffer_t tracking;
322     rfcomm_multiplexer_t data;
323 } btstack_memory_rfcomm_multiplexer_t;
324 
325 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
326     btstack_memory_rfcomm_multiplexer_t * buffer = (btstack_memory_rfcomm_multiplexer_t *) malloc(sizeof(btstack_memory_rfcomm_multiplexer_t));
327     if (buffer){
328         memset(buffer, 0, sizeof(btstack_memory_rfcomm_multiplexer_t));
329         btstack_memory_tracking_add(&buffer->tracking);
330         return &buffer->data;
331     } else {
332         return NULL;
333     }
334 }
335 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
336     // reconstruct buffer start
337     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) rfcomm_multiplexer)[-1];
338     btstack_memory_tracking_remove(buffer);
339     free(buffer);
340 }
341 #endif
342 
343 
344 // MARK: rfcomm_service_t
345 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_RFCOMM_SERVICES)
346     #if defined(MAX_NO_RFCOMM_SERVICES)
347         #error "Deprecated MAX_NO_RFCOMM_SERVICES defined instead of MAX_NR_RFCOMM_SERVICES. Please update your btstack_config.h to use MAX_NR_RFCOMM_SERVICES."
348     #else
349         #define MAX_NR_RFCOMM_SERVICES 0
350     #endif
351 #endif
352 
353 #ifdef MAX_NR_RFCOMM_SERVICES
354 #if MAX_NR_RFCOMM_SERVICES > 0
355 static rfcomm_service_t rfcomm_service_storage[MAX_NR_RFCOMM_SERVICES];
356 static btstack_memory_pool_t rfcomm_service_pool;
357 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
358     void * buffer = btstack_memory_pool_get(&rfcomm_service_pool);
359     if (buffer){
360         memset(buffer, 0, sizeof(rfcomm_service_t));
361     }
362     return (rfcomm_service_t *) buffer;
363 }
364 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
365     btstack_memory_pool_free(&rfcomm_service_pool, rfcomm_service);
366 }
367 #else
368 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
369     return NULL;
370 }
371 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
372     UNUSED(rfcomm_service);
373 };
374 #endif
375 #elif defined(HAVE_MALLOC)
376 
377 typedef struct {
378     btstack_memory_buffer_t tracking;
379     rfcomm_service_t data;
380 } btstack_memory_rfcomm_service_t;
381 
382 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
383     btstack_memory_rfcomm_service_t * buffer = (btstack_memory_rfcomm_service_t *) malloc(sizeof(btstack_memory_rfcomm_service_t));
384     if (buffer){
385         memset(buffer, 0, sizeof(btstack_memory_rfcomm_service_t));
386         btstack_memory_tracking_add(&buffer->tracking);
387         return &buffer->data;
388     } else {
389         return NULL;
390     }
391 }
392 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
393     // reconstruct buffer start
394     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) rfcomm_service)[-1];
395     btstack_memory_tracking_remove(buffer);
396     free(buffer);
397 }
398 #endif
399 
400 
401 // MARK: rfcomm_channel_t
402 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_RFCOMM_CHANNELS)
403     #if defined(MAX_NO_RFCOMM_CHANNELS)
404         #error "Deprecated MAX_NO_RFCOMM_CHANNELS defined instead of MAX_NR_RFCOMM_CHANNELS. Please update your btstack_config.h to use MAX_NR_RFCOMM_CHANNELS."
405     #else
406         #define MAX_NR_RFCOMM_CHANNELS 0
407     #endif
408 #endif
409 
410 #ifdef MAX_NR_RFCOMM_CHANNELS
411 #if MAX_NR_RFCOMM_CHANNELS > 0
412 static rfcomm_channel_t rfcomm_channel_storage[MAX_NR_RFCOMM_CHANNELS];
413 static btstack_memory_pool_t rfcomm_channel_pool;
414 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
415     void * buffer = btstack_memory_pool_get(&rfcomm_channel_pool);
416     if (buffer){
417         memset(buffer, 0, sizeof(rfcomm_channel_t));
418     }
419     return (rfcomm_channel_t *) buffer;
420 }
421 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
422     btstack_memory_pool_free(&rfcomm_channel_pool, rfcomm_channel);
423 }
424 #else
425 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
426     return NULL;
427 }
428 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
429     UNUSED(rfcomm_channel);
430 };
431 #endif
432 #elif defined(HAVE_MALLOC)
433 
434 typedef struct {
435     btstack_memory_buffer_t tracking;
436     rfcomm_channel_t data;
437 } btstack_memory_rfcomm_channel_t;
438 
439 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
440     btstack_memory_rfcomm_channel_t * buffer = (btstack_memory_rfcomm_channel_t *) malloc(sizeof(btstack_memory_rfcomm_channel_t));
441     if (buffer){
442         memset(buffer, 0, sizeof(btstack_memory_rfcomm_channel_t));
443         btstack_memory_tracking_add(&buffer->tracking);
444         return &buffer->data;
445     } else {
446         return NULL;
447     }
448 }
449 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
450     // reconstruct buffer start
451     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) rfcomm_channel)[-1];
452     btstack_memory_tracking_remove(buffer);
453     free(buffer);
454 }
455 #endif
456 
457 
458 
459 // MARK: btstack_link_key_db_memory_entry_t
460 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES)
461     #if defined(MAX_NO_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES)
462         #error "Deprecated MAX_NO_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES defined instead of MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES. Please update your btstack_config.h to use MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES."
463     #else
464         #define MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES 0
465     #endif
466 #endif
467 
468 #ifdef MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES
469 #if MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES > 0
470 static btstack_link_key_db_memory_entry_t btstack_link_key_db_memory_entry_storage[MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES];
471 static btstack_memory_pool_t btstack_link_key_db_memory_entry_pool;
472 btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){
473     void * buffer = btstack_memory_pool_get(&btstack_link_key_db_memory_entry_pool);
474     if (buffer){
475         memset(buffer, 0, sizeof(btstack_link_key_db_memory_entry_t));
476     }
477     return (btstack_link_key_db_memory_entry_t *) buffer;
478 }
479 void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){
480     btstack_memory_pool_free(&btstack_link_key_db_memory_entry_pool, btstack_link_key_db_memory_entry);
481 }
482 #else
483 btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){
484     return NULL;
485 }
486 void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){
487     UNUSED(btstack_link_key_db_memory_entry);
488 };
489 #endif
490 #elif defined(HAVE_MALLOC)
491 
492 typedef struct {
493     btstack_memory_buffer_t tracking;
494     btstack_link_key_db_memory_entry_t data;
495 } btstack_memory_btstack_link_key_db_memory_entry_t;
496 
497 btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){
498     btstack_memory_btstack_link_key_db_memory_entry_t * buffer = (btstack_memory_btstack_link_key_db_memory_entry_t *) malloc(sizeof(btstack_memory_btstack_link_key_db_memory_entry_t));
499     if (buffer){
500         memset(buffer, 0, sizeof(btstack_memory_btstack_link_key_db_memory_entry_t));
501         btstack_memory_tracking_add(&buffer->tracking);
502         return &buffer->data;
503     } else {
504         return NULL;
505     }
506 }
507 void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){
508     // reconstruct buffer start
509     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) btstack_link_key_db_memory_entry)[-1];
510     btstack_memory_tracking_remove(buffer);
511     free(buffer);
512 }
513 #endif
514 
515 
516 
517 // MARK: bnep_service_t
518 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BNEP_SERVICES)
519     #if defined(MAX_NO_BNEP_SERVICES)
520         #error "Deprecated MAX_NO_BNEP_SERVICES defined instead of MAX_NR_BNEP_SERVICES. Please update your btstack_config.h to use MAX_NR_BNEP_SERVICES."
521     #else
522         #define MAX_NR_BNEP_SERVICES 0
523     #endif
524 #endif
525 
526 #ifdef MAX_NR_BNEP_SERVICES
527 #if MAX_NR_BNEP_SERVICES > 0
528 static bnep_service_t bnep_service_storage[MAX_NR_BNEP_SERVICES];
529 static btstack_memory_pool_t bnep_service_pool;
530 bnep_service_t * btstack_memory_bnep_service_get(void){
531     void * buffer = btstack_memory_pool_get(&bnep_service_pool);
532     if (buffer){
533         memset(buffer, 0, sizeof(bnep_service_t));
534     }
535     return (bnep_service_t *) buffer;
536 }
537 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
538     btstack_memory_pool_free(&bnep_service_pool, bnep_service);
539 }
540 #else
541 bnep_service_t * btstack_memory_bnep_service_get(void){
542     return NULL;
543 }
544 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
545     UNUSED(bnep_service);
546 };
547 #endif
548 #elif defined(HAVE_MALLOC)
549 
550 typedef struct {
551     btstack_memory_buffer_t tracking;
552     bnep_service_t data;
553 } btstack_memory_bnep_service_t;
554 
555 bnep_service_t * btstack_memory_bnep_service_get(void){
556     btstack_memory_bnep_service_t * buffer = (btstack_memory_bnep_service_t *) malloc(sizeof(btstack_memory_bnep_service_t));
557     if (buffer){
558         memset(buffer, 0, sizeof(btstack_memory_bnep_service_t));
559         btstack_memory_tracking_add(&buffer->tracking);
560         return &buffer->data;
561     } else {
562         return NULL;
563     }
564 }
565 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
566     // reconstruct buffer start
567     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) bnep_service)[-1];
568     btstack_memory_tracking_remove(buffer);
569     free(buffer);
570 }
571 #endif
572 
573 
574 // MARK: bnep_channel_t
575 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BNEP_CHANNELS)
576     #if defined(MAX_NO_BNEP_CHANNELS)
577         #error "Deprecated MAX_NO_BNEP_CHANNELS defined instead of MAX_NR_BNEP_CHANNELS. Please update your btstack_config.h to use MAX_NR_BNEP_CHANNELS."
578     #else
579         #define MAX_NR_BNEP_CHANNELS 0
580     #endif
581 #endif
582 
583 #ifdef MAX_NR_BNEP_CHANNELS
584 #if MAX_NR_BNEP_CHANNELS > 0
585 static bnep_channel_t bnep_channel_storage[MAX_NR_BNEP_CHANNELS];
586 static btstack_memory_pool_t bnep_channel_pool;
587 bnep_channel_t * btstack_memory_bnep_channel_get(void){
588     void * buffer = btstack_memory_pool_get(&bnep_channel_pool);
589     if (buffer){
590         memset(buffer, 0, sizeof(bnep_channel_t));
591     }
592     return (bnep_channel_t *) buffer;
593 }
594 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
595     btstack_memory_pool_free(&bnep_channel_pool, bnep_channel);
596 }
597 #else
598 bnep_channel_t * btstack_memory_bnep_channel_get(void){
599     return NULL;
600 }
601 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
602     UNUSED(bnep_channel);
603 };
604 #endif
605 #elif defined(HAVE_MALLOC)
606 
607 typedef struct {
608     btstack_memory_buffer_t tracking;
609     bnep_channel_t data;
610 } btstack_memory_bnep_channel_t;
611 
612 bnep_channel_t * btstack_memory_bnep_channel_get(void){
613     btstack_memory_bnep_channel_t * buffer = (btstack_memory_bnep_channel_t *) malloc(sizeof(btstack_memory_bnep_channel_t));
614     if (buffer){
615         memset(buffer, 0, sizeof(btstack_memory_bnep_channel_t));
616         btstack_memory_tracking_add(&buffer->tracking);
617         return &buffer->data;
618     } else {
619         return NULL;
620     }
621 }
622 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
623     // reconstruct buffer start
624     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) bnep_channel)[-1];
625     btstack_memory_tracking_remove(buffer);
626     free(buffer);
627 }
628 #endif
629 
630 
631 
632 // MARK: hfp_connection_t
633 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_HFP_CONNECTIONS)
634     #if defined(MAX_NO_HFP_CONNECTIONS)
635         #error "Deprecated MAX_NO_HFP_CONNECTIONS defined instead of MAX_NR_HFP_CONNECTIONS. Please update your btstack_config.h to use MAX_NR_HFP_CONNECTIONS."
636     #else
637         #define MAX_NR_HFP_CONNECTIONS 0
638     #endif
639 #endif
640 
641 #ifdef MAX_NR_HFP_CONNECTIONS
642 #if MAX_NR_HFP_CONNECTIONS > 0
643 static hfp_connection_t hfp_connection_storage[MAX_NR_HFP_CONNECTIONS];
644 static btstack_memory_pool_t hfp_connection_pool;
645 hfp_connection_t * btstack_memory_hfp_connection_get(void){
646     void * buffer = btstack_memory_pool_get(&hfp_connection_pool);
647     if (buffer){
648         memset(buffer, 0, sizeof(hfp_connection_t));
649     }
650     return (hfp_connection_t *) buffer;
651 }
652 void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){
653     btstack_memory_pool_free(&hfp_connection_pool, hfp_connection);
654 }
655 #else
656 hfp_connection_t * btstack_memory_hfp_connection_get(void){
657     return NULL;
658 }
659 void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){
660     UNUSED(hfp_connection);
661 };
662 #endif
663 #elif defined(HAVE_MALLOC)
664 
665 typedef struct {
666     btstack_memory_buffer_t tracking;
667     hfp_connection_t data;
668 } btstack_memory_hfp_connection_t;
669 
670 hfp_connection_t * btstack_memory_hfp_connection_get(void){
671     btstack_memory_hfp_connection_t * buffer = (btstack_memory_hfp_connection_t *) malloc(sizeof(btstack_memory_hfp_connection_t));
672     if (buffer){
673         memset(buffer, 0, sizeof(btstack_memory_hfp_connection_t));
674         btstack_memory_tracking_add(&buffer->tracking);
675         return &buffer->data;
676     } else {
677         return NULL;
678     }
679 }
680 void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){
681     // reconstruct buffer start
682     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) hfp_connection)[-1];
683     btstack_memory_tracking_remove(buffer);
684     free(buffer);
685 }
686 #endif
687 
688 
689 
690 // MARK: service_record_item_t
691 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_SERVICE_RECORD_ITEMS)
692     #if defined(MAX_NO_SERVICE_RECORD_ITEMS)
693         #error "Deprecated MAX_NO_SERVICE_RECORD_ITEMS defined instead of MAX_NR_SERVICE_RECORD_ITEMS. Please update your btstack_config.h to use MAX_NR_SERVICE_RECORD_ITEMS."
694     #else
695         #define MAX_NR_SERVICE_RECORD_ITEMS 0
696     #endif
697 #endif
698 
699 #ifdef MAX_NR_SERVICE_RECORD_ITEMS
700 #if MAX_NR_SERVICE_RECORD_ITEMS > 0
701 static service_record_item_t service_record_item_storage[MAX_NR_SERVICE_RECORD_ITEMS];
702 static btstack_memory_pool_t service_record_item_pool;
703 service_record_item_t * btstack_memory_service_record_item_get(void){
704     void * buffer = btstack_memory_pool_get(&service_record_item_pool);
705     if (buffer){
706         memset(buffer, 0, sizeof(service_record_item_t));
707     }
708     return (service_record_item_t *) buffer;
709 }
710 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
711     btstack_memory_pool_free(&service_record_item_pool, service_record_item);
712 }
713 #else
714 service_record_item_t * btstack_memory_service_record_item_get(void){
715     return NULL;
716 }
717 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
718     UNUSED(service_record_item);
719 };
720 #endif
721 #elif defined(HAVE_MALLOC)
722 
723 typedef struct {
724     btstack_memory_buffer_t tracking;
725     service_record_item_t data;
726 } btstack_memory_service_record_item_t;
727 
728 service_record_item_t * btstack_memory_service_record_item_get(void){
729     btstack_memory_service_record_item_t * buffer = (btstack_memory_service_record_item_t *) malloc(sizeof(btstack_memory_service_record_item_t));
730     if (buffer){
731         memset(buffer, 0, sizeof(btstack_memory_service_record_item_t));
732         btstack_memory_tracking_add(&buffer->tracking);
733         return &buffer->data;
734     } else {
735         return NULL;
736     }
737 }
738 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
739     // reconstruct buffer start
740     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) service_record_item)[-1];
741     btstack_memory_tracking_remove(buffer);
742     free(buffer);
743 }
744 #endif
745 
746 
747 
748 // MARK: avdtp_stream_endpoint_t
749 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVDTP_STREAM_ENDPOINTS)
750     #if defined(MAX_NO_AVDTP_STREAM_ENDPOINTS)
751         #error "Deprecated MAX_NO_AVDTP_STREAM_ENDPOINTS defined instead of MAX_NR_AVDTP_STREAM_ENDPOINTS. Please update your btstack_config.h to use MAX_NR_AVDTP_STREAM_ENDPOINTS."
752     #else
753         #define MAX_NR_AVDTP_STREAM_ENDPOINTS 0
754     #endif
755 #endif
756 
757 #ifdef MAX_NR_AVDTP_STREAM_ENDPOINTS
758 #if MAX_NR_AVDTP_STREAM_ENDPOINTS > 0
759 static avdtp_stream_endpoint_t avdtp_stream_endpoint_storage[MAX_NR_AVDTP_STREAM_ENDPOINTS];
760 static btstack_memory_pool_t avdtp_stream_endpoint_pool;
761 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){
762     void * buffer = btstack_memory_pool_get(&avdtp_stream_endpoint_pool);
763     if (buffer){
764         memset(buffer, 0, sizeof(avdtp_stream_endpoint_t));
765     }
766     return (avdtp_stream_endpoint_t *) buffer;
767 }
768 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){
769     btstack_memory_pool_free(&avdtp_stream_endpoint_pool, avdtp_stream_endpoint);
770 }
771 #else
772 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){
773     return NULL;
774 }
775 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){
776     UNUSED(avdtp_stream_endpoint);
777 };
778 #endif
779 #elif defined(HAVE_MALLOC)
780 
781 typedef struct {
782     btstack_memory_buffer_t tracking;
783     avdtp_stream_endpoint_t data;
784 } btstack_memory_avdtp_stream_endpoint_t;
785 
786 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){
787     btstack_memory_avdtp_stream_endpoint_t * buffer = (btstack_memory_avdtp_stream_endpoint_t *) malloc(sizeof(btstack_memory_avdtp_stream_endpoint_t));
788     if (buffer){
789         memset(buffer, 0, sizeof(btstack_memory_avdtp_stream_endpoint_t));
790         btstack_memory_tracking_add(&buffer->tracking);
791         return &buffer->data;
792     } else {
793         return NULL;
794     }
795 }
796 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){
797     // reconstruct buffer start
798     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) avdtp_stream_endpoint)[-1];
799     btstack_memory_tracking_remove(buffer);
800     free(buffer);
801 }
802 #endif
803 
804 
805 
806 // MARK: avdtp_connection_t
807 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVDTP_CONNECTIONS)
808     #if defined(MAX_NO_AVDTP_CONNECTIONS)
809         #error "Deprecated MAX_NO_AVDTP_CONNECTIONS defined instead of MAX_NR_AVDTP_CONNECTIONS. Please update your btstack_config.h to use MAX_NR_AVDTP_CONNECTIONS."
810     #else
811         #define MAX_NR_AVDTP_CONNECTIONS 0
812     #endif
813 #endif
814 
815 #ifdef MAX_NR_AVDTP_CONNECTIONS
816 #if MAX_NR_AVDTP_CONNECTIONS > 0
817 static avdtp_connection_t avdtp_connection_storage[MAX_NR_AVDTP_CONNECTIONS];
818 static btstack_memory_pool_t avdtp_connection_pool;
819 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){
820     void * buffer = btstack_memory_pool_get(&avdtp_connection_pool);
821     if (buffer){
822         memset(buffer, 0, sizeof(avdtp_connection_t));
823     }
824     return (avdtp_connection_t *) buffer;
825 }
826 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){
827     btstack_memory_pool_free(&avdtp_connection_pool, avdtp_connection);
828 }
829 #else
830 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){
831     return NULL;
832 }
833 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){
834     UNUSED(avdtp_connection);
835 };
836 #endif
837 #elif defined(HAVE_MALLOC)
838 
839 typedef struct {
840     btstack_memory_buffer_t tracking;
841     avdtp_connection_t data;
842 } btstack_memory_avdtp_connection_t;
843 
844 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){
845     btstack_memory_avdtp_connection_t * buffer = (btstack_memory_avdtp_connection_t *) malloc(sizeof(btstack_memory_avdtp_connection_t));
846     if (buffer){
847         memset(buffer, 0, sizeof(btstack_memory_avdtp_connection_t));
848         btstack_memory_tracking_add(&buffer->tracking);
849         return &buffer->data;
850     } else {
851         return NULL;
852     }
853 }
854 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){
855     // reconstruct buffer start
856     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) avdtp_connection)[-1];
857     btstack_memory_tracking_remove(buffer);
858     free(buffer);
859 }
860 #endif
861 
862 
863 
864 // MARK: avrcp_connection_t
865 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVRCP_CONNECTIONS)
866     #if defined(MAX_NO_AVRCP_CONNECTIONS)
867         #error "Deprecated MAX_NO_AVRCP_CONNECTIONS defined instead of MAX_NR_AVRCP_CONNECTIONS. Please update your btstack_config.h to use MAX_NR_AVRCP_CONNECTIONS."
868     #else
869         #define MAX_NR_AVRCP_CONNECTIONS 0
870     #endif
871 #endif
872 
873 #ifdef MAX_NR_AVRCP_CONNECTIONS
874 #if MAX_NR_AVRCP_CONNECTIONS > 0
875 static avrcp_connection_t avrcp_connection_storage[MAX_NR_AVRCP_CONNECTIONS];
876 static btstack_memory_pool_t avrcp_connection_pool;
877 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){
878     void * buffer = btstack_memory_pool_get(&avrcp_connection_pool);
879     if (buffer){
880         memset(buffer, 0, sizeof(avrcp_connection_t));
881     }
882     return (avrcp_connection_t *) buffer;
883 }
884 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){
885     btstack_memory_pool_free(&avrcp_connection_pool, avrcp_connection);
886 }
887 #else
888 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){
889     return NULL;
890 }
891 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){
892     UNUSED(avrcp_connection);
893 };
894 #endif
895 #elif defined(HAVE_MALLOC)
896 
897 typedef struct {
898     btstack_memory_buffer_t tracking;
899     avrcp_connection_t data;
900 } btstack_memory_avrcp_connection_t;
901 
902 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){
903     btstack_memory_avrcp_connection_t * buffer = (btstack_memory_avrcp_connection_t *) malloc(sizeof(btstack_memory_avrcp_connection_t));
904     if (buffer){
905         memset(buffer, 0, sizeof(btstack_memory_avrcp_connection_t));
906         btstack_memory_tracking_add(&buffer->tracking);
907         return &buffer->data;
908     } else {
909         return NULL;
910     }
911 }
912 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){
913     // reconstruct buffer start
914     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) avrcp_connection)[-1];
915     btstack_memory_tracking_remove(buffer);
916     free(buffer);
917 }
918 #endif
919 
920 
921 
922 // MARK: avrcp_browsing_connection_t
923 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVRCP_BROWSING_CONNECTIONS)
924     #if defined(MAX_NO_AVRCP_BROWSING_CONNECTIONS)
925         #error "Deprecated MAX_NO_AVRCP_BROWSING_CONNECTIONS defined instead of MAX_NR_AVRCP_BROWSING_CONNECTIONS. Please update your btstack_config.h to use MAX_NR_AVRCP_BROWSING_CONNECTIONS."
926     #else
927         #define MAX_NR_AVRCP_BROWSING_CONNECTIONS 0
928     #endif
929 #endif
930 
931 #ifdef MAX_NR_AVRCP_BROWSING_CONNECTIONS
932 #if MAX_NR_AVRCP_BROWSING_CONNECTIONS > 0
933 static avrcp_browsing_connection_t avrcp_browsing_connection_storage[MAX_NR_AVRCP_BROWSING_CONNECTIONS];
934 static btstack_memory_pool_t avrcp_browsing_connection_pool;
935 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){
936     void * buffer = btstack_memory_pool_get(&avrcp_browsing_connection_pool);
937     if (buffer){
938         memset(buffer, 0, sizeof(avrcp_browsing_connection_t));
939     }
940     return (avrcp_browsing_connection_t *) buffer;
941 }
942 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){
943     btstack_memory_pool_free(&avrcp_browsing_connection_pool, avrcp_browsing_connection);
944 }
945 #else
946 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){
947     return NULL;
948 }
949 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){
950     UNUSED(avrcp_browsing_connection);
951 };
952 #endif
953 #elif defined(HAVE_MALLOC)
954 
955 typedef struct {
956     btstack_memory_buffer_t tracking;
957     avrcp_browsing_connection_t data;
958 } btstack_memory_avrcp_browsing_connection_t;
959 
960 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){
961     btstack_memory_avrcp_browsing_connection_t * buffer = (btstack_memory_avrcp_browsing_connection_t *) malloc(sizeof(btstack_memory_avrcp_browsing_connection_t));
962     if (buffer){
963         memset(buffer, 0, sizeof(btstack_memory_avrcp_browsing_connection_t));
964         btstack_memory_tracking_add(&buffer->tracking);
965         return &buffer->data;
966     } else {
967         return NULL;
968     }
969 }
970 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){
971     // reconstruct buffer start
972     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) avrcp_browsing_connection)[-1];
973     btstack_memory_tracking_remove(buffer);
974     free(buffer);
975 }
976 #endif
977 
978 
979 #endif
980 #ifdef ENABLE_BLE
981 
982 // MARK: gatt_client_t
983 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_GATT_CLIENTS)
984     #if defined(MAX_NO_GATT_CLIENTS)
985         #error "Deprecated MAX_NO_GATT_CLIENTS defined instead of MAX_NR_GATT_CLIENTS. Please update your btstack_config.h to use MAX_NR_GATT_CLIENTS."
986     #else
987         #define MAX_NR_GATT_CLIENTS 0
988     #endif
989 #endif
990 
991 #ifdef MAX_NR_GATT_CLIENTS
992 #if MAX_NR_GATT_CLIENTS > 0
993 static gatt_client_t gatt_client_storage[MAX_NR_GATT_CLIENTS];
994 static btstack_memory_pool_t gatt_client_pool;
995 gatt_client_t * btstack_memory_gatt_client_get(void){
996     void * buffer = btstack_memory_pool_get(&gatt_client_pool);
997     if (buffer){
998         memset(buffer, 0, sizeof(gatt_client_t));
999     }
1000     return (gatt_client_t *) buffer;
1001 }
1002 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
1003     btstack_memory_pool_free(&gatt_client_pool, gatt_client);
1004 }
1005 #else
1006 gatt_client_t * btstack_memory_gatt_client_get(void){
1007     return NULL;
1008 }
1009 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
1010     UNUSED(gatt_client);
1011 };
1012 #endif
1013 #elif defined(HAVE_MALLOC)
1014 
1015 typedef struct {
1016     btstack_memory_buffer_t tracking;
1017     gatt_client_t data;
1018 } btstack_memory_gatt_client_t;
1019 
1020 gatt_client_t * btstack_memory_gatt_client_get(void){
1021     btstack_memory_gatt_client_t * buffer = (btstack_memory_gatt_client_t *) malloc(sizeof(btstack_memory_gatt_client_t));
1022     if (buffer){
1023         memset(buffer, 0, sizeof(btstack_memory_gatt_client_t));
1024         btstack_memory_tracking_add(&buffer->tracking);
1025         return &buffer->data;
1026     } else {
1027         return NULL;
1028     }
1029 }
1030 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
1031     // reconstruct buffer start
1032     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) gatt_client)[-1];
1033     btstack_memory_tracking_remove(buffer);
1034     free(buffer);
1035 }
1036 #endif
1037 
1038 
1039 // MARK: whitelist_entry_t
1040 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_WHITELIST_ENTRIES)
1041     #if defined(MAX_NO_WHITELIST_ENTRIES)
1042         #error "Deprecated MAX_NO_WHITELIST_ENTRIES defined instead of MAX_NR_WHITELIST_ENTRIES. Please update your btstack_config.h to use MAX_NR_WHITELIST_ENTRIES."
1043     #else
1044         #define MAX_NR_WHITELIST_ENTRIES 0
1045     #endif
1046 #endif
1047 
1048 #ifdef MAX_NR_WHITELIST_ENTRIES
1049 #if MAX_NR_WHITELIST_ENTRIES > 0
1050 static whitelist_entry_t whitelist_entry_storage[MAX_NR_WHITELIST_ENTRIES];
1051 static btstack_memory_pool_t whitelist_entry_pool;
1052 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
1053     void * buffer = btstack_memory_pool_get(&whitelist_entry_pool);
1054     if (buffer){
1055         memset(buffer, 0, sizeof(whitelist_entry_t));
1056     }
1057     return (whitelist_entry_t *) buffer;
1058 }
1059 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
1060     btstack_memory_pool_free(&whitelist_entry_pool, whitelist_entry);
1061 }
1062 #else
1063 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
1064     return NULL;
1065 }
1066 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
1067     UNUSED(whitelist_entry);
1068 };
1069 #endif
1070 #elif defined(HAVE_MALLOC)
1071 
1072 typedef struct {
1073     btstack_memory_buffer_t tracking;
1074     whitelist_entry_t data;
1075 } btstack_memory_whitelist_entry_t;
1076 
1077 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
1078     btstack_memory_whitelist_entry_t * buffer = (btstack_memory_whitelist_entry_t *) malloc(sizeof(btstack_memory_whitelist_entry_t));
1079     if (buffer){
1080         memset(buffer, 0, sizeof(btstack_memory_whitelist_entry_t));
1081         btstack_memory_tracking_add(&buffer->tracking);
1082         return &buffer->data;
1083     } else {
1084         return NULL;
1085     }
1086 }
1087 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
1088     // reconstruct buffer start
1089     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) whitelist_entry)[-1];
1090     btstack_memory_tracking_remove(buffer);
1091     free(buffer);
1092 }
1093 #endif
1094 
1095 
1096 // MARK: sm_lookup_entry_t
1097 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_SM_LOOKUP_ENTRIES)
1098     #if defined(MAX_NO_SM_LOOKUP_ENTRIES)
1099         #error "Deprecated MAX_NO_SM_LOOKUP_ENTRIES defined instead of MAX_NR_SM_LOOKUP_ENTRIES. Please update your btstack_config.h to use MAX_NR_SM_LOOKUP_ENTRIES."
1100     #else
1101         #define MAX_NR_SM_LOOKUP_ENTRIES 0
1102     #endif
1103 #endif
1104 
1105 #ifdef MAX_NR_SM_LOOKUP_ENTRIES
1106 #if MAX_NR_SM_LOOKUP_ENTRIES > 0
1107 static sm_lookup_entry_t sm_lookup_entry_storage[MAX_NR_SM_LOOKUP_ENTRIES];
1108 static btstack_memory_pool_t sm_lookup_entry_pool;
1109 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
1110     void * buffer = btstack_memory_pool_get(&sm_lookup_entry_pool);
1111     if (buffer){
1112         memset(buffer, 0, sizeof(sm_lookup_entry_t));
1113     }
1114     return (sm_lookup_entry_t *) buffer;
1115 }
1116 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
1117     btstack_memory_pool_free(&sm_lookup_entry_pool, sm_lookup_entry);
1118 }
1119 #else
1120 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
1121     return NULL;
1122 }
1123 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
1124     UNUSED(sm_lookup_entry);
1125 };
1126 #endif
1127 #elif defined(HAVE_MALLOC)
1128 
1129 typedef struct {
1130     btstack_memory_buffer_t tracking;
1131     sm_lookup_entry_t data;
1132 } btstack_memory_sm_lookup_entry_t;
1133 
1134 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
1135     btstack_memory_sm_lookup_entry_t * buffer = (btstack_memory_sm_lookup_entry_t *) malloc(sizeof(btstack_memory_sm_lookup_entry_t));
1136     if (buffer){
1137         memset(buffer, 0, sizeof(btstack_memory_sm_lookup_entry_t));
1138         btstack_memory_tracking_add(&buffer->tracking);
1139         return &buffer->data;
1140     } else {
1141         return NULL;
1142     }
1143 }
1144 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
1145     // reconstruct buffer start
1146     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) sm_lookup_entry)[-1];
1147     btstack_memory_tracking_remove(buffer);
1148     free(buffer);
1149 }
1150 #endif
1151 
1152 
1153 #endif
1154 #ifdef ENABLE_MESH
1155 
1156 // MARK: mesh_network_pdu_t
1157 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_NETWORK_PDUS)
1158     #if defined(MAX_NO_MESH_NETWORK_PDUS)
1159         #error "Deprecated MAX_NO_MESH_NETWORK_PDUS defined instead of MAX_NR_MESH_NETWORK_PDUS. Please update your btstack_config.h to use MAX_NR_MESH_NETWORK_PDUS."
1160     #else
1161         #define MAX_NR_MESH_NETWORK_PDUS 0
1162     #endif
1163 #endif
1164 
1165 #ifdef MAX_NR_MESH_NETWORK_PDUS
1166 #if MAX_NR_MESH_NETWORK_PDUS > 0
1167 static mesh_network_pdu_t mesh_network_pdu_storage[MAX_NR_MESH_NETWORK_PDUS];
1168 static btstack_memory_pool_t mesh_network_pdu_pool;
1169 mesh_network_pdu_t * btstack_memory_mesh_network_pdu_get(void){
1170     void * buffer = btstack_memory_pool_get(&mesh_network_pdu_pool);
1171     if (buffer){
1172         memset(buffer, 0, sizeof(mesh_network_pdu_t));
1173     }
1174     return (mesh_network_pdu_t *) buffer;
1175 }
1176 void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){
1177     btstack_memory_pool_free(&mesh_network_pdu_pool, mesh_network_pdu);
1178 }
1179 #else
1180 mesh_network_pdu_t * btstack_memory_mesh_network_pdu_get(void){
1181     return NULL;
1182 }
1183 void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){
1184     UNUSED(mesh_network_pdu);
1185 };
1186 #endif
1187 #elif defined(HAVE_MALLOC)
1188 
1189 typedef struct {
1190     btstack_memory_buffer_t tracking;
1191     mesh_network_pdu_t data;
1192 } btstack_memory_mesh_network_pdu_t;
1193 
1194 mesh_network_pdu_t * btstack_memory_mesh_network_pdu_get(void){
1195     btstack_memory_mesh_network_pdu_t * buffer = (btstack_memory_mesh_network_pdu_t *) malloc(sizeof(btstack_memory_mesh_network_pdu_t));
1196     if (buffer){
1197         memset(buffer, 0, sizeof(btstack_memory_mesh_network_pdu_t));
1198         btstack_memory_tracking_add(&buffer->tracking);
1199         return &buffer->data;
1200     } else {
1201         return NULL;
1202     }
1203 }
1204 void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){
1205     // reconstruct buffer start
1206     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_network_pdu)[-1];
1207     btstack_memory_tracking_remove(buffer);
1208     free(buffer);
1209 }
1210 #endif
1211 
1212 
1213 // MARK: mesh_segmented_pdu_t
1214 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_SEGMENTED_PDUS)
1215     #if defined(MAX_NO_MESH_SEGMENTED_PDUS)
1216         #error "Deprecated MAX_NO_MESH_SEGMENTED_PDUS defined instead of MAX_NR_MESH_SEGMENTED_PDUS. Please update your btstack_config.h to use MAX_NR_MESH_SEGMENTED_PDUS."
1217     #else
1218         #define MAX_NR_MESH_SEGMENTED_PDUS 0
1219     #endif
1220 #endif
1221 
1222 #ifdef MAX_NR_MESH_SEGMENTED_PDUS
1223 #if MAX_NR_MESH_SEGMENTED_PDUS > 0
1224 static mesh_segmented_pdu_t mesh_segmented_pdu_storage[MAX_NR_MESH_SEGMENTED_PDUS];
1225 static btstack_memory_pool_t mesh_segmented_pdu_pool;
1226 mesh_segmented_pdu_t * btstack_memory_mesh_segmented_pdu_get(void){
1227     void * buffer = btstack_memory_pool_get(&mesh_segmented_pdu_pool);
1228     if (buffer){
1229         memset(buffer, 0, sizeof(mesh_segmented_pdu_t));
1230     }
1231     return (mesh_segmented_pdu_t *) buffer;
1232 }
1233 void btstack_memory_mesh_segmented_pdu_free(mesh_segmented_pdu_t *mesh_segmented_pdu){
1234     btstack_memory_pool_free(&mesh_segmented_pdu_pool, mesh_segmented_pdu);
1235 }
1236 #else
1237 mesh_segmented_pdu_t * btstack_memory_mesh_segmented_pdu_get(void){
1238     return NULL;
1239 }
1240 void btstack_memory_mesh_segmented_pdu_free(mesh_segmented_pdu_t *mesh_segmented_pdu){
1241     UNUSED(mesh_segmented_pdu);
1242 };
1243 #endif
1244 #elif defined(HAVE_MALLOC)
1245 
1246 typedef struct {
1247     btstack_memory_buffer_t tracking;
1248     mesh_segmented_pdu_t data;
1249 } btstack_memory_mesh_segmented_pdu_t;
1250 
1251 mesh_segmented_pdu_t * btstack_memory_mesh_segmented_pdu_get(void){
1252     btstack_memory_mesh_segmented_pdu_t * buffer = (btstack_memory_mesh_segmented_pdu_t *) malloc(sizeof(btstack_memory_mesh_segmented_pdu_t));
1253     if (buffer){
1254         memset(buffer, 0, sizeof(btstack_memory_mesh_segmented_pdu_t));
1255         btstack_memory_tracking_add(&buffer->tracking);
1256         return &buffer->data;
1257     } else {
1258         return NULL;
1259     }
1260 }
1261 void btstack_memory_mesh_segmented_pdu_free(mesh_segmented_pdu_t *mesh_segmented_pdu){
1262     // reconstruct buffer start
1263     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_segmented_pdu)[-1];
1264     btstack_memory_tracking_remove(buffer);
1265     free(buffer);
1266 }
1267 #endif
1268 
1269 
1270 // MARK: mesh_upper_transport_pdu_t
1271 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_UPPER_TRANSPORT_PDUS)
1272     #if defined(MAX_NO_MESH_UPPER_TRANSPORT_PDUS)
1273         #error "Deprecated MAX_NO_MESH_UPPER_TRANSPORT_PDUS defined instead of MAX_NR_MESH_UPPER_TRANSPORT_PDUS. Please update your btstack_config.h to use MAX_NR_MESH_UPPER_TRANSPORT_PDUS."
1274     #else
1275         #define MAX_NR_MESH_UPPER_TRANSPORT_PDUS 0
1276     #endif
1277 #endif
1278 
1279 #ifdef MAX_NR_MESH_UPPER_TRANSPORT_PDUS
1280 #if MAX_NR_MESH_UPPER_TRANSPORT_PDUS > 0
1281 static mesh_upper_transport_pdu_t mesh_upper_transport_pdu_storage[MAX_NR_MESH_UPPER_TRANSPORT_PDUS];
1282 static btstack_memory_pool_t mesh_upper_transport_pdu_pool;
1283 mesh_upper_transport_pdu_t * btstack_memory_mesh_upper_transport_pdu_get(void){
1284     void * buffer = btstack_memory_pool_get(&mesh_upper_transport_pdu_pool);
1285     if (buffer){
1286         memset(buffer, 0, sizeof(mesh_upper_transport_pdu_t));
1287     }
1288     return (mesh_upper_transport_pdu_t *) buffer;
1289 }
1290 void btstack_memory_mesh_upper_transport_pdu_free(mesh_upper_transport_pdu_t *mesh_upper_transport_pdu){
1291     btstack_memory_pool_free(&mesh_upper_transport_pdu_pool, mesh_upper_transport_pdu);
1292 }
1293 #else
1294 mesh_upper_transport_pdu_t * btstack_memory_mesh_upper_transport_pdu_get(void){
1295     return NULL;
1296 }
1297 void btstack_memory_mesh_upper_transport_pdu_free(mesh_upper_transport_pdu_t *mesh_upper_transport_pdu){
1298     UNUSED(mesh_upper_transport_pdu);
1299 };
1300 #endif
1301 #elif defined(HAVE_MALLOC)
1302 
1303 typedef struct {
1304     btstack_memory_buffer_t tracking;
1305     mesh_upper_transport_pdu_t data;
1306 } btstack_memory_mesh_upper_transport_pdu_t;
1307 
1308 mesh_upper_transport_pdu_t * btstack_memory_mesh_upper_transport_pdu_get(void){
1309     btstack_memory_mesh_upper_transport_pdu_t * buffer = (btstack_memory_mesh_upper_transport_pdu_t *) malloc(sizeof(btstack_memory_mesh_upper_transport_pdu_t));
1310     if (buffer){
1311         memset(buffer, 0, sizeof(btstack_memory_mesh_upper_transport_pdu_t));
1312         btstack_memory_tracking_add(&buffer->tracking);
1313         return &buffer->data;
1314     } else {
1315         return NULL;
1316     }
1317 }
1318 void btstack_memory_mesh_upper_transport_pdu_free(mesh_upper_transport_pdu_t *mesh_upper_transport_pdu){
1319     // reconstruct buffer start
1320     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_upper_transport_pdu)[-1];
1321     btstack_memory_tracking_remove(buffer);
1322     free(buffer);
1323 }
1324 #endif
1325 
1326 
1327 // MARK: mesh_network_key_t
1328 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_NETWORK_KEYS)
1329     #if defined(MAX_NO_MESH_NETWORK_KEYS)
1330         #error "Deprecated MAX_NO_MESH_NETWORK_KEYS defined instead of MAX_NR_MESH_NETWORK_KEYS. Please update your btstack_config.h to use MAX_NR_MESH_NETWORK_KEYS."
1331     #else
1332         #define MAX_NR_MESH_NETWORK_KEYS 0
1333     #endif
1334 #endif
1335 
1336 #ifdef MAX_NR_MESH_NETWORK_KEYS
1337 #if MAX_NR_MESH_NETWORK_KEYS > 0
1338 static mesh_network_key_t mesh_network_key_storage[MAX_NR_MESH_NETWORK_KEYS];
1339 static btstack_memory_pool_t mesh_network_key_pool;
1340 mesh_network_key_t * btstack_memory_mesh_network_key_get(void){
1341     void * buffer = btstack_memory_pool_get(&mesh_network_key_pool);
1342     if (buffer){
1343         memset(buffer, 0, sizeof(mesh_network_key_t));
1344     }
1345     return (mesh_network_key_t *) buffer;
1346 }
1347 void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){
1348     btstack_memory_pool_free(&mesh_network_key_pool, mesh_network_key);
1349 }
1350 #else
1351 mesh_network_key_t * btstack_memory_mesh_network_key_get(void){
1352     return NULL;
1353 }
1354 void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){
1355     UNUSED(mesh_network_key);
1356 };
1357 #endif
1358 #elif defined(HAVE_MALLOC)
1359 
1360 typedef struct {
1361     btstack_memory_buffer_t tracking;
1362     mesh_network_key_t data;
1363 } btstack_memory_mesh_network_key_t;
1364 
1365 mesh_network_key_t * btstack_memory_mesh_network_key_get(void){
1366     btstack_memory_mesh_network_key_t * buffer = (btstack_memory_mesh_network_key_t *) malloc(sizeof(btstack_memory_mesh_network_key_t));
1367     if (buffer){
1368         memset(buffer, 0, sizeof(btstack_memory_mesh_network_key_t));
1369         btstack_memory_tracking_add(&buffer->tracking);
1370         return &buffer->data;
1371     } else {
1372         return NULL;
1373     }
1374 }
1375 void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){
1376     // reconstruct buffer start
1377     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_network_key)[-1];
1378     btstack_memory_tracking_remove(buffer);
1379     free(buffer);
1380 }
1381 #endif
1382 
1383 
1384 // MARK: mesh_transport_key_t
1385 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_TRANSPORT_KEYS)
1386     #if defined(MAX_NO_MESH_TRANSPORT_KEYS)
1387         #error "Deprecated MAX_NO_MESH_TRANSPORT_KEYS defined instead of MAX_NR_MESH_TRANSPORT_KEYS. Please update your btstack_config.h to use MAX_NR_MESH_TRANSPORT_KEYS."
1388     #else
1389         #define MAX_NR_MESH_TRANSPORT_KEYS 0
1390     #endif
1391 #endif
1392 
1393 #ifdef MAX_NR_MESH_TRANSPORT_KEYS
1394 #if MAX_NR_MESH_TRANSPORT_KEYS > 0
1395 static mesh_transport_key_t mesh_transport_key_storage[MAX_NR_MESH_TRANSPORT_KEYS];
1396 static btstack_memory_pool_t mesh_transport_key_pool;
1397 mesh_transport_key_t * btstack_memory_mesh_transport_key_get(void){
1398     void * buffer = btstack_memory_pool_get(&mesh_transport_key_pool);
1399     if (buffer){
1400         memset(buffer, 0, sizeof(mesh_transport_key_t));
1401     }
1402     return (mesh_transport_key_t *) buffer;
1403 }
1404 void btstack_memory_mesh_transport_key_free(mesh_transport_key_t *mesh_transport_key){
1405     btstack_memory_pool_free(&mesh_transport_key_pool, mesh_transport_key);
1406 }
1407 #else
1408 mesh_transport_key_t * btstack_memory_mesh_transport_key_get(void){
1409     return NULL;
1410 }
1411 void btstack_memory_mesh_transport_key_free(mesh_transport_key_t *mesh_transport_key){
1412     UNUSED(mesh_transport_key);
1413 };
1414 #endif
1415 #elif defined(HAVE_MALLOC)
1416 
1417 typedef struct {
1418     btstack_memory_buffer_t tracking;
1419     mesh_transport_key_t data;
1420 } btstack_memory_mesh_transport_key_t;
1421 
1422 mesh_transport_key_t * btstack_memory_mesh_transport_key_get(void){
1423     btstack_memory_mesh_transport_key_t * buffer = (btstack_memory_mesh_transport_key_t *) malloc(sizeof(btstack_memory_mesh_transport_key_t));
1424     if (buffer){
1425         memset(buffer, 0, sizeof(btstack_memory_mesh_transport_key_t));
1426         btstack_memory_tracking_add(&buffer->tracking);
1427         return &buffer->data;
1428     } else {
1429         return NULL;
1430     }
1431 }
1432 void btstack_memory_mesh_transport_key_free(mesh_transport_key_t *mesh_transport_key){
1433     // reconstruct buffer start
1434     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_transport_key)[-1];
1435     btstack_memory_tracking_remove(buffer);
1436     free(buffer);
1437 }
1438 #endif
1439 
1440 
1441 // MARK: mesh_virtual_address_t
1442 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_VIRTUAL_ADDRESSS)
1443     #if defined(MAX_NO_MESH_VIRTUAL_ADDRESSS)
1444         #error "Deprecated MAX_NO_MESH_VIRTUAL_ADDRESSS defined instead of MAX_NR_MESH_VIRTUAL_ADDRESSS. Please update your btstack_config.h to use MAX_NR_MESH_VIRTUAL_ADDRESSS."
1445     #else
1446         #define MAX_NR_MESH_VIRTUAL_ADDRESSS 0
1447     #endif
1448 #endif
1449 
1450 #ifdef MAX_NR_MESH_VIRTUAL_ADDRESSS
1451 #if MAX_NR_MESH_VIRTUAL_ADDRESSS > 0
1452 static mesh_virtual_address_t mesh_virtual_address_storage[MAX_NR_MESH_VIRTUAL_ADDRESSS];
1453 static btstack_memory_pool_t mesh_virtual_address_pool;
1454 mesh_virtual_address_t * btstack_memory_mesh_virtual_address_get(void){
1455     void * buffer = btstack_memory_pool_get(&mesh_virtual_address_pool);
1456     if (buffer){
1457         memset(buffer, 0, sizeof(mesh_virtual_address_t));
1458     }
1459     return (mesh_virtual_address_t *) buffer;
1460 }
1461 void btstack_memory_mesh_virtual_address_free(mesh_virtual_address_t *mesh_virtual_address){
1462     btstack_memory_pool_free(&mesh_virtual_address_pool, mesh_virtual_address);
1463 }
1464 #else
1465 mesh_virtual_address_t * btstack_memory_mesh_virtual_address_get(void){
1466     return NULL;
1467 }
1468 void btstack_memory_mesh_virtual_address_free(mesh_virtual_address_t *mesh_virtual_address){
1469     UNUSED(mesh_virtual_address);
1470 };
1471 #endif
1472 #elif defined(HAVE_MALLOC)
1473 
1474 typedef struct {
1475     btstack_memory_buffer_t tracking;
1476     mesh_virtual_address_t data;
1477 } btstack_memory_mesh_virtual_address_t;
1478 
1479 mesh_virtual_address_t * btstack_memory_mesh_virtual_address_get(void){
1480     btstack_memory_mesh_virtual_address_t * buffer = (btstack_memory_mesh_virtual_address_t *) malloc(sizeof(btstack_memory_mesh_virtual_address_t));
1481     if (buffer){
1482         memset(buffer, 0, sizeof(btstack_memory_mesh_virtual_address_t));
1483         btstack_memory_tracking_add(&buffer->tracking);
1484         return &buffer->data;
1485     } else {
1486         return NULL;
1487     }
1488 }
1489 void btstack_memory_mesh_virtual_address_free(mesh_virtual_address_t *mesh_virtual_address){
1490     // reconstruct buffer start
1491     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_virtual_address)[-1];
1492     btstack_memory_tracking_remove(buffer);
1493     free(buffer);
1494 }
1495 #endif
1496 
1497 
1498 // MARK: mesh_subnet_t
1499 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_SUBNETS)
1500     #if defined(MAX_NO_MESH_SUBNETS)
1501         #error "Deprecated MAX_NO_MESH_SUBNETS defined instead of MAX_NR_MESH_SUBNETS. Please update your btstack_config.h to use MAX_NR_MESH_SUBNETS."
1502     #else
1503         #define MAX_NR_MESH_SUBNETS 0
1504     #endif
1505 #endif
1506 
1507 #ifdef MAX_NR_MESH_SUBNETS
1508 #if MAX_NR_MESH_SUBNETS > 0
1509 static mesh_subnet_t mesh_subnet_storage[MAX_NR_MESH_SUBNETS];
1510 static btstack_memory_pool_t mesh_subnet_pool;
1511 mesh_subnet_t * btstack_memory_mesh_subnet_get(void){
1512     void * buffer = btstack_memory_pool_get(&mesh_subnet_pool);
1513     if (buffer){
1514         memset(buffer, 0, sizeof(mesh_subnet_t));
1515     }
1516     return (mesh_subnet_t *) buffer;
1517 }
1518 void btstack_memory_mesh_subnet_free(mesh_subnet_t *mesh_subnet){
1519     btstack_memory_pool_free(&mesh_subnet_pool, mesh_subnet);
1520 }
1521 #else
1522 mesh_subnet_t * btstack_memory_mesh_subnet_get(void){
1523     return NULL;
1524 }
1525 void btstack_memory_mesh_subnet_free(mesh_subnet_t *mesh_subnet){
1526     UNUSED(mesh_subnet);
1527 };
1528 #endif
1529 #elif defined(HAVE_MALLOC)
1530 
1531 typedef struct {
1532     btstack_memory_buffer_t tracking;
1533     mesh_subnet_t data;
1534 } btstack_memory_mesh_subnet_t;
1535 
1536 mesh_subnet_t * btstack_memory_mesh_subnet_get(void){
1537     btstack_memory_mesh_subnet_t * buffer = (btstack_memory_mesh_subnet_t *) malloc(sizeof(btstack_memory_mesh_subnet_t));
1538     if (buffer){
1539         memset(buffer, 0, sizeof(btstack_memory_mesh_subnet_t));
1540         btstack_memory_tracking_add(&buffer->tracking);
1541         return &buffer->data;
1542     } else {
1543         return NULL;
1544     }
1545 }
1546 void btstack_memory_mesh_subnet_free(mesh_subnet_t *mesh_subnet){
1547     // reconstruct buffer start
1548     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_subnet)[-1];
1549     btstack_memory_tracking_remove(buffer);
1550     free(buffer);
1551 }
1552 #endif
1553 
1554 
1555 #endif
1556 
1557 // init
1558 void btstack_memory_init(void){
1559 #ifdef HAVE_MALLOC
1560     // assert that there is no unexpected padding for combined buffer
1561     btstack_assert(sizeof(test_buffer_t) == sizeof(btstack_memory_buffer_t) + sizeof(void *));
1562 #endif
1563 
1564 #if MAX_NR_HCI_CONNECTIONS > 0
1565     btstack_memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NR_HCI_CONNECTIONS, sizeof(hci_connection_t));
1566 #endif
1567 #if MAX_NR_L2CAP_SERVICES > 0
1568     btstack_memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NR_L2CAP_SERVICES, sizeof(l2cap_service_t));
1569 #endif
1570 #if MAX_NR_L2CAP_CHANNELS > 0
1571     btstack_memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NR_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
1572 #endif
1573 #ifdef ENABLE_CLASSIC
1574 #if MAX_NR_RFCOMM_MULTIPLEXERS > 0
1575     btstack_memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NR_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
1576 #endif
1577 #if MAX_NR_RFCOMM_SERVICES > 0
1578     btstack_memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NR_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
1579 #endif
1580 #if MAX_NR_RFCOMM_CHANNELS > 0
1581     btstack_memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NR_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
1582 #endif
1583 #if MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES > 0
1584     btstack_memory_pool_create(&btstack_link_key_db_memory_entry_pool, btstack_link_key_db_memory_entry_storage, MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES, sizeof(btstack_link_key_db_memory_entry_t));
1585 #endif
1586 #if MAX_NR_BNEP_SERVICES > 0
1587     btstack_memory_pool_create(&bnep_service_pool, bnep_service_storage, MAX_NR_BNEP_SERVICES, sizeof(bnep_service_t));
1588 #endif
1589 #if MAX_NR_BNEP_CHANNELS > 0
1590     btstack_memory_pool_create(&bnep_channel_pool, bnep_channel_storage, MAX_NR_BNEP_CHANNELS, sizeof(bnep_channel_t));
1591 #endif
1592 #if MAX_NR_HFP_CONNECTIONS > 0
1593     btstack_memory_pool_create(&hfp_connection_pool, hfp_connection_storage, MAX_NR_HFP_CONNECTIONS, sizeof(hfp_connection_t));
1594 #endif
1595 #if MAX_NR_SERVICE_RECORD_ITEMS > 0
1596     btstack_memory_pool_create(&service_record_item_pool, service_record_item_storage, MAX_NR_SERVICE_RECORD_ITEMS, sizeof(service_record_item_t));
1597 #endif
1598 #if MAX_NR_AVDTP_STREAM_ENDPOINTS > 0
1599     btstack_memory_pool_create(&avdtp_stream_endpoint_pool, avdtp_stream_endpoint_storage, MAX_NR_AVDTP_STREAM_ENDPOINTS, sizeof(avdtp_stream_endpoint_t));
1600 #endif
1601 #if MAX_NR_AVDTP_CONNECTIONS > 0
1602     btstack_memory_pool_create(&avdtp_connection_pool, avdtp_connection_storage, MAX_NR_AVDTP_CONNECTIONS, sizeof(avdtp_connection_t));
1603 #endif
1604 #if MAX_NR_AVRCP_CONNECTIONS > 0
1605     btstack_memory_pool_create(&avrcp_connection_pool, avrcp_connection_storage, MAX_NR_AVRCP_CONNECTIONS, sizeof(avrcp_connection_t));
1606 #endif
1607 #if MAX_NR_AVRCP_BROWSING_CONNECTIONS > 0
1608     btstack_memory_pool_create(&avrcp_browsing_connection_pool, avrcp_browsing_connection_storage, MAX_NR_AVRCP_BROWSING_CONNECTIONS, sizeof(avrcp_browsing_connection_t));
1609 #endif
1610 #endif
1611 #ifdef ENABLE_BLE
1612 #if MAX_NR_GATT_CLIENTS > 0
1613     btstack_memory_pool_create(&gatt_client_pool, gatt_client_storage, MAX_NR_GATT_CLIENTS, sizeof(gatt_client_t));
1614 #endif
1615 #if MAX_NR_WHITELIST_ENTRIES > 0
1616     btstack_memory_pool_create(&whitelist_entry_pool, whitelist_entry_storage, MAX_NR_WHITELIST_ENTRIES, sizeof(whitelist_entry_t));
1617 #endif
1618 #if MAX_NR_SM_LOOKUP_ENTRIES > 0
1619     btstack_memory_pool_create(&sm_lookup_entry_pool, sm_lookup_entry_storage, MAX_NR_SM_LOOKUP_ENTRIES, sizeof(sm_lookup_entry_t));
1620 #endif
1621 #endif
1622 #ifdef ENABLE_MESH
1623 #if MAX_NR_MESH_NETWORK_PDUS > 0
1624     btstack_memory_pool_create(&mesh_network_pdu_pool, mesh_network_pdu_storage, MAX_NR_MESH_NETWORK_PDUS, sizeof(mesh_network_pdu_t));
1625 #endif
1626 #if MAX_NR_MESH_SEGMENTED_PDUS > 0
1627     btstack_memory_pool_create(&mesh_segmented_pdu_pool, mesh_segmented_pdu_storage, MAX_NR_MESH_SEGMENTED_PDUS, sizeof(mesh_segmented_pdu_t));
1628 #endif
1629 #if MAX_NR_MESH_UPPER_TRANSPORT_PDUS > 0
1630     btstack_memory_pool_create(&mesh_upper_transport_pdu_pool, mesh_upper_transport_pdu_storage, MAX_NR_MESH_UPPER_TRANSPORT_PDUS, sizeof(mesh_upper_transport_pdu_t));
1631 #endif
1632 #if MAX_NR_MESH_NETWORK_KEYS > 0
1633     btstack_memory_pool_create(&mesh_network_key_pool, mesh_network_key_storage, MAX_NR_MESH_NETWORK_KEYS, sizeof(mesh_network_key_t));
1634 #endif
1635 #if MAX_NR_MESH_TRANSPORT_KEYS > 0
1636     btstack_memory_pool_create(&mesh_transport_key_pool, mesh_transport_key_storage, MAX_NR_MESH_TRANSPORT_KEYS, sizeof(mesh_transport_key_t));
1637 #endif
1638 #if MAX_NR_MESH_VIRTUAL_ADDRESSS > 0
1639     btstack_memory_pool_create(&mesh_virtual_address_pool, mesh_virtual_address_storage, MAX_NR_MESH_VIRTUAL_ADDRESSS, sizeof(mesh_virtual_address_t));
1640 #endif
1641 #if MAX_NR_MESH_SUBNETS > 0
1642     btstack_memory_pool_create(&mesh_subnet_pool, mesh_subnet_storage, MAX_NR_MESH_SUBNETS, sizeof(mesh_subnet_t));
1643 #endif
1644 #endif
1645 }
1646