xref: /btstack/src/btstack_memory.c (revision 91ff78dd38369b93df0fb672d85eb95f6d8e93d5)
1 /*
2  * Copyright (C) 2009-2012 by Matthias Ringwald
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 MATTHIAS RINGWALD 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 [email protected]
34  *
35  */
36 
37 /*
38  *  btstsack_memory.h
39  *
40  *  @brief BTstack memory management via configurable memory pools
41  *
42  *  @note code semi-atuomatically generated by btstack_memory_generator.py
43  *
44  */
45 
46 #include "btstack_memory.h"
47 #include <btstack/memory_pool.h>
48 
49 #include <stdlib.h>
50 
51 #include "config.h"
52 #include "hci.h"
53 #include "l2cap.h"
54 #include "rfcomm.h"
55 
56 // MARK: hci_connection_t
57 #ifdef MAX_NO_HCI_CONNECTIONS
58 #if MAX_NO_HCI_CONNECTIONS > 0
59 static hci_connection_t hci_connection_storage[MAX_NO_HCI_CONNECTIONS];
60 static memory_pool_t hci_connection_pool;
61 void * btstack_memory_hci_connection_get(void){
62     return memory_pool_get(&hci_connection_pool);
63 }
64 void btstack_memory_hci_connection_free(void *hci_connection){
65     memory_pool_free(&hci_connection_pool, hci_connection);
66 }
67 #else
68 void * btstack_memory_hci_connection_get(void){
69     return NULL;
70 }
71 void btstack_memory_hci_connection_free(void *hci_connection){
72 };
73 #endif
74 #elif defined(HAVE_MALLOC)
75 void * btstack_memory_hci_connection_get(void){
76     return malloc(sizeof(hci_connection_t));
77 }
78 void  btstack_memory_hci_connection_free(void *hci_connection){
79     free(hci_connection);
80 }
81 #else error "The struct hci_connection has neither HAVE_MALLOC nor MAX_NO_HCI_CONNECTIONS defined. Please, edit the config file."
82 #endif
83 
84 
85 // MARK: l2cap_service_t
86 #ifdef MAX_NO_L2CAP_SERVICES
87 #if MAX_NO_L2CAP_SERVICES > 0
88 static l2cap_service_t l2cap_service_storage[MAX_NO_L2CAP_SERVICES];
89 static memory_pool_t l2cap_service_pool;
90 void * btstack_memory_l2cap_service_get(void){
91     return memory_pool_get(&l2cap_service_pool);
92 }
93 void btstack_memory_l2cap_service_free(void *l2cap_service){
94     memory_pool_free(&l2cap_service_pool, l2cap_service);
95 }
96 #else
97 void * btstack_memory_l2cap_service_get(void){
98     return NULL;
99 }
100 void btstack_memory_l2cap_service_free(void *l2cap_service){
101 };
102 #endif
103 #elif defined(HAVE_MALLOC)
104 void * btstack_memory_l2cap_service_get(void){
105     return malloc(sizeof(l2cap_service_t));
106 }
107 void  btstack_memory_l2cap_service_free(void *l2cap_service){
108     free(l2cap_service);
109 }
110 #else error "The struct l2cap_service has neither HAVE_MALLOC nor MAX_NO_L2CAP_SERVICES defined. Please, edit the config file."
111 #endif
112 
113 
114 // MARK: l2cap_channel_t
115 #ifdef MAX_NO_L2CAP_CHANNELS
116 #if MAX_NO_L2CAP_CHANNELS > 0
117 static l2cap_channel_t l2cap_channel_storage[MAX_NO_L2CAP_CHANNELS];
118 static memory_pool_t l2cap_channel_pool;
119 void * btstack_memory_l2cap_channel_get(void){
120     return memory_pool_get(&l2cap_channel_pool);
121 }
122 void btstack_memory_l2cap_channel_free(void *l2cap_channel){
123     memory_pool_free(&l2cap_channel_pool, l2cap_channel);
124 }
125 #else
126 void * btstack_memory_l2cap_channel_get(void){
127     return NULL;
128 }
129 void btstack_memory_l2cap_channel_free(void *l2cap_channel){
130 };
131 #endif
132 #elif defined(HAVE_MALLOC)
133 void * btstack_memory_l2cap_channel_get(void){
134     return malloc(sizeof(l2cap_channel_t));
135 }
136 void  btstack_memory_l2cap_channel_free(void *l2cap_channel){
137     free(l2cap_channel);
138 }
139 #else error "The struct l2cap_channel has neither HAVE_MALLOC nor MAX_NO_L2CAP_CHANNELS defined. Please, edit the config file."
140 #endif
141 
142 
143 // MARK: rfcomm_multiplexer_t
144 #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
145 #if MAX_NO_RFCOMM_MULTIPLEXERS > 0
146 static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NO_RFCOMM_MULTIPLEXERS];
147 static memory_pool_t rfcomm_multiplexer_pool;
148 void * btstack_memory_rfcomm_multiplexer_get(void){
149     return memory_pool_get(&rfcomm_multiplexer_pool);
150 }
151 void btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
152     memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer);
153 }
154 #else
155 void * btstack_memory_rfcomm_multiplexer_get(void){
156     return NULL;
157 }
158 void btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
159 };
160 #endif
161 #elif defined(HAVE_MALLOC)
162 void * btstack_memory_rfcomm_multiplexer_get(void){
163     return malloc(sizeof(rfcomm_multiplexer_t));
164 }
165 void  btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
166     free(rfcomm_multiplexer);
167 }
168 #else error "The struct rfcomm_multiplexer has neither HAVE_MALLOC nor MAX_NO_RFCOMM_MULTIPLEXERS defined. Please, edit the config file."
169 #endif
170 
171 
172 // MARK: rfcomm_service_t
173 #ifdef MAX_NO_RFCOMM_SERVICES
174 #if MAX_NO_RFCOMM_SERVICES > 0
175 static rfcomm_service_t rfcomm_service_storage[MAX_NO_RFCOMM_SERVICES];
176 static memory_pool_t rfcomm_service_pool;
177 void * btstack_memory_rfcomm_service_get(void){
178     return memory_pool_get(&rfcomm_service_pool);
179 }
180 void btstack_memory_rfcomm_service_free(void *rfcomm_service){
181     memory_pool_free(&rfcomm_service_pool, rfcomm_service);
182 }
183 #else
184 void * btstack_memory_rfcomm_service_get(void){
185     return NULL;
186 }
187 void btstack_memory_rfcomm_service_free(void *rfcomm_service){
188 };
189 #endif
190 #elif defined(HAVE_MALLOC)
191 void * btstack_memory_rfcomm_service_get(void){
192     return malloc(sizeof(rfcomm_service_t));
193 }
194 void  btstack_memory_rfcomm_service_free(void *rfcomm_service){
195     free(rfcomm_service);
196 }
197 #else error "The struct rfcomm_service has neither HAVE_MALLOC nor MAX_NO_RFCOMM_SERVICES defined. Please, edit the config file."
198 #endif
199 
200 
201 // MARK: rfcomm_channel_t
202 #ifdef MAX_NO_RFCOMM_CHANNELS
203 #if MAX_NO_RFCOMM_CHANNELS > 0
204 static rfcomm_channel_t rfcomm_channel_storage[MAX_NO_RFCOMM_CHANNELS];
205 static memory_pool_t rfcomm_channel_pool;
206 void * btstack_memory_rfcomm_channel_get(void){
207     return memory_pool_get(&rfcomm_channel_pool);
208 }
209 void btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
210     memory_pool_free(&rfcomm_channel_pool, rfcomm_channel);
211 }
212 #else
213 void * btstack_memory_rfcomm_channel_get(void){
214     return NULL;
215 }
216 void btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
217 };
218 #endif
219 #elif defined(HAVE_MALLOC)
220 void * btstack_memory_rfcomm_channel_get(void){
221     return malloc(sizeof(rfcomm_channel_t));
222 }
223 void  btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
224     free(rfcomm_channel);
225 }
226 #else error "The struct rfcomm_channel has neither HAVE_MALLOC nor MAX_NO_RFCOMM_CHANNELS defined. Please, edit the config file."
227 #endif
228 
229 
230 // MARK: db_mem_device_name_t
231 #ifdef MAX_NO_DB_MEM_DEVICE_NAMES
232 #if MAX_NO_DB_MEM_DEVICE_NAMES > 0
233 static db_mem_device_name_t db_mem_device_name_storage[MAX_NO_DB_MEM_DEVICE_NAMES];
234 static memory_pool_t db_mem_device_name_pool;
235 void * btstack_memory_db_mem_device_name_get(void){
236     return memory_pool_get(&db_mem_device_name_pool);
237 }
238 void btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
239     memory_pool_free(&db_mem_device_name_pool, db_mem_device_name);
240 }
241 #else
242 void * btstack_memory_db_mem_device_name_get(void){
243     return NULL;
244 }
245 void btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
246 };
247 #endif
248 #elif defined(HAVE_MALLOC)
249 void * btstack_memory_db_mem_device_name_get(void){
250     return malloc(sizeof(db_mem_device_name_t));
251 }
252 void  btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
253     free(db_mem_device_name);
254 }
255 #else error "The struct db_mem_device_name has neither HAVE_MALLOC nor MAX_NO_DB_MEM_DEVICE_NAMES defined. Please, edit the config file."
256 #endif
257 
258 
259 // MARK: db_mem_device_link_key_t
260 #ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS
261 #if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0
262 static db_mem_device_link_key_t db_mem_device_link_key_storage[MAX_NO_DB_MEM_DEVICE_LINK_KEYS];
263 static memory_pool_t db_mem_device_link_key_pool;
264 void * btstack_memory_db_mem_device_link_key_get(void){
265     return memory_pool_get(&db_mem_device_link_key_pool);
266 }
267 void btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
268     memory_pool_free(&db_mem_device_link_key_pool, db_mem_device_link_key);
269 }
270 #else
271 void * btstack_memory_db_mem_device_link_key_get(void){
272     return NULL;
273 }
274 void btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
275 };
276 #endif
277 #elif defined(HAVE_MALLOC)
278 void * btstack_memory_db_mem_device_link_key_get(void){
279     return malloc(sizeof(db_mem_device_link_key_t));
280 }
281 void  btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
282     free(db_mem_device_link_key);
283 }
284 #else error "The struct db_mem_device_link_key has neither HAVE_MALLOC nor MAX_NO_DB_MEM_DEVICE_LINK_KEYS defined. Please, edit the config file."
285 #endif
286 
287 
288 // MARK: db_mem_service_t
289 #ifdef MAX_NO_DB_MEM_SERVICES
290 #if MAX_NO_DB_MEM_SERVICES > 0
291 static db_mem_service_t db_mem_service_storage[MAX_NO_DB_MEM_SERVICES];
292 static memory_pool_t db_mem_service_pool;
293 void * btstack_memory_db_mem_service_get(void){
294     return memory_pool_get(&db_mem_service_pool);
295 }
296 void btstack_memory_db_mem_service_free(void *db_mem_service){
297     memory_pool_free(&db_mem_service_pool, db_mem_service);
298 }
299 #else
300 void * btstack_memory_db_mem_service_get(void){
301     return NULL;
302 }
303 void btstack_memory_db_mem_service_free(void *db_mem_service){
304 };
305 #endif
306 #elif defined(HAVE_MALLOC)
307 void * btstack_memory_db_mem_service_get(void){
308     return malloc(sizeof(db_mem_service_t));
309 }
310 void  btstack_memory_db_mem_service_free(void *db_mem_service){
311     free(db_mem_service);
312 }
313 #else error "The struct db_mem_service has neither HAVE_MALLOC nor MAX_NO_DB_MEM_SERVICES defined. Please, edit the config file."
314 #endif
315 
316 // init
317 void btstack_memory_init(void){
318 #if MAX_NO_HCI_CONNECTIONS > 0
319     memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t));
320 #endif
321 #if MAX_NO_L2CAP_SERVICES > 0
322     memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t));
323 #endif
324 #if MAX_NO_L2CAP_CHANNELS > 0
325     memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
326 #endif
327 #if MAX_NO_RFCOMM_MULTIPLEXERS > 0
328     memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
329 #endif
330 #if MAX_NO_RFCOMM_SERVICES > 0
331     memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
332 #endif
333 #if MAX_NO_RFCOMM_CHANNELS > 0
334     memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
335 #endif
336 #if MAX_NO_DB_MEM_DEVICE_NAMES > 0
337     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));
338 #endif
339 #if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0
340     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));
341 #endif
342 #if MAX_NO_DB_MEM_SERVICES > 0
343     memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t));
344 #endif
345 }
346 
347 // init
348 void btstack_memory_init(void){
349 #if MAX_NO_HCI_CONNECTIONS > 0
350     memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t));
351 #endif
352 #if MAX_NO_L2CAP_SERVICES > 0
353     memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t));
354 #endif
355 #if MAX_NO_L2CAP_CHANNELS > 0
356     memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
357 #endif
358 #if MAX_NO_RFCOMM_MULTIPLEXERS > 0
359     memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
360 #endif
361 #if MAX_NO_RFCOMM_SERVICES > 0
362     memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
363 #endif
364 #if MAX_NO_RFCOMM_CHANNELS > 0
365     memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
366 #endif
367 #if MAX_NO_DB_MEM_DEVICE_NAMES > 0
368     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));
369 #endif
370 #if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0
371     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));
372 #endif
373 #if MAX_NO_DB_MEM_SERVICES > 0
374     memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t));
375 #endif
376 }
377