xref: /btstack/src/btstack_memory.c (revision b149c1b9276b0e9d738e8038776af3dded42725e)
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 ENABLE_MALLOC_TEST
59 extern "C" void * test_malloc(size_t size);
60 #define malloc test_malloc
61 #endif
62 
63 #ifdef HAVE_MALLOC
64 typedef struct btstack_memory_buffer {
65     struct btstack_memory_buffer * next;
66     struct btstack_memory_buffer * prev;
67 } btstack_memory_buffer_t;
68 
69 typedef struct {
70     btstack_memory_buffer_t tracking;
71     void * pointer;
72 } test_buffer_t;
73 
74 static btstack_memory_buffer_t * btstack_memory_malloc_buffers;
75 static uint32_t btstack_memory_malloc_counter;
76 
77 static void btstack_memory_tracking_add(btstack_memory_buffer_t * buffer){
78     btstack_assert(buffer != NULL);
79     if (btstack_memory_malloc_buffers != NULL) {
80         // let current first item prev point to new first item
81         btstack_memory_malloc_buffers->prev = buffer;
82     }
83     buffer->prev = NULL;
84     buffer->next = btstack_memory_malloc_buffers;
85     btstack_memory_malloc_buffers = buffer;
86 
87     btstack_memory_malloc_counter++;
88 }
89 
90 static void btstack_memory_tracking_remove(btstack_memory_buffer_t * buffer){
91     btstack_assert(buffer != NULL);
92     if (buffer->prev == NULL){
93         // first item
94         btstack_memory_malloc_buffers = buffer->next;
95     } else {
96         buffer->prev->next = buffer->next;
97     }
98     if (buffer->next != NULL){
99         buffer->next->prev = buffer->prev;
100     }
101 
102     btstack_memory_malloc_counter--;
103 }
104 #endif
105 
106 void btstack_memory_deinit(void){
107 #ifdef HAVE_MALLOC
108     while (btstack_memory_malloc_buffers != NULL){
109         btstack_memory_buffer_t * buffer = btstack_memory_malloc_buffers;
110         btstack_memory_malloc_buffers = buffer->next;
111         free(buffer);
112     }
113     btstack_assert(btstack_memory_malloc_counter == 0);
114 #endif
115 }
116 
117 
118 // MARK: hci_connection_t
119 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_HCI_CONNECTIONS)
120     #if defined(MAX_NO_HCI_CONNECTIONS)
121         #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."
122     #else
123         #define MAX_NR_HCI_CONNECTIONS 0
124     #endif
125 #endif
126 
127 #ifdef MAX_NR_HCI_CONNECTIONS
128 #if MAX_NR_HCI_CONNECTIONS > 0
129 static hci_connection_t hci_connection_storage[MAX_NR_HCI_CONNECTIONS];
130 static btstack_memory_pool_t hci_connection_pool;
131 hci_connection_t * btstack_memory_hci_connection_get(void){
132     void * buffer = btstack_memory_pool_get(&hci_connection_pool);
133     if (buffer){
134         memset(buffer, 0, sizeof(hci_connection_t));
135     }
136     return (hci_connection_t *) buffer;
137 }
138 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
139     btstack_memory_pool_free(&hci_connection_pool, hci_connection);
140 }
141 #else
142 hci_connection_t * btstack_memory_hci_connection_get(void){
143     return NULL;
144 }
145 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
146     UNUSED(hci_connection);
147 };
148 #endif
149 #elif defined(HAVE_MALLOC)
150 
151 typedef struct {
152     btstack_memory_buffer_t tracking;
153     hci_connection_t data;
154 } btstack_memory_hci_connection_t;
155 
156 hci_connection_t * btstack_memory_hci_connection_get(void){
157     btstack_memory_hci_connection_t * buffer = (btstack_memory_hci_connection_t *) malloc(sizeof(btstack_memory_hci_connection_t));
158     if (buffer){
159         memset(buffer, 0, sizeof(btstack_memory_hci_connection_t));
160         btstack_memory_tracking_add(&buffer->tracking);
161         return &buffer->data;
162     } else {
163         return NULL;
164     }
165 }
166 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
167     // reconstruct buffer start
168     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) hci_connection)[-1];
169     btstack_memory_tracking_remove(buffer);
170     free(buffer);
171 }
172 #endif
173 
174 
175 
176 // MARK: l2cap_service_t
177 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_L2CAP_SERVICES)
178     #if defined(MAX_NO_L2CAP_SERVICES)
179         #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."
180     #else
181         #define MAX_NR_L2CAP_SERVICES 0
182     #endif
183 #endif
184 
185 #ifdef MAX_NR_L2CAP_SERVICES
186 #if MAX_NR_L2CAP_SERVICES > 0
187 static l2cap_service_t l2cap_service_storage[MAX_NR_L2CAP_SERVICES];
188 static btstack_memory_pool_t l2cap_service_pool;
189 l2cap_service_t * btstack_memory_l2cap_service_get(void){
190     void * buffer = btstack_memory_pool_get(&l2cap_service_pool);
191     if (buffer){
192         memset(buffer, 0, sizeof(l2cap_service_t));
193     }
194     return (l2cap_service_t *) buffer;
195 }
196 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
197     btstack_memory_pool_free(&l2cap_service_pool, l2cap_service);
198 }
199 #else
200 l2cap_service_t * btstack_memory_l2cap_service_get(void){
201     return NULL;
202 }
203 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
204     UNUSED(l2cap_service);
205 };
206 #endif
207 #elif defined(HAVE_MALLOC)
208 
209 typedef struct {
210     btstack_memory_buffer_t tracking;
211     l2cap_service_t data;
212 } btstack_memory_l2cap_service_t;
213 
214 l2cap_service_t * btstack_memory_l2cap_service_get(void){
215     btstack_memory_l2cap_service_t * buffer = (btstack_memory_l2cap_service_t *) malloc(sizeof(btstack_memory_l2cap_service_t));
216     if (buffer){
217         memset(buffer, 0, sizeof(btstack_memory_l2cap_service_t));
218         btstack_memory_tracking_add(&buffer->tracking);
219         return &buffer->data;
220     } else {
221         return NULL;
222     }
223 }
224 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
225     // reconstruct buffer start
226     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) l2cap_service)[-1];
227     btstack_memory_tracking_remove(buffer);
228     free(buffer);
229 }
230 #endif
231 
232 
233 // MARK: l2cap_channel_t
234 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_L2CAP_CHANNELS)
235     #if defined(MAX_NO_L2CAP_CHANNELS)
236         #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."
237     #else
238         #define MAX_NR_L2CAP_CHANNELS 0
239     #endif
240 #endif
241 
242 #ifdef MAX_NR_L2CAP_CHANNELS
243 #if MAX_NR_L2CAP_CHANNELS > 0
244 static l2cap_channel_t l2cap_channel_storage[MAX_NR_L2CAP_CHANNELS];
245 static btstack_memory_pool_t l2cap_channel_pool;
246 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
247     void * buffer = btstack_memory_pool_get(&l2cap_channel_pool);
248     if (buffer){
249         memset(buffer, 0, sizeof(l2cap_channel_t));
250     }
251     return (l2cap_channel_t *) buffer;
252 }
253 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
254     btstack_memory_pool_free(&l2cap_channel_pool, l2cap_channel);
255 }
256 #else
257 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
258     return NULL;
259 }
260 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
261     UNUSED(l2cap_channel);
262 };
263 #endif
264 #elif defined(HAVE_MALLOC)
265 
266 typedef struct {
267     btstack_memory_buffer_t tracking;
268     l2cap_channel_t data;
269 } btstack_memory_l2cap_channel_t;
270 
271 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
272     btstack_memory_l2cap_channel_t * buffer = (btstack_memory_l2cap_channel_t *) malloc(sizeof(btstack_memory_l2cap_channel_t));
273     if (buffer){
274         memset(buffer, 0, sizeof(btstack_memory_l2cap_channel_t));
275         btstack_memory_tracking_add(&buffer->tracking);
276         return &buffer->data;
277     } else {
278         return NULL;
279     }
280 }
281 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
282     // reconstruct buffer start
283     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) l2cap_channel)[-1];
284     btstack_memory_tracking_remove(buffer);
285     free(buffer);
286 }
287 #endif
288 
289 
290 #ifdef ENABLE_CLASSIC
291 
292 // MARK: rfcomm_multiplexer_t
293 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_RFCOMM_MULTIPLEXERS)
294     #if defined(MAX_NO_RFCOMM_MULTIPLEXERS)
295         #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."
296     #else
297         #define MAX_NR_RFCOMM_MULTIPLEXERS 0
298     #endif
299 #endif
300 
301 #ifdef MAX_NR_RFCOMM_MULTIPLEXERS
302 #if MAX_NR_RFCOMM_MULTIPLEXERS > 0
303 static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NR_RFCOMM_MULTIPLEXERS];
304 static btstack_memory_pool_t rfcomm_multiplexer_pool;
305 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
306     void * buffer = btstack_memory_pool_get(&rfcomm_multiplexer_pool);
307     if (buffer){
308         memset(buffer, 0, sizeof(rfcomm_multiplexer_t));
309     }
310     return (rfcomm_multiplexer_t *) buffer;
311 }
312 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
313     btstack_memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer);
314 }
315 #else
316 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
317     return NULL;
318 }
319 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
320     UNUSED(rfcomm_multiplexer);
321 };
322 #endif
323 #elif defined(HAVE_MALLOC)
324 
325 typedef struct {
326     btstack_memory_buffer_t tracking;
327     rfcomm_multiplexer_t data;
328 } btstack_memory_rfcomm_multiplexer_t;
329 
330 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
331     btstack_memory_rfcomm_multiplexer_t * buffer = (btstack_memory_rfcomm_multiplexer_t *) malloc(sizeof(btstack_memory_rfcomm_multiplexer_t));
332     if (buffer){
333         memset(buffer, 0, sizeof(btstack_memory_rfcomm_multiplexer_t));
334         btstack_memory_tracking_add(&buffer->tracking);
335         return &buffer->data;
336     } else {
337         return NULL;
338     }
339 }
340 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
341     // reconstruct buffer start
342     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) rfcomm_multiplexer)[-1];
343     btstack_memory_tracking_remove(buffer);
344     free(buffer);
345 }
346 #endif
347 
348 
349 // MARK: rfcomm_service_t
350 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_RFCOMM_SERVICES)
351     #if defined(MAX_NO_RFCOMM_SERVICES)
352         #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."
353     #else
354         #define MAX_NR_RFCOMM_SERVICES 0
355     #endif
356 #endif
357 
358 #ifdef MAX_NR_RFCOMM_SERVICES
359 #if MAX_NR_RFCOMM_SERVICES > 0
360 static rfcomm_service_t rfcomm_service_storage[MAX_NR_RFCOMM_SERVICES];
361 static btstack_memory_pool_t rfcomm_service_pool;
362 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
363     void * buffer = btstack_memory_pool_get(&rfcomm_service_pool);
364     if (buffer){
365         memset(buffer, 0, sizeof(rfcomm_service_t));
366     }
367     return (rfcomm_service_t *) buffer;
368 }
369 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
370     btstack_memory_pool_free(&rfcomm_service_pool, rfcomm_service);
371 }
372 #else
373 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
374     return NULL;
375 }
376 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
377     UNUSED(rfcomm_service);
378 };
379 #endif
380 #elif defined(HAVE_MALLOC)
381 
382 typedef struct {
383     btstack_memory_buffer_t tracking;
384     rfcomm_service_t data;
385 } btstack_memory_rfcomm_service_t;
386 
387 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
388     btstack_memory_rfcomm_service_t * buffer = (btstack_memory_rfcomm_service_t *) malloc(sizeof(btstack_memory_rfcomm_service_t));
389     if (buffer){
390         memset(buffer, 0, sizeof(btstack_memory_rfcomm_service_t));
391         btstack_memory_tracking_add(&buffer->tracking);
392         return &buffer->data;
393     } else {
394         return NULL;
395     }
396 }
397 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
398     // reconstruct buffer start
399     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) rfcomm_service)[-1];
400     btstack_memory_tracking_remove(buffer);
401     free(buffer);
402 }
403 #endif
404 
405 
406 // MARK: rfcomm_channel_t
407 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_RFCOMM_CHANNELS)
408     #if defined(MAX_NO_RFCOMM_CHANNELS)
409         #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."
410     #else
411         #define MAX_NR_RFCOMM_CHANNELS 0
412     #endif
413 #endif
414 
415 #ifdef MAX_NR_RFCOMM_CHANNELS
416 #if MAX_NR_RFCOMM_CHANNELS > 0
417 static rfcomm_channel_t rfcomm_channel_storage[MAX_NR_RFCOMM_CHANNELS];
418 static btstack_memory_pool_t rfcomm_channel_pool;
419 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
420     void * buffer = btstack_memory_pool_get(&rfcomm_channel_pool);
421     if (buffer){
422         memset(buffer, 0, sizeof(rfcomm_channel_t));
423     }
424     return (rfcomm_channel_t *) buffer;
425 }
426 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
427     btstack_memory_pool_free(&rfcomm_channel_pool, rfcomm_channel);
428 }
429 #else
430 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
431     return NULL;
432 }
433 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
434     UNUSED(rfcomm_channel);
435 };
436 #endif
437 #elif defined(HAVE_MALLOC)
438 
439 typedef struct {
440     btstack_memory_buffer_t tracking;
441     rfcomm_channel_t data;
442 } btstack_memory_rfcomm_channel_t;
443 
444 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
445     btstack_memory_rfcomm_channel_t * buffer = (btstack_memory_rfcomm_channel_t *) malloc(sizeof(btstack_memory_rfcomm_channel_t));
446     if (buffer){
447         memset(buffer, 0, sizeof(btstack_memory_rfcomm_channel_t));
448         btstack_memory_tracking_add(&buffer->tracking);
449         return &buffer->data;
450     } else {
451         return NULL;
452     }
453 }
454 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
455     // reconstruct buffer start
456     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) rfcomm_channel)[-1];
457     btstack_memory_tracking_remove(buffer);
458     free(buffer);
459 }
460 #endif
461 
462 
463 
464 // MARK: btstack_link_key_db_memory_entry_t
465 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES)
466     #if defined(MAX_NO_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES)
467         #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."
468     #else
469         #define MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES 0
470     #endif
471 #endif
472 
473 #ifdef MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES
474 #if MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES > 0
475 static btstack_link_key_db_memory_entry_t btstack_link_key_db_memory_entry_storage[MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES];
476 static btstack_memory_pool_t btstack_link_key_db_memory_entry_pool;
477 btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){
478     void * buffer = btstack_memory_pool_get(&btstack_link_key_db_memory_entry_pool);
479     if (buffer){
480         memset(buffer, 0, sizeof(btstack_link_key_db_memory_entry_t));
481     }
482     return (btstack_link_key_db_memory_entry_t *) buffer;
483 }
484 void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){
485     btstack_memory_pool_free(&btstack_link_key_db_memory_entry_pool, btstack_link_key_db_memory_entry);
486 }
487 #else
488 btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){
489     return NULL;
490 }
491 void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){
492     UNUSED(btstack_link_key_db_memory_entry);
493 };
494 #endif
495 #elif defined(HAVE_MALLOC)
496 
497 typedef struct {
498     btstack_memory_buffer_t tracking;
499     btstack_link_key_db_memory_entry_t data;
500 } btstack_memory_btstack_link_key_db_memory_entry_t;
501 
502 btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){
503     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));
504     if (buffer){
505         memset(buffer, 0, sizeof(btstack_memory_btstack_link_key_db_memory_entry_t));
506         btstack_memory_tracking_add(&buffer->tracking);
507         return &buffer->data;
508     } else {
509         return NULL;
510     }
511 }
512 void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){
513     // reconstruct buffer start
514     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) btstack_link_key_db_memory_entry)[-1];
515     btstack_memory_tracking_remove(buffer);
516     free(buffer);
517 }
518 #endif
519 
520 
521 
522 // MARK: bnep_service_t
523 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BNEP_SERVICES)
524     #if defined(MAX_NO_BNEP_SERVICES)
525         #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."
526     #else
527         #define MAX_NR_BNEP_SERVICES 0
528     #endif
529 #endif
530 
531 #ifdef MAX_NR_BNEP_SERVICES
532 #if MAX_NR_BNEP_SERVICES > 0
533 static bnep_service_t bnep_service_storage[MAX_NR_BNEP_SERVICES];
534 static btstack_memory_pool_t bnep_service_pool;
535 bnep_service_t * btstack_memory_bnep_service_get(void){
536     void * buffer = btstack_memory_pool_get(&bnep_service_pool);
537     if (buffer){
538         memset(buffer, 0, sizeof(bnep_service_t));
539     }
540     return (bnep_service_t *) buffer;
541 }
542 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
543     btstack_memory_pool_free(&bnep_service_pool, bnep_service);
544 }
545 #else
546 bnep_service_t * btstack_memory_bnep_service_get(void){
547     return NULL;
548 }
549 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
550     UNUSED(bnep_service);
551 };
552 #endif
553 #elif defined(HAVE_MALLOC)
554 
555 typedef struct {
556     btstack_memory_buffer_t tracking;
557     bnep_service_t data;
558 } btstack_memory_bnep_service_t;
559 
560 bnep_service_t * btstack_memory_bnep_service_get(void){
561     btstack_memory_bnep_service_t * buffer = (btstack_memory_bnep_service_t *) malloc(sizeof(btstack_memory_bnep_service_t));
562     if (buffer){
563         memset(buffer, 0, sizeof(btstack_memory_bnep_service_t));
564         btstack_memory_tracking_add(&buffer->tracking);
565         return &buffer->data;
566     } else {
567         return NULL;
568     }
569 }
570 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
571     // reconstruct buffer start
572     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) bnep_service)[-1];
573     btstack_memory_tracking_remove(buffer);
574     free(buffer);
575 }
576 #endif
577 
578 
579 // MARK: bnep_channel_t
580 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BNEP_CHANNELS)
581     #if defined(MAX_NO_BNEP_CHANNELS)
582         #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."
583     #else
584         #define MAX_NR_BNEP_CHANNELS 0
585     #endif
586 #endif
587 
588 #ifdef MAX_NR_BNEP_CHANNELS
589 #if MAX_NR_BNEP_CHANNELS > 0
590 static bnep_channel_t bnep_channel_storage[MAX_NR_BNEP_CHANNELS];
591 static btstack_memory_pool_t bnep_channel_pool;
592 bnep_channel_t * btstack_memory_bnep_channel_get(void){
593     void * buffer = btstack_memory_pool_get(&bnep_channel_pool);
594     if (buffer){
595         memset(buffer, 0, sizeof(bnep_channel_t));
596     }
597     return (bnep_channel_t *) buffer;
598 }
599 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
600     btstack_memory_pool_free(&bnep_channel_pool, bnep_channel);
601 }
602 #else
603 bnep_channel_t * btstack_memory_bnep_channel_get(void){
604     return NULL;
605 }
606 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
607     UNUSED(bnep_channel);
608 };
609 #endif
610 #elif defined(HAVE_MALLOC)
611 
612 typedef struct {
613     btstack_memory_buffer_t tracking;
614     bnep_channel_t data;
615 } btstack_memory_bnep_channel_t;
616 
617 bnep_channel_t * btstack_memory_bnep_channel_get(void){
618     btstack_memory_bnep_channel_t * buffer = (btstack_memory_bnep_channel_t *) malloc(sizeof(btstack_memory_bnep_channel_t));
619     if (buffer){
620         memset(buffer, 0, sizeof(btstack_memory_bnep_channel_t));
621         btstack_memory_tracking_add(&buffer->tracking);
622         return &buffer->data;
623     } else {
624         return NULL;
625     }
626 }
627 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
628     // reconstruct buffer start
629     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) bnep_channel)[-1];
630     btstack_memory_tracking_remove(buffer);
631     free(buffer);
632 }
633 #endif
634 
635 
636 
637 // MARK: hfp_connection_t
638 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_HFP_CONNECTIONS)
639     #if defined(MAX_NO_HFP_CONNECTIONS)
640         #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."
641     #else
642         #define MAX_NR_HFP_CONNECTIONS 0
643     #endif
644 #endif
645 
646 #ifdef MAX_NR_HFP_CONNECTIONS
647 #if MAX_NR_HFP_CONNECTIONS > 0
648 static hfp_connection_t hfp_connection_storage[MAX_NR_HFP_CONNECTIONS];
649 static btstack_memory_pool_t hfp_connection_pool;
650 hfp_connection_t * btstack_memory_hfp_connection_get(void){
651     void * buffer = btstack_memory_pool_get(&hfp_connection_pool);
652     if (buffer){
653         memset(buffer, 0, sizeof(hfp_connection_t));
654     }
655     return (hfp_connection_t *) buffer;
656 }
657 void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){
658     btstack_memory_pool_free(&hfp_connection_pool, hfp_connection);
659 }
660 #else
661 hfp_connection_t * btstack_memory_hfp_connection_get(void){
662     return NULL;
663 }
664 void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){
665     UNUSED(hfp_connection);
666 };
667 #endif
668 #elif defined(HAVE_MALLOC)
669 
670 typedef struct {
671     btstack_memory_buffer_t tracking;
672     hfp_connection_t data;
673 } btstack_memory_hfp_connection_t;
674 
675 hfp_connection_t * btstack_memory_hfp_connection_get(void){
676     btstack_memory_hfp_connection_t * buffer = (btstack_memory_hfp_connection_t *) malloc(sizeof(btstack_memory_hfp_connection_t));
677     if (buffer){
678         memset(buffer, 0, sizeof(btstack_memory_hfp_connection_t));
679         btstack_memory_tracking_add(&buffer->tracking);
680         return &buffer->data;
681     } else {
682         return NULL;
683     }
684 }
685 void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){
686     // reconstruct buffer start
687     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) hfp_connection)[-1];
688     btstack_memory_tracking_remove(buffer);
689     free(buffer);
690 }
691 #endif
692 
693 
694 
695 // MARK: hid_host_connection_t
696 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_HID_HOST_CONNECTIONS)
697     #if defined(MAX_NO_HID_HOST_CONNECTIONS)
698         #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."
699     #else
700         #define MAX_NR_HID_HOST_CONNECTIONS 0
701     #endif
702 #endif
703 
704 #ifdef MAX_NR_HID_HOST_CONNECTIONS
705 #if MAX_NR_HID_HOST_CONNECTIONS > 0
706 static hid_host_connection_t hid_host_connection_storage[MAX_NR_HID_HOST_CONNECTIONS];
707 static btstack_memory_pool_t hid_host_connection_pool;
708 hid_host_connection_t * btstack_memory_hid_host_connection_get(void){
709     void * buffer = btstack_memory_pool_get(&hid_host_connection_pool);
710     if (buffer){
711         memset(buffer, 0, sizeof(hid_host_connection_t));
712     }
713     return (hid_host_connection_t *) buffer;
714 }
715 void btstack_memory_hid_host_connection_free(hid_host_connection_t *hid_host_connection){
716     btstack_memory_pool_free(&hid_host_connection_pool, hid_host_connection);
717 }
718 #else
719 hid_host_connection_t * btstack_memory_hid_host_connection_get(void){
720     return NULL;
721 }
722 void btstack_memory_hid_host_connection_free(hid_host_connection_t *hid_host_connection){
723     UNUSED(hid_host_connection);
724 };
725 #endif
726 #elif defined(HAVE_MALLOC)
727 
728 typedef struct {
729     btstack_memory_buffer_t tracking;
730     hid_host_connection_t data;
731 } btstack_memory_hid_host_connection_t;
732 
733 hid_host_connection_t * btstack_memory_hid_host_connection_get(void){
734     btstack_memory_hid_host_connection_t * buffer = (btstack_memory_hid_host_connection_t *) malloc(sizeof(btstack_memory_hid_host_connection_t));
735     if (buffer){
736         memset(buffer, 0, sizeof(btstack_memory_hid_host_connection_t));
737         btstack_memory_tracking_add(&buffer->tracking);
738         return &buffer->data;
739     } else {
740         return NULL;
741     }
742 }
743 void btstack_memory_hid_host_connection_free(hid_host_connection_t *hid_host_connection){
744     // reconstruct buffer start
745     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) hid_host_connection)[-1];
746     btstack_memory_tracking_remove(buffer);
747     free(buffer);
748 }
749 #endif
750 
751 
752 
753 // MARK: service_record_item_t
754 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_SERVICE_RECORD_ITEMS)
755     #if defined(MAX_NO_SERVICE_RECORD_ITEMS)
756         #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."
757     #else
758         #define MAX_NR_SERVICE_RECORD_ITEMS 0
759     #endif
760 #endif
761 
762 #ifdef MAX_NR_SERVICE_RECORD_ITEMS
763 #if MAX_NR_SERVICE_RECORD_ITEMS > 0
764 static service_record_item_t service_record_item_storage[MAX_NR_SERVICE_RECORD_ITEMS];
765 static btstack_memory_pool_t service_record_item_pool;
766 service_record_item_t * btstack_memory_service_record_item_get(void){
767     void * buffer = btstack_memory_pool_get(&service_record_item_pool);
768     if (buffer){
769         memset(buffer, 0, sizeof(service_record_item_t));
770     }
771     return (service_record_item_t *) buffer;
772 }
773 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
774     btstack_memory_pool_free(&service_record_item_pool, service_record_item);
775 }
776 #else
777 service_record_item_t * btstack_memory_service_record_item_get(void){
778     return NULL;
779 }
780 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
781     UNUSED(service_record_item);
782 };
783 #endif
784 #elif defined(HAVE_MALLOC)
785 
786 typedef struct {
787     btstack_memory_buffer_t tracking;
788     service_record_item_t data;
789 } btstack_memory_service_record_item_t;
790 
791 service_record_item_t * btstack_memory_service_record_item_get(void){
792     btstack_memory_service_record_item_t * buffer = (btstack_memory_service_record_item_t *) malloc(sizeof(btstack_memory_service_record_item_t));
793     if (buffer){
794         memset(buffer, 0, sizeof(btstack_memory_service_record_item_t));
795         btstack_memory_tracking_add(&buffer->tracking);
796         return &buffer->data;
797     } else {
798         return NULL;
799     }
800 }
801 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
802     // reconstruct buffer start
803     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) service_record_item)[-1];
804     btstack_memory_tracking_remove(buffer);
805     free(buffer);
806 }
807 #endif
808 
809 
810 
811 // MARK: avdtp_stream_endpoint_t
812 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVDTP_STREAM_ENDPOINTS)
813     #if defined(MAX_NO_AVDTP_STREAM_ENDPOINTS)
814         #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."
815     #else
816         #define MAX_NR_AVDTP_STREAM_ENDPOINTS 0
817     #endif
818 #endif
819 
820 #ifdef MAX_NR_AVDTP_STREAM_ENDPOINTS
821 #if MAX_NR_AVDTP_STREAM_ENDPOINTS > 0
822 static avdtp_stream_endpoint_t avdtp_stream_endpoint_storage[MAX_NR_AVDTP_STREAM_ENDPOINTS];
823 static btstack_memory_pool_t avdtp_stream_endpoint_pool;
824 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){
825     void * buffer = btstack_memory_pool_get(&avdtp_stream_endpoint_pool);
826     if (buffer){
827         memset(buffer, 0, sizeof(avdtp_stream_endpoint_t));
828     }
829     return (avdtp_stream_endpoint_t *) buffer;
830 }
831 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){
832     btstack_memory_pool_free(&avdtp_stream_endpoint_pool, avdtp_stream_endpoint);
833 }
834 #else
835 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){
836     return NULL;
837 }
838 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){
839     UNUSED(avdtp_stream_endpoint);
840 };
841 #endif
842 #elif defined(HAVE_MALLOC)
843 
844 typedef struct {
845     btstack_memory_buffer_t tracking;
846     avdtp_stream_endpoint_t data;
847 } btstack_memory_avdtp_stream_endpoint_t;
848 
849 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){
850     btstack_memory_avdtp_stream_endpoint_t * buffer = (btstack_memory_avdtp_stream_endpoint_t *) malloc(sizeof(btstack_memory_avdtp_stream_endpoint_t));
851     if (buffer){
852         memset(buffer, 0, sizeof(btstack_memory_avdtp_stream_endpoint_t));
853         btstack_memory_tracking_add(&buffer->tracking);
854         return &buffer->data;
855     } else {
856         return NULL;
857     }
858 }
859 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){
860     // reconstruct buffer start
861     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) avdtp_stream_endpoint)[-1];
862     btstack_memory_tracking_remove(buffer);
863     free(buffer);
864 }
865 #endif
866 
867 
868 
869 // MARK: avdtp_connection_t
870 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVDTP_CONNECTIONS)
871     #if defined(MAX_NO_AVDTP_CONNECTIONS)
872         #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."
873     #else
874         #define MAX_NR_AVDTP_CONNECTIONS 0
875     #endif
876 #endif
877 
878 #ifdef MAX_NR_AVDTP_CONNECTIONS
879 #if MAX_NR_AVDTP_CONNECTIONS > 0
880 static avdtp_connection_t avdtp_connection_storage[MAX_NR_AVDTP_CONNECTIONS];
881 static btstack_memory_pool_t avdtp_connection_pool;
882 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){
883     void * buffer = btstack_memory_pool_get(&avdtp_connection_pool);
884     if (buffer){
885         memset(buffer, 0, sizeof(avdtp_connection_t));
886     }
887     return (avdtp_connection_t *) buffer;
888 }
889 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){
890     btstack_memory_pool_free(&avdtp_connection_pool, avdtp_connection);
891 }
892 #else
893 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){
894     return NULL;
895 }
896 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){
897     UNUSED(avdtp_connection);
898 };
899 #endif
900 #elif defined(HAVE_MALLOC)
901 
902 typedef struct {
903     btstack_memory_buffer_t tracking;
904     avdtp_connection_t data;
905 } btstack_memory_avdtp_connection_t;
906 
907 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){
908     btstack_memory_avdtp_connection_t * buffer = (btstack_memory_avdtp_connection_t *) malloc(sizeof(btstack_memory_avdtp_connection_t));
909     if (buffer){
910         memset(buffer, 0, sizeof(btstack_memory_avdtp_connection_t));
911         btstack_memory_tracking_add(&buffer->tracking);
912         return &buffer->data;
913     } else {
914         return NULL;
915     }
916 }
917 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){
918     // reconstruct buffer start
919     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) avdtp_connection)[-1];
920     btstack_memory_tracking_remove(buffer);
921     free(buffer);
922 }
923 #endif
924 
925 
926 
927 // MARK: avrcp_connection_t
928 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVRCP_CONNECTIONS)
929     #if defined(MAX_NO_AVRCP_CONNECTIONS)
930         #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."
931     #else
932         #define MAX_NR_AVRCP_CONNECTIONS 0
933     #endif
934 #endif
935 
936 #ifdef MAX_NR_AVRCP_CONNECTIONS
937 #if MAX_NR_AVRCP_CONNECTIONS > 0
938 static avrcp_connection_t avrcp_connection_storage[MAX_NR_AVRCP_CONNECTIONS];
939 static btstack_memory_pool_t avrcp_connection_pool;
940 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){
941     void * buffer = btstack_memory_pool_get(&avrcp_connection_pool);
942     if (buffer){
943         memset(buffer, 0, sizeof(avrcp_connection_t));
944     }
945     return (avrcp_connection_t *) buffer;
946 }
947 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){
948     btstack_memory_pool_free(&avrcp_connection_pool, avrcp_connection);
949 }
950 #else
951 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){
952     return NULL;
953 }
954 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){
955     UNUSED(avrcp_connection);
956 };
957 #endif
958 #elif defined(HAVE_MALLOC)
959 
960 typedef struct {
961     btstack_memory_buffer_t tracking;
962     avrcp_connection_t data;
963 } btstack_memory_avrcp_connection_t;
964 
965 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){
966     btstack_memory_avrcp_connection_t * buffer = (btstack_memory_avrcp_connection_t *) malloc(sizeof(btstack_memory_avrcp_connection_t));
967     if (buffer){
968         memset(buffer, 0, sizeof(btstack_memory_avrcp_connection_t));
969         btstack_memory_tracking_add(&buffer->tracking);
970         return &buffer->data;
971     } else {
972         return NULL;
973     }
974 }
975 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){
976     // reconstruct buffer start
977     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) avrcp_connection)[-1];
978     btstack_memory_tracking_remove(buffer);
979     free(buffer);
980 }
981 #endif
982 
983 
984 
985 // MARK: avrcp_browsing_connection_t
986 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVRCP_BROWSING_CONNECTIONS)
987     #if defined(MAX_NO_AVRCP_BROWSING_CONNECTIONS)
988         #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."
989     #else
990         #define MAX_NR_AVRCP_BROWSING_CONNECTIONS 0
991     #endif
992 #endif
993 
994 #ifdef MAX_NR_AVRCP_BROWSING_CONNECTIONS
995 #if MAX_NR_AVRCP_BROWSING_CONNECTIONS > 0
996 static avrcp_browsing_connection_t avrcp_browsing_connection_storage[MAX_NR_AVRCP_BROWSING_CONNECTIONS];
997 static btstack_memory_pool_t avrcp_browsing_connection_pool;
998 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){
999     void * buffer = btstack_memory_pool_get(&avrcp_browsing_connection_pool);
1000     if (buffer){
1001         memset(buffer, 0, sizeof(avrcp_browsing_connection_t));
1002     }
1003     return (avrcp_browsing_connection_t *) buffer;
1004 }
1005 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){
1006     btstack_memory_pool_free(&avrcp_browsing_connection_pool, avrcp_browsing_connection);
1007 }
1008 #else
1009 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){
1010     return NULL;
1011 }
1012 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){
1013     UNUSED(avrcp_browsing_connection);
1014 };
1015 #endif
1016 #elif defined(HAVE_MALLOC)
1017 
1018 typedef struct {
1019     btstack_memory_buffer_t tracking;
1020     avrcp_browsing_connection_t data;
1021 } btstack_memory_avrcp_browsing_connection_t;
1022 
1023 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){
1024     btstack_memory_avrcp_browsing_connection_t * buffer = (btstack_memory_avrcp_browsing_connection_t *) malloc(sizeof(btstack_memory_avrcp_browsing_connection_t));
1025     if (buffer){
1026         memset(buffer, 0, sizeof(btstack_memory_avrcp_browsing_connection_t));
1027         btstack_memory_tracking_add(&buffer->tracking);
1028         return &buffer->data;
1029     } else {
1030         return NULL;
1031     }
1032 }
1033 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){
1034     // reconstruct buffer start
1035     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) avrcp_browsing_connection)[-1];
1036     btstack_memory_tracking_remove(buffer);
1037     free(buffer);
1038 }
1039 #endif
1040 
1041 
1042 #endif
1043 #ifdef ENABLE_BLE
1044 
1045 // MARK: battery_service_client_t
1046 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BATTERY_SERVICE_CLIENTS)
1047     #if defined(MAX_NO_BATTERY_SERVICE_CLIENTS)
1048         #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."
1049     #else
1050         #define MAX_NR_BATTERY_SERVICE_CLIENTS 0
1051     #endif
1052 #endif
1053 
1054 #ifdef MAX_NR_BATTERY_SERVICE_CLIENTS
1055 #if MAX_NR_BATTERY_SERVICE_CLIENTS > 0
1056 static battery_service_client_t battery_service_client_storage[MAX_NR_BATTERY_SERVICE_CLIENTS];
1057 static btstack_memory_pool_t battery_service_client_pool;
1058 battery_service_client_t * btstack_memory_battery_service_client_get(void){
1059     void * buffer = btstack_memory_pool_get(&battery_service_client_pool);
1060     if (buffer){
1061         memset(buffer, 0, sizeof(battery_service_client_t));
1062     }
1063     return (battery_service_client_t *) buffer;
1064 }
1065 void btstack_memory_battery_service_client_free(battery_service_client_t *battery_service_client){
1066     btstack_memory_pool_free(&battery_service_client_pool, battery_service_client);
1067 }
1068 #else
1069 battery_service_client_t * btstack_memory_battery_service_client_get(void){
1070     return NULL;
1071 }
1072 void btstack_memory_battery_service_client_free(battery_service_client_t *battery_service_client){
1073     UNUSED(battery_service_client);
1074 };
1075 #endif
1076 #elif defined(HAVE_MALLOC)
1077 
1078 typedef struct {
1079     btstack_memory_buffer_t tracking;
1080     battery_service_client_t data;
1081 } btstack_memory_battery_service_client_t;
1082 
1083 battery_service_client_t * btstack_memory_battery_service_client_get(void){
1084     btstack_memory_battery_service_client_t * buffer = (btstack_memory_battery_service_client_t *) malloc(sizeof(btstack_memory_battery_service_client_t));
1085     if (buffer){
1086         memset(buffer, 0, sizeof(btstack_memory_battery_service_client_t));
1087         btstack_memory_tracking_add(&buffer->tracking);
1088         return &buffer->data;
1089     } else {
1090         return NULL;
1091     }
1092 }
1093 void btstack_memory_battery_service_client_free(battery_service_client_t *battery_service_client){
1094     // reconstruct buffer start
1095     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) battery_service_client)[-1];
1096     btstack_memory_tracking_remove(buffer);
1097     free(buffer);
1098 }
1099 #endif
1100 
1101 
1102 // MARK: gatt_client_t
1103 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_GATT_CLIENTS)
1104     #if defined(MAX_NO_GATT_CLIENTS)
1105         #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."
1106     #else
1107         #define MAX_NR_GATT_CLIENTS 0
1108     #endif
1109 #endif
1110 
1111 #ifdef MAX_NR_GATT_CLIENTS
1112 #if MAX_NR_GATT_CLIENTS > 0
1113 static gatt_client_t gatt_client_storage[MAX_NR_GATT_CLIENTS];
1114 static btstack_memory_pool_t gatt_client_pool;
1115 gatt_client_t * btstack_memory_gatt_client_get(void){
1116     void * buffer = btstack_memory_pool_get(&gatt_client_pool);
1117     if (buffer){
1118         memset(buffer, 0, sizeof(gatt_client_t));
1119     }
1120     return (gatt_client_t *) buffer;
1121 }
1122 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
1123     btstack_memory_pool_free(&gatt_client_pool, gatt_client);
1124 }
1125 #else
1126 gatt_client_t * btstack_memory_gatt_client_get(void){
1127     return NULL;
1128 }
1129 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
1130     UNUSED(gatt_client);
1131 };
1132 #endif
1133 #elif defined(HAVE_MALLOC)
1134 
1135 typedef struct {
1136     btstack_memory_buffer_t tracking;
1137     gatt_client_t data;
1138 } btstack_memory_gatt_client_t;
1139 
1140 gatt_client_t * btstack_memory_gatt_client_get(void){
1141     btstack_memory_gatt_client_t * buffer = (btstack_memory_gatt_client_t *) malloc(sizeof(btstack_memory_gatt_client_t));
1142     if (buffer){
1143         memset(buffer, 0, sizeof(btstack_memory_gatt_client_t));
1144         btstack_memory_tracking_add(&buffer->tracking);
1145         return &buffer->data;
1146     } else {
1147         return NULL;
1148     }
1149 }
1150 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
1151     // reconstruct buffer start
1152     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) gatt_client)[-1];
1153     btstack_memory_tracking_remove(buffer);
1154     free(buffer);
1155 }
1156 #endif
1157 
1158 
1159 // MARK: hids_client_t
1160 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_HIDS_CLIENTS)
1161     #if defined(MAX_NO_HIDS_CLIENTS)
1162         #error "Deprecated MAX_NO_HIDS_CLIENTS defined instead of MAX_NR_HIDS_CLIENTS. Please update your btstack_config.h to use MAX_NR_HIDS_CLIENTS."
1163     #else
1164         #define MAX_NR_HIDS_CLIENTS 0
1165     #endif
1166 #endif
1167 
1168 #ifdef MAX_NR_HIDS_CLIENTS
1169 #if MAX_NR_HIDS_CLIENTS > 0
1170 static hids_client_t hids_client_storage[MAX_NR_HIDS_CLIENTS];
1171 static btstack_memory_pool_t hids_client_pool;
1172 hids_client_t * btstack_memory_hids_client_get(void){
1173     void * buffer = btstack_memory_pool_get(&hids_client_pool);
1174     if (buffer){
1175         memset(buffer, 0, sizeof(hids_client_t));
1176     }
1177     return (hids_client_t *) buffer;
1178 }
1179 void btstack_memory_hids_client_free(hids_client_t *hids_client){
1180     btstack_memory_pool_free(&hids_client_pool, hids_client);
1181 }
1182 #else
1183 hids_client_t * btstack_memory_hids_client_get(void){
1184     return NULL;
1185 }
1186 void btstack_memory_hids_client_free(hids_client_t *hids_client){
1187     UNUSED(hids_client);
1188 };
1189 #endif
1190 #elif defined(HAVE_MALLOC)
1191 
1192 typedef struct {
1193     btstack_memory_buffer_t tracking;
1194     hids_client_t data;
1195 } btstack_memory_hids_client_t;
1196 
1197 hids_client_t * btstack_memory_hids_client_get(void){
1198     btstack_memory_hids_client_t * buffer = (btstack_memory_hids_client_t *) malloc(sizeof(btstack_memory_hids_client_t));
1199     if (buffer){
1200         memset(buffer, 0, sizeof(btstack_memory_hids_client_t));
1201         btstack_memory_tracking_add(&buffer->tracking);
1202         return &buffer->data;
1203     } else {
1204         return NULL;
1205     }
1206 }
1207 void btstack_memory_hids_client_free(hids_client_t *hids_client){
1208     // reconstruct buffer start
1209     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) hids_client)[-1];
1210     btstack_memory_tracking_remove(buffer);
1211     free(buffer);
1212 }
1213 #endif
1214 
1215 
1216 // MARK: scan_parameters_service_client_t
1217 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS)
1218     #if defined(MAX_NO_SCAN_PARAMETERS_SERVICE_CLIENTS)
1219         #error "Deprecated MAX_NO_SCAN_PARAMETERS_SERVICE_CLIENTS defined instead of MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS. Please update your btstack_config.h to use MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS."
1220     #else
1221         #define MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS 0
1222     #endif
1223 #endif
1224 
1225 #ifdef MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS
1226 #if MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS > 0
1227 static scan_parameters_service_client_t scan_parameters_service_client_storage[MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS];
1228 static btstack_memory_pool_t scan_parameters_service_client_pool;
1229 scan_parameters_service_client_t * btstack_memory_scan_parameters_service_client_get(void){
1230     void * buffer = btstack_memory_pool_get(&scan_parameters_service_client_pool);
1231     if (buffer){
1232         memset(buffer, 0, sizeof(scan_parameters_service_client_t));
1233     }
1234     return (scan_parameters_service_client_t *) buffer;
1235 }
1236 void btstack_memory_scan_parameters_service_client_free(scan_parameters_service_client_t *scan_parameters_service_client){
1237     btstack_memory_pool_free(&scan_parameters_service_client_pool, scan_parameters_service_client);
1238 }
1239 #else
1240 scan_parameters_service_client_t * btstack_memory_scan_parameters_service_client_get(void){
1241     return NULL;
1242 }
1243 void btstack_memory_scan_parameters_service_client_free(scan_parameters_service_client_t *scan_parameters_service_client){
1244     UNUSED(scan_parameters_service_client);
1245 };
1246 #endif
1247 #elif defined(HAVE_MALLOC)
1248 
1249 typedef struct {
1250     btstack_memory_buffer_t tracking;
1251     scan_parameters_service_client_t data;
1252 } btstack_memory_scan_parameters_service_client_t;
1253 
1254 scan_parameters_service_client_t * btstack_memory_scan_parameters_service_client_get(void){
1255     btstack_memory_scan_parameters_service_client_t * buffer = (btstack_memory_scan_parameters_service_client_t *) malloc(sizeof(btstack_memory_scan_parameters_service_client_t));
1256     if (buffer){
1257         memset(buffer, 0, sizeof(btstack_memory_scan_parameters_service_client_t));
1258         btstack_memory_tracking_add(&buffer->tracking);
1259         return &buffer->data;
1260     } else {
1261         return NULL;
1262     }
1263 }
1264 void btstack_memory_scan_parameters_service_client_free(scan_parameters_service_client_t *scan_parameters_service_client){
1265     // reconstruct buffer start
1266     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) scan_parameters_service_client)[-1];
1267     btstack_memory_tracking_remove(buffer);
1268     free(buffer);
1269 }
1270 #endif
1271 
1272 
1273 // MARK: sm_lookup_entry_t
1274 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_SM_LOOKUP_ENTRIES)
1275     #if defined(MAX_NO_SM_LOOKUP_ENTRIES)
1276         #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."
1277     #else
1278         #define MAX_NR_SM_LOOKUP_ENTRIES 0
1279     #endif
1280 #endif
1281 
1282 #ifdef MAX_NR_SM_LOOKUP_ENTRIES
1283 #if MAX_NR_SM_LOOKUP_ENTRIES > 0
1284 static sm_lookup_entry_t sm_lookup_entry_storage[MAX_NR_SM_LOOKUP_ENTRIES];
1285 static btstack_memory_pool_t sm_lookup_entry_pool;
1286 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
1287     void * buffer = btstack_memory_pool_get(&sm_lookup_entry_pool);
1288     if (buffer){
1289         memset(buffer, 0, sizeof(sm_lookup_entry_t));
1290     }
1291     return (sm_lookup_entry_t *) buffer;
1292 }
1293 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
1294     btstack_memory_pool_free(&sm_lookup_entry_pool, sm_lookup_entry);
1295 }
1296 #else
1297 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
1298     return NULL;
1299 }
1300 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
1301     UNUSED(sm_lookup_entry);
1302 };
1303 #endif
1304 #elif defined(HAVE_MALLOC)
1305 
1306 typedef struct {
1307     btstack_memory_buffer_t tracking;
1308     sm_lookup_entry_t data;
1309 } btstack_memory_sm_lookup_entry_t;
1310 
1311 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
1312     btstack_memory_sm_lookup_entry_t * buffer = (btstack_memory_sm_lookup_entry_t *) malloc(sizeof(btstack_memory_sm_lookup_entry_t));
1313     if (buffer){
1314         memset(buffer, 0, sizeof(btstack_memory_sm_lookup_entry_t));
1315         btstack_memory_tracking_add(&buffer->tracking);
1316         return &buffer->data;
1317     } else {
1318         return NULL;
1319     }
1320 }
1321 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
1322     // reconstruct buffer start
1323     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) sm_lookup_entry)[-1];
1324     btstack_memory_tracking_remove(buffer);
1325     free(buffer);
1326 }
1327 #endif
1328 
1329 
1330 // MARK: whitelist_entry_t
1331 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_WHITELIST_ENTRIES)
1332     #if defined(MAX_NO_WHITELIST_ENTRIES)
1333         #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."
1334     #else
1335         #define MAX_NR_WHITELIST_ENTRIES 0
1336     #endif
1337 #endif
1338 
1339 #ifdef MAX_NR_WHITELIST_ENTRIES
1340 #if MAX_NR_WHITELIST_ENTRIES > 0
1341 static whitelist_entry_t whitelist_entry_storage[MAX_NR_WHITELIST_ENTRIES];
1342 static btstack_memory_pool_t whitelist_entry_pool;
1343 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
1344     void * buffer = btstack_memory_pool_get(&whitelist_entry_pool);
1345     if (buffer){
1346         memset(buffer, 0, sizeof(whitelist_entry_t));
1347     }
1348     return (whitelist_entry_t *) buffer;
1349 }
1350 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
1351     btstack_memory_pool_free(&whitelist_entry_pool, whitelist_entry);
1352 }
1353 #else
1354 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
1355     return NULL;
1356 }
1357 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
1358     UNUSED(whitelist_entry);
1359 };
1360 #endif
1361 #elif defined(HAVE_MALLOC)
1362 
1363 typedef struct {
1364     btstack_memory_buffer_t tracking;
1365     whitelist_entry_t data;
1366 } btstack_memory_whitelist_entry_t;
1367 
1368 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
1369     btstack_memory_whitelist_entry_t * buffer = (btstack_memory_whitelist_entry_t *) malloc(sizeof(btstack_memory_whitelist_entry_t));
1370     if (buffer){
1371         memset(buffer, 0, sizeof(btstack_memory_whitelist_entry_t));
1372         btstack_memory_tracking_add(&buffer->tracking);
1373         return &buffer->data;
1374     } else {
1375         return NULL;
1376     }
1377 }
1378 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
1379     // reconstruct buffer start
1380     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) whitelist_entry)[-1];
1381     btstack_memory_tracking_remove(buffer);
1382     free(buffer);
1383 }
1384 #endif
1385 
1386 
1387 #endif
1388 #ifdef ENABLE_MESH
1389 
1390 // MARK: mesh_network_pdu_t
1391 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_NETWORK_PDUS)
1392     #if defined(MAX_NO_MESH_NETWORK_PDUS)
1393         #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."
1394     #else
1395         #define MAX_NR_MESH_NETWORK_PDUS 0
1396     #endif
1397 #endif
1398 
1399 #ifdef MAX_NR_MESH_NETWORK_PDUS
1400 #if MAX_NR_MESH_NETWORK_PDUS > 0
1401 static mesh_network_pdu_t mesh_network_pdu_storage[MAX_NR_MESH_NETWORK_PDUS];
1402 static btstack_memory_pool_t mesh_network_pdu_pool;
1403 mesh_network_pdu_t * btstack_memory_mesh_network_pdu_get(void){
1404     void * buffer = btstack_memory_pool_get(&mesh_network_pdu_pool);
1405     if (buffer){
1406         memset(buffer, 0, sizeof(mesh_network_pdu_t));
1407     }
1408     return (mesh_network_pdu_t *) buffer;
1409 }
1410 void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){
1411     btstack_memory_pool_free(&mesh_network_pdu_pool, mesh_network_pdu);
1412 }
1413 #else
1414 mesh_network_pdu_t * btstack_memory_mesh_network_pdu_get(void){
1415     return NULL;
1416 }
1417 void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){
1418     UNUSED(mesh_network_pdu);
1419 };
1420 #endif
1421 #elif defined(HAVE_MALLOC)
1422 
1423 typedef struct {
1424     btstack_memory_buffer_t tracking;
1425     mesh_network_pdu_t data;
1426 } btstack_memory_mesh_network_pdu_t;
1427 
1428 mesh_network_pdu_t * btstack_memory_mesh_network_pdu_get(void){
1429     btstack_memory_mesh_network_pdu_t * buffer = (btstack_memory_mesh_network_pdu_t *) malloc(sizeof(btstack_memory_mesh_network_pdu_t));
1430     if (buffer){
1431         memset(buffer, 0, sizeof(btstack_memory_mesh_network_pdu_t));
1432         btstack_memory_tracking_add(&buffer->tracking);
1433         return &buffer->data;
1434     } else {
1435         return NULL;
1436     }
1437 }
1438 void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){
1439     // reconstruct buffer start
1440     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_network_pdu)[-1];
1441     btstack_memory_tracking_remove(buffer);
1442     free(buffer);
1443 }
1444 #endif
1445 
1446 
1447 // MARK: mesh_segmented_pdu_t
1448 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_SEGMENTED_PDUS)
1449     #if defined(MAX_NO_MESH_SEGMENTED_PDUS)
1450         #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."
1451     #else
1452         #define MAX_NR_MESH_SEGMENTED_PDUS 0
1453     #endif
1454 #endif
1455 
1456 #ifdef MAX_NR_MESH_SEGMENTED_PDUS
1457 #if MAX_NR_MESH_SEGMENTED_PDUS > 0
1458 static mesh_segmented_pdu_t mesh_segmented_pdu_storage[MAX_NR_MESH_SEGMENTED_PDUS];
1459 static btstack_memory_pool_t mesh_segmented_pdu_pool;
1460 mesh_segmented_pdu_t * btstack_memory_mesh_segmented_pdu_get(void){
1461     void * buffer = btstack_memory_pool_get(&mesh_segmented_pdu_pool);
1462     if (buffer){
1463         memset(buffer, 0, sizeof(mesh_segmented_pdu_t));
1464     }
1465     return (mesh_segmented_pdu_t *) buffer;
1466 }
1467 void btstack_memory_mesh_segmented_pdu_free(mesh_segmented_pdu_t *mesh_segmented_pdu){
1468     btstack_memory_pool_free(&mesh_segmented_pdu_pool, mesh_segmented_pdu);
1469 }
1470 #else
1471 mesh_segmented_pdu_t * btstack_memory_mesh_segmented_pdu_get(void){
1472     return NULL;
1473 }
1474 void btstack_memory_mesh_segmented_pdu_free(mesh_segmented_pdu_t *mesh_segmented_pdu){
1475     UNUSED(mesh_segmented_pdu);
1476 };
1477 #endif
1478 #elif defined(HAVE_MALLOC)
1479 
1480 typedef struct {
1481     btstack_memory_buffer_t tracking;
1482     mesh_segmented_pdu_t data;
1483 } btstack_memory_mesh_segmented_pdu_t;
1484 
1485 mesh_segmented_pdu_t * btstack_memory_mesh_segmented_pdu_get(void){
1486     btstack_memory_mesh_segmented_pdu_t * buffer = (btstack_memory_mesh_segmented_pdu_t *) malloc(sizeof(btstack_memory_mesh_segmented_pdu_t));
1487     if (buffer){
1488         memset(buffer, 0, sizeof(btstack_memory_mesh_segmented_pdu_t));
1489         btstack_memory_tracking_add(&buffer->tracking);
1490         return &buffer->data;
1491     } else {
1492         return NULL;
1493     }
1494 }
1495 void btstack_memory_mesh_segmented_pdu_free(mesh_segmented_pdu_t *mesh_segmented_pdu){
1496     // reconstruct buffer start
1497     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_segmented_pdu)[-1];
1498     btstack_memory_tracking_remove(buffer);
1499     free(buffer);
1500 }
1501 #endif
1502 
1503 
1504 // MARK: mesh_upper_transport_pdu_t
1505 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_UPPER_TRANSPORT_PDUS)
1506     #if defined(MAX_NO_MESH_UPPER_TRANSPORT_PDUS)
1507         #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."
1508     #else
1509         #define MAX_NR_MESH_UPPER_TRANSPORT_PDUS 0
1510     #endif
1511 #endif
1512 
1513 #ifdef MAX_NR_MESH_UPPER_TRANSPORT_PDUS
1514 #if MAX_NR_MESH_UPPER_TRANSPORT_PDUS > 0
1515 static mesh_upper_transport_pdu_t mesh_upper_transport_pdu_storage[MAX_NR_MESH_UPPER_TRANSPORT_PDUS];
1516 static btstack_memory_pool_t mesh_upper_transport_pdu_pool;
1517 mesh_upper_transport_pdu_t * btstack_memory_mesh_upper_transport_pdu_get(void){
1518     void * buffer = btstack_memory_pool_get(&mesh_upper_transport_pdu_pool);
1519     if (buffer){
1520         memset(buffer, 0, sizeof(mesh_upper_transport_pdu_t));
1521     }
1522     return (mesh_upper_transport_pdu_t *) buffer;
1523 }
1524 void btstack_memory_mesh_upper_transport_pdu_free(mesh_upper_transport_pdu_t *mesh_upper_transport_pdu){
1525     btstack_memory_pool_free(&mesh_upper_transport_pdu_pool, mesh_upper_transport_pdu);
1526 }
1527 #else
1528 mesh_upper_transport_pdu_t * btstack_memory_mesh_upper_transport_pdu_get(void){
1529     return NULL;
1530 }
1531 void btstack_memory_mesh_upper_transport_pdu_free(mesh_upper_transport_pdu_t *mesh_upper_transport_pdu){
1532     UNUSED(mesh_upper_transport_pdu);
1533 };
1534 #endif
1535 #elif defined(HAVE_MALLOC)
1536 
1537 typedef struct {
1538     btstack_memory_buffer_t tracking;
1539     mesh_upper_transport_pdu_t data;
1540 } btstack_memory_mesh_upper_transport_pdu_t;
1541 
1542 mesh_upper_transport_pdu_t * btstack_memory_mesh_upper_transport_pdu_get(void){
1543     btstack_memory_mesh_upper_transport_pdu_t * buffer = (btstack_memory_mesh_upper_transport_pdu_t *) malloc(sizeof(btstack_memory_mesh_upper_transport_pdu_t));
1544     if (buffer){
1545         memset(buffer, 0, sizeof(btstack_memory_mesh_upper_transport_pdu_t));
1546         btstack_memory_tracking_add(&buffer->tracking);
1547         return &buffer->data;
1548     } else {
1549         return NULL;
1550     }
1551 }
1552 void btstack_memory_mesh_upper_transport_pdu_free(mesh_upper_transport_pdu_t *mesh_upper_transport_pdu){
1553     // reconstruct buffer start
1554     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_upper_transport_pdu)[-1];
1555     btstack_memory_tracking_remove(buffer);
1556     free(buffer);
1557 }
1558 #endif
1559 
1560 
1561 // MARK: mesh_network_key_t
1562 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_NETWORK_KEYS)
1563     #if defined(MAX_NO_MESH_NETWORK_KEYS)
1564         #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."
1565     #else
1566         #define MAX_NR_MESH_NETWORK_KEYS 0
1567     #endif
1568 #endif
1569 
1570 #ifdef MAX_NR_MESH_NETWORK_KEYS
1571 #if MAX_NR_MESH_NETWORK_KEYS > 0
1572 static mesh_network_key_t mesh_network_key_storage[MAX_NR_MESH_NETWORK_KEYS];
1573 static btstack_memory_pool_t mesh_network_key_pool;
1574 mesh_network_key_t * btstack_memory_mesh_network_key_get(void){
1575     void * buffer = btstack_memory_pool_get(&mesh_network_key_pool);
1576     if (buffer){
1577         memset(buffer, 0, sizeof(mesh_network_key_t));
1578     }
1579     return (mesh_network_key_t *) buffer;
1580 }
1581 void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){
1582     btstack_memory_pool_free(&mesh_network_key_pool, mesh_network_key);
1583 }
1584 #else
1585 mesh_network_key_t * btstack_memory_mesh_network_key_get(void){
1586     return NULL;
1587 }
1588 void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){
1589     UNUSED(mesh_network_key);
1590 };
1591 #endif
1592 #elif defined(HAVE_MALLOC)
1593 
1594 typedef struct {
1595     btstack_memory_buffer_t tracking;
1596     mesh_network_key_t data;
1597 } btstack_memory_mesh_network_key_t;
1598 
1599 mesh_network_key_t * btstack_memory_mesh_network_key_get(void){
1600     btstack_memory_mesh_network_key_t * buffer = (btstack_memory_mesh_network_key_t *) malloc(sizeof(btstack_memory_mesh_network_key_t));
1601     if (buffer){
1602         memset(buffer, 0, sizeof(btstack_memory_mesh_network_key_t));
1603         btstack_memory_tracking_add(&buffer->tracking);
1604         return &buffer->data;
1605     } else {
1606         return NULL;
1607     }
1608 }
1609 void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){
1610     // reconstruct buffer start
1611     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_network_key)[-1];
1612     btstack_memory_tracking_remove(buffer);
1613     free(buffer);
1614 }
1615 #endif
1616 
1617 
1618 // MARK: mesh_transport_key_t
1619 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_TRANSPORT_KEYS)
1620     #if defined(MAX_NO_MESH_TRANSPORT_KEYS)
1621         #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."
1622     #else
1623         #define MAX_NR_MESH_TRANSPORT_KEYS 0
1624     #endif
1625 #endif
1626 
1627 #ifdef MAX_NR_MESH_TRANSPORT_KEYS
1628 #if MAX_NR_MESH_TRANSPORT_KEYS > 0
1629 static mesh_transport_key_t mesh_transport_key_storage[MAX_NR_MESH_TRANSPORT_KEYS];
1630 static btstack_memory_pool_t mesh_transport_key_pool;
1631 mesh_transport_key_t * btstack_memory_mesh_transport_key_get(void){
1632     void * buffer = btstack_memory_pool_get(&mesh_transport_key_pool);
1633     if (buffer){
1634         memset(buffer, 0, sizeof(mesh_transport_key_t));
1635     }
1636     return (mesh_transport_key_t *) buffer;
1637 }
1638 void btstack_memory_mesh_transport_key_free(mesh_transport_key_t *mesh_transport_key){
1639     btstack_memory_pool_free(&mesh_transport_key_pool, mesh_transport_key);
1640 }
1641 #else
1642 mesh_transport_key_t * btstack_memory_mesh_transport_key_get(void){
1643     return NULL;
1644 }
1645 void btstack_memory_mesh_transport_key_free(mesh_transport_key_t *mesh_transport_key){
1646     UNUSED(mesh_transport_key);
1647 };
1648 #endif
1649 #elif defined(HAVE_MALLOC)
1650 
1651 typedef struct {
1652     btstack_memory_buffer_t tracking;
1653     mesh_transport_key_t data;
1654 } btstack_memory_mesh_transport_key_t;
1655 
1656 mesh_transport_key_t * btstack_memory_mesh_transport_key_get(void){
1657     btstack_memory_mesh_transport_key_t * buffer = (btstack_memory_mesh_transport_key_t *) malloc(sizeof(btstack_memory_mesh_transport_key_t));
1658     if (buffer){
1659         memset(buffer, 0, sizeof(btstack_memory_mesh_transport_key_t));
1660         btstack_memory_tracking_add(&buffer->tracking);
1661         return &buffer->data;
1662     } else {
1663         return NULL;
1664     }
1665 }
1666 void btstack_memory_mesh_transport_key_free(mesh_transport_key_t *mesh_transport_key){
1667     // reconstruct buffer start
1668     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_transport_key)[-1];
1669     btstack_memory_tracking_remove(buffer);
1670     free(buffer);
1671 }
1672 #endif
1673 
1674 
1675 // MARK: mesh_virtual_address_t
1676 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_VIRTUAL_ADDRESSS)
1677     #if defined(MAX_NO_MESH_VIRTUAL_ADDRESSS)
1678         #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."
1679     #else
1680         #define MAX_NR_MESH_VIRTUAL_ADDRESSS 0
1681     #endif
1682 #endif
1683 
1684 #ifdef MAX_NR_MESH_VIRTUAL_ADDRESSS
1685 #if MAX_NR_MESH_VIRTUAL_ADDRESSS > 0
1686 static mesh_virtual_address_t mesh_virtual_address_storage[MAX_NR_MESH_VIRTUAL_ADDRESSS];
1687 static btstack_memory_pool_t mesh_virtual_address_pool;
1688 mesh_virtual_address_t * btstack_memory_mesh_virtual_address_get(void){
1689     void * buffer = btstack_memory_pool_get(&mesh_virtual_address_pool);
1690     if (buffer){
1691         memset(buffer, 0, sizeof(mesh_virtual_address_t));
1692     }
1693     return (mesh_virtual_address_t *) buffer;
1694 }
1695 void btstack_memory_mesh_virtual_address_free(mesh_virtual_address_t *mesh_virtual_address){
1696     btstack_memory_pool_free(&mesh_virtual_address_pool, mesh_virtual_address);
1697 }
1698 #else
1699 mesh_virtual_address_t * btstack_memory_mesh_virtual_address_get(void){
1700     return NULL;
1701 }
1702 void btstack_memory_mesh_virtual_address_free(mesh_virtual_address_t *mesh_virtual_address){
1703     UNUSED(mesh_virtual_address);
1704 };
1705 #endif
1706 #elif defined(HAVE_MALLOC)
1707 
1708 typedef struct {
1709     btstack_memory_buffer_t tracking;
1710     mesh_virtual_address_t data;
1711 } btstack_memory_mesh_virtual_address_t;
1712 
1713 mesh_virtual_address_t * btstack_memory_mesh_virtual_address_get(void){
1714     btstack_memory_mesh_virtual_address_t * buffer = (btstack_memory_mesh_virtual_address_t *) malloc(sizeof(btstack_memory_mesh_virtual_address_t));
1715     if (buffer){
1716         memset(buffer, 0, sizeof(btstack_memory_mesh_virtual_address_t));
1717         btstack_memory_tracking_add(&buffer->tracking);
1718         return &buffer->data;
1719     } else {
1720         return NULL;
1721     }
1722 }
1723 void btstack_memory_mesh_virtual_address_free(mesh_virtual_address_t *mesh_virtual_address){
1724     // reconstruct buffer start
1725     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_virtual_address)[-1];
1726     btstack_memory_tracking_remove(buffer);
1727     free(buffer);
1728 }
1729 #endif
1730 
1731 
1732 // MARK: mesh_subnet_t
1733 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_SUBNETS)
1734     #if defined(MAX_NO_MESH_SUBNETS)
1735         #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."
1736     #else
1737         #define MAX_NR_MESH_SUBNETS 0
1738     #endif
1739 #endif
1740 
1741 #ifdef MAX_NR_MESH_SUBNETS
1742 #if MAX_NR_MESH_SUBNETS > 0
1743 static mesh_subnet_t mesh_subnet_storage[MAX_NR_MESH_SUBNETS];
1744 static btstack_memory_pool_t mesh_subnet_pool;
1745 mesh_subnet_t * btstack_memory_mesh_subnet_get(void){
1746     void * buffer = btstack_memory_pool_get(&mesh_subnet_pool);
1747     if (buffer){
1748         memset(buffer, 0, sizeof(mesh_subnet_t));
1749     }
1750     return (mesh_subnet_t *) buffer;
1751 }
1752 void btstack_memory_mesh_subnet_free(mesh_subnet_t *mesh_subnet){
1753     btstack_memory_pool_free(&mesh_subnet_pool, mesh_subnet);
1754 }
1755 #else
1756 mesh_subnet_t * btstack_memory_mesh_subnet_get(void){
1757     return NULL;
1758 }
1759 void btstack_memory_mesh_subnet_free(mesh_subnet_t *mesh_subnet){
1760     UNUSED(mesh_subnet);
1761 };
1762 #endif
1763 #elif defined(HAVE_MALLOC)
1764 
1765 typedef struct {
1766     btstack_memory_buffer_t tracking;
1767     mesh_subnet_t data;
1768 } btstack_memory_mesh_subnet_t;
1769 
1770 mesh_subnet_t * btstack_memory_mesh_subnet_get(void){
1771     btstack_memory_mesh_subnet_t * buffer = (btstack_memory_mesh_subnet_t *) malloc(sizeof(btstack_memory_mesh_subnet_t));
1772     if (buffer){
1773         memset(buffer, 0, sizeof(btstack_memory_mesh_subnet_t));
1774         btstack_memory_tracking_add(&buffer->tracking);
1775         return &buffer->data;
1776     } else {
1777         return NULL;
1778     }
1779 }
1780 void btstack_memory_mesh_subnet_free(mesh_subnet_t *mesh_subnet){
1781     // reconstruct buffer start
1782     btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) mesh_subnet)[-1];
1783     btstack_memory_tracking_remove(buffer);
1784     free(buffer);
1785 }
1786 #endif
1787 
1788 
1789 #endif
1790 
1791 // init
1792 void btstack_memory_init(void){
1793 #ifdef HAVE_MALLOC
1794     // assert that there is no unexpected padding for combined buffer
1795     btstack_assert(sizeof(test_buffer_t) == sizeof(btstack_memory_buffer_t) + sizeof(void *));
1796 #endif
1797 
1798 #if MAX_NR_HCI_CONNECTIONS > 0
1799     btstack_memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NR_HCI_CONNECTIONS, sizeof(hci_connection_t));
1800 #endif
1801 #if MAX_NR_L2CAP_SERVICES > 0
1802     btstack_memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NR_L2CAP_SERVICES, sizeof(l2cap_service_t));
1803 #endif
1804 #if MAX_NR_L2CAP_CHANNELS > 0
1805     btstack_memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NR_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
1806 #endif
1807 #ifdef ENABLE_CLASSIC
1808 #if MAX_NR_RFCOMM_MULTIPLEXERS > 0
1809     btstack_memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NR_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
1810 #endif
1811 #if MAX_NR_RFCOMM_SERVICES > 0
1812     btstack_memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NR_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
1813 #endif
1814 #if MAX_NR_RFCOMM_CHANNELS > 0
1815     btstack_memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NR_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
1816 #endif
1817 #if MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES > 0
1818     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));
1819 #endif
1820 #if MAX_NR_BNEP_SERVICES > 0
1821     btstack_memory_pool_create(&bnep_service_pool, bnep_service_storage, MAX_NR_BNEP_SERVICES, sizeof(bnep_service_t));
1822 #endif
1823 #if MAX_NR_BNEP_CHANNELS > 0
1824     btstack_memory_pool_create(&bnep_channel_pool, bnep_channel_storage, MAX_NR_BNEP_CHANNELS, sizeof(bnep_channel_t));
1825 #endif
1826 #if MAX_NR_HFP_CONNECTIONS > 0
1827     btstack_memory_pool_create(&hfp_connection_pool, hfp_connection_storage, MAX_NR_HFP_CONNECTIONS, sizeof(hfp_connection_t));
1828 #endif
1829 #if MAX_NR_HID_HOST_CONNECTIONS > 0
1830     btstack_memory_pool_create(&hid_host_connection_pool, hid_host_connection_storage, MAX_NR_HID_HOST_CONNECTIONS, sizeof(hid_host_connection_t));
1831 #endif
1832 #if MAX_NR_SERVICE_RECORD_ITEMS > 0
1833     btstack_memory_pool_create(&service_record_item_pool, service_record_item_storage, MAX_NR_SERVICE_RECORD_ITEMS, sizeof(service_record_item_t));
1834 #endif
1835 #if MAX_NR_AVDTP_STREAM_ENDPOINTS > 0
1836     btstack_memory_pool_create(&avdtp_stream_endpoint_pool, avdtp_stream_endpoint_storage, MAX_NR_AVDTP_STREAM_ENDPOINTS, sizeof(avdtp_stream_endpoint_t));
1837 #endif
1838 #if MAX_NR_AVDTP_CONNECTIONS > 0
1839     btstack_memory_pool_create(&avdtp_connection_pool, avdtp_connection_storage, MAX_NR_AVDTP_CONNECTIONS, sizeof(avdtp_connection_t));
1840 #endif
1841 #if MAX_NR_AVRCP_CONNECTIONS > 0
1842     btstack_memory_pool_create(&avrcp_connection_pool, avrcp_connection_storage, MAX_NR_AVRCP_CONNECTIONS, sizeof(avrcp_connection_t));
1843 #endif
1844 #if MAX_NR_AVRCP_BROWSING_CONNECTIONS > 0
1845     btstack_memory_pool_create(&avrcp_browsing_connection_pool, avrcp_browsing_connection_storage, MAX_NR_AVRCP_BROWSING_CONNECTIONS, sizeof(avrcp_browsing_connection_t));
1846 #endif
1847 #endif
1848 #ifdef ENABLE_BLE
1849 #if MAX_NR_BATTERY_SERVICE_CLIENTS > 0
1850     btstack_memory_pool_create(&battery_service_client_pool, battery_service_client_storage, MAX_NR_BATTERY_SERVICE_CLIENTS, sizeof(battery_service_client_t));
1851 #endif
1852 #if MAX_NR_GATT_CLIENTS > 0
1853     btstack_memory_pool_create(&gatt_client_pool, gatt_client_storage, MAX_NR_GATT_CLIENTS, sizeof(gatt_client_t));
1854 #endif
1855 #if MAX_NR_HIDS_CLIENTS > 0
1856     btstack_memory_pool_create(&hids_client_pool, hids_client_storage, MAX_NR_HIDS_CLIENTS, sizeof(hids_client_t));
1857 #endif
1858 #if MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS > 0
1859     btstack_memory_pool_create(&scan_parameters_service_client_pool, scan_parameters_service_client_storage, MAX_NR_SCAN_PARAMETERS_SERVICE_CLIENTS, sizeof(scan_parameters_service_client_t));
1860 #endif
1861 #if MAX_NR_SM_LOOKUP_ENTRIES > 0
1862     btstack_memory_pool_create(&sm_lookup_entry_pool, sm_lookup_entry_storage, MAX_NR_SM_LOOKUP_ENTRIES, sizeof(sm_lookup_entry_t));
1863 #endif
1864 #if MAX_NR_WHITELIST_ENTRIES > 0
1865     btstack_memory_pool_create(&whitelist_entry_pool, whitelist_entry_storage, MAX_NR_WHITELIST_ENTRIES, sizeof(whitelist_entry_t));
1866 #endif
1867 #endif
1868 #ifdef ENABLE_MESH
1869 #if MAX_NR_MESH_NETWORK_PDUS > 0
1870     btstack_memory_pool_create(&mesh_network_pdu_pool, mesh_network_pdu_storage, MAX_NR_MESH_NETWORK_PDUS, sizeof(mesh_network_pdu_t));
1871 #endif
1872 #if MAX_NR_MESH_SEGMENTED_PDUS > 0
1873     btstack_memory_pool_create(&mesh_segmented_pdu_pool, mesh_segmented_pdu_storage, MAX_NR_MESH_SEGMENTED_PDUS, sizeof(mesh_segmented_pdu_t));
1874 #endif
1875 #if MAX_NR_MESH_UPPER_TRANSPORT_PDUS > 0
1876     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));
1877 #endif
1878 #if MAX_NR_MESH_NETWORK_KEYS > 0
1879     btstack_memory_pool_create(&mesh_network_key_pool, mesh_network_key_storage, MAX_NR_MESH_NETWORK_KEYS, sizeof(mesh_network_key_t));
1880 #endif
1881 #if MAX_NR_MESH_TRANSPORT_KEYS > 0
1882     btstack_memory_pool_create(&mesh_transport_key_pool, mesh_transport_key_storage, MAX_NR_MESH_TRANSPORT_KEYS, sizeof(mesh_transport_key_t));
1883 #endif
1884 #if MAX_NR_MESH_VIRTUAL_ADDRESSS > 0
1885     btstack_memory_pool_create(&mesh_virtual_address_pool, mesh_virtual_address_storage, MAX_NR_MESH_VIRTUAL_ADDRESSS, sizeof(mesh_virtual_address_t));
1886 #endif
1887 #if MAX_NR_MESH_SUBNETS > 0
1888     btstack_memory_pool_create(&mesh_subnet_pool, mesh_subnet_storage, MAX_NR_MESH_SUBNETS, sizeof(mesh_subnet_t));
1889 #endif
1890 #endif
1891 }
1892