xref: /btstack/src/btstack_memory.c (revision c8901d41d658dce37692240aaccfb2e5dffe4ac8)
1a3b02b71Smatthias.ringwald /*
2a3b02b71Smatthias.ringwald  * Copyright (C) 2011 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.
16a3b02b71Smatthias.ringwald  *
17a3b02b71Smatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
18a3b02b71Smatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19a3b02b71Smatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20a3b02b71Smatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
21a3b02b71Smatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22a3b02b71Smatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23a3b02b71Smatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24a3b02b71Smatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25a3b02b71Smatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26a3b02b71Smatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27a3b02b71Smatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28a3b02b71Smatthias.ringwald  * SUCH DAMAGE.
29a3b02b71Smatthias.ringwald  *
30a3b02b71Smatthias.ringwald  */
31a3b02b71Smatthias.ringwald 
32a3b02b71Smatthias.ringwald /*
33a3b02b71Smatthias.ringwald  *  btstsack_memory.h
34a3b02b71Smatthias.ringwald  *
35a3b02b71Smatthias.ringwald  *  @brief BTstack memory management via configurable memory pools
36a3b02b71Smatthias.ringwald  *
37e0e5e285Smatthias.ringwald  *  @note code semi-atuomatically generated by btstack_memory_generator.py
38a3b02b71Smatthias.ringwald  *
39a3b02b71Smatthias.ringwald  */
40a3b02b71Smatthias.ringwald 
41a3b02b71Smatthias.ringwald #include "btstack_memory.h"
42a3b02b71Smatthias.ringwald #include <btstack/memory_pool.h>
43a3b02b71Smatthias.ringwald 
44a3b02b71Smatthias.ringwald #include <stdlib.h>
45a3b02b71Smatthias.ringwald 
46*c8901d41Smatthias.ringwald #include "config.h"
47a3b02b71Smatthias.ringwald #include "hci.h"
48a3b02b71Smatthias.ringwald #include "l2cap.h"
49a3b02b71Smatthias.ringwald #include "rfcomm.h"
50a3b02b71Smatthias.ringwald 
51a3b02b71Smatthias.ringwald // MARK: hci_connection_t
52a3b02b71Smatthias.ringwald #ifdef MAX_NO_HCI_CONNECTIONS
53a3b02b71Smatthias.ringwald static hci_connection_t hci_connection_storage[MAX_NO_HCI_CONNECTIONS];
54a3b02b71Smatthias.ringwald static memory_pool_t hci_connection_pool;
55a3b02b71Smatthias.ringwald void * btstack_memory_hci_connection_get(void){
56a3b02b71Smatthias.ringwald     return memory_pool_get(&hci_connection_pool);
57a3b02b71Smatthias.ringwald }
58a3b02b71Smatthias.ringwald void   btstack_memory_hci_connection_free(void *hci_connection){
59a3b02b71Smatthias.ringwald     memory_pool_free(&hci_connection_pool, hci_connection);
60a3b02b71Smatthias.ringwald }
61a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
62a3b02b71Smatthias.ringwald void * btstack_memory_hci_connection_get(void){
63a3b02b71Smatthias.ringwald     return malloc(sizeof(hci_connection_t));
64a3b02b71Smatthias.ringwald }
65a3b02b71Smatthias.ringwald void  btstack_memory_hci_connection_free(void *hci_connection){
66a3b02b71Smatthias.ringwald     free(hci_connection);
67a3b02b71Smatthias.ringwald }
68a3b02b71Smatthias.ringwald #endif
69a3b02b71Smatthias.ringwald 
70a3b02b71Smatthias.ringwald 
71a3b02b71Smatthias.ringwald // MARK: l2cap_service_t
72a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_SERVICES
73a3b02b71Smatthias.ringwald static l2cap_service_t l2cap_service_storage[MAX_NO_L2CAP_SERVICES];
74a3b02b71Smatthias.ringwald static memory_pool_t l2cap_service_pool;
75a3b02b71Smatthias.ringwald void * btstack_memory_l2cap_service_get(void){
76a3b02b71Smatthias.ringwald     return memory_pool_get(&l2cap_service_pool);
77a3b02b71Smatthias.ringwald }
78a3b02b71Smatthias.ringwald void   btstack_memory_l2cap_service_free(void *l2cap_service){
79a3b02b71Smatthias.ringwald     memory_pool_free(&l2cap_service_pool, l2cap_service);
80a3b02b71Smatthias.ringwald }
81a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
82a3b02b71Smatthias.ringwald void * btstack_memory_l2cap_service_get(void){
83a3b02b71Smatthias.ringwald     return malloc(sizeof(l2cap_service_t));
84a3b02b71Smatthias.ringwald }
85a3b02b71Smatthias.ringwald void  btstack_memory_l2cap_service_free(void *l2cap_service){
86a3b02b71Smatthias.ringwald     free(l2cap_service);
87a3b02b71Smatthias.ringwald }
88a3b02b71Smatthias.ringwald #endif
89a3b02b71Smatthias.ringwald 
90a3b02b71Smatthias.ringwald 
91a3b02b71Smatthias.ringwald // MARK: l2cap_channel_t
92a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_CHANNELS
93a3b02b71Smatthias.ringwald static l2cap_channel_t l2cap_channel_storage[MAX_NO_L2CAP_CHANNELS];
94a3b02b71Smatthias.ringwald static memory_pool_t l2cap_channel_pool;
95a3b02b71Smatthias.ringwald void * btstack_memory_l2cap_channel_get(void){
96a3b02b71Smatthias.ringwald     return memory_pool_get(&l2cap_channel_pool);
97a3b02b71Smatthias.ringwald }
98a3b02b71Smatthias.ringwald void   btstack_memory_l2cap_channel_free(void *l2cap_channel){
99a3b02b71Smatthias.ringwald     memory_pool_free(&l2cap_channel_pool, l2cap_channel);
100a3b02b71Smatthias.ringwald }
101a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
102a3b02b71Smatthias.ringwald void * btstack_memory_l2cap_channel_get(void){
103a3b02b71Smatthias.ringwald     return malloc(sizeof(l2cap_channel_t));
104a3b02b71Smatthias.ringwald }
105a3b02b71Smatthias.ringwald void  btstack_memory_l2cap_channel_free(void *l2cap_channel){
106a3b02b71Smatthias.ringwald     free(l2cap_channel);
107a3b02b71Smatthias.ringwald }
108a3b02b71Smatthias.ringwald #endif
109a3b02b71Smatthias.ringwald 
110a3b02b71Smatthias.ringwald 
111a3b02b71Smatthias.ringwald // MARK: rfcomm_multiplexer_t
112a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
113a3b02b71Smatthias.ringwald static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NO_RFCOMM_MULTIPLEXERS];
114a3b02b71Smatthias.ringwald static memory_pool_t rfcomm_multiplexer_pool;
115a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_multiplexer_get(void){
116a3b02b71Smatthias.ringwald     return memory_pool_get(&rfcomm_multiplexer_pool);
117a3b02b71Smatthias.ringwald }
118a3b02b71Smatthias.ringwald void   btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
119a3b02b71Smatthias.ringwald     memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer);
120a3b02b71Smatthias.ringwald }
121a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
122a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_multiplexer_get(void){
123a3b02b71Smatthias.ringwald     return malloc(sizeof(rfcomm_multiplexer_t));
124a3b02b71Smatthias.ringwald }
125a3b02b71Smatthias.ringwald void  btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
126a3b02b71Smatthias.ringwald     free(rfcomm_multiplexer);
127a3b02b71Smatthias.ringwald }
128a3b02b71Smatthias.ringwald #endif
129a3b02b71Smatthias.ringwald 
130a3b02b71Smatthias.ringwald 
131a3b02b71Smatthias.ringwald // MARK: rfcomm_service_t
132a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_SERVICES
133a3b02b71Smatthias.ringwald static rfcomm_service_t rfcomm_service_storage[MAX_NO_RFCOMM_SERVICES];
134a3b02b71Smatthias.ringwald static memory_pool_t rfcomm_service_pool;
135a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_service_get(void){
136a3b02b71Smatthias.ringwald     return memory_pool_get(&rfcomm_service_pool);
137a3b02b71Smatthias.ringwald }
138a3b02b71Smatthias.ringwald void   btstack_memory_rfcomm_service_free(void *rfcomm_service){
139a3b02b71Smatthias.ringwald     memory_pool_free(&rfcomm_service_pool, rfcomm_service);
140a3b02b71Smatthias.ringwald }
141a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
142a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_service_get(void){
143a3b02b71Smatthias.ringwald     return malloc(sizeof(rfcomm_service_t));
144a3b02b71Smatthias.ringwald }
145a3b02b71Smatthias.ringwald void  btstack_memory_rfcomm_service_free(void *rfcomm_service){
146a3b02b71Smatthias.ringwald     free(rfcomm_service);
147a3b02b71Smatthias.ringwald }
148a3b02b71Smatthias.ringwald #endif
149a3b02b71Smatthias.ringwald 
150a3b02b71Smatthias.ringwald 
151a3b02b71Smatthias.ringwald // MARK: rfcomm_channel_t
152a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_CHANNELS
153a3b02b71Smatthias.ringwald static rfcomm_channel_t rfcomm_channel_storage[MAX_NO_RFCOMM_CHANNELS];
154a3b02b71Smatthias.ringwald static memory_pool_t rfcomm_channel_pool;
155a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_channel_get(void){
156a3b02b71Smatthias.ringwald     return memory_pool_get(&rfcomm_channel_pool);
157a3b02b71Smatthias.ringwald }
158a3b02b71Smatthias.ringwald void   btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
159a3b02b71Smatthias.ringwald     memory_pool_free(&rfcomm_channel_pool, rfcomm_channel);
160a3b02b71Smatthias.ringwald }
161a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
162a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_channel_get(void){
163a3b02b71Smatthias.ringwald     return malloc(sizeof(rfcomm_channel_t));
164a3b02b71Smatthias.ringwald }
165a3b02b71Smatthias.ringwald void  btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
166a3b02b71Smatthias.ringwald     free(rfcomm_channel);
167a3b02b71Smatthias.ringwald }
168a3b02b71Smatthias.ringwald #endif
169a3b02b71Smatthias.ringwald 
170e0e5e285Smatthias.ringwald 
171e0e5e285Smatthias.ringwald // MARK: db_mem_device_name_t
172e0e5e285Smatthias.ringwald #ifdef MAX_NO_DB_MEM_DEVICE_NAMES
173e0e5e285Smatthias.ringwald static db_mem_device_name_t db_mem_device_name_storage[MAX_NO_DB_MEM_DEVICE_NAMES];
174e0e5e285Smatthias.ringwald static memory_pool_t db_mem_device_name_pool;
175e0e5e285Smatthias.ringwald void * btstack_memory_db_mem_device_name_get(void){
176e0e5e285Smatthias.ringwald     return memory_pool_get(&db_mem_device_name_pool);
1771801b596Smatthias.ringwald }
178e0e5e285Smatthias.ringwald void   btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
179e0e5e285Smatthias.ringwald     memory_pool_free(&db_mem_device_name_pool, db_mem_device_name);
1801801b596Smatthias.ringwald }
1811801b596Smatthias.ringwald #elif defined(HAVE_MALLOC)
182e0e5e285Smatthias.ringwald void * btstack_memory_db_mem_device_name_get(void){
183e0e5e285Smatthias.ringwald     return malloc(sizeof(db_mem_device_name_t));
1841801b596Smatthias.ringwald }
185e0e5e285Smatthias.ringwald void  btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
186e0e5e285Smatthias.ringwald     free(db_mem_device_name);
187e0e5e285Smatthias.ringwald }
188e0e5e285Smatthias.ringwald #endif
189e0e5e285Smatthias.ringwald 
190e0e5e285Smatthias.ringwald 
191e0e5e285Smatthias.ringwald // MARK: db_mem_device_link_key_t
192e0e5e285Smatthias.ringwald #ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS
193e0e5e285Smatthias.ringwald static db_mem_device_link_key_t db_mem_device_link_key_storage[MAX_NO_DB_MEM_DEVICE_LINK_KEYS];
194e0e5e285Smatthias.ringwald static memory_pool_t db_mem_device_link_key_pool;
195e0e5e285Smatthias.ringwald void * btstack_memory_db_mem_device_link_key_get(void){
196e0e5e285Smatthias.ringwald     return memory_pool_get(&db_mem_device_link_key_pool);
197e0e5e285Smatthias.ringwald }
198e0e5e285Smatthias.ringwald void   btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
199e0e5e285Smatthias.ringwald     memory_pool_free(&db_mem_device_link_key_pool, db_mem_device_link_key);
200e0e5e285Smatthias.ringwald }
201e0e5e285Smatthias.ringwald #elif defined(HAVE_MALLOC)
202e0e5e285Smatthias.ringwald void * btstack_memory_db_mem_device_link_key_get(void){
203e0e5e285Smatthias.ringwald     return malloc(sizeof(db_mem_device_link_key_t));
204e0e5e285Smatthias.ringwald }
205e0e5e285Smatthias.ringwald void  btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
206e0e5e285Smatthias.ringwald     free(db_mem_device_link_key);
2071801b596Smatthias.ringwald }
2081801b596Smatthias.ringwald #endif
2091801b596Smatthias.ringwald 
2101801b596Smatthias.ringwald 
2111801b596Smatthias.ringwald // MARK: db_mem_service_t
2121801b596Smatthias.ringwald #ifdef MAX_NO_DB_MEM_SERVICES
2131801b596Smatthias.ringwald static db_mem_service_t db_mem_service_storage[MAX_NO_DB_MEM_SERVICES];
2141801b596Smatthias.ringwald static memory_pool_t db_mem_service_pool;
2151801b596Smatthias.ringwald void * btstack_memory_db_mem_service_get(void){
2161801b596Smatthias.ringwald     return memory_pool_get(&db_mem_service_pool);
2171801b596Smatthias.ringwald }
2181801b596Smatthias.ringwald void   btstack_memory_db_mem_service_free(void *db_mem_service){
2191801b596Smatthias.ringwald     memory_pool_free(&db_mem_service_pool, db_mem_service);
2201801b596Smatthias.ringwald }
2211801b596Smatthias.ringwald #elif defined(HAVE_MALLOC)
2221801b596Smatthias.ringwald void * btstack_memory_db_mem_service_get(void){
2231801b596Smatthias.ringwald     return malloc(sizeof(db_mem_service_t));
2241801b596Smatthias.ringwald }
2251801b596Smatthias.ringwald void  btstack_memory_db_mem_service_free(void *db_mem_service){
2261801b596Smatthias.ringwald     free(db_mem_service);
2271801b596Smatthias.ringwald }
2281801b596Smatthias.ringwald #endif
2291801b596Smatthias.ringwald 
230a3b02b71Smatthias.ringwald // init
231a3b02b71Smatthias.ringwald void btstack_memory_init(void){
232a3b02b71Smatthias.ringwald #ifdef MAX_NO_HCI_CONNECTIONS
233a3b02b71Smatthias.ringwald     memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t));
234a3b02b71Smatthias.ringwald #endif
235a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_SERVICES
236a3b02b71Smatthias.ringwald     memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t));
237a3b02b71Smatthias.ringwald #endif
238a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_CHANNELS
239a3b02b71Smatthias.ringwald     memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
240a3b02b71Smatthias.ringwald #endif
241a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
242a3b02b71Smatthias.ringwald     memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
243a3b02b71Smatthias.ringwald #endif
244a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_SERVICES
245a3b02b71Smatthias.ringwald     memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
246a3b02b71Smatthias.ringwald #endif
247a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_CHANNELS
248a3b02b71Smatthias.ringwald     memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
249a3b02b71Smatthias.ringwald #endif
250e0e5e285Smatthias.ringwald #ifdef MAX_NO_DB_MEM_DEVICE_NAMES
251e0e5e285Smatthias.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));
252e0e5e285Smatthias.ringwald #endif
253e0e5e285Smatthias.ringwald #ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS
254e0e5e285Smatthias.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));
2551801b596Smatthias.ringwald #endif
2561801b596Smatthias.ringwald #ifdef MAX_NO_DB_MEM_SERVICES
2571801b596Smatthias.ringwald     memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t));
2581801b596Smatthias.ringwald #endif
259a3b02b71Smatthias.ringwald }