xref: /btstack/src/btstack_memory.c (revision 6f7ecb09e3e3720a536f385066af319f1970052b)
1a3b02b71Smatthias.ringwald /*
26b64433eSmatthias.ringwald  * Copyright (C) 2009-2012 by Matthias Ringwald
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  *
20a3b02b71Smatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD 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  *
336b64433eSmatthias.ringwald  * Please inquire about commercial licensing options at [email protected]
346b64433eSmatthias.ringwald  *
35a3b02b71Smatthias.ringwald  */
36a3b02b71Smatthias.ringwald 
37a3b02b71Smatthias.ringwald /*
38a3b02b71Smatthias.ringwald  *  btstsack_memory.h
39a3b02b71Smatthias.ringwald  *
40a3b02b71Smatthias.ringwald  *  @brief BTstack memory management via configurable memory pools
41a3b02b71Smatthias.ringwald  *
42*6f7ecb09S[email protected]  *  @note code semi-atuomatically generated by tools/btstack_memory_generator.py
43a3b02b71Smatthias.ringwald  *
44a3b02b71Smatthias.ringwald  */
45a3b02b71Smatthias.ringwald 
46a3b02b71Smatthias.ringwald #include "btstack_memory.h"
47a3b02b71Smatthias.ringwald #include <btstack/memory_pool.h>
48a3b02b71Smatthias.ringwald 
49a3b02b71Smatthias.ringwald #include <stdlib.h>
50a3b02b71Smatthias.ringwald 
51bde315ceS[email protected] #include "btstack-config.h"
52a3b02b71Smatthias.ringwald #include "hci.h"
53a3b02b71Smatthias.ringwald #include "l2cap.h"
54a3b02b71Smatthias.ringwald #include "rfcomm.h"
55a3b02b71Smatthias.ringwald 
56a3b02b71Smatthias.ringwald // MARK: hci_connection_t
57a3b02b71Smatthias.ringwald #ifdef MAX_NO_HCI_CONNECTIONS
58c4d3f927Smatthias.ringwald #if MAX_NO_HCI_CONNECTIONS > 0
59a3b02b71Smatthias.ringwald static hci_connection_t hci_connection_storage[MAX_NO_HCI_CONNECTIONS];
60a3b02b71Smatthias.ringwald static memory_pool_t hci_connection_pool;
616527a633S[email protected] hci_connection_t * btstack_memory_hci_connection_get(void){
62a3b02b71Smatthias.ringwald     return memory_pool_get(&hci_connection_pool);
63a3b02b71Smatthias.ringwald }
646527a633S[email protected] void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
65a3b02b71Smatthias.ringwald     memory_pool_free(&hci_connection_pool, hci_connection);
66a3b02b71Smatthias.ringwald }
67c4d3f927Smatthias.ringwald #else
686527a633S[email protected] hci_connection_t * btstack_memory_hci_connection_get(void){
69c4d3f927Smatthias.ringwald     return NULL;
70c4d3f927Smatthias.ringwald }
716527a633S[email protected] void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
72*6f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
73*6f7ecb09S[email protected]     hci_connection;
74c4d3f927Smatthias.ringwald };
75c4d3f927Smatthias.ringwald #endif
76a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
776527a633S[email protected] hci_connection_t * btstack_memory_hci_connection_get(void){
786527a633S[email protected]     return (hci_connection_t*) malloc(sizeof(hci_connection_t));
79a3b02b71Smatthias.ringwald }
806527a633S[email protected] void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
81a3b02b71Smatthias.ringwald     free(hci_connection);
82a3b02b71Smatthias.ringwald }
83e045abdeSmatthias.ringwald #else
846527a633S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_HCI_CONNECTIONS for struct hci_connection is defined. Please, edit the config file."
85a3b02b71Smatthias.ringwald #endif
86a3b02b71Smatthias.ringwald 
87a3b02b71Smatthias.ringwald 
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];
92a3b02b71Smatthias.ringwald static memory_pool_t l2cap_service_pool;
936527a633S[email protected] l2cap_service_t * btstack_memory_l2cap_service_get(void){
94a3b02b71Smatthias.ringwald     return memory_pool_get(&l2cap_service_pool);
95a3b02b71Smatthias.ringwald }
966527a633S[email protected] void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
97a3b02b71Smatthias.ringwald     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){
104*6f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
105*6f7ecb09S[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];
124a3b02b71Smatthias.ringwald static memory_pool_t l2cap_channel_pool;
1256527a633S[email protected] l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
126a3b02b71Smatthias.ringwald     return memory_pool_get(&l2cap_channel_pool);
127a3b02b71Smatthias.ringwald }
1286527a633S[email protected] void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
129a3b02b71Smatthias.ringwald     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){
136*6f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
137*6f7ecb09S[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 
152a3b02b71Smatthias.ringwald // MARK: rfcomm_multiplexer_t
153a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
154c4d3f927Smatthias.ringwald #if MAX_NO_RFCOMM_MULTIPLEXERS > 0
155a3b02b71Smatthias.ringwald static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NO_RFCOMM_MULTIPLEXERS];
156a3b02b71Smatthias.ringwald static memory_pool_t rfcomm_multiplexer_pool;
1576527a633S[email protected] rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
158a3b02b71Smatthias.ringwald     return memory_pool_get(&rfcomm_multiplexer_pool);
159a3b02b71Smatthias.ringwald }
1606527a633S[email protected] void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
161a3b02b71Smatthias.ringwald     memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer);
162a3b02b71Smatthias.ringwald }
163c4d3f927Smatthias.ringwald #else
1646527a633S[email protected] rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
165c4d3f927Smatthias.ringwald     return NULL;
166c4d3f927Smatthias.ringwald }
1676527a633S[email protected] void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
168*6f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
169*6f7ecb09S[email protected]     (void) rfcomm_multiplexer;
170c4d3f927Smatthias.ringwald };
171c4d3f927Smatthias.ringwald #endif
172a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
1736527a633S[email protected] rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){
1746527a633S[email protected]     return (rfcomm_multiplexer_t*) malloc(sizeof(rfcomm_multiplexer_t));
175a3b02b71Smatthias.ringwald }
1766527a633S[email protected] void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){
177a3b02b71Smatthias.ringwald     free(rfcomm_multiplexer);
178a3b02b71Smatthias.ringwald }
179e045abdeSmatthias.ringwald #else
1806527a633S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_RFCOMM_MULTIPLEXERS for struct rfcomm_multiplexer is defined. Please, edit the config file."
181a3b02b71Smatthias.ringwald #endif
182a3b02b71Smatthias.ringwald 
183a3b02b71Smatthias.ringwald 
184a3b02b71Smatthias.ringwald // MARK: rfcomm_service_t
185a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_SERVICES
186c4d3f927Smatthias.ringwald #if MAX_NO_RFCOMM_SERVICES > 0
187a3b02b71Smatthias.ringwald static rfcomm_service_t rfcomm_service_storage[MAX_NO_RFCOMM_SERVICES];
188a3b02b71Smatthias.ringwald static memory_pool_t rfcomm_service_pool;
1896527a633S[email protected] rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
190a3b02b71Smatthias.ringwald     return memory_pool_get(&rfcomm_service_pool);
191a3b02b71Smatthias.ringwald }
1926527a633S[email protected] void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
193a3b02b71Smatthias.ringwald     memory_pool_free(&rfcomm_service_pool, rfcomm_service);
194a3b02b71Smatthias.ringwald }
195c4d3f927Smatthias.ringwald #else
1966527a633S[email protected] rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
197c4d3f927Smatthias.ringwald     return NULL;
198c4d3f927Smatthias.ringwald }
1996527a633S[email protected] void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
200*6f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
201*6f7ecb09S[email protected]     (void) rfcomm_service;
202c4d3f927Smatthias.ringwald };
203c4d3f927Smatthias.ringwald #endif
204a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
2056527a633S[email protected] rfcomm_service_t * btstack_memory_rfcomm_service_get(void){
2066527a633S[email protected]     return (rfcomm_service_t*) malloc(sizeof(rfcomm_service_t));
207a3b02b71Smatthias.ringwald }
2086527a633S[email protected] void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){
209a3b02b71Smatthias.ringwald     free(rfcomm_service);
210a3b02b71Smatthias.ringwald }
211e045abdeSmatthias.ringwald #else
2126527a633S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_RFCOMM_SERVICES for struct rfcomm_service is defined. Please, edit the config file."
213a3b02b71Smatthias.ringwald #endif
214a3b02b71Smatthias.ringwald 
215a3b02b71Smatthias.ringwald 
216a3b02b71Smatthias.ringwald // MARK: rfcomm_channel_t
217a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_CHANNELS
218c4d3f927Smatthias.ringwald #if MAX_NO_RFCOMM_CHANNELS > 0
219a3b02b71Smatthias.ringwald static rfcomm_channel_t rfcomm_channel_storage[MAX_NO_RFCOMM_CHANNELS];
220a3b02b71Smatthias.ringwald static memory_pool_t rfcomm_channel_pool;
2216527a633S[email protected] rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
222a3b02b71Smatthias.ringwald     return memory_pool_get(&rfcomm_channel_pool);
223a3b02b71Smatthias.ringwald }
2246527a633S[email protected] void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
225a3b02b71Smatthias.ringwald     memory_pool_free(&rfcomm_channel_pool, rfcomm_channel);
226a3b02b71Smatthias.ringwald }
227c4d3f927Smatthias.ringwald #else
2286527a633S[email protected] rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
229c4d3f927Smatthias.ringwald     return NULL;
230c4d3f927Smatthias.ringwald }
2316527a633S[email protected] void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
232*6f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
233*6f7ecb09S[email protected]     (void) rfcomm_channel;
234c4d3f927Smatthias.ringwald };
235c4d3f927Smatthias.ringwald #endif
236a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
2376527a633S[email protected] rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){
2386527a633S[email protected]     return (rfcomm_channel_t*) malloc(sizeof(rfcomm_channel_t));
239a3b02b71Smatthias.ringwald }
2406527a633S[email protected] void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
241a3b02b71Smatthias.ringwald     free(rfcomm_channel);
242a3b02b71Smatthias.ringwald }
243e045abdeSmatthias.ringwald #else
2446527a633S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_RFCOMM_CHANNELS for struct rfcomm_channel is defined. Please, edit the config file."
245a3b02b71Smatthias.ringwald #endif
246a3b02b71Smatthias.ringwald 
247e0e5e285Smatthias.ringwald 
248e0e5e285Smatthias.ringwald // MARK: db_mem_device_name_t
249e0e5e285Smatthias.ringwald #ifdef MAX_NO_DB_MEM_DEVICE_NAMES
250c4d3f927Smatthias.ringwald #if MAX_NO_DB_MEM_DEVICE_NAMES > 0
251e0e5e285Smatthias.ringwald static db_mem_device_name_t db_mem_device_name_storage[MAX_NO_DB_MEM_DEVICE_NAMES];
252e0e5e285Smatthias.ringwald static memory_pool_t db_mem_device_name_pool;
2536527a633S[email protected] db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void){
254e0e5e285Smatthias.ringwald     return memory_pool_get(&db_mem_device_name_pool);
2551801b596Smatthias.ringwald }
2566527a633S[email protected] void btstack_memory_db_mem_device_name_free(db_mem_device_name_t *db_mem_device_name){
257e0e5e285Smatthias.ringwald     memory_pool_free(&db_mem_device_name_pool, db_mem_device_name);
2581801b596Smatthias.ringwald }
259c4d3f927Smatthias.ringwald #else
2606527a633S[email protected] db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void){
261c4d3f927Smatthias.ringwald     return NULL;
262c4d3f927Smatthias.ringwald }
2636527a633S[email protected] void btstack_memory_db_mem_device_name_free(db_mem_device_name_t * db_mem_device_name){
264*6f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
265*6f7ecb09S[email protected]     (void) db_mem_device_name;
266c4d3f927Smatthias.ringwald };
267c4d3f927Smatthias.ringwald #endif
2681801b596Smatthias.ringwald #elif defined(HAVE_MALLOC)
2696527a633S[email protected] db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void){
2706527a633S[email protected]     return (db_mem_device_name_t*) malloc(sizeof(db_mem_device_name_t));
2711801b596Smatthias.ringwald }
2726527a633S[email protected] void btstack_memory_db_mem_device_name_free(db_mem_device_name_t *db_mem_device_name){
273e0e5e285Smatthias.ringwald     free(db_mem_device_name);
274e0e5e285Smatthias.ringwald }
275e045abdeSmatthias.ringwald #else
2766527a633S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_DB_MEM_DEVICE_NAMES for struct db_mem_device_name is defined. Please, edit the config file."
277e0e5e285Smatthias.ringwald #endif
278e0e5e285Smatthias.ringwald 
279e0e5e285Smatthias.ringwald 
280e0e5e285Smatthias.ringwald // MARK: db_mem_device_link_key_t
281e0e5e285Smatthias.ringwald #ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS
282c4d3f927Smatthias.ringwald #if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0
283e0e5e285Smatthias.ringwald static db_mem_device_link_key_t db_mem_device_link_key_storage[MAX_NO_DB_MEM_DEVICE_LINK_KEYS];
284e0e5e285Smatthias.ringwald static memory_pool_t db_mem_device_link_key_pool;
2856527a633S[email protected] db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void){
286e0e5e285Smatthias.ringwald     return memory_pool_get(&db_mem_device_link_key_pool);
287e0e5e285Smatthias.ringwald }
2886527a633S[email protected] void btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key){
289e0e5e285Smatthias.ringwald     memory_pool_free(&db_mem_device_link_key_pool, db_mem_device_link_key);
290e0e5e285Smatthias.ringwald }
291c4d3f927Smatthias.ringwald #else
2926527a633S[email protected] db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void){
293c4d3f927Smatthias.ringwald     return NULL;
294c4d3f927Smatthias.ringwald }
2956527a633S[email protected] void btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key){
296*6f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
297*6f7ecb09S[email protected]     (void) db_mem_device_link_key;
298c4d3f927Smatthias.ringwald };
299c4d3f927Smatthias.ringwald #endif
300e0e5e285Smatthias.ringwald #elif defined(HAVE_MALLOC)
3016527a633S[email protected] db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void){
3026527a633S[email protected]     return (db_mem_device_link_key_t*) malloc(sizeof(db_mem_device_link_key_t));
303e0e5e285Smatthias.ringwald }
3046527a633S[email protected] void btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key){
305e0e5e285Smatthias.ringwald     free(db_mem_device_link_key);
3061801b596Smatthias.ringwald }
307e045abdeSmatthias.ringwald #else
3086527a633S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_DB_MEM_DEVICE_LINK_KEYS for struct db_mem_device_link_key is defined. Please, edit the config file."
3091801b596Smatthias.ringwald #endif
3101801b596Smatthias.ringwald 
3111801b596Smatthias.ringwald 
3121801b596Smatthias.ringwald // MARK: db_mem_service_t
3131801b596Smatthias.ringwald #ifdef MAX_NO_DB_MEM_SERVICES
314c4d3f927Smatthias.ringwald #if MAX_NO_DB_MEM_SERVICES > 0
3151801b596Smatthias.ringwald static db_mem_service_t db_mem_service_storage[MAX_NO_DB_MEM_SERVICES];
3161801b596Smatthias.ringwald static memory_pool_t db_mem_service_pool;
3176527a633S[email protected] db_mem_service_t * btstack_memory_db_mem_service_get(void){
3181801b596Smatthias.ringwald     return memory_pool_get(&db_mem_service_pool);
3191801b596Smatthias.ringwald }
3206527a633S[email protected] void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service){
3211801b596Smatthias.ringwald     memory_pool_free(&db_mem_service_pool, db_mem_service);
3221801b596Smatthias.ringwald }
323c4d3f927Smatthias.ringwald #else
3246527a633S[email protected] db_mem_service_t * btstack_memory_db_mem_service_get(void){
325c4d3f927Smatthias.ringwald     return NULL;
326c4d3f927Smatthias.ringwald }
3276527a633S[email protected] void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service){
328*6f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
329*6f7ecb09S[email protected]     (void) db_mem_service;
330c4d3f927Smatthias.ringwald };
331c4d3f927Smatthias.ringwald #endif
3321801b596Smatthias.ringwald #elif defined(HAVE_MALLOC)
3336527a633S[email protected] db_mem_service_t * btstack_memory_db_mem_service_get(void){
3346527a633S[email protected]     return (db_mem_service_t*) malloc(sizeof(db_mem_service_t));
3351801b596Smatthias.ringwald }
3366527a633S[email protected] void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service){
3371801b596Smatthias.ringwald     free(db_mem_service);
3381801b596Smatthias.ringwald }
339e045abdeSmatthias.ringwald #else
3406527a633S[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_DB_MEM_SERVICES for struct db_mem_service is defined. Please, edit the config file."
3411801b596Smatthias.ringwald #endif
3421801b596Smatthias.ringwald 
343d0fdae3cS[email protected] // MARK: gatt_client_t
344d0fdae3cS[email protected] #ifdef HAVE_BLE
345d0fdae3cS[email protected] #ifdef MAX_NO_GATT_CLIENTS
346d0fdae3cS[email protected] #if MAX_NO_GATT_CLIENTS > 0
347d0fdae3cS[email protected] static gatt_client_t gatt_client_storage[MAX_NO_GATT_CLIENTS];
348d0fdae3cS[email protected] static memory_pool_t gatt_client_pool;
349d0fdae3cS[email protected] gatt_client_t * btstack_memory_gatt_client_get(void){
350d0fdae3cS[email protected]     return memory_pool_get(&gatt_client_pool);
351d0fdae3cS[email protected] }
352d0fdae3cS[email protected] void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
353d0fdae3cS[email protected]     memory_pool_free(&gatt_client_pool, gatt_client);
354d0fdae3cS[email protected] }
355d0fdae3cS[email protected] #else
356d0fdae3cS[email protected] gatt_client_t * btstack_memory_gatt_client_get(void){
357d0fdae3cS[email protected]     return NULL;
358d0fdae3cS[email protected] }
359d0fdae3cS[email protected] void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
360*6f7ecb09S[email protected]     // silence compiler warning about unused parameter in a portable way
361*6f7ecb09S[email protected]     (void) gatt_client;
362d0fdae3cS[email protected] };
363d0fdae3cS[email protected] #endif
364d0fdae3cS[email protected] #elif defined(HAVE_MALLOC)
365d0fdae3cS[email protected] gatt_client_t * btstack_memory_gatt_client_get(void){
366d0fdae3cS[email protected]     return (gatt_client_t*) malloc(sizeof(gatt_client_t));
367d0fdae3cS[email protected] }
368d0fdae3cS[email protected] void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
369d0fdae3cS[email protected]     free(gatt_client);
370d0fdae3cS[email protected] }
371d0fdae3cS[email protected] #else
372d0fdae3cS[email protected] #error "Neither HAVE_MALLOC nor MAX_NO_GATT_CLIENTS for struct gatt_client is defined. Please, edit the config file."
373d0fdae3cS[email protected] #endif
374d0fdae3cS[email protected] #endif
375a3b02b71Smatthias.ringwald // init
376a3b02b71Smatthias.ringwald void btstack_memory_init(void){
377c4d3f927Smatthias.ringwald #if MAX_NO_HCI_CONNECTIONS > 0
378a3b02b71Smatthias.ringwald     memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t));
379a3b02b71Smatthias.ringwald #endif
380c4d3f927Smatthias.ringwald #if MAX_NO_L2CAP_SERVICES > 0
381a3b02b71Smatthias.ringwald     memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t));
382a3b02b71Smatthias.ringwald #endif
383c4d3f927Smatthias.ringwald #if MAX_NO_L2CAP_CHANNELS > 0
384a3b02b71Smatthias.ringwald     memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
385a3b02b71Smatthias.ringwald #endif
386c4d3f927Smatthias.ringwald #if MAX_NO_RFCOMM_MULTIPLEXERS > 0
387a3b02b71Smatthias.ringwald     memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
388a3b02b71Smatthias.ringwald #endif
389c4d3f927Smatthias.ringwald #if MAX_NO_RFCOMM_SERVICES > 0
390a3b02b71Smatthias.ringwald     memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
391a3b02b71Smatthias.ringwald #endif
392c4d3f927Smatthias.ringwald #if MAX_NO_RFCOMM_CHANNELS > 0
393a3b02b71Smatthias.ringwald     memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
394a3b02b71Smatthias.ringwald #endif
395c4d3f927Smatthias.ringwald #if MAX_NO_DB_MEM_DEVICE_NAMES > 0
396e0e5e285Smatthias.ringwald     memory_pool_create(&db_mem_device_name_pool, db_mem_device_name_storage, MAX_NO_DB_MEM_DEVICE_NAMES, sizeof(db_mem_device_name_t));
397e0e5e285Smatthias.ringwald #endif
398c4d3f927Smatthias.ringwald #if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0
399e0e5e285Smatthias.ringwald     memory_pool_create(&db_mem_device_link_key_pool, db_mem_device_link_key_storage, MAX_NO_DB_MEM_DEVICE_LINK_KEYS, sizeof(db_mem_device_link_key_t));
4001801b596Smatthias.ringwald #endif
401c4d3f927Smatthias.ringwald #if MAX_NO_DB_MEM_SERVICES > 0
4021801b596Smatthias.ringwald     memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t));
4031801b596Smatthias.ringwald #endif
404a7d12effS[email protected] #ifdef HAVE_BLE
405d0fdae3cS[email protected] #if MAX_NO_GATT_CLIENTS > 0
406d0fdae3cS[email protected]     memory_pool_create(&gatt_client_pool, gatt_client_storage, MAX_NO_GATT_CLIENTS, sizeof(gatt_client_t));
407d0fdae3cS[email protected] #endif
408a7d12effS[email protected] #endif
409a3b02b71Smatthias.ringwald }
410e045abdeSmatthias.ringwald 
411