xref: /btstack/src/btstack_memory.c (revision 2c455dad9a589f886e28f6a5d10bb735305e4f1b)
1a3b02b71Smatthias.ringwald /*
2a0c35809S[email protected]  * Copyright (C) 2014 BlueKitchen GmbH
3a3b02b71Smatthias.ringwald  *
4a3b02b71Smatthias.ringwald  * Redistribution and use in source and binary forms, with or without
5a3b02b71Smatthias.ringwald  * modification, are permitted provided that the following conditions
6a3b02b71Smatthias.ringwald  * are met:
7a3b02b71Smatthias.ringwald  *
8a3b02b71Smatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
9a3b02b71Smatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
10a3b02b71Smatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11a3b02b71Smatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
12a3b02b71Smatthias.ringwald  *    documentation and/or other materials provided with the distribution.
13a3b02b71Smatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
14a3b02b71Smatthias.ringwald  *    contributors may be used to endorse or promote products derived
15a3b02b71Smatthias.ringwald  *    from this software without specific prior written permission.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
19a3b02b71Smatthias.ringwald  *
202e97c149S[email protected]  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21a3b02b71Smatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22a3b02b71Smatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23a3b02b71Smatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24a3b02b71Smatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25a3b02b71Smatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26a3b02b71Smatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27a3b02b71Smatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28a3b02b71Smatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29a3b02b71Smatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30a3b02b71Smatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31a3b02b71Smatthias.ringwald  * SUCH DAMAGE.
32a3b02b71Smatthias.ringwald  *
332e97c149S[email protected]  * Please inquire about commercial licensing options at
342e97c149S[email protected]  * [email protected]
356b64433eSmatthias.ringwald  *
36a3b02b71Smatthias.ringwald  */
37a3b02b71Smatthias.ringwald 
382e97c149S[email protected] 
39a3b02b71Smatthias.ringwald /*
40a98592bcSMatthias Ringwald  *  btstack_memory.h
41a3b02b71Smatthias.ringwald  *
42a3b02b71Smatthias.ringwald  *  @brief BTstack memory management via configurable memory pools
43a3b02b71Smatthias.ringwald  *
44a98592bcSMatthias Ringwald  *  @note code generated by tool/btstack_memory_generator.py
45a3b02b71Smatthias.ringwald  *
46a3b02b71Smatthias.ringwald  */
47a3b02b71Smatthias.ringwald 
48a3b02b71Smatthias.ringwald #include "btstack_memory.h"
49d2e6c4b7SMatthias Ringwald #include "btstack_memory_pool.h"
50a3b02b71Smatthias.ringwald 
51a3b02b71Smatthias.ringwald #include <stdlib.h>
52a3b02b71Smatthias.ringwald 
53a3b02b71Smatthias.ringwald 
542e97c149S[email protected] 
55a3b02b71Smatthias.ringwald // MARK: hci_connection_t
56a3b02b71Smatthias.ringwald #ifdef MAX_NO_HCI_CONNECTIONS
57c4d3f927Smatthias.ringwald #if MAX_NO_HCI_CONNECTIONS > 0
58a3b02b71Smatthias.ringwald static hci_connection_t hci_connection_storage[MAX_NO_HCI_CONNECTIONS];
5929d0c4f7SMatthias Ringwald static btstack_memory_pool_t hci_connection_pool;
606527a633S[email protected] hci_connection_t * btstack_memory_hci_connection_get(void){
6129d0c4f7SMatthias Ringwald     return (hci_connection_t *) btstack_memory_pool_get(&hci_connection_pool);
62a3b02b71Smatthias.ringwald }
636527a633S[email protected] void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
6429d0c4f7SMatthias Ringwald     btstack_memory_pool_free(&hci_connection_pool, hci_connection);
65a3b02b71Smatthias.ringwald }
66c4d3f927Smatthias.ringwald #else
676527a633S[email protected] hci_connection_t * btstack_memory_hci_connection_get(void){
68c4d3f927Smatthias.ringwald     return NULL;
69c4d3f927Smatthias.ringwald }
706527a633S[email protected] void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
716f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
722e97c149S[email protected]     (void) hci_connection;
73c4d3f927Smatthias.ringwald };
74c4d3f927Smatthias.ringwald #endif
75a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
766527a633S[email protected] hci_connection_t * btstack_memory_hci_connection_get(void){
776527a633S[email protected]     return (hci_connection_t*) malloc(sizeof(hci_connection_t));
78a3b02b71Smatthias.ringwald }
796527a633S[email protected] void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
80a3b02b71Smatthias.ringwald     free(hci_connection);
81a3b02b71Smatthias.ringwald }
82e045abdeSmatthias.ringwald #else
836527a633S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_HCI_CONNECTIONS for struct hci_connection is defined. Please, edit the config file."
84a3b02b71Smatthias.ringwald #endif
85a3b02b71Smatthias.ringwald 
86a3b02b71Smatthias.ringwald 
872e97c149S[email protected] 
88a3b02b71Smatthias.ringwald // MARK: l2cap_service_t
89a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_SERVICES
90c4d3f927Smatthias.ringwald #if MAX_NO_L2CAP_SERVICES > 0
91a3b02b71Smatthias.ringwald static l2cap_service_t l2cap_service_storage[MAX_NO_L2CAP_SERVICES];
9229d0c4f7SMatthias Ringwald static btstack_memory_pool_t l2cap_service_pool;
936527a633S[email protected] l2cap_service_t * btstack_memory_l2cap_service_get(void){
9429d0c4f7SMatthias Ringwald     return (l2cap_service_t *) btstack_memory_pool_get(&l2cap_service_pool);
95a3b02b71Smatthias.ringwald }
966527a633S[email protected] void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
9729d0c4f7SMatthias Ringwald     btstack_memory_pool_free(&l2cap_service_pool, l2cap_service);
98a3b02b71Smatthias.ringwald }
99c4d3f927Smatthias.ringwald #else
1006527a633S[email protected] l2cap_service_t * btstack_memory_l2cap_service_get(void){
101c4d3f927Smatthias.ringwald     return NULL;
102c4d3f927Smatthias.ringwald }
1036527a633S[email protected] void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
1046f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
1056f7ecb09S[email protected]     (void) l2cap_service;
106c4d3f927Smatthias.ringwald };
107c4d3f927Smatthias.ringwald #endif
108a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
1096527a633S[email protected] l2cap_service_t * btstack_memory_l2cap_service_get(void){
1106527a633S[email protected]     return (l2cap_service_t*) malloc(sizeof(l2cap_service_t));
111a3b02b71Smatthias.ringwald }
1126527a633S[email protected] void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
113a3b02b71Smatthias.ringwald     free(l2cap_service);
114a3b02b71Smatthias.ringwald }
115e045abdeSmatthias.ringwald #else
1166527a633S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_L2CAP_SERVICES for struct l2cap_service is defined. Please, edit the config file."
117a3b02b71Smatthias.ringwald #endif
118a3b02b71Smatthias.ringwald 
119a3b02b71Smatthias.ringwald 
120a3b02b71Smatthias.ringwald // MARK: l2cap_channel_t
121a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_CHANNELS
122c4d3f927Smatthias.ringwald #if MAX_NO_L2CAP_CHANNELS > 0
123a3b02b71Smatthias.ringwald static l2cap_channel_t l2cap_channel_storage[MAX_NO_L2CAP_CHANNELS];
12429d0c4f7SMatthias Ringwald static btstack_memory_pool_t l2cap_channel_pool;
1256527a633S[email protected] l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
12629d0c4f7SMatthias Ringwald     return (l2cap_channel_t *) btstack_memory_pool_get(&l2cap_channel_pool);
127a3b02b71Smatthias.ringwald }
1286527a633S[email protected] void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
12929d0c4f7SMatthias Ringwald     btstack_memory_pool_free(&l2cap_channel_pool, l2cap_channel);
130a3b02b71Smatthias.ringwald }
131c4d3f927Smatthias.ringwald #else
1326527a633S[email protected] l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
133c4d3f927Smatthias.ringwald     return NULL;
134c4d3f927Smatthias.ringwald }
1356527a633S[email protected] void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
1366f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
1376f7ecb09S[email protected]     (void) l2cap_channel;
138c4d3f927Smatthias.ringwald };
139c4d3f927Smatthias.ringwald #endif
140a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
1416527a633S[email protected] l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
1426527a633S[email protected]     return (l2cap_channel_t*) malloc(sizeof(l2cap_channel_t));
143a3b02b71Smatthias.ringwald }
1446527a633S[email protected] void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
145a3b02b71Smatthias.ringwald     free(l2cap_channel);
146a3b02b71Smatthias.ringwald }
147e045abdeSmatthias.ringwald #else
1486527a633S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_L2CAP_CHANNELS for struct l2cap_channel is defined. Please, edit the config file."
149a3b02b71Smatthias.ringwald #endif
150a3b02b71Smatthias.ringwald 
151a3b02b71Smatthias.ringwald 
1522e97c149S[email protected] 
153a3b02b71Smatthias.ringwald // MARK: rfcomm_multiplexer_t
154a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
155c4d3f927Smatthias.ringwald #if MAX_NO_RFCOMM_MULTIPLEXERS > 0
156a3b02b71Smatthias.ringwald static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NO_RFCOMM_MULTIPLEXERS];
15729d0c4f7SMatthias Ringwald static btstack_memory_pool_t rfcomm_multiplexer_pool;
1586527a633S[email protected] rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
15929d0c4f7SMatthias Ringwald     return (rfcomm_multiplexer_t *) btstack_memory_pool_get(&rfcomm_multiplexer_pool);
160a3b02b71Smatthias.ringwald }
1616527a633S[email protected] void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
16229d0c4f7SMatthias Ringwald     btstack_memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer);
163a3b02b71Smatthias.ringwald }
164c4d3f927Smatthias.ringwald #else
1656527a633S[email protected] rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
166c4d3f927Smatthias.ringwald     return NULL;
167c4d3f927Smatthias.ringwald }
1686527a633S[email protected] void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
1696f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
1706f7ecb09S[email protected]     (void) rfcomm_multiplexer;
171c4d3f927Smatthias.ringwald };
172c4d3f927Smatthias.ringwald #endif
173a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
1746527a633S[email protected] rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
1756527a633S[email protected]     return (rfcomm_multiplexer_t*) malloc(sizeof(rfcomm_multiplexer_t));
176a3b02b71Smatthias.ringwald }
1776527a633S[email protected] void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
178a3b02b71Smatthias.ringwald     free(rfcomm_multiplexer);
179a3b02b71Smatthias.ringwald }
180e045abdeSmatthias.ringwald #else
1816527a633S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_RFCOMM_MULTIPLEXERS for struct rfcomm_multiplexer is defined. Please, edit the config file."
182a3b02b71Smatthias.ringwald #endif
183a3b02b71Smatthias.ringwald 
184a3b02b71Smatthias.ringwald 
185a3b02b71Smatthias.ringwald // MARK: rfcomm_service_t
186a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_SERVICES
187c4d3f927Smatthias.ringwald #if MAX_NO_RFCOMM_SERVICES > 0
188a3b02b71Smatthias.ringwald static rfcomm_service_t rfcomm_service_storage[MAX_NO_RFCOMM_SERVICES];
18929d0c4f7SMatthias Ringwald static btstack_memory_pool_t rfcomm_service_pool;
1906527a633S[email protected] rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
19129d0c4f7SMatthias Ringwald     return (rfcomm_service_t *) btstack_memory_pool_get(&rfcomm_service_pool);
192a3b02b71Smatthias.ringwald }
1936527a633S[email protected] void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
19429d0c4f7SMatthias Ringwald     btstack_memory_pool_free(&rfcomm_service_pool, rfcomm_service);
195a3b02b71Smatthias.ringwald }
196c4d3f927Smatthias.ringwald #else
1976527a633S[email protected] rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
198c4d3f927Smatthias.ringwald     return NULL;
199c4d3f927Smatthias.ringwald }
2006527a633S[email protected] void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
2016f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
2026f7ecb09S[email protected]     (void) rfcomm_service;
203c4d3f927Smatthias.ringwald };
204c4d3f927Smatthias.ringwald #endif
205a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
2066527a633S[email protected] rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
2076527a633S[email protected]     return (rfcomm_service_t*) malloc(sizeof(rfcomm_service_t));
208a3b02b71Smatthias.ringwald }
2096527a633S[email protected] void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
210a3b02b71Smatthias.ringwald     free(rfcomm_service);
211a3b02b71Smatthias.ringwald }
212e045abdeSmatthias.ringwald #else
2136527a633S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_RFCOMM_SERVICES for struct rfcomm_service is defined. Please, edit the config file."
214a3b02b71Smatthias.ringwald #endif
215a3b02b71Smatthias.ringwald 
216a3b02b71Smatthias.ringwald 
217a3b02b71Smatthias.ringwald // MARK: rfcomm_channel_t
218a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_CHANNELS
219c4d3f927Smatthias.ringwald #if MAX_NO_RFCOMM_CHANNELS > 0
220a3b02b71Smatthias.ringwald static rfcomm_channel_t rfcomm_channel_storage[MAX_NO_RFCOMM_CHANNELS];
22129d0c4f7SMatthias Ringwald static btstack_memory_pool_t rfcomm_channel_pool;
2226527a633S[email protected] rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
22329d0c4f7SMatthias Ringwald     return (rfcomm_channel_t *) btstack_memory_pool_get(&rfcomm_channel_pool);
224a3b02b71Smatthias.ringwald }
2256527a633S[email protected] void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
22629d0c4f7SMatthias Ringwald     btstack_memory_pool_free(&rfcomm_channel_pool, rfcomm_channel);
227a3b02b71Smatthias.ringwald }
228c4d3f927Smatthias.ringwald #else
2296527a633S[email protected] rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
230c4d3f927Smatthias.ringwald     return NULL;
231c4d3f927Smatthias.ringwald }
2326527a633S[email protected] void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
2336f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
2346f7ecb09S[email protected]     (void) rfcomm_channel;
235c4d3f927Smatthias.ringwald };
236c4d3f927Smatthias.ringwald #endif
237a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
2386527a633S[email protected] rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
2396527a633S[email protected]     return (rfcomm_channel_t*) malloc(sizeof(rfcomm_channel_t));
240a3b02b71Smatthias.ringwald }
2416527a633S[email protected] void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
242a3b02b71Smatthias.ringwald     free(rfcomm_channel);
243a3b02b71Smatthias.ringwald }
244e045abdeSmatthias.ringwald #else
2456527a633S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_RFCOMM_CHANNELS for struct rfcomm_channel is defined. Please, edit the config file."
246a3b02b71Smatthias.ringwald #endif
247a3b02b71Smatthias.ringwald 
248fdb398c2S[email protected] 
249e0e5e285Smatthias.ringwald 
250*2c455dadSMatthias Ringwald // MARK: btstack_link_key_db_memory_entry_t
251*2c455dadSMatthias Ringwald #ifdef MAX_NO_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES
252*2c455dadSMatthias Ringwald #if MAX_NO_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES > 0
253*2c455dadSMatthias Ringwald static btstack_link_key_db_memory_entry_t btstack_link_key_db_memory_entry_storage[MAX_NO_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES];
254*2c455dadSMatthias Ringwald static btstack_memory_pool_t btstack_link_key_db_memory_entry_pool;
255*2c455dadSMatthias Ringwald btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){
256*2c455dadSMatthias Ringwald     return (btstack_link_key_db_memory_entry_t *) btstack_memory_pool_get(&btstack_link_key_db_memory_entry_pool);
2571801b596Smatthias.ringwald }
258*2c455dadSMatthias Ringwald void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){
259*2c455dadSMatthias Ringwald     btstack_memory_pool_free(&btstack_link_key_db_memory_entry_pool, btstack_link_key_db_memory_entry);
2601801b596Smatthias.ringwald }
261c4d3f927Smatthias.ringwald #else
262*2c455dadSMatthias Ringwald btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){
263c4d3f927Smatthias.ringwald     return NULL;
264c4d3f927Smatthias.ringwald }
265*2c455dadSMatthias Ringwald void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){
2666f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
267*2c455dadSMatthias Ringwald     (void) btstack_link_key_db_memory_entry;
268c4d3f927Smatthias.ringwald };
269c4d3f927Smatthias.ringwald #endif
2701801b596Smatthias.ringwald #elif defined(HAVE_MALLOC)
271*2c455dadSMatthias Ringwald btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){
272*2c455dadSMatthias Ringwald     return (btstack_link_key_db_memory_entry_t*) malloc(sizeof(btstack_link_key_db_memory_entry_t));
2731801b596Smatthias.ringwald }
274*2c455dadSMatthias Ringwald void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){
275*2c455dadSMatthias Ringwald     free(btstack_link_key_db_memory_entry);
276e0e5e285Smatthias.ringwald }
277e045abdeSmatthias.ringwald #else
278*2c455dadSMatthias Ringwald #error "Neither HAVE_MALLOC nor MAX_NO_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES for struct btstack_link_key_db_memory_entry is defined. Please, edit the config file."
2791801b596Smatthias.ringwald #endif
2801801b596Smatthias.ringwald 
2812e97c149S[email protected] 
2822e97c149S[email protected] 
2832e97c149S[email protected] // MARK: bnep_service_t
2842e97c149S[email protected] #ifdef MAX_NO_BNEP_SERVICES
2852e97c149S[email protected] #if MAX_NO_BNEP_SERVICES > 0
2862e97c149S[email protected] static bnep_service_t bnep_service_storage[MAX_NO_BNEP_SERVICES];
28729d0c4f7SMatthias Ringwald static btstack_memory_pool_t bnep_service_pool;
2882e97c149S[email protected] bnep_service_t * btstack_memory_bnep_service_get(void){
28929d0c4f7SMatthias Ringwald     return (bnep_service_t *) btstack_memory_pool_get(&bnep_service_pool);
2902e97c149S[email protected] }
2912e97c149S[email protected] void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
29229d0c4f7SMatthias Ringwald     btstack_memory_pool_free(&bnep_service_pool, bnep_service);
2932e97c149S[email protected] }
2942e97c149S[email protected] #else
2952e97c149S[email protected] bnep_service_t * btstack_memory_bnep_service_get(void){
2962e97c149S[email protected]     return NULL;
2972e97c149S[email protected] }
2982e97c149S[email protected] void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
2992e97c149S[email protected]     // silence compiler warning about unused parameter in a portable way
3002e97c149S[email protected]     (void) bnep_service;
3012e97c149S[email protected] };
3022e97c149S[email protected] #endif
3032e97c149S[email protected] #elif defined(HAVE_MALLOC)
3042e97c149S[email protected] bnep_service_t * btstack_memory_bnep_service_get(void){
3052e97c149S[email protected]     return (bnep_service_t*) malloc(sizeof(bnep_service_t));
3062e97c149S[email protected] }
3072e97c149S[email protected] void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
3082e97c149S[email protected]     free(bnep_service);
3092e97c149S[email protected] }
3102e97c149S[email protected] #else
3112e97c149S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_BNEP_SERVICES for struct bnep_service is defined. Please, edit the config file."
3122e97c149S[email protected] #endif
3132e97c149S[email protected] 
3142e97c149S[email protected] 
3152e97c149S[email protected] // MARK: bnep_channel_t
3162e97c149S[email protected] #ifdef MAX_NO_BNEP_CHANNELS
3172e97c149S[email protected] #if MAX_NO_BNEP_CHANNELS > 0
3182e97c149S[email protected] static bnep_channel_t bnep_channel_storage[MAX_NO_BNEP_CHANNELS];
31929d0c4f7SMatthias Ringwald static btstack_memory_pool_t bnep_channel_pool;
3202e97c149S[email protected] bnep_channel_t * btstack_memory_bnep_channel_get(void){
32129d0c4f7SMatthias Ringwald     return (bnep_channel_t *) btstack_memory_pool_get(&bnep_channel_pool);
3222e97c149S[email protected] }
3232e97c149S[email protected] void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
32429d0c4f7SMatthias Ringwald     btstack_memory_pool_free(&bnep_channel_pool, bnep_channel);
3252e97c149S[email protected] }
3262e97c149S[email protected] #else
3272e97c149S[email protected] bnep_channel_t * btstack_memory_bnep_channel_get(void){
3282e97c149S[email protected]     return NULL;
3292e97c149S[email protected] }
3302e97c149S[email protected] void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
3312e97c149S[email protected]     // silence compiler warning about unused parameter in a portable way
3322e97c149S[email protected]     (void) bnep_channel;
3332e97c149S[email protected] };
3342e97c149S[email protected] #endif
3352e97c149S[email protected] #elif defined(HAVE_MALLOC)
3362e97c149S[email protected] bnep_channel_t * btstack_memory_bnep_channel_get(void){
3372e97c149S[email protected]     return (bnep_channel_t*) malloc(sizeof(bnep_channel_t));
3382e97c149S[email protected] }
3392e97c149S[email protected] void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
3402e97c149S[email protected]     free(bnep_channel);
3412e97c149S[email protected] }
3422e97c149S[email protected] #else
3432e97c149S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_BNEP_CHANNELS for struct bnep_channel is defined. Please, edit the config file."
3442e97c149S[email protected] #endif
3452e97c149S[email protected] 
3462e97c149S[email protected] 
347ea5029c9SMilanka Ringwald 
348ea5029c9SMilanka Ringwald // MARK: hfp_connection_t
349ea5029c9SMilanka Ringwald #ifdef MAX_NO_HFP_CONNECTIONS
350ea5029c9SMilanka Ringwald #if MAX_NO_HFP_CONNECTIONS > 0
351ea5029c9SMilanka Ringwald static hfp_connection_t hfp_connection_storage[MAX_NO_HFP_CONNECTIONS];
35229d0c4f7SMatthias Ringwald static btstack_memory_pool_t hfp_connection_pool;
353ea5029c9SMilanka Ringwald hfp_connection_t * btstack_memory_hfp_connection_get(void){
35429d0c4f7SMatthias Ringwald     return (hfp_connection_t *) btstack_memory_pool_get(&hfp_connection_pool);
355ea5029c9SMilanka Ringwald }
356ea5029c9SMilanka Ringwald void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){
35729d0c4f7SMatthias Ringwald     btstack_memory_pool_free(&hfp_connection_pool, hfp_connection);
358ea5029c9SMilanka Ringwald }
359ea5029c9SMilanka Ringwald #else
360ea5029c9SMilanka Ringwald hfp_connection_t * btstack_memory_hfp_connection_get(void){
361ea5029c9SMilanka Ringwald     return NULL;
362ea5029c9SMilanka Ringwald }
363ea5029c9SMilanka Ringwald void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){
364ea5029c9SMilanka Ringwald     // silence compiler warning about unused parameter in a portable way
365ea5029c9SMilanka Ringwald     (void) hfp_connection;
366ea5029c9SMilanka Ringwald };
367ea5029c9SMilanka Ringwald #endif
368ea5029c9SMilanka Ringwald #elif defined(HAVE_MALLOC)
369ea5029c9SMilanka Ringwald hfp_connection_t * btstack_memory_hfp_connection_get(void){
370ea5029c9SMilanka Ringwald     return (hfp_connection_t*) malloc(sizeof(hfp_connection_t));
371ea5029c9SMilanka Ringwald }
372ea5029c9SMilanka Ringwald void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){
373ea5029c9SMilanka Ringwald     free(hfp_connection);
374ea5029c9SMilanka Ringwald }
375ea5029c9SMilanka Ringwald #else
376ea5029c9SMilanka Ringwald #error "Neither HAVE_MALLOC nor MAX_NO_HFP_CONNECTIONS for struct hfp_connection is defined. Please, edit the config file."
377ea5029c9SMilanka Ringwald #endif
378ea5029c9SMilanka Ringwald 
379ea5029c9SMilanka Ringwald 
380cd9ee144SMatthias Ringwald 
381cd9ee144SMatthias Ringwald // MARK: service_record_item_t
382cd9ee144SMatthias Ringwald #ifdef MAX_NO_SERVICE_RECORD_ITEMS
383cd9ee144SMatthias Ringwald #if MAX_NO_SERVICE_RECORD_ITEMS > 0
384cd9ee144SMatthias Ringwald static service_record_item_t service_record_item_storage[MAX_NO_SERVICE_RECORD_ITEMS];
38529d0c4f7SMatthias Ringwald static btstack_memory_pool_t service_record_item_pool;
386cd9ee144SMatthias Ringwald service_record_item_t * btstack_memory_service_record_item_get(void){
38729d0c4f7SMatthias Ringwald     return (service_record_item_t *) btstack_memory_pool_get(&service_record_item_pool);
388cd9ee144SMatthias Ringwald }
389cd9ee144SMatthias Ringwald void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
39029d0c4f7SMatthias Ringwald     btstack_memory_pool_free(&service_record_item_pool, service_record_item);
391cd9ee144SMatthias Ringwald }
392cd9ee144SMatthias Ringwald #else
393cd9ee144SMatthias Ringwald service_record_item_t * btstack_memory_service_record_item_get(void){
394cd9ee144SMatthias Ringwald     return NULL;
395cd9ee144SMatthias Ringwald }
396cd9ee144SMatthias Ringwald void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
397cd9ee144SMatthias Ringwald     // silence compiler warning about unused parameter in a portable way
398cd9ee144SMatthias Ringwald     (void) service_record_item;
399cd9ee144SMatthias Ringwald };
400cd9ee144SMatthias Ringwald #endif
401cd9ee144SMatthias Ringwald #elif defined(HAVE_MALLOC)
402cd9ee144SMatthias Ringwald service_record_item_t * btstack_memory_service_record_item_get(void){
403cd9ee144SMatthias Ringwald     return (service_record_item_t*) malloc(sizeof(service_record_item_t));
404cd9ee144SMatthias Ringwald }
405cd9ee144SMatthias Ringwald void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){
406cd9ee144SMatthias Ringwald     free(service_record_item);
407cd9ee144SMatthias Ringwald }
408cd9ee144SMatthias Ringwald #else
409cd9ee144SMatthias Ringwald #error "Neither HAVE_MALLOC nor MAX_NO_SERVICE_RECORD_ITEMS for struct service_record_item is defined. Please, edit the config file."
410cd9ee144SMatthias Ringwald #endif
411cd9ee144SMatthias Ringwald 
412cd9ee144SMatthias Ringwald 
413a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
4142e97c149S[email protected] 
4152e97c149S[email protected] // MARK: gatt_client_t
416d0fdae3cS[email protected] #ifdef MAX_NO_GATT_CLIENTS
417d0fdae3cS[email protected] #if MAX_NO_GATT_CLIENTS > 0
418d0fdae3cS[email protected] static gatt_client_t gatt_client_storage[MAX_NO_GATT_CLIENTS];
41929d0c4f7SMatthias Ringwald static btstack_memory_pool_t gatt_client_pool;
420d0fdae3cS[email protected] gatt_client_t * btstack_memory_gatt_client_get(void){
42129d0c4f7SMatthias Ringwald     return (gatt_client_t *) btstack_memory_pool_get(&gatt_client_pool);
422d0fdae3cS[email protected] }
423d0fdae3cS[email protected] void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
42429d0c4f7SMatthias Ringwald     btstack_memory_pool_free(&gatt_client_pool, gatt_client);
425d0fdae3cS[email protected] }
426d0fdae3cS[email protected] #else
427d0fdae3cS[email protected] gatt_client_t * btstack_memory_gatt_client_get(void){
428d0fdae3cS[email protected]     return NULL;
429d0fdae3cS[email protected] }
430d0fdae3cS[email protected] void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
4316f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
4326f7ecb09S[email protected]     (void) gatt_client;
433d0fdae3cS[email protected] };
434d0fdae3cS[email protected] #endif
435d0fdae3cS[email protected] #elif defined(HAVE_MALLOC)
436d0fdae3cS[email protected] gatt_client_t * btstack_memory_gatt_client_get(void){
437d0fdae3cS[email protected]     return (gatt_client_t*) malloc(sizeof(gatt_client_t));
438d0fdae3cS[email protected] }
439d0fdae3cS[email protected] void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
440d0fdae3cS[email protected]     free(gatt_client);
441d0fdae3cS[email protected] }
442d0fdae3cS[email protected] #else
443d0fdae3cS[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_GATT_CLIENTS for struct gatt_client is defined. Please, edit the config file."
444d0fdae3cS[email protected] #endif
4452e97c149S[email protected] 
4462e97c149S[email protected] 
447656bec4fSMatthias Ringwald // MARK: whitelist_entry_t
448656bec4fSMatthias Ringwald #ifdef MAX_NO_WHITELIST_ENTRIES
449656bec4fSMatthias Ringwald #if MAX_NO_WHITELIST_ENTRIES > 0
450656bec4fSMatthias Ringwald static whitelist_entry_t whitelist_entry_storage[MAX_NO_WHITELIST_ENTRIES];
45129d0c4f7SMatthias Ringwald static btstack_memory_pool_t whitelist_entry_pool;
452656bec4fSMatthias Ringwald whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
45329d0c4f7SMatthias Ringwald     return (whitelist_entry_t *) btstack_memory_pool_get(&whitelist_entry_pool);
454656bec4fSMatthias Ringwald }
455656bec4fSMatthias Ringwald void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
45629d0c4f7SMatthias Ringwald     btstack_memory_pool_free(&whitelist_entry_pool, whitelist_entry);
457656bec4fSMatthias Ringwald }
458656bec4fSMatthias Ringwald #else
459656bec4fSMatthias Ringwald whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
460656bec4fSMatthias Ringwald     return NULL;
461656bec4fSMatthias Ringwald }
462656bec4fSMatthias Ringwald void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
463656bec4fSMatthias Ringwald     // silence compiler warning about unused parameter in a portable way
464656bec4fSMatthias Ringwald     (void) whitelist_entry;
465656bec4fSMatthias Ringwald };
466656bec4fSMatthias Ringwald #endif
467656bec4fSMatthias Ringwald #elif defined(HAVE_MALLOC)
468656bec4fSMatthias Ringwald whitelist_entry_t * btstack_memory_whitelist_entry_get(void){
469656bec4fSMatthias Ringwald     return (whitelist_entry_t*) malloc(sizeof(whitelist_entry_t));
470656bec4fSMatthias Ringwald }
471656bec4fSMatthias Ringwald void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){
472656bec4fSMatthias Ringwald     free(whitelist_entry);
473656bec4fSMatthias Ringwald }
474656bec4fSMatthias Ringwald #else
475656bec4fSMatthias Ringwald #error "Neither HAVE_MALLOC nor MAX_NO_WHITELIST_ENTRIES for struct whitelist_entry is defined. Please, edit the config file."
476656bec4fSMatthias Ringwald #endif
477656bec4fSMatthias Ringwald 
478656bec4fSMatthias Ringwald 
479cf49570bSMatthias Ringwald // MARK: sm_lookup_entry_t
480cf49570bSMatthias Ringwald #ifdef MAX_NO_SM_LOOKUP_ENTRIES
481cf49570bSMatthias Ringwald #if MAX_NO_SM_LOOKUP_ENTRIES > 0
482cf49570bSMatthias Ringwald static sm_lookup_entry_t sm_lookup_entry_storage[MAX_NO_SM_LOOKUP_ENTRIES];
48329d0c4f7SMatthias Ringwald static btstack_memory_pool_t sm_lookup_entry_pool;
484cf49570bSMatthias Ringwald sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
48529d0c4f7SMatthias Ringwald     return (sm_lookup_entry_t *) btstack_memory_pool_get(&sm_lookup_entry_pool);
486cf49570bSMatthias Ringwald }
487cf49570bSMatthias Ringwald void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
48829d0c4f7SMatthias Ringwald     btstack_memory_pool_free(&sm_lookup_entry_pool, sm_lookup_entry);
489cf49570bSMatthias Ringwald }
490cf49570bSMatthias Ringwald #else
491cf49570bSMatthias Ringwald sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
492cf49570bSMatthias Ringwald     return NULL;
493cf49570bSMatthias Ringwald }
494cf49570bSMatthias Ringwald void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
495cf49570bSMatthias Ringwald     // silence compiler warning about unused parameter in a portable way
496cf49570bSMatthias Ringwald     (void) sm_lookup_entry;
497cf49570bSMatthias Ringwald };
498cf49570bSMatthias Ringwald #endif
499cf49570bSMatthias Ringwald #elif defined(HAVE_MALLOC)
500cf49570bSMatthias Ringwald sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){
501cf49570bSMatthias Ringwald     return (sm_lookup_entry_t*) malloc(sizeof(sm_lookup_entry_t));
502cf49570bSMatthias Ringwald }
503cf49570bSMatthias Ringwald void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){
504cf49570bSMatthias Ringwald     free(sm_lookup_entry);
505cf49570bSMatthias Ringwald }
506cf49570bSMatthias Ringwald #else
507cf49570bSMatthias Ringwald #error "Neither HAVE_MALLOC nor MAX_NO_SM_LOOKUP_ENTRIES for struct sm_lookup_entry is defined. Please, edit the config file."
508cf49570bSMatthias Ringwald #endif
509cf49570bSMatthias Ringwald 
510cf49570bSMatthias Ringwald 
5112e97c149S[email protected] #endif
512a3b02b71Smatthias.ringwald // init
513a3b02b71Smatthias.ringwald void btstack_memory_init(void){
514c4d3f927Smatthias.ringwald #if MAX_NO_HCI_CONNECTIONS > 0
51529d0c4f7SMatthias Ringwald     btstack_memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t));
516a3b02b71Smatthias.ringwald #endif
517c4d3f927Smatthias.ringwald #if MAX_NO_L2CAP_SERVICES > 0
51829d0c4f7SMatthias Ringwald     btstack_memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t));
519a3b02b71Smatthias.ringwald #endif
520c4d3f927Smatthias.ringwald #if MAX_NO_L2CAP_CHANNELS > 0
52129d0c4f7SMatthias Ringwald     btstack_memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
522a3b02b71Smatthias.ringwald #endif
523c4d3f927Smatthias.ringwald #if MAX_NO_RFCOMM_MULTIPLEXERS > 0
52429d0c4f7SMatthias Ringwald     btstack_memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
525a3b02b71Smatthias.ringwald #endif
526c4d3f927Smatthias.ringwald #if MAX_NO_RFCOMM_SERVICES > 0
52729d0c4f7SMatthias Ringwald     btstack_memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
528a3b02b71Smatthias.ringwald #endif
529c4d3f927Smatthias.ringwald #if MAX_NO_RFCOMM_CHANNELS > 0
53029d0c4f7SMatthias Ringwald     btstack_memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
531a3b02b71Smatthias.ringwald #endif
532*2c455dadSMatthias Ringwald #if MAX_NO_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES > 0
533*2c455dadSMatthias Ringwald     btstack_memory_pool_create(&btstack_link_key_db_memory_entry_pool, btstack_link_key_db_memory_entry_storage, MAX_NO_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES, sizeof(btstack_link_key_db_memory_entry_t));
5341801b596Smatthias.ringwald #endif
5352e97c149S[email protected] #if MAX_NO_BNEP_SERVICES > 0
53629d0c4f7SMatthias Ringwald     btstack_memory_pool_create(&bnep_service_pool, bnep_service_storage, MAX_NO_BNEP_SERVICES, sizeof(bnep_service_t));
5372e97c149S[email protected] #endif
5382e97c149S[email protected] #if MAX_NO_BNEP_CHANNELS > 0
53929d0c4f7SMatthias Ringwald     btstack_memory_pool_create(&bnep_channel_pool, bnep_channel_storage, MAX_NO_BNEP_CHANNELS, sizeof(bnep_channel_t));
5402e97c149S[email protected] #endif
541ea5029c9SMilanka Ringwald #if MAX_NO_HFP_CONNECTIONS > 0
54229d0c4f7SMatthias Ringwald     btstack_memory_pool_create(&hfp_connection_pool, hfp_connection_storage, MAX_NO_HFP_CONNECTIONS, sizeof(hfp_connection_t));
543ea5029c9SMilanka Ringwald #endif
544cd9ee144SMatthias Ringwald #if MAX_NO_SERVICE_RECORD_ITEMS > 0
54529d0c4f7SMatthias Ringwald     btstack_memory_pool_create(&service_record_item_pool, service_record_item_storage, MAX_NO_SERVICE_RECORD_ITEMS, sizeof(service_record_item_t));
546cd9ee144SMatthias Ringwald #endif
547a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
548d0fdae3cS[email protected] #if MAX_NO_GATT_CLIENTS > 0
54929d0c4f7SMatthias Ringwald     btstack_memory_pool_create(&gatt_client_pool, gatt_client_storage, MAX_NO_GATT_CLIENTS, sizeof(gatt_client_t));
550d0fdae3cS[email protected] #endif
551656bec4fSMatthias Ringwald #if MAX_NO_WHITELIST_ENTRIES > 0
55229d0c4f7SMatthias Ringwald     btstack_memory_pool_create(&whitelist_entry_pool, whitelist_entry_storage, MAX_NO_WHITELIST_ENTRIES, sizeof(whitelist_entry_t));
553656bec4fSMatthias Ringwald #endif
554cf49570bSMatthias Ringwald #if MAX_NO_SM_LOOKUP_ENTRIES > 0
55529d0c4f7SMatthias Ringwald     btstack_memory_pool_create(&sm_lookup_entry_pool, sm_lookup_entry_storage, MAX_NO_SM_LOOKUP_ENTRIES, sizeof(sm_lookup_entry_t));
556cf49570bSMatthias Ringwald #endif
557a7d12effS[email protected] #endif
558a3b02b71Smatthias.ringwald }
559