xref: /btstack/src/btstack_memory.c (revision f12924e0a61c0582b548a1e70cea1bd59ba21f1d)
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 #ifdef ENABLE_BLE
558 
559 // MARK: gatt_client_t
560 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_GATT_CLIENTS)
561     #if defined(MAX_NO_GATT_CLIENTS)
562         #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."
563     #else
564         #define MAX_NR_GATT_CLIENTS 0
565     #endif
566 #endif
567 
568 #ifdef MAX_NR_GATT_CLIENTS
569 #if MAX_NR_GATT_CLIENTS > 0
570 static gatt_client_t gatt_client_storage[MAX_NR_GATT_CLIENTS];
571 static btstack_memory_pool_t gatt_client_pool;
572 gatt_client_t * btstack_memory_gatt_client_get(void){
573     return (gatt_client_t *) btstack_memory_pool_get(&gatt_client_pool);
574 }
575 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
576     btstack_memory_pool_free(&gatt_client_pool, gatt_client);
577 }
578 #else
579 gatt_client_t * btstack_memory_gatt_client_get(void){
580     return NULL;
581 }
582 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
583     // silence compiler warning about unused parameter in a portable way
584     (void) gatt_client;
585 };
586 #endif
587 #elif defined(HAVE_MALLOC)
588 gatt_client_t * btstack_memory_gatt_client_get(void){
589     return (gatt_client_t*) malloc(sizeof(gatt_client_t));
590 }
591 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
592     free(gatt_client);
593 }
594 #endif
595 
596 
597 // MARK: whitelist_entry_t
598 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_WHITELIST_ENTRIES)
599     #if defined(MAX_NO_WHITELIST_ENTRIES)
600         #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."
601     #else
602         #define MAX_NR_WHITELIST_ENTRIES 0
603     #endif
604 #endif
605 
606 #ifdef MAX_NR_WHITELIST_ENTRIES
607 #if MAX_NR_WHITELIST_ENTRIES > 0
608 static whitelist_entry_t whitelist_entry_storage[MAX_NR_WHITELIST_ENTRIES];
609 static btstack_memory_pool_t whitelist_entry_pool;
610 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
611     return (whitelist_entry_t *) btstack_memory_pool_get(&whitelist_entry_pool);
612 }
613 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
614     btstack_memory_pool_free(&whitelist_entry_pool, whitelist_entry);
615 }
616 #else
617 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
618     return NULL;
619 }
620 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
621     // silence compiler warning about unused parameter in a portable way
622     (void) whitelist_entry;
623 };
624 #endif
625 #elif defined(HAVE_MALLOC)
626 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
627     return (whitelist_entry_t*) malloc(sizeof(whitelist_entry_t));
628 }
629 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
630     free(whitelist_entry);
631 }
632 #endif
633 
634 
635 // MARK: sm_lookup_entry_t
636 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_SM_LOOKUP_ENTRIES)
637     #if defined(MAX_NO_SM_LOOKUP_ENTRIES)
638         #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."
639     #else
640         #define MAX_NR_SM_LOOKUP_ENTRIES 0
641     #endif
642 #endif
643 
644 #ifdef MAX_NR_SM_LOOKUP_ENTRIES
645 #if MAX_NR_SM_LOOKUP_ENTRIES > 0
646 static sm_lookup_entry_t sm_lookup_entry_storage[MAX_NR_SM_LOOKUP_ENTRIES];
647 static btstack_memory_pool_t sm_lookup_entry_pool;
648 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
649     return (sm_lookup_entry_t *) btstack_memory_pool_get(&sm_lookup_entry_pool);
650 }
651 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
652     btstack_memory_pool_free(&sm_lookup_entry_pool, sm_lookup_entry);
653 }
654 #else
655 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
656     return NULL;
657 }
658 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
659     // silence compiler warning about unused parameter in a portable way
660     (void) sm_lookup_entry;
661 };
662 #endif
663 #elif defined(HAVE_MALLOC)
664 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
665     return (sm_lookup_entry_t*) malloc(sizeof(sm_lookup_entry_t));
666 }
667 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
668     free(sm_lookup_entry);
669 }
670 #endif
671 
672 
673 #endif
674 // init
675 void btstack_memory_init(void){
676 #if MAX_NR_HCI_CONNECTIONS > 0
677     btstack_memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NR_HCI_CONNECTIONS, sizeof(hci_connection_t));
678 #endif
679 #if MAX_NR_L2CAP_SERVICES > 0
680     btstack_memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NR_L2CAP_SERVICES, sizeof(l2cap_service_t));
681 #endif
682 #if MAX_NR_L2CAP_CHANNELS > 0
683     btstack_memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NR_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
684 #endif
685 #if MAX_NR_RFCOMM_MULTIPLEXERS > 0
686     btstack_memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NR_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
687 #endif
688 #if MAX_NR_RFCOMM_SERVICES > 0
689     btstack_memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NR_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
690 #endif
691 #if MAX_NR_RFCOMM_CHANNELS > 0
692     btstack_memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NR_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
693 #endif
694 #if MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES > 0
695     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));
696 #endif
697 #if MAX_NR_BNEP_SERVICES > 0
698     btstack_memory_pool_create(&bnep_service_pool, bnep_service_storage, MAX_NR_BNEP_SERVICES, sizeof(bnep_service_t));
699 #endif
700 #if MAX_NR_BNEP_CHANNELS > 0
701     btstack_memory_pool_create(&bnep_channel_pool, bnep_channel_storage, MAX_NR_BNEP_CHANNELS, sizeof(bnep_channel_t));
702 #endif
703 #if MAX_NR_HFP_CONNECTIONS > 0
704     btstack_memory_pool_create(&hfp_connection_pool, hfp_connection_storage, MAX_NR_HFP_CONNECTIONS, sizeof(hfp_connection_t));
705 #endif
706 #if MAX_NR_SERVICE_RECORD_ITEMS > 0
707     btstack_memory_pool_create(&service_record_item_pool, service_record_item_storage, MAX_NR_SERVICE_RECORD_ITEMS, sizeof(service_record_item_t));
708 #endif
709 #if MAX_NR_AVDTP_STREAM_ENDPOINTS > 0
710     btstack_memory_pool_create(&avdtp_stream_endpoint_pool, avdtp_stream_endpoint_storage, MAX_NR_AVDTP_STREAM_ENDPOINTS, sizeof(avdtp_stream_endpoint_t));
711 #endif
712 #if MAX_NR_AVDTP_CONNECTIONS > 0
713     btstack_memory_pool_create(&avdtp_connection_pool, avdtp_connection_storage, MAX_NR_AVDTP_CONNECTIONS, sizeof(avdtp_connection_t));
714 #endif
715 #ifdef ENABLE_BLE
716 #if MAX_NR_GATT_CLIENTS > 0
717     btstack_memory_pool_create(&gatt_client_pool, gatt_client_storage, MAX_NR_GATT_CLIENTS, sizeof(gatt_client_t));
718 #endif
719 #if MAX_NR_WHITELIST_ENTRIES > 0
720     btstack_memory_pool_create(&whitelist_entry_pool, whitelist_entry_storage, MAX_NR_WHITELIST_ENTRIES, sizeof(whitelist_entry_t));
721 #endif
722 #if MAX_NR_SM_LOOKUP_ENTRIES > 0
723     btstack_memory_pool_create(&sm_lookup_entry_pool, sm_lookup_entry_storage, MAX_NR_SM_LOOKUP_ENTRIES, sizeof(sm_lookup_entry_t));
724 #endif
725 #endif
726 }
727