xref: /btstack/src/btstack_memory.c (revision 174a0c1c8d4a5183616f89c4f14b8b88926d49b0)
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: hid_host_connection_t
691 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_HID_HOST_CONNECTIONS)
692     #if defined(MAX_NO_HID_HOST_CONNECTIONS)
693         #error "Deprecated MAX_NO_HID_HOST_CONNECTIONS defined instead of MAX_NR_HID_HOST_CONNECTIONS. Please update your btstack_config.h to use MAX_NR_HID_HOST_CONNECTIONS."
694     #else
695         #define MAX_NR_HID_HOST_CONNECTIONS 0
696     #endif
697 #endif
698 
699 #ifdef MAX_NR_HID_HOST_CONNECTIONS
700 #if MAX_NR_HID_HOST_CONNECTIONS > 0
701 static hid_host_connection_t hid_host_connection_storage[MAX_NR_HID_HOST_CONNECTIONS];
702 static btstack_memory_pool_t hid_host_connection_pool;
703 hid_host_connection_t * btstack_memory_hid_host_connection_get(void){
704     void * buffer = btstack_memory_pool_get(&hid_host_connection_pool);
705     if (buffer){
706         memset(buffer, 0, sizeof(hid_host_connection_t));
707     }
708     return (hid_host_connection_t *) buffer;
709 }
710 void btstack_memory_hid_host_connection_free(hid_host_connection_t *hid_host_connection){
711     btstack_memory_pool_free(&hid_host_connection_pool, hid_host_connection);
712 }
713 #else
714 hid_host_connection_t * btstack_memory_hid_host_connection_get(void){
715     return NULL;
716 }
717 void btstack_memory_hid_host_connection_free(hid_host_connection_t *hid_host_connection){
718     UNUSED(hid_host_connection);
719 };
720 #endif
721 #elif defined(HAVE_MALLOC)
722 
723 typedef struct {
724     btstack_memory_buffer_t tracking;
725     hid_host_connection_t data;
726 } btstack_memory_hid_host_connection_t;
727 
728 hid_host_connection_t * btstack_memory_hid_host_connection_get(void){
729     btstack_memory_hid_host_connection_t * buffer = (btstack_memory_hid_host_connection_t *) malloc(sizeof(btstack_memory_hid_host_connection_t));
730     if (buffer){
731         memset(buffer, 0, sizeof(btstack_memory_hid_host_connection_t));
732         btstack_memory_tracking_add(&buffer->tracking);
733         return &buffer->data;
734     } else {
735         return NULL;
736     }
737 }
738 void btstack_memory_hid_host_connection_free(hid_host_connection_t *hid_host_connection){
739     // reconstruct buffer start
740     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) hid_host_connection)[-1];
741     btstack_memory_tracking_remove(buffer);
742     free(buffer);
743 }
744 #endif
745 
746 
747 
748 // MARK: service_record_item_t
749 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_SERVICE_RECORD_ITEMS)
750     #if defined(MAX_NO_SERVICE_RECORD_ITEMS)
751         #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."
752     #else
753         #define MAX_NR_SERVICE_RECORD_ITEMS 0
754     #endif
755 #endif
756 
757 #ifdef MAX_NR_SERVICE_RECORD_ITEMS
758 #if MAX_NR_SERVICE_RECORD_ITEMS > 0
759 static service_record_item_t service_record_item_storage[MAX_NR_SERVICE_RECORD_ITEMS];
760 static btstack_memory_pool_t service_record_item_pool;
761 service_record_item_t * btstack_memory_service_record_item_get(void){
762     void * buffer = btstack_memory_pool_get(&service_record_item_pool);
763     if (buffer){
764         memset(buffer, 0, sizeof(service_record_item_t));
765     }
766     return (service_record_item_t *) buffer;
767 }
768 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
769     btstack_memory_pool_free(&service_record_item_pool, service_record_item);
770 }
771 #else
772 service_record_item_t * btstack_memory_service_record_item_get(void){
773     return NULL;
774 }
775 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
776     UNUSED(service_record_item);
777 };
778 #endif
779 #elif defined(HAVE_MALLOC)
780 
781 typedef struct {
782     btstack_memory_buffer_t tracking;
783     service_record_item_t data;
784 } btstack_memory_service_record_item_t;
785 
786 service_record_item_t * btstack_memory_service_record_item_get(void){
787     btstack_memory_service_record_item_t * buffer = (btstack_memory_service_record_item_t *) malloc(sizeof(btstack_memory_service_record_item_t));
788     if (buffer){
789         memset(buffer, 0, sizeof(btstack_memory_service_record_item_t));
790         btstack_memory_tracking_add(&buffer->tracking);
791         return &buffer->data;
792     } else {
793         return NULL;
794     }
795 }
796 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
797     // reconstruct buffer start
798     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) service_record_item)[-1];
799     btstack_memory_tracking_remove(buffer);
800     free(buffer);
801 }
802 #endif
803 
804 
805 
806 // MARK: avdtp_stream_endpoint_t
807 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVDTP_STREAM_ENDPOINTS)
808     #if defined(MAX_NO_AVDTP_STREAM_ENDPOINTS)
809         #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."
810     #else
811         #define MAX_NR_AVDTP_STREAM_ENDPOINTS 0
812     #endif
813 #endif
814 
815 #ifdef MAX_NR_AVDTP_STREAM_ENDPOINTS
816 #if MAX_NR_AVDTP_STREAM_ENDPOINTS > 0
817 static avdtp_stream_endpoint_t avdtp_stream_endpoint_storage[MAX_NR_AVDTP_STREAM_ENDPOINTS];
818 static btstack_memory_pool_t avdtp_stream_endpoint_pool;
819 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){
820     void * buffer = btstack_memory_pool_get(&avdtp_stream_endpoint_pool);
821     if (buffer){
822         memset(buffer, 0, sizeof(avdtp_stream_endpoint_t));
823     }
824     return (avdtp_stream_endpoint_t *) buffer;
825 }
826 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){
827     btstack_memory_pool_free(&avdtp_stream_endpoint_pool, avdtp_stream_endpoint);
828 }
829 #else
830 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){
831     return NULL;
832 }
833 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){
834     UNUSED(avdtp_stream_endpoint);
835 };
836 #endif
837 #elif defined(HAVE_MALLOC)
838 
839 typedef struct {
840     btstack_memory_buffer_t tracking;
841     avdtp_stream_endpoint_t data;
842 } btstack_memory_avdtp_stream_endpoint_t;
843 
844 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){
845     btstack_memory_avdtp_stream_endpoint_t * buffer = (btstack_memory_avdtp_stream_endpoint_t *) malloc(sizeof(btstack_memory_avdtp_stream_endpoint_t));
846     if (buffer){
847         memset(buffer, 0, sizeof(btstack_memory_avdtp_stream_endpoint_t));
848         btstack_memory_tracking_add(&buffer->tracking);
849         return &buffer->data;
850     } else {
851         return NULL;
852     }
853 }
854 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){
855     // reconstruct buffer start
856     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) avdtp_stream_endpoint)[-1];
857     btstack_memory_tracking_remove(buffer);
858     free(buffer);
859 }
860 #endif
861 
862 
863 
864 // MARK: avdtp_connection_t
865 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVDTP_CONNECTIONS)
866     #if defined(MAX_NO_AVDTP_CONNECTIONS)
867         #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."
868     #else
869         #define MAX_NR_AVDTP_CONNECTIONS 0
870     #endif
871 #endif
872 
873 #ifdef MAX_NR_AVDTP_CONNECTIONS
874 #if MAX_NR_AVDTP_CONNECTIONS > 0
875 static avdtp_connection_t avdtp_connection_storage[MAX_NR_AVDTP_CONNECTIONS];
876 static btstack_memory_pool_t avdtp_connection_pool;
877 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){
878     void * buffer = btstack_memory_pool_get(&avdtp_connection_pool);
879     if (buffer){
880         memset(buffer, 0, sizeof(avdtp_connection_t));
881     }
882     return (avdtp_connection_t *) buffer;
883 }
884 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){
885     btstack_memory_pool_free(&avdtp_connection_pool, avdtp_connection);
886 }
887 #else
888 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){
889     return NULL;
890 }
891 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){
892     UNUSED(avdtp_connection);
893 };
894 #endif
895 #elif defined(HAVE_MALLOC)
896 
897 typedef struct {
898     btstack_memory_buffer_t tracking;
899     avdtp_connection_t data;
900 } btstack_memory_avdtp_connection_t;
901 
902 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){
903     btstack_memory_avdtp_connection_t * buffer = (btstack_memory_avdtp_connection_t *) malloc(sizeof(btstack_memory_avdtp_connection_t));
904     if (buffer){
905         memset(buffer, 0, sizeof(btstack_memory_avdtp_connection_t));
906         btstack_memory_tracking_add(&buffer->tracking);
907         return &buffer->data;
908     } else {
909         return NULL;
910     }
911 }
912 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){
913     // reconstruct buffer start
914     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) avdtp_connection)[-1];
915     btstack_memory_tracking_remove(buffer);
916     free(buffer);
917 }
918 #endif
919 
920 
921 
922 // MARK: avrcp_connection_t
923 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVRCP_CONNECTIONS)
924     #if defined(MAX_NO_AVRCP_CONNECTIONS)
925         #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."
926     #else
927         #define MAX_NR_AVRCP_CONNECTIONS 0
928     #endif
929 #endif
930 
931 #ifdef MAX_NR_AVRCP_CONNECTIONS
932 #if MAX_NR_AVRCP_CONNECTIONS > 0
933 static avrcp_connection_t avrcp_connection_storage[MAX_NR_AVRCP_CONNECTIONS];
934 static btstack_memory_pool_t avrcp_connection_pool;
935 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){
936     void * buffer = btstack_memory_pool_get(&avrcp_connection_pool);
937     if (buffer){
938         memset(buffer, 0, sizeof(avrcp_connection_t));
939     }
940     return (avrcp_connection_t *) buffer;
941 }
942 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){
943     btstack_memory_pool_free(&avrcp_connection_pool, avrcp_connection);
944 }
945 #else
946 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){
947     return NULL;
948 }
949 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){
950     UNUSED(avrcp_connection);
951 };
952 #endif
953 #elif defined(HAVE_MALLOC)
954 
955 typedef struct {
956     btstack_memory_buffer_t tracking;
957     avrcp_connection_t data;
958 } btstack_memory_avrcp_connection_t;
959 
960 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){
961     btstack_memory_avrcp_connection_t * buffer = (btstack_memory_avrcp_connection_t *) malloc(sizeof(btstack_memory_avrcp_connection_t));
962     if (buffer){
963         memset(buffer, 0, sizeof(btstack_memory_avrcp_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_connection_free(avrcp_connection_t *avrcp_connection){
971     // reconstruct buffer start
972     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) avrcp_connection)[-1];
973     btstack_memory_tracking_remove(buffer);
974     free(buffer);
975 }
976 #endif
977 
978 
979 
980 // MARK: avrcp_browsing_connection_t
981 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVRCP_BROWSING_CONNECTIONS)
982     #if defined(MAX_NO_AVRCP_BROWSING_CONNECTIONS)
983         #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."
984     #else
985         #define MAX_NR_AVRCP_BROWSING_CONNECTIONS 0
986     #endif
987 #endif
988 
989 #ifdef MAX_NR_AVRCP_BROWSING_CONNECTIONS
990 #if MAX_NR_AVRCP_BROWSING_CONNECTIONS > 0
991 static avrcp_browsing_connection_t avrcp_browsing_connection_storage[MAX_NR_AVRCP_BROWSING_CONNECTIONS];
992 static btstack_memory_pool_t avrcp_browsing_connection_pool;
993 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){
994     void * buffer = btstack_memory_pool_get(&avrcp_browsing_connection_pool);
995     if (buffer){
996         memset(buffer, 0, sizeof(avrcp_browsing_connection_t));
997     }
998     return (avrcp_browsing_connection_t *) buffer;
999 }
1000 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){
1001     btstack_memory_pool_free(&avrcp_browsing_connection_pool, avrcp_browsing_connection);
1002 }
1003 #else
1004 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){
1005     return NULL;
1006 }
1007 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){
1008     UNUSED(avrcp_browsing_connection);
1009 };
1010 #endif
1011 #elif defined(HAVE_MALLOC)
1012 
1013 typedef struct {
1014     btstack_memory_buffer_t tracking;
1015     avrcp_browsing_connection_t data;
1016 } btstack_memory_avrcp_browsing_connection_t;
1017 
1018 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){
1019     btstack_memory_avrcp_browsing_connection_t * buffer = (btstack_memory_avrcp_browsing_connection_t *) malloc(sizeof(btstack_memory_avrcp_browsing_connection_t));
1020     if (buffer){
1021         memset(buffer, 0, sizeof(btstack_memory_avrcp_browsing_connection_t));
1022         btstack_memory_tracking_add(&buffer->tracking);
1023         return &buffer->data;
1024     } else {
1025         return NULL;
1026     }
1027 }
1028 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){
1029     // reconstruct buffer start
1030     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) avrcp_browsing_connection)[-1];
1031     btstack_memory_tracking_remove(buffer);
1032     free(buffer);
1033 }
1034 #endif
1035 
1036 
1037 #endif
1038 #ifdef ENABLE_BLE
1039 
1040 // MARK: battery_service_client_t
1041 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BATTERY_SERVICE_CLIENTS)
1042     #if defined(MAX_NO_BATTERY_SERVICE_CLIENTS)
1043         #error "Deprecated MAX_NO_BATTERY_SERVICE_CLIENTS defined instead of MAX_NR_BATTERY_SERVICE_CLIENTS. Please update your btstack_config.h to use MAX_NR_BATTERY_SERVICE_CLIENTS."
1044     #else
1045         #define MAX_NR_BATTERY_SERVICE_CLIENTS 0
1046     #endif
1047 #endif
1048 
1049 #ifdef MAX_NR_BATTERY_SERVICE_CLIENTS
1050 #if MAX_NR_BATTERY_SERVICE_CLIENTS > 0
1051 static battery_service_client_t battery_service_client_storage[MAX_NR_BATTERY_SERVICE_CLIENTS];
1052 static btstack_memory_pool_t battery_service_client_pool;
1053 battery_service_client_t * btstack_memory_battery_service_client_get(void){
1054     void * buffer = btstack_memory_pool_get(&battery_service_client_pool);
1055     if (buffer){
1056         memset(buffer, 0, sizeof(battery_service_client_t));
1057     }
1058     return (battery_service_client_t *) buffer;
1059 }
1060 void btstack_memory_battery_service_client_free(battery_service_client_t *battery_service_client){
1061     btstack_memory_pool_free(&battery_service_client_pool, battery_service_client);
1062 }
1063 #else
1064 battery_service_client_t * btstack_memory_battery_service_client_get(void){
1065     return NULL;
1066 }
1067 void btstack_memory_battery_service_client_free(battery_service_client_t *battery_service_client){
1068     UNUSED(battery_service_client);
1069 };
1070 #endif
1071 #elif defined(HAVE_MALLOC)
1072 
1073 typedef struct {
1074     btstack_memory_buffer_t tracking;
1075     battery_service_client_t data;
1076 } btstack_memory_battery_service_client_t;
1077 
1078 battery_service_client_t * btstack_memory_battery_service_client_get(void){
1079     btstack_memory_battery_service_client_t * buffer = (btstack_memory_battery_service_client_t *) malloc(sizeof(btstack_memory_battery_service_client_t));
1080     if (buffer){
1081         memset(buffer, 0, sizeof(btstack_memory_battery_service_client_t));
1082         btstack_memory_tracking_add(&buffer->tracking);
1083         return &buffer->data;
1084     } else {
1085         return NULL;
1086     }
1087 }
1088 void btstack_memory_battery_service_client_free(battery_service_client_t *battery_service_client){
1089     // reconstruct buffer start
1090     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) battery_service_client)[-1];
1091     btstack_memory_tracking_remove(buffer);
1092     free(buffer);
1093 }
1094 #endif
1095 
1096 
1097 // MARK: gatt_client_t
1098 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_GATT_CLIENTS)
1099     #if defined(MAX_NO_GATT_CLIENTS)
1100         #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."
1101     #else
1102         #define MAX_NR_GATT_CLIENTS 0
1103     #endif
1104 #endif
1105 
1106 #ifdef MAX_NR_GATT_CLIENTS
1107 #if MAX_NR_GATT_CLIENTS > 0
1108 static gatt_client_t gatt_client_storage[MAX_NR_GATT_CLIENTS];
1109 static btstack_memory_pool_t gatt_client_pool;
1110 gatt_client_t * btstack_memory_gatt_client_get(void){
1111     void * buffer = btstack_memory_pool_get(&gatt_client_pool);
1112     if (buffer){
1113         memset(buffer, 0, sizeof(gatt_client_t));
1114     }
1115     return (gatt_client_t *) buffer;
1116 }
1117 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
1118     btstack_memory_pool_free(&gatt_client_pool, gatt_client);
1119 }
1120 #else
1121 gatt_client_t * btstack_memory_gatt_client_get(void){
1122     return NULL;
1123 }
1124 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
1125     UNUSED(gatt_client);
1126 };
1127 #endif
1128 #elif defined(HAVE_MALLOC)
1129 
1130 typedef struct {
1131     btstack_memory_buffer_t tracking;
1132     gatt_client_t data;
1133 } btstack_memory_gatt_client_t;
1134 
1135 gatt_client_t * btstack_memory_gatt_client_get(void){
1136     btstack_memory_gatt_client_t * buffer = (btstack_memory_gatt_client_t *) malloc(sizeof(btstack_memory_gatt_client_t));
1137     if (buffer){
1138         memset(buffer, 0, sizeof(btstack_memory_gatt_client_t));
1139         btstack_memory_tracking_add(&buffer->tracking);
1140         return &buffer->data;
1141     } else {
1142         return NULL;
1143     }
1144 }
1145 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
1146     // reconstruct buffer start
1147     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) gatt_client)[-1];
1148     btstack_memory_tracking_remove(buffer);
1149     free(buffer);
1150 }
1151 #endif
1152 
1153 
1154 // MARK: sm_lookup_entry_t
1155 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_SM_LOOKUP_ENTRIES)
1156     #if defined(MAX_NO_SM_LOOKUP_ENTRIES)
1157         #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."
1158     #else
1159         #define MAX_NR_SM_LOOKUP_ENTRIES 0
1160     #endif
1161 #endif
1162 
1163 #ifdef MAX_NR_SM_LOOKUP_ENTRIES
1164 #if MAX_NR_SM_LOOKUP_ENTRIES > 0
1165 static sm_lookup_entry_t sm_lookup_entry_storage[MAX_NR_SM_LOOKUP_ENTRIES];
1166 static btstack_memory_pool_t sm_lookup_entry_pool;
1167 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
1168     void * buffer = btstack_memory_pool_get(&sm_lookup_entry_pool);
1169     if (buffer){
1170         memset(buffer, 0, sizeof(sm_lookup_entry_t));
1171     }
1172     return (sm_lookup_entry_t *) buffer;
1173 }
1174 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
1175     btstack_memory_pool_free(&sm_lookup_entry_pool, sm_lookup_entry);
1176 }
1177 #else
1178 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
1179     return NULL;
1180 }
1181 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
1182     UNUSED(sm_lookup_entry);
1183 };
1184 #endif
1185 #elif defined(HAVE_MALLOC)
1186 
1187 typedef struct {
1188     btstack_memory_buffer_t tracking;
1189     sm_lookup_entry_t data;
1190 } btstack_memory_sm_lookup_entry_t;
1191 
1192 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
1193     btstack_memory_sm_lookup_entry_t * buffer = (btstack_memory_sm_lookup_entry_t *) malloc(sizeof(btstack_memory_sm_lookup_entry_t));
1194     if (buffer){
1195         memset(buffer, 0, sizeof(btstack_memory_sm_lookup_entry_t));
1196         btstack_memory_tracking_add(&buffer->tracking);
1197         return &buffer->data;
1198     } else {
1199         return NULL;
1200     }
1201 }
1202 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
1203     // reconstruct buffer start
1204     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) sm_lookup_entry)[-1];
1205     btstack_memory_tracking_remove(buffer);
1206     free(buffer);
1207 }
1208 #endif
1209 
1210 
1211 // MARK: whitelist_entry_t
1212 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_WHITELIST_ENTRIES)
1213     #if defined(MAX_NO_WHITELIST_ENTRIES)
1214         #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."
1215     #else
1216         #define MAX_NR_WHITELIST_ENTRIES 0
1217     #endif
1218 #endif
1219 
1220 #ifdef MAX_NR_WHITELIST_ENTRIES
1221 #if MAX_NR_WHITELIST_ENTRIES > 0
1222 static whitelist_entry_t whitelist_entry_storage[MAX_NR_WHITELIST_ENTRIES];
1223 static btstack_memory_pool_t whitelist_entry_pool;
1224 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
1225     void * buffer = btstack_memory_pool_get(&whitelist_entry_pool);
1226     if (buffer){
1227         memset(buffer, 0, sizeof(whitelist_entry_t));
1228     }
1229     return (whitelist_entry_t *) buffer;
1230 }
1231 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
1232     btstack_memory_pool_free(&whitelist_entry_pool, whitelist_entry);
1233 }
1234 #else
1235 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
1236     return NULL;
1237 }
1238 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
1239     UNUSED(whitelist_entry);
1240 };
1241 #endif
1242 #elif defined(HAVE_MALLOC)
1243 
1244 typedef struct {
1245     btstack_memory_buffer_t tracking;
1246     whitelist_entry_t data;
1247 } btstack_memory_whitelist_entry_t;
1248 
1249 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
1250     btstack_memory_whitelist_entry_t * buffer = (btstack_memory_whitelist_entry_t *) malloc(sizeof(btstack_memory_whitelist_entry_t));
1251     if (buffer){
1252         memset(buffer, 0, sizeof(btstack_memory_whitelist_entry_t));
1253         btstack_memory_tracking_add(&buffer->tracking);
1254         return &buffer->data;
1255     } else {
1256         return NULL;
1257     }
1258 }
1259 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
1260     // reconstruct buffer start
1261     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) whitelist_entry)[-1];
1262     btstack_memory_tracking_remove(buffer);
1263     free(buffer);
1264 }
1265 #endif
1266 
1267 
1268 #endif
1269 #ifdef ENABLE_MESH
1270 
1271 // MARK: mesh_network_pdu_t
1272 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_NETWORK_PDUS)
1273     #if defined(MAX_NO_MESH_NETWORK_PDUS)
1274         #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."
1275     #else
1276         #define MAX_NR_MESH_NETWORK_PDUS 0
1277     #endif
1278 #endif
1279 
1280 #ifdef MAX_NR_MESH_NETWORK_PDUS
1281 #if MAX_NR_MESH_NETWORK_PDUS > 0
1282 static mesh_network_pdu_t mesh_network_pdu_storage[MAX_NR_MESH_NETWORK_PDUS];
1283 static btstack_memory_pool_t mesh_network_pdu_pool;
1284 mesh_network_pdu_t * btstack_memory_mesh_network_pdu_get(void){
1285     void * buffer = btstack_memory_pool_get(&mesh_network_pdu_pool);
1286     if (buffer){
1287         memset(buffer, 0, sizeof(mesh_network_pdu_t));
1288     }
1289     return (mesh_network_pdu_t *) buffer;
1290 }
1291 void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){
1292     btstack_memory_pool_free(&mesh_network_pdu_pool, mesh_network_pdu);
1293 }
1294 #else
1295 mesh_network_pdu_t * btstack_memory_mesh_network_pdu_get(void){
1296     return NULL;
1297 }
1298 void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){
1299     UNUSED(mesh_network_pdu);
1300 };
1301 #endif
1302 #elif defined(HAVE_MALLOC)
1303 
1304 typedef struct {
1305     btstack_memory_buffer_t tracking;
1306     mesh_network_pdu_t data;
1307 } btstack_memory_mesh_network_pdu_t;
1308 
1309 mesh_network_pdu_t * btstack_memory_mesh_network_pdu_get(void){
1310     btstack_memory_mesh_network_pdu_t * buffer = (btstack_memory_mesh_network_pdu_t *) malloc(sizeof(btstack_memory_mesh_network_pdu_t));
1311     if (buffer){
1312         memset(buffer, 0, sizeof(btstack_memory_mesh_network_pdu_t));
1313         btstack_memory_tracking_add(&buffer->tracking);
1314         return &buffer->data;
1315     } else {
1316         return NULL;
1317     }
1318 }
1319 void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){
1320     // reconstruct buffer start
1321     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_network_pdu)[-1];
1322     btstack_memory_tracking_remove(buffer);
1323     free(buffer);
1324 }
1325 #endif
1326 
1327 
1328 // MARK: mesh_segmented_pdu_t
1329 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_SEGMENTED_PDUS)
1330     #if defined(MAX_NO_MESH_SEGMENTED_PDUS)
1331         #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."
1332     #else
1333         #define MAX_NR_MESH_SEGMENTED_PDUS 0
1334     #endif
1335 #endif
1336 
1337 #ifdef MAX_NR_MESH_SEGMENTED_PDUS
1338 #if MAX_NR_MESH_SEGMENTED_PDUS > 0
1339 static mesh_segmented_pdu_t mesh_segmented_pdu_storage[MAX_NR_MESH_SEGMENTED_PDUS];
1340 static btstack_memory_pool_t mesh_segmented_pdu_pool;
1341 mesh_segmented_pdu_t * btstack_memory_mesh_segmented_pdu_get(void){
1342     void * buffer = btstack_memory_pool_get(&mesh_segmented_pdu_pool);
1343     if (buffer){
1344         memset(buffer, 0, sizeof(mesh_segmented_pdu_t));
1345     }
1346     return (mesh_segmented_pdu_t *) buffer;
1347 }
1348 void btstack_memory_mesh_segmented_pdu_free(mesh_segmented_pdu_t *mesh_segmented_pdu){
1349     btstack_memory_pool_free(&mesh_segmented_pdu_pool, mesh_segmented_pdu);
1350 }
1351 #else
1352 mesh_segmented_pdu_t * btstack_memory_mesh_segmented_pdu_get(void){
1353     return NULL;
1354 }
1355 void btstack_memory_mesh_segmented_pdu_free(mesh_segmented_pdu_t *mesh_segmented_pdu){
1356     UNUSED(mesh_segmented_pdu);
1357 };
1358 #endif
1359 #elif defined(HAVE_MALLOC)
1360 
1361 typedef struct {
1362     btstack_memory_buffer_t tracking;
1363     mesh_segmented_pdu_t data;
1364 } btstack_memory_mesh_segmented_pdu_t;
1365 
1366 mesh_segmented_pdu_t * btstack_memory_mesh_segmented_pdu_get(void){
1367     btstack_memory_mesh_segmented_pdu_t * buffer = (btstack_memory_mesh_segmented_pdu_t *) malloc(sizeof(btstack_memory_mesh_segmented_pdu_t));
1368     if (buffer){
1369         memset(buffer, 0, sizeof(btstack_memory_mesh_segmented_pdu_t));
1370         btstack_memory_tracking_add(&buffer->tracking);
1371         return &buffer->data;
1372     } else {
1373         return NULL;
1374     }
1375 }
1376 void btstack_memory_mesh_segmented_pdu_free(mesh_segmented_pdu_t *mesh_segmented_pdu){
1377     // reconstruct buffer start
1378     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_segmented_pdu)[-1];
1379     btstack_memory_tracking_remove(buffer);
1380     free(buffer);
1381 }
1382 #endif
1383 
1384 
1385 // MARK: mesh_upper_transport_pdu_t
1386 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_UPPER_TRANSPORT_PDUS)
1387     #if defined(MAX_NO_MESH_UPPER_TRANSPORT_PDUS)
1388         #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."
1389     #else
1390         #define MAX_NR_MESH_UPPER_TRANSPORT_PDUS 0
1391     #endif
1392 #endif
1393 
1394 #ifdef MAX_NR_MESH_UPPER_TRANSPORT_PDUS
1395 #if MAX_NR_MESH_UPPER_TRANSPORT_PDUS > 0
1396 static mesh_upper_transport_pdu_t mesh_upper_transport_pdu_storage[MAX_NR_MESH_UPPER_TRANSPORT_PDUS];
1397 static btstack_memory_pool_t mesh_upper_transport_pdu_pool;
1398 mesh_upper_transport_pdu_t * btstack_memory_mesh_upper_transport_pdu_get(void){
1399     void * buffer = btstack_memory_pool_get(&mesh_upper_transport_pdu_pool);
1400     if (buffer){
1401         memset(buffer, 0, sizeof(mesh_upper_transport_pdu_t));
1402     }
1403     return (mesh_upper_transport_pdu_t *) buffer;
1404 }
1405 void btstack_memory_mesh_upper_transport_pdu_free(mesh_upper_transport_pdu_t *mesh_upper_transport_pdu){
1406     btstack_memory_pool_free(&mesh_upper_transport_pdu_pool, mesh_upper_transport_pdu);
1407 }
1408 #else
1409 mesh_upper_transport_pdu_t * btstack_memory_mesh_upper_transport_pdu_get(void){
1410     return NULL;
1411 }
1412 void btstack_memory_mesh_upper_transport_pdu_free(mesh_upper_transport_pdu_t *mesh_upper_transport_pdu){
1413     UNUSED(mesh_upper_transport_pdu);
1414 };
1415 #endif
1416 #elif defined(HAVE_MALLOC)
1417 
1418 typedef struct {
1419     btstack_memory_buffer_t tracking;
1420     mesh_upper_transport_pdu_t data;
1421 } btstack_memory_mesh_upper_transport_pdu_t;
1422 
1423 mesh_upper_transport_pdu_t * btstack_memory_mesh_upper_transport_pdu_get(void){
1424     btstack_memory_mesh_upper_transport_pdu_t * buffer = (btstack_memory_mesh_upper_transport_pdu_t *) malloc(sizeof(btstack_memory_mesh_upper_transport_pdu_t));
1425     if (buffer){
1426         memset(buffer, 0, sizeof(btstack_memory_mesh_upper_transport_pdu_t));
1427         btstack_memory_tracking_add(&buffer->tracking);
1428         return &buffer->data;
1429     } else {
1430         return NULL;
1431     }
1432 }
1433 void btstack_memory_mesh_upper_transport_pdu_free(mesh_upper_transport_pdu_t *mesh_upper_transport_pdu){
1434     // reconstruct buffer start
1435     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_upper_transport_pdu)[-1];
1436     btstack_memory_tracking_remove(buffer);
1437     free(buffer);
1438 }
1439 #endif
1440 
1441 
1442 // MARK: mesh_network_key_t
1443 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_NETWORK_KEYS)
1444     #if defined(MAX_NO_MESH_NETWORK_KEYS)
1445         #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."
1446     #else
1447         #define MAX_NR_MESH_NETWORK_KEYS 0
1448     #endif
1449 #endif
1450 
1451 #ifdef MAX_NR_MESH_NETWORK_KEYS
1452 #if MAX_NR_MESH_NETWORK_KEYS > 0
1453 static mesh_network_key_t mesh_network_key_storage[MAX_NR_MESH_NETWORK_KEYS];
1454 static btstack_memory_pool_t mesh_network_key_pool;
1455 mesh_network_key_t * btstack_memory_mesh_network_key_get(void){
1456     void * buffer = btstack_memory_pool_get(&mesh_network_key_pool);
1457     if (buffer){
1458         memset(buffer, 0, sizeof(mesh_network_key_t));
1459     }
1460     return (mesh_network_key_t *) buffer;
1461 }
1462 void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){
1463     btstack_memory_pool_free(&mesh_network_key_pool, mesh_network_key);
1464 }
1465 #else
1466 mesh_network_key_t * btstack_memory_mesh_network_key_get(void){
1467     return NULL;
1468 }
1469 void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){
1470     UNUSED(mesh_network_key);
1471 };
1472 #endif
1473 #elif defined(HAVE_MALLOC)
1474 
1475 typedef struct {
1476     btstack_memory_buffer_t tracking;
1477     mesh_network_key_t data;
1478 } btstack_memory_mesh_network_key_t;
1479 
1480 mesh_network_key_t * btstack_memory_mesh_network_key_get(void){
1481     btstack_memory_mesh_network_key_t * buffer = (btstack_memory_mesh_network_key_t *) malloc(sizeof(btstack_memory_mesh_network_key_t));
1482     if (buffer){
1483         memset(buffer, 0, sizeof(btstack_memory_mesh_network_key_t));
1484         btstack_memory_tracking_add(&buffer->tracking);
1485         return &buffer->data;
1486     } else {
1487         return NULL;
1488     }
1489 }
1490 void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){
1491     // reconstruct buffer start
1492     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_network_key)[-1];
1493     btstack_memory_tracking_remove(buffer);
1494     free(buffer);
1495 }
1496 #endif
1497 
1498 
1499 // MARK: mesh_transport_key_t
1500 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_TRANSPORT_KEYS)
1501     #if defined(MAX_NO_MESH_TRANSPORT_KEYS)
1502         #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."
1503     #else
1504         #define MAX_NR_MESH_TRANSPORT_KEYS 0
1505     #endif
1506 #endif
1507 
1508 #ifdef MAX_NR_MESH_TRANSPORT_KEYS
1509 #if MAX_NR_MESH_TRANSPORT_KEYS > 0
1510 static mesh_transport_key_t mesh_transport_key_storage[MAX_NR_MESH_TRANSPORT_KEYS];
1511 static btstack_memory_pool_t mesh_transport_key_pool;
1512 mesh_transport_key_t * btstack_memory_mesh_transport_key_get(void){
1513     void * buffer = btstack_memory_pool_get(&mesh_transport_key_pool);
1514     if (buffer){
1515         memset(buffer, 0, sizeof(mesh_transport_key_t));
1516     }
1517     return (mesh_transport_key_t *) buffer;
1518 }
1519 void btstack_memory_mesh_transport_key_free(mesh_transport_key_t *mesh_transport_key){
1520     btstack_memory_pool_free(&mesh_transport_key_pool, mesh_transport_key);
1521 }
1522 #else
1523 mesh_transport_key_t * btstack_memory_mesh_transport_key_get(void){
1524     return NULL;
1525 }
1526 void btstack_memory_mesh_transport_key_free(mesh_transport_key_t *mesh_transport_key){
1527     UNUSED(mesh_transport_key);
1528 };
1529 #endif
1530 #elif defined(HAVE_MALLOC)
1531 
1532 typedef struct {
1533     btstack_memory_buffer_t tracking;
1534     mesh_transport_key_t data;
1535 } btstack_memory_mesh_transport_key_t;
1536 
1537 mesh_transport_key_t * btstack_memory_mesh_transport_key_get(void){
1538     btstack_memory_mesh_transport_key_t * buffer = (btstack_memory_mesh_transport_key_t *) malloc(sizeof(btstack_memory_mesh_transport_key_t));
1539     if (buffer){
1540         memset(buffer, 0, sizeof(btstack_memory_mesh_transport_key_t));
1541         btstack_memory_tracking_add(&buffer->tracking);
1542         return &buffer->data;
1543     } else {
1544         return NULL;
1545     }
1546 }
1547 void btstack_memory_mesh_transport_key_free(mesh_transport_key_t *mesh_transport_key){
1548     // reconstruct buffer start
1549     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_transport_key)[-1];
1550     btstack_memory_tracking_remove(buffer);
1551     free(buffer);
1552 }
1553 #endif
1554 
1555 
1556 // MARK: mesh_virtual_address_t
1557 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_VIRTUAL_ADDRESSS)
1558     #if defined(MAX_NO_MESH_VIRTUAL_ADDRESSS)
1559         #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."
1560     #else
1561         #define MAX_NR_MESH_VIRTUAL_ADDRESSS 0
1562     #endif
1563 #endif
1564 
1565 #ifdef MAX_NR_MESH_VIRTUAL_ADDRESSS
1566 #if MAX_NR_MESH_VIRTUAL_ADDRESSS > 0
1567 static mesh_virtual_address_t mesh_virtual_address_storage[MAX_NR_MESH_VIRTUAL_ADDRESSS];
1568 static btstack_memory_pool_t mesh_virtual_address_pool;
1569 mesh_virtual_address_t * btstack_memory_mesh_virtual_address_get(void){
1570     void * buffer = btstack_memory_pool_get(&mesh_virtual_address_pool);
1571     if (buffer){
1572         memset(buffer, 0, sizeof(mesh_virtual_address_t));
1573     }
1574     return (mesh_virtual_address_t *) buffer;
1575 }
1576 void btstack_memory_mesh_virtual_address_free(mesh_virtual_address_t *mesh_virtual_address){
1577     btstack_memory_pool_free(&mesh_virtual_address_pool, mesh_virtual_address);
1578 }
1579 #else
1580 mesh_virtual_address_t * btstack_memory_mesh_virtual_address_get(void){
1581     return NULL;
1582 }
1583 void btstack_memory_mesh_virtual_address_free(mesh_virtual_address_t *mesh_virtual_address){
1584     UNUSED(mesh_virtual_address);
1585 };
1586 #endif
1587 #elif defined(HAVE_MALLOC)
1588 
1589 typedef struct {
1590     btstack_memory_buffer_t tracking;
1591     mesh_virtual_address_t data;
1592 } btstack_memory_mesh_virtual_address_t;
1593 
1594 mesh_virtual_address_t * btstack_memory_mesh_virtual_address_get(void){
1595     btstack_memory_mesh_virtual_address_t * buffer = (btstack_memory_mesh_virtual_address_t *) malloc(sizeof(btstack_memory_mesh_virtual_address_t));
1596     if (buffer){
1597         memset(buffer, 0, sizeof(btstack_memory_mesh_virtual_address_t));
1598         btstack_memory_tracking_add(&buffer->tracking);
1599         return &buffer->data;
1600     } else {
1601         return NULL;
1602     }
1603 }
1604 void btstack_memory_mesh_virtual_address_free(mesh_virtual_address_t *mesh_virtual_address){
1605     // reconstruct buffer start
1606     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_virtual_address)[-1];
1607     btstack_memory_tracking_remove(buffer);
1608     free(buffer);
1609 }
1610 #endif
1611 
1612 
1613 // MARK: mesh_subnet_t
1614 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_SUBNETS)
1615     #if defined(MAX_NO_MESH_SUBNETS)
1616         #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."
1617     #else
1618         #define MAX_NR_MESH_SUBNETS 0
1619     #endif
1620 #endif
1621 
1622 #ifdef MAX_NR_MESH_SUBNETS
1623 #if MAX_NR_MESH_SUBNETS > 0
1624 static mesh_subnet_t mesh_subnet_storage[MAX_NR_MESH_SUBNETS];
1625 static btstack_memory_pool_t mesh_subnet_pool;
1626 mesh_subnet_t * btstack_memory_mesh_subnet_get(void){
1627     void * buffer = btstack_memory_pool_get(&mesh_subnet_pool);
1628     if (buffer){
1629         memset(buffer, 0, sizeof(mesh_subnet_t));
1630     }
1631     return (mesh_subnet_t *) buffer;
1632 }
1633 void btstack_memory_mesh_subnet_free(mesh_subnet_t *mesh_subnet){
1634     btstack_memory_pool_free(&mesh_subnet_pool, mesh_subnet);
1635 }
1636 #else
1637 mesh_subnet_t * btstack_memory_mesh_subnet_get(void){
1638     return NULL;
1639 }
1640 void btstack_memory_mesh_subnet_free(mesh_subnet_t *mesh_subnet){
1641     UNUSED(mesh_subnet);
1642 };
1643 #endif
1644 #elif defined(HAVE_MALLOC)
1645 
1646 typedef struct {
1647     btstack_memory_buffer_t tracking;
1648     mesh_subnet_t data;
1649 } btstack_memory_mesh_subnet_t;
1650 
1651 mesh_subnet_t * btstack_memory_mesh_subnet_get(void){
1652     btstack_memory_mesh_subnet_t * buffer = (btstack_memory_mesh_subnet_t *) malloc(sizeof(btstack_memory_mesh_subnet_t));
1653     if (buffer){
1654         memset(buffer, 0, sizeof(btstack_memory_mesh_subnet_t));
1655         btstack_memory_tracking_add(&buffer->tracking);
1656         return &buffer->data;
1657     } else {
1658         return NULL;
1659     }
1660 }
1661 void btstack_memory_mesh_subnet_free(mesh_subnet_t *mesh_subnet){
1662     // reconstruct buffer start
1663     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_subnet)[-1];
1664     btstack_memory_tracking_remove(buffer);
1665     free(buffer);
1666 }
1667 #endif
1668 
1669 
1670 #endif
1671 
1672 // init
1673 void btstack_memory_init(void){
1674 #ifdef HAVE_MALLOC
1675     // assert that there is no unexpected padding for combined buffer
1676     btstack_assert(sizeof(test_buffer_t) == sizeof(btstack_memory_buffer_t) + sizeof(void *));
1677 #endif
1678 
1679 #if MAX_NR_HCI_CONNECTIONS > 0
1680     btstack_memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NR_HCI_CONNECTIONS, sizeof(hci_connection_t));
1681 #endif
1682 #if MAX_NR_L2CAP_SERVICES > 0
1683     btstack_memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NR_L2CAP_SERVICES, sizeof(l2cap_service_t));
1684 #endif
1685 #if MAX_NR_L2CAP_CHANNELS > 0
1686     btstack_memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NR_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
1687 #endif
1688 #ifdef ENABLE_CLASSIC
1689 #if MAX_NR_RFCOMM_MULTIPLEXERS > 0
1690     btstack_memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NR_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
1691 #endif
1692 #if MAX_NR_RFCOMM_SERVICES > 0
1693     btstack_memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NR_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
1694 #endif
1695 #if MAX_NR_RFCOMM_CHANNELS > 0
1696     btstack_memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NR_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
1697 #endif
1698 #if MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES > 0
1699     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));
1700 #endif
1701 #if MAX_NR_BNEP_SERVICES > 0
1702     btstack_memory_pool_create(&bnep_service_pool, bnep_service_storage, MAX_NR_BNEP_SERVICES, sizeof(bnep_service_t));
1703 #endif
1704 #if MAX_NR_BNEP_CHANNELS > 0
1705     btstack_memory_pool_create(&bnep_channel_pool, bnep_channel_storage, MAX_NR_BNEP_CHANNELS, sizeof(bnep_channel_t));
1706 #endif
1707 #if MAX_NR_HFP_CONNECTIONS > 0
1708     btstack_memory_pool_create(&hfp_connection_pool, hfp_connection_storage, MAX_NR_HFP_CONNECTIONS, sizeof(hfp_connection_t));
1709 #endif
1710 #if MAX_NR_HID_HOST_CONNECTIONS > 0
1711     btstack_memory_pool_create(&hid_host_connection_pool, hid_host_connection_storage, MAX_NR_HID_HOST_CONNECTIONS, sizeof(hid_host_connection_t));
1712 #endif
1713 #if MAX_NR_SERVICE_RECORD_ITEMS > 0
1714     btstack_memory_pool_create(&service_record_item_pool, service_record_item_storage, MAX_NR_SERVICE_RECORD_ITEMS, sizeof(service_record_item_t));
1715 #endif
1716 #if MAX_NR_AVDTP_STREAM_ENDPOINTS > 0
1717     btstack_memory_pool_create(&avdtp_stream_endpoint_pool, avdtp_stream_endpoint_storage, MAX_NR_AVDTP_STREAM_ENDPOINTS, sizeof(avdtp_stream_endpoint_t));
1718 #endif
1719 #if MAX_NR_AVDTP_CONNECTIONS > 0
1720     btstack_memory_pool_create(&avdtp_connection_pool, avdtp_connection_storage, MAX_NR_AVDTP_CONNECTIONS, sizeof(avdtp_connection_t));
1721 #endif
1722 #if MAX_NR_AVRCP_CONNECTIONS > 0
1723     btstack_memory_pool_create(&avrcp_connection_pool, avrcp_connection_storage, MAX_NR_AVRCP_CONNECTIONS, sizeof(avrcp_connection_t));
1724 #endif
1725 #if MAX_NR_AVRCP_BROWSING_CONNECTIONS > 0
1726     btstack_memory_pool_create(&avrcp_browsing_connection_pool, avrcp_browsing_connection_storage, MAX_NR_AVRCP_BROWSING_CONNECTIONS, sizeof(avrcp_browsing_connection_t));
1727 #endif
1728 #endif
1729 #ifdef ENABLE_BLE
1730 #if MAX_NR_BATTERY_SERVICE_CLIENTS > 0
1731     btstack_memory_pool_create(&battery_service_client_pool, battery_service_client_storage, MAX_NR_BATTERY_SERVICE_CLIENTS, sizeof(battery_service_client_t));
1732 #endif
1733 #if MAX_NR_GATT_CLIENTS > 0
1734     btstack_memory_pool_create(&gatt_client_pool, gatt_client_storage, MAX_NR_GATT_CLIENTS, sizeof(gatt_client_t));
1735 #endif
1736 #if MAX_NR_SM_LOOKUP_ENTRIES > 0
1737     btstack_memory_pool_create(&sm_lookup_entry_pool, sm_lookup_entry_storage, MAX_NR_SM_LOOKUP_ENTRIES, sizeof(sm_lookup_entry_t));
1738 #endif
1739 #if MAX_NR_WHITELIST_ENTRIES > 0
1740     btstack_memory_pool_create(&whitelist_entry_pool, whitelist_entry_storage, MAX_NR_WHITELIST_ENTRIES, sizeof(whitelist_entry_t));
1741 #endif
1742 #endif
1743 #ifdef ENABLE_MESH
1744 #if MAX_NR_MESH_NETWORK_PDUS > 0
1745     btstack_memory_pool_create(&mesh_network_pdu_pool, mesh_network_pdu_storage, MAX_NR_MESH_NETWORK_PDUS, sizeof(mesh_network_pdu_t));
1746 #endif
1747 #if MAX_NR_MESH_SEGMENTED_PDUS > 0
1748     btstack_memory_pool_create(&mesh_segmented_pdu_pool, mesh_segmented_pdu_storage, MAX_NR_MESH_SEGMENTED_PDUS, sizeof(mesh_segmented_pdu_t));
1749 #endif
1750 #if MAX_NR_MESH_UPPER_TRANSPORT_PDUS > 0
1751     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));
1752 #endif
1753 #if MAX_NR_MESH_NETWORK_KEYS > 0
1754     btstack_memory_pool_create(&mesh_network_key_pool, mesh_network_key_storage, MAX_NR_MESH_NETWORK_KEYS, sizeof(mesh_network_key_t));
1755 #endif
1756 #if MAX_NR_MESH_TRANSPORT_KEYS > 0
1757     btstack_memory_pool_create(&mesh_transport_key_pool, mesh_transport_key_storage, MAX_NR_MESH_TRANSPORT_KEYS, sizeof(mesh_transport_key_t));
1758 #endif
1759 #if MAX_NR_MESH_VIRTUAL_ADDRESSS > 0
1760     btstack_memory_pool_create(&mesh_virtual_address_pool, mesh_virtual_address_storage, MAX_NR_MESH_VIRTUAL_ADDRESSS, sizeof(mesh_virtual_address_t));
1761 #endif
1762 #if MAX_NR_MESH_SUBNETS > 0
1763     btstack_memory_pool_create(&mesh_subnet_pool, mesh_subnet_storage, MAX_NR_MESH_SUBNETS, sizeof(mesh_subnet_t));
1764 #endif
1765 #endif
1766 }
1767