xref: /btstack/src/btstack_memory.c (revision 6c099da017fdc786e6cea93a272d1635a8ab2cf5)
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 /*
40  *  btstack_memory.h
41  *
42  *  @brief BTstack memory management via configurable memory pools
43  *
44  *  @note code generated by tool/btstack_memory_generator.py
45  *
46  */
47 
48 #include "btstack_memory.h"
49 #include "btstack_memory_pool.h"
50 
51 #include <stdlib.h>
52 
53 
54 
55 // MARK: hci_connection_t
56 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_HCI_CONNECTIONS)
57     #if defined(MAX_NO_HCI_CONNECTIONS)
58         #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."
59     #else
60         #define MAX_NR_HCI_CONNECTIONS 0
61     #endif
62 #endif
63 
64 #ifdef MAX_NR_HCI_CONNECTIONS
65 #if MAX_NR_HCI_CONNECTIONS > 0
66 static hci_connection_t hci_connection_storage[MAX_NR_HCI_CONNECTIONS];
67 static btstack_memory_pool_t hci_connection_pool;
68 hci_connection_t * btstack_memory_hci_connection_get(void){
69     return (hci_connection_t *) btstack_memory_pool_get(&hci_connection_pool);
70 }
71 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
72     btstack_memory_pool_free(&hci_connection_pool, hci_connection);
73 }
74 #else
75 hci_connection_t * btstack_memory_hci_connection_get(void){
76     return NULL;
77 }
78 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
79     // silence compiler warning about unused parameter in a portable way
80     (void) hci_connection;
81 };
82 #endif
83 #elif defined(HAVE_MALLOC)
84 hci_connection_t * btstack_memory_hci_connection_get(void){
85     return (hci_connection_t*) malloc(sizeof(hci_connection_t));
86 }
87 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
88     free(hci_connection);
89 }
90 #endif
91 
92 
93 
94 // MARK: l2cap_service_t
95 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_L2CAP_SERVICES)
96     #if defined(MAX_NO_L2CAP_SERVICES)
97         #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."
98     #else
99         #define MAX_NR_L2CAP_SERVICES 0
100     #endif
101 #endif
102 
103 #ifdef MAX_NR_L2CAP_SERVICES
104 #if MAX_NR_L2CAP_SERVICES > 0
105 static l2cap_service_t l2cap_service_storage[MAX_NR_L2CAP_SERVICES];
106 static btstack_memory_pool_t l2cap_service_pool;
107 l2cap_service_t * btstack_memory_l2cap_service_get(void){
108     return (l2cap_service_t *) btstack_memory_pool_get(&l2cap_service_pool);
109 }
110 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
111     btstack_memory_pool_free(&l2cap_service_pool, l2cap_service);
112 }
113 #else
114 l2cap_service_t * btstack_memory_l2cap_service_get(void){
115     return NULL;
116 }
117 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
118     // silence compiler warning about unused parameter in a portable way
119     (void) l2cap_service;
120 };
121 #endif
122 #elif defined(HAVE_MALLOC)
123 l2cap_service_t * btstack_memory_l2cap_service_get(void){
124     return (l2cap_service_t*) malloc(sizeof(l2cap_service_t));
125 }
126 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
127     free(l2cap_service);
128 }
129 #endif
130 
131 
132 // MARK: l2cap_channel_t
133 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_L2CAP_CHANNELS)
134     #if defined(MAX_NO_L2CAP_CHANNELS)
135         #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."
136     #else
137         #define MAX_NR_L2CAP_CHANNELS 0
138     #endif
139 #endif
140 
141 #ifdef MAX_NR_L2CAP_CHANNELS
142 #if MAX_NR_L2CAP_CHANNELS > 0
143 static l2cap_channel_t l2cap_channel_storage[MAX_NR_L2CAP_CHANNELS];
144 static btstack_memory_pool_t l2cap_channel_pool;
145 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
146     return (l2cap_channel_t *) btstack_memory_pool_get(&l2cap_channel_pool);
147 }
148 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
149     btstack_memory_pool_free(&l2cap_channel_pool, l2cap_channel);
150 }
151 #else
152 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
153     return NULL;
154 }
155 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
156     // silence compiler warning about unused parameter in a portable way
157     (void) l2cap_channel;
158 };
159 #endif
160 #elif defined(HAVE_MALLOC)
161 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
162     return (l2cap_channel_t*) malloc(sizeof(l2cap_channel_t));
163 }
164 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
165     free(l2cap_channel);
166 }
167 #endif
168 
169 
170 
171 // MARK: rfcomm_multiplexer_t
172 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_RFCOMM_MULTIPLEXERS)
173     #if defined(MAX_NO_RFCOMM_MULTIPLEXERS)
174         #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."
175     #else
176         #define MAX_NR_RFCOMM_MULTIPLEXERS 0
177     #endif
178 #endif
179 
180 #ifdef MAX_NR_RFCOMM_MULTIPLEXERS
181 #if MAX_NR_RFCOMM_MULTIPLEXERS > 0
182 static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NR_RFCOMM_MULTIPLEXERS];
183 static btstack_memory_pool_t rfcomm_multiplexer_pool;
184 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
185     return (rfcomm_multiplexer_t *) btstack_memory_pool_get(&rfcomm_multiplexer_pool);
186 }
187 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
188     btstack_memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer);
189 }
190 #else
191 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
192     return NULL;
193 }
194 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
195     // silence compiler warning about unused parameter in a portable way
196     (void) rfcomm_multiplexer;
197 };
198 #endif
199 #elif defined(HAVE_MALLOC)
200 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
201     return (rfcomm_multiplexer_t*) malloc(sizeof(rfcomm_multiplexer_t));
202 }
203 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
204     free(rfcomm_multiplexer);
205 }
206 #endif
207 
208 
209 // MARK: rfcomm_service_t
210 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_RFCOMM_SERVICES)
211     #if defined(MAX_NO_RFCOMM_SERVICES)
212         #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."
213     #else
214         #define MAX_NR_RFCOMM_SERVICES 0
215     #endif
216 #endif
217 
218 #ifdef MAX_NR_RFCOMM_SERVICES
219 #if MAX_NR_RFCOMM_SERVICES > 0
220 static rfcomm_service_t rfcomm_service_storage[MAX_NR_RFCOMM_SERVICES];
221 static btstack_memory_pool_t rfcomm_service_pool;
222 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
223     return (rfcomm_service_t *) btstack_memory_pool_get(&rfcomm_service_pool);
224 }
225 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
226     btstack_memory_pool_free(&rfcomm_service_pool, rfcomm_service);
227 }
228 #else
229 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
230     return NULL;
231 }
232 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
233     // silence compiler warning about unused parameter in a portable way
234     (void) rfcomm_service;
235 };
236 #endif
237 #elif defined(HAVE_MALLOC)
238 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
239     return (rfcomm_service_t*) malloc(sizeof(rfcomm_service_t));
240 }
241 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
242     free(rfcomm_service);
243 }
244 #endif
245 
246 
247 // MARK: rfcomm_channel_t
248 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_RFCOMM_CHANNELS)
249     #if defined(MAX_NO_RFCOMM_CHANNELS)
250         #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."
251     #else
252         #define MAX_NR_RFCOMM_CHANNELS 0
253     #endif
254 #endif
255 
256 #ifdef MAX_NR_RFCOMM_CHANNELS
257 #if MAX_NR_RFCOMM_CHANNELS > 0
258 static rfcomm_channel_t rfcomm_channel_storage[MAX_NR_RFCOMM_CHANNELS];
259 static btstack_memory_pool_t rfcomm_channel_pool;
260 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
261     return (rfcomm_channel_t *) btstack_memory_pool_get(&rfcomm_channel_pool);
262 }
263 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
264     btstack_memory_pool_free(&rfcomm_channel_pool, rfcomm_channel);
265 }
266 #else
267 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
268     return NULL;
269 }
270 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
271     // silence compiler warning about unused parameter in a portable way
272     (void) rfcomm_channel;
273 };
274 #endif
275 #elif defined(HAVE_MALLOC)
276 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
277     return (rfcomm_channel_t*) malloc(sizeof(rfcomm_channel_t));
278 }
279 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
280     free(rfcomm_channel);
281 }
282 #endif
283 
284 
285 
286 // MARK: btstack_link_key_db_memory_entry_t
287 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES)
288     #if defined(MAX_NO_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES)
289         #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."
290     #else
291         #define MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES 0
292     #endif
293 #endif
294 
295 #ifdef MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES
296 #if MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES > 0
297 static btstack_link_key_db_memory_entry_t btstack_link_key_db_memory_entry_storage[MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES];
298 static btstack_memory_pool_t btstack_link_key_db_memory_entry_pool;
299 btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){
300     return (btstack_link_key_db_memory_entry_t *) btstack_memory_pool_get(&btstack_link_key_db_memory_entry_pool);
301 }
302 void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){
303     btstack_memory_pool_free(&btstack_link_key_db_memory_entry_pool, btstack_link_key_db_memory_entry);
304 }
305 #else
306 btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){
307     return NULL;
308 }
309 void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){
310     // silence compiler warning about unused parameter in a portable way
311     (void) btstack_link_key_db_memory_entry;
312 };
313 #endif
314 #elif defined(HAVE_MALLOC)
315 btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){
316     return (btstack_link_key_db_memory_entry_t*) malloc(sizeof(btstack_link_key_db_memory_entry_t));
317 }
318 void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){
319     free(btstack_link_key_db_memory_entry);
320 }
321 #endif
322 
323 
324 
325 // MARK: bnep_service_t
326 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BNEP_SERVICES)
327     #if defined(MAX_NO_BNEP_SERVICES)
328         #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."
329     #else
330         #define MAX_NR_BNEP_SERVICES 0
331     #endif
332 #endif
333 
334 #ifdef MAX_NR_BNEP_SERVICES
335 #if MAX_NR_BNEP_SERVICES > 0
336 static bnep_service_t bnep_service_storage[MAX_NR_BNEP_SERVICES];
337 static btstack_memory_pool_t bnep_service_pool;
338 bnep_service_t * btstack_memory_bnep_service_get(void){
339     return (bnep_service_t *) btstack_memory_pool_get(&bnep_service_pool);
340 }
341 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
342     btstack_memory_pool_free(&bnep_service_pool, bnep_service);
343 }
344 #else
345 bnep_service_t * btstack_memory_bnep_service_get(void){
346     return NULL;
347 }
348 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
349     // silence compiler warning about unused parameter in a portable way
350     (void) bnep_service;
351 };
352 #endif
353 #elif defined(HAVE_MALLOC)
354 bnep_service_t * btstack_memory_bnep_service_get(void){
355     return (bnep_service_t*) malloc(sizeof(bnep_service_t));
356 }
357 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
358     free(bnep_service);
359 }
360 #endif
361 
362 
363 // MARK: bnep_channel_t
364 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BNEP_CHANNELS)
365     #if defined(MAX_NO_BNEP_CHANNELS)
366         #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."
367     #else
368         #define MAX_NR_BNEP_CHANNELS 0
369     #endif
370 #endif
371 
372 #ifdef MAX_NR_BNEP_CHANNELS
373 #if MAX_NR_BNEP_CHANNELS > 0
374 static bnep_channel_t bnep_channel_storage[MAX_NR_BNEP_CHANNELS];
375 static btstack_memory_pool_t bnep_channel_pool;
376 bnep_channel_t * btstack_memory_bnep_channel_get(void){
377     return (bnep_channel_t *) btstack_memory_pool_get(&bnep_channel_pool);
378 }
379 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
380     btstack_memory_pool_free(&bnep_channel_pool, bnep_channel);
381 }
382 #else
383 bnep_channel_t * btstack_memory_bnep_channel_get(void){
384     return NULL;
385 }
386 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
387     // silence compiler warning about unused parameter in a portable way
388     (void) bnep_channel;
389 };
390 #endif
391 #elif defined(HAVE_MALLOC)
392 bnep_channel_t * btstack_memory_bnep_channel_get(void){
393     return (bnep_channel_t*) malloc(sizeof(bnep_channel_t));
394 }
395 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
396     free(bnep_channel);
397 }
398 #endif
399 
400 
401 
402 // MARK: hfp_connection_t
403 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_HFP_CONNECTIONS)
404     #if defined(MAX_NO_HFP_CONNECTIONS)
405         #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."
406     #else
407         #define MAX_NR_HFP_CONNECTIONS 0
408     #endif
409 #endif
410 
411 #ifdef MAX_NR_HFP_CONNECTIONS
412 #if MAX_NR_HFP_CONNECTIONS > 0
413 static hfp_connection_t hfp_connection_storage[MAX_NR_HFP_CONNECTIONS];
414 static btstack_memory_pool_t hfp_connection_pool;
415 hfp_connection_t * btstack_memory_hfp_connection_get(void){
416     return (hfp_connection_t *) btstack_memory_pool_get(&hfp_connection_pool);
417 }
418 void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){
419     btstack_memory_pool_free(&hfp_connection_pool, hfp_connection);
420 }
421 #else
422 hfp_connection_t * btstack_memory_hfp_connection_get(void){
423     return NULL;
424 }
425 void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){
426     // silence compiler warning about unused parameter in a portable way
427     (void) hfp_connection;
428 };
429 #endif
430 #elif defined(HAVE_MALLOC)
431 hfp_connection_t * btstack_memory_hfp_connection_get(void){
432     return (hfp_connection_t*) malloc(sizeof(hfp_connection_t));
433 }
434 void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){
435     free(hfp_connection);
436 }
437 #endif
438 
439 
440 
441 // MARK: service_record_item_t
442 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_SERVICE_RECORD_ITEMS)
443     #if defined(MAX_NO_SERVICE_RECORD_ITEMS)
444         #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."
445     #else
446         #define MAX_NR_SERVICE_RECORD_ITEMS 0
447     #endif
448 #endif
449 
450 #ifdef MAX_NR_SERVICE_RECORD_ITEMS
451 #if MAX_NR_SERVICE_RECORD_ITEMS > 0
452 static service_record_item_t service_record_item_storage[MAX_NR_SERVICE_RECORD_ITEMS];
453 static btstack_memory_pool_t service_record_item_pool;
454 service_record_item_t * btstack_memory_service_record_item_get(void){
455     return (service_record_item_t *) btstack_memory_pool_get(&service_record_item_pool);
456 }
457 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
458     btstack_memory_pool_free(&service_record_item_pool, service_record_item);
459 }
460 #else
461 service_record_item_t * btstack_memory_service_record_item_get(void){
462     return NULL;
463 }
464 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
465     // silence compiler warning about unused parameter in a portable way
466     (void) service_record_item;
467 };
468 #endif
469 #elif defined(HAVE_MALLOC)
470 service_record_item_t * btstack_memory_service_record_item_get(void){
471     return (service_record_item_t*) malloc(sizeof(service_record_item_t));
472 }
473 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
474     free(service_record_item);
475 }
476 #endif
477 
478 
479 
480 // MARK: avdtp_stream_endpoint_t
481 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVDTP_STREAM_ENDPOINTS)
482     #if defined(MAX_NO_AVDTP_STREAM_ENDPOINTS)
483         #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."
484     #else
485         #define MAX_NR_AVDTP_STREAM_ENDPOINTS 0
486     #endif
487 #endif
488 
489 #ifdef MAX_NR_AVDTP_STREAM_ENDPOINTS
490 #if MAX_NR_AVDTP_STREAM_ENDPOINTS > 0
491 static avdtp_stream_endpoint_t avdtp_stream_endpoint_storage[MAX_NR_AVDTP_STREAM_ENDPOINTS];
492 static btstack_memory_pool_t avdtp_stream_endpoint_pool;
493 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){
494     return (avdtp_stream_endpoint_t *) btstack_memory_pool_get(&avdtp_stream_endpoint_pool);
495 }
496 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){
497     btstack_memory_pool_free(&avdtp_stream_endpoint_pool, avdtp_stream_endpoint);
498 }
499 #else
500 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){
501     return NULL;
502 }
503 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){
504     // silence compiler warning about unused parameter in a portable way
505     (void) avdtp_stream_endpoint;
506 };
507 #endif
508 #elif defined(HAVE_MALLOC)
509 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){
510     return (avdtp_stream_endpoint_t*) malloc(sizeof(avdtp_stream_endpoint_t));
511 }
512 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){
513     free(avdtp_stream_endpoint);
514 }
515 #endif
516 
517 
518 
519 // MARK: avdtp_connection_t
520 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVDTP_CONNECTIONS)
521     #if defined(MAX_NO_AVDTP_CONNECTIONS)
522         #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."
523     #else
524         #define MAX_NR_AVDTP_CONNECTIONS 0
525     #endif
526 #endif
527 
528 #ifdef MAX_NR_AVDTP_CONNECTIONS
529 #if MAX_NR_AVDTP_CONNECTIONS > 0
530 static avdtp_connection_t avdtp_connection_storage[MAX_NR_AVDTP_CONNECTIONS];
531 static btstack_memory_pool_t avdtp_connection_pool;
532 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){
533     return (avdtp_connection_t *) btstack_memory_pool_get(&avdtp_connection_pool);
534 }
535 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){
536     btstack_memory_pool_free(&avdtp_connection_pool, avdtp_connection);
537 }
538 #else
539 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){
540     return NULL;
541 }
542 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){
543     // silence compiler warning about unused parameter in a portable way
544     (void) avdtp_connection;
545 };
546 #endif
547 #elif defined(HAVE_MALLOC)
548 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){
549     return (avdtp_connection_t*) malloc(sizeof(avdtp_connection_t));
550 }
551 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){
552     free(avdtp_connection);
553 }
554 #endif
555 
556 
557 
558 // MARK: avrcp_connection_t
559 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVRCP_CONNECTIONS)
560     #if defined(MAX_NO_AVRCP_CONNECTIONS)
561         #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."
562     #else
563         #define MAX_NR_AVRCP_CONNECTIONS 0
564     #endif
565 #endif
566 
567 #ifdef MAX_NR_AVRCP_CONNECTIONS
568 #if MAX_NR_AVRCP_CONNECTIONS > 0
569 static avrcp_connection_t avrcp_connection_storage[MAX_NR_AVRCP_CONNECTIONS];
570 static btstack_memory_pool_t avrcp_connection_pool;
571 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){
572     return (avrcp_connection_t *) btstack_memory_pool_get(&avrcp_connection_pool);
573 }
574 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){
575     btstack_memory_pool_free(&avrcp_connection_pool, avrcp_connection);
576 }
577 #else
578 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){
579     return NULL;
580 }
581 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){
582     // silence compiler warning about unused parameter in a portable way
583     (void) avrcp_connection;
584 };
585 #endif
586 #elif defined(HAVE_MALLOC)
587 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){
588     return (avrcp_connection_t*) malloc(sizeof(avrcp_connection_t));
589 }
590 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){
591     free(avrcp_connection);
592 }
593 #endif
594 
595 
596 
597 // MARK: avrcp_browsing_connection_t
598 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVRCP_BROWSING_CONNECTIONS)
599     #if defined(MAX_NO_AVRCP_BROWSING_CONNECTIONS)
600         #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."
601     #else
602         #define MAX_NR_AVRCP_BROWSING_CONNECTIONS 0
603     #endif
604 #endif
605 
606 #ifdef MAX_NR_AVRCP_BROWSING_CONNECTIONS
607 #if MAX_NR_AVRCP_BROWSING_CONNECTIONS > 0
608 static avrcp_browsing_connection_t avrcp_browsing_connection_storage[MAX_NR_AVRCP_BROWSING_CONNECTIONS];
609 static btstack_memory_pool_t avrcp_browsing_connection_pool;
610 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){
611     return (avrcp_browsing_connection_t *) btstack_memory_pool_get(&avrcp_browsing_connection_pool);
612 }
613 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){
614     btstack_memory_pool_free(&avrcp_browsing_connection_pool, avrcp_browsing_connection);
615 }
616 #else
617 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){
618     return NULL;
619 }
620 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){
621     // silence compiler warning about unused parameter in a portable way
622     (void) avrcp_browsing_connection;
623 };
624 #endif
625 #elif defined(HAVE_MALLOC)
626 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){
627     return (avrcp_browsing_connection_t*) malloc(sizeof(avrcp_browsing_connection_t));
628 }
629 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){
630     free(avrcp_browsing_connection);
631 }
632 #endif
633 
634 
635 #ifdef ENABLE_BLE
636 
637 // MARK: gatt_client_t
638 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_GATT_CLIENTS)
639     #if defined(MAX_NO_GATT_CLIENTS)
640         #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."
641     #else
642         #define MAX_NR_GATT_CLIENTS 0
643     #endif
644 #endif
645 
646 #ifdef MAX_NR_GATT_CLIENTS
647 #if MAX_NR_GATT_CLIENTS > 0
648 static gatt_client_t gatt_client_storage[MAX_NR_GATT_CLIENTS];
649 static btstack_memory_pool_t gatt_client_pool;
650 gatt_client_t * btstack_memory_gatt_client_get(void){
651     return (gatt_client_t *) btstack_memory_pool_get(&gatt_client_pool);
652 }
653 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
654     btstack_memory_pool_free(&gatt_client_pool, gatt_client);
655 }
656 #else
657 gatt_client_t * btstack_memory_gatt_client_get(void){
658     return NULL;
659 }
660 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
661     // silence compiler warning about unused parameter in a portable way
662     (void) gatt_client;
663 };
664 #endif
665 #elif defined(HAVE_MALLOC)
666 gatt_client_t * btstack_memory_gatt_client_get(void){
667     return (gatt_client_t*) malloc(sizeof(gatt_client_t));
668 }
669 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
670     free(gatt_client);
671 }
672 #endif
673 
674 
675 // MARK: whitelist_entry_t
676 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_WHITELIST_ENTRIES)
677     #if defined(MAX_NO_WHITELIST_ENTRIES)
678         #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."
679     #else
680         #define MAX_NR_WHITELIST_ENTRIES 0
681     #endif
682 #endif
683 
684 #ifdef MAX_NR_WHITELIST_ENTRIES
685 #if MAX_NR_WHITELIST_ENTRIES > 0
686 static whitelist_entry_t whitelist_entry_storage[MAX_NR_WHITELIST_ENTRIES];
687 static btstack_memory_pool_t whitelist_entry_pool;
688 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
689     return (whitelist_entry_t *) btstack_memory_pool_get(&whitelist_entry_pool);
690 }
691 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
692     btstack_memory_pool_free(&whitelist_entry_pool, whitelist_entry);
693 }
694 #else
695 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
696     return NULL;
697 }
698 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
699     // silence compiler warning about unused parameter in a portable way
700     (void) whitelist_entry;
701 };
702 #endif
703 #elif defined(HAVE_MALLOC)
704 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
705     return (whitelist_entry_t*) malloc(sizeof(whitelist_entry_t));
706 }
707 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
708     free(whitelist_entry);
709 }
710 #endif
711 
712 
713 // MARK: sm_lookup_entry_t
714 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_SM_LOOKUP_ENTRIES)
715     #if defined(MAX_NO_SM_LOOKUP_ENTRIES)
716         #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."
717     #else
718         #define MAX_NR_SM_LOOKUP_ENTRIES 0
719     #endif
720 #endif
721 
722 #ifdef MAX_NR_SM_LOOKUP_ENTRIES
723 #if MAX_NR_SM_LOOKUP_ENTRIES > 0
724 static sm_lookup_entry_t sm_lookup_entry_storage[MAX_NR_SM_LOOKUP_ENTRIES];
725 static btstack_memory_pool_t sm_lookup_entry_pool;
726 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
727     return (sm_lookup_entry_t *) btstack_memory_pool_get(&sm_lookup_entry_pool);
728 }
729 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
730     btstack_memory_pool_free(&sm_lookup_entry_pool, sm_lookup_entry);
731 }
732 #else
733 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
734     return NULL;
735 }
736 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
737     // silence compiler warning about unused parameter in a portable way
738     (void) sm_lookup_entry;
739 };
740 #endif
741 #elif defined(HAVE_MALLOC)
742 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
743     return (sm_lookup_entry_t*) malloc(sizeof(sm_lookup_entry_t));
744 }
745 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
746     free(sm_lookup_entry);
747 }
748 #endif
749 
750 
751 #endif
752 // init
753 void btstack_memory_init(void){
754 #if MAX_NR_HCI_CONNECTIONS > 0
755     btstack_memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NR_HCI_CONNECTIONS, sizeof(hci_connection_t));
756 #endif
757 #if MAX_NR_L2CAP_SERVICES > 0
758     btstack_memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NR_L2CAP_SERVICES, sizeof(l2cap_service_t));
759 #endif
760 #if MAX_NR_L2CAP_CHANNELS > 0
761     btstack_memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NR_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
762 #endif
763 #if MAX_NR_RFCOMM_MULTIPLEXERS > 0
764     btstack_memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NR_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
765 #endif
766 #if MAX_NR_RFCOMM_SERVICES > 0
767     btstack_memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NR_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
768 #endif
769 #if MAX_NR_RFCOMM_CHANNELS > 0
770     btstack_memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NR_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
771 #endif
772 #if MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES > 0
773     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));
774 #endif
775 #if MAX_NR_BNEP_SERVICES > 0
776     btstack_memory_pool_create(&bnep_service_pool, bnep_service_storage, MAX_NR_BNEP_SERVICES, sizeof(bnep_service_t));
777 #endif
778 #if MAX_NR_BNEP_CHANNELS > 0
779     btstack_memory_pool_create(&bnep_channel_pool, bnep_channel_storage, MAX_NR_BNEP_CHANNELS, sizeof(bnep_channel_t));
780 #endif
781 #if MAX_NR_HFP_CONNECTIONS > 0
782     btstack_memory_pool_create(&hfp_connection_pool, hfp_connection_storage, MAX_NR_HFP_CONNECTIONS, sizeof(hfp_connection_t));
783 #endif
784 #if MAX_NR_SERVICE_RECORD_ITEMS > 0
785     btstack_memory_pool_create(&service_record_item_pool, service_record_item_storage, MAX_NR_SERVICE_RECORD_ITEMS, sizeof(service_record_item_t));
786 #endif
787 #if MAX_NR_AVDTP_STREAM_ENDPOINTS > 0
788     btstack_memory_pool_create(&avdtp_stream_endpoint_pool, avdtp_stream_endpoint_storage, MAX_NR_AVDTP_STREAM_ENDPOINTS, sizeof(avdtp_stream_endpoint_t));
789 #endif
790 #if MAX_NR_AVDTP_CONNECTIONS > 0
791     btstack_memory_pool_create(&avdtp_connection_pool, avdtp_connection_storage, MAX_NR_AVDTP_CONNECTIONS, sizeof(avdtp_connection_t));
792 #endif
793 #if MAX_NR_AVRCP_CONNECTIONS > 0
794     btstack_memory_pool_create(&avrcp_connection_pool, avrcp_connection_storage, MAX_NR_AVRCP_CONNECTIONS, sizeof(avrcp_connection_t));
795 #endif
796 #if MAX_NR_AVRCP_BROWSING_CONNECTIONS > 0
797     btstack_memory_pool_create(&avrcp_browsing_connection_pool, avrcp_browsing_connection_storage, MAX_NR_AVRCP_BROWSING_CONNECTIONS, sizeof(avrcp_browsing_connection_t));
798 #endif
799 #ifdef ENABLE_BLE
800 #if MAX_NR_GATT_CLIENTS > 0
801     btstack_memory_pool_create(&gatt_client_pool, gatt_client_storage, MAX_NR_GATT_CLIENTS, sizeof(gatt_client_t));
802 #endif
803 #if MAX_NR_WHITELIST_ENTRIES > 0
804     btstack_memory_pool_create(&whitelist_entry_pool, whitelist_entry_storage, MAX_NR_WHITELIST_ENTRIES, sizeof(whitelist_entry_t));
805 #endif
806 #if MAX_NR_SM_LOOKUP_ENTRIES > 0
807     btstack_memory_pool_create(&sm_lookup_entry_pool, sm_lookup_entry_storage, MAX_NR_SM_LOOKUP_ENTRIES, sizeof(sm_lookup_entry_t));
808 #endif
809 #endif
810 }
811