xref: /btstack/src/btstack_memory.c (revision 6b64433e6a17b046d431031fa3995f31c29aa188)
1a3b02b71Smatthias.ringwald /*
2*6b64433eSmatthias.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.
16*6b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
17*6b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
18*6b64433eSmatthias.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  *
33*6b64433eSmatthias.ringwald  * Please inquire about commercial licensing options at [email protected]
34*6b64433eSmatthias.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  *
42e0e5e285Smatthias.ringwald  *  @note code semi-atuomatically generated by 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 
51c8901d41Smatthias.ringwald #include "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
58a3b02b71Smatthias.ringwald static hci_connection_t hci_connection_storage[MAX_NO_HCI_CONNECTIONS];
59a3b02b71Smatthias.ringwald static memory_pool_t hci_connection_pool;
60a3b02b71Smatthias.ringwald void * btstack_memory_hci_connection_get(void){
61a3b02b71Smatthias.ringwald     return memory_pool_get(&hci_connection_pool);
62a3b02b71Smatthias.ringwald }
63a3b02b71Smatthias.ringwald void   btstack_memory_hci_connection_free(void *hci_connection){
64a3b02b71Smatthias.ringwald     memory_pool_free(&hci_connection_pool, hci_connection);
65a3b02b71Smatthias.ringwald }
66a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
67a3b02b71Smatthias.ringwald void * btstack_memory_hci_connection_get(void){
68a3b02b71Smatthias.ringwald     return malloc(sizeof(hci_connection_t));
69a3b02b71Smatthias.ringwald }
70a3b02b71Smatthias.ringwald void  btstack_memory_hci_connection_free(void *hci_connection){
71a3b02b71Smatthias.ringwald     free(hci_connection);
72a3b02b71Smatthias.ringwald }
73a3b02b71Smatthias.ringwald #endif
74a3b02b71Smatthias.ringwald 
75a3b02b71Smatthias.ringwald 
76a3b02b71Smatthias.ringwald // MARK: l2cap_service_t
77a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_SERVICES
78a3b02b71Smatthias.ringwald static l2cap_service_t l2cap_service_storage[MAX_NO_L2CAP_SERVICES];
79a3b02b71Smatthias.ringwald static memory_pool_t l2cap_service_pool;
80a3b02b71Smatthias.ringwald void * btstack_memory_l2cap_service_get(void){
81a3b02b71Smatthias.ringwald     return memory_pool_get(&l2cap_service_pool);
82a3b02b71Smatthias.ringwald }
83a3b02b71Smatthias.ringwald void   btstack_memory_l2cap_service_free(void *l2cap_service){
84a3b02b71Smatthias.ringwald     memory_pool_free(&l2cap_service_pool, l2cap_service);
85a3b02b71Smatthias.ringwald }
86a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
87a3b02b71Smatthias.ringwald void * btstack_memory_l2cap_service_get(void){
88a3b02b71Smatthias.ringwald     return malloc(sizeof(l2cap_service_t));
89a3b02b71Smatthias.ringwald }
90a3b02b71Smatthias.ringwald void  btstack_memory_l2cap_service_free(void *l2cap_service){
91a3b02b71Smatthias.ringwald     free(l2cap_service);
92a3b02b71Smatthias.ringwald }
93a3b02b71Smatthias.ringwald #endif
94a3b02b71Smatthias.ringwald 
95a3b02b71Smatthias.ringwald 
96a3b02b71Smatthias.ringwald // MARK: l2cap_channel_t
97a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_CHANNELS
98a3b02b71Smatthias.ringwald static l2cap_channel_t l2cap_channel_storage[MAX_NO_L2CAP_CHANNELS];
99a3b02b71Smatthias.ringwald static memory_pool_t l2cap_channel_pool;
100a3b02b71Smatthias.ringwald void * btstack_memory_l2cap_channel_get(void){
101a3b02b71Smatthias.ringwald     return memory_pool_get(&l2cap_channel_pool);
102a3b02b71Smatthias.ringwald }
103a3b02b71Smatthias.ringwald void   btstack_memory_l2cap_channel_free(void *l2cap_channel){
104a3b02b71Smatthias.ringwald     memory_pool_free(&l2cap_channel_pool, l2cap_channel);
105a3b02b71Smatthias.ringwald }
106a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
107a3b02b71Smatthias.ringwald void * btstack_memory_l2cap_channel_get(void){
108a3b02b71Smatthias.ringwald     return malloc(sizeof(l2cap_channel_t));
109a3b02b71Smatthias.ringwald }
110a3b02b71Smatthias.ringwald void  btstack_memory_l2cap_channel_free(void *l2cap_channel){
111a3b02b71Smatthias.ringwald     free(l2cap_channel);
112a3b02b71Smatthias.ringwald }
113a3b02b71Smatthias.ringwald #endif
114a3b02b71Smatthias.ringwald 
115a3b02b71Smatthias.ringwald 
116a3b02b71Smatthias.ringwald // MARK: rfcomm_multiplexer_t
117a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
118a3b02b71Smatthias.ringwald static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NO_RFCOMM_MULTIPLEXERS];
119a3b02b71Smatthias.ringwald static memory_pool_t rfcomm_multiplexer_pool;
120a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_multiplexer_get(void){
121a3b02b71Smatthias.ringwald     return memory_pool_get(&rfcomm_multiplexer_pool);
122a3b02b71Smatthias.ringwald }
123a3b02b71Smatthias.ringwald void   btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
124a3b02b71Smatthias.ringwald     memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer);
125a3b02b71Smatthias.ringwald }
126a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
127a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_multiplexer_get(void){
128a3b02b71Smatthias.ringwald     return malloc(sizeof(rfcomm_multiplexer_t));
129a3b02b71Smatthias.ringwald }
130a3b02b71Smatthias.ringwald void  btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
131a3b02b71Smatthias.ringwald     free(rfcomm_multiplexer);
132a3b02b71Smatthias.ringwald }
133a3b02b71Smatthias.ringwald #endif
134a3b02b71Smatthias.ringwald 
135a3b02b71Smatthias.ringwald 
136a3b02b71Smatthias.ringwald // MARK: rfcomm_service_t
137a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_SERVICES
138a3b02b71Smatthias.ringwald static rfcomm_service_t rfcomm_service_storage[MAX_NO_RFCOMM_SERVICES];
139a3b02b71Smatthias.ringwald static memory_pool_t rfcomm_service_pool;
140a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_service_get(void){
141a3b02b71Smatthias.ringwald     return memory_pool_get(&rfcomm_service_pool);
142a3b02b71Smatthias.ringwald }
143a3b02b71Smatthias.ringwald void   btstack_memory_rfcomm_service_free(void *rfcomm_service){
144a3b02b71Smatthias.ringwald     memory_pool_free(&rfcomm_service_pool, rfcomm_service);
145a3b02b71Smatthias.ringwald }
146a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
147a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_service_get(void){
148a3b02b71Smatthias.ringwald     return malloc(sizeof(rfcomm_service_t));
149a3b02b71Smatthias.ringwald }
150a3b02b71Smatthias.ringwald void  btstack_memory_rfcomm_service_free(void *rfcomm_service){
151a3b02b71Smatthias.ringwald     free(rfcomm_service);
152a3b02b71Smatthias.ringwald }
153a3b02b71Smatthias.ringwald #endif
154a3b02b71Smatthias.ringwald 
155a3b02b71Smatthias.ringwald 
156a3b02b71Smatthias.ringwald // MARK: rfcomm_channel_t
157a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_CHANNELS
158a3b02b71Smatthias.ringwald static rfcomm_channel_t rfcomm_channel_storage[MAX_NO_RFCOMM_CHANNELS];
159a3b02b71Smatthias.ringwald static memory_pool_t rfcomm_channel_pool;
160a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_channel_get(void){
161a3b02b71Smatthias.ringwald     return memory_pool_get(&rfcomm_channel_pool);
162a3b02b71Smatthias.ringwald }
163a3b02b71Smatthias.ringwald void   btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
164a3b02b71Smatthias.ringwald     memory_pool_free(&rfcomm_channel_pool, rfcomm_channel);
165a3b02b71Smatthias.ringwald }
166a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
167a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_channel_get(void){
168a3b02b71Smatthias.ringwald     return malloc(sizeof(rfcomm_channel_t));
169a3b02b71Smatthias.ringwald }
170a3b02b71Smatthias.ringwald void  btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
171a3b02b71Smatthias.ringwald     free(rfcomm_channel);
172a3b02b71Smatthias.ringwald }
173a3b02b71Smatthias.ringwald #endif
174a3b02b71Smatthias.ringwald 
175e0e5e285Smatthias.ringwald 
176e0e5e285Smatthias.ringwald // MARK: db_mem_device_name_t
177e0e5e285Smatthias.ringwald #ifdef MAX_NO_DB_MEM_DEVICE_NAMES
178e0e5e285Smatthias.ringwald static db_mem_device_name_t db_mem_device_name_storage[MAX_NO_DB_MEM_DEVICE_NAMES];
179e0e5e285Smatthias.ringwald static memory_pool_t db_mem_device_name_pool;
180e0e5e285Smatthias.ringwald void * btstack_memory_db_mem_device_name_get(void){
181e0e5e285Smatthias.ringwald     return memory_pool_get(&db_mem_device_name_pool);
1821801b596Smatthias.ringwald }
183e0e5e285Smatthias.ringwald void   btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
184e0e5e285Smatthias.ringwald     memory_pool_free(&db_mem_device_name_pool, db_mem_device_name);
1851801b596Smatthias.ringwald }
1861801b596Smatthias.ringwald #elif defined(HAVE_MALLOC)
187e0e5e285Smatthias.ringwald void * btstack_memory_db_mem_device_name_get(void){
188e0e5e285Smatthias.ringwald     return malloc(sizeof(db_mem_device_name_t));
1891801b596Smatthias.ringwald }
190e0e5e285Smatthias.ringwald void  btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
191e0e5e285Smatthias.ringwald     free(db_mem_device_name);
192e0e5e285Smatthias.ringwald }
193e0e5e285Smatthias.ringwald #endif
194e0e5e285Smatthias.ringwald 
195e0e5e285Smatthias.ringwald 
196e0e5e285Smatthias.ringwald // MARK: db_mem_device_link_key_t
197e0e5e285Smatthias.ringwald #ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS
198e0e5e285Smatthias.ringwald static db_mem_device_link_key_t db_mem_device_link_key_storage[MAX_NO_DB_MEM_DEVICE_LINK_KEYS];
199e0e5e285Smatthias.ringwald static memory_pool_t db_mem_device_link_key_pool;
200e0e5e285Smatthias.ringwald void * btstack_memory_db_mem_device_link_key_get(void){
201e0e5e285Smatthias.ringwald     return memory_pool_get(&db_mem_device_link_key_pool);
202e0e5e285Smatthias.ringwald }
203e0e5e285Smatthias.ringwald void   btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
204e0e5e285Smatthias.ringwald     memory_pool_free(&db_mem_device_link_key_pool, db_mem_device_link_key);
205e0e5e285Smatthias.ringwald }
206e0e5e285Smatthias.ringwald #elif defined(HAVE_MALLOC)
207e0e5e285Smatthias.ringwald void * btstack_memory_db_mem_device_link_key_get(void){
208e0e5e285Smatthias.ringwald     return malloc(sizeof(db_mem_device_link_key_t));
209e0e5e285Smatthias.ringwald }
210e0e5e285Smatthias.ringwald void  btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
211e0e5e285Smatthias.ringwald     free(db_mem_device_link_key);
2121801b596Smatthias.ringwald }
2131801b596Smatthias.ringwald #endif
2141801b596Smatthias.ringwald 
2151801b596Smatthias.ringwald 
2161801b596Smatthias.ringwald // MARK: db_mem_service_t
2171801b596Smatthias.ringwald #ifdef MAX_NO_DB_MEM_SERVICES
2181801b596Smatthias.ringwald static db_mem_service_t db_mem_service_storage[MAX_NO_DB_MEM_SERVICES];
2191801b596Smatthias.ringwald static memory_pool_t db_mem_service_pool;
2201801b596Smatthias.ringwald void * btstack_memory_db_mem_service_get(void){
2211801b596Smatthias.ringwald     return memory_pool_get(&db_mem_service_pool);
2221801b596Smatthias.ringwald }
2231801b596Smatthias.ringwald void   btstack_memory_db_mem_service_free(void *db_mem_service){
2241801b596Smatthias.ringwald     memory_pool_free(&db_mem_service_pool, db_mem_service);
2251801b596Smatthias.ringwald }
2261801b596Smatthias.ringwald #elif defined(HAVE_MALLOC)
2271801b596Smatthias.ringwald void * btstack_memory_db_mem_service_get(void){
2281801b596Smatthias.ringwald     return malloc(sizeof(db_mem_service_t));
2291801b596Smatthias.ringwald }
2301801b596Smatthias.ringwald void  btstack_memory_db_mem_service_free(void *db_mem_service){
2311801b596Smatthias.ringwald     free(db_mem_service);
2321801b596Smatthias.ringwald }
2331801b596Smatthias.ringwald #endif
2341801b596Smatthias.ringwald 
235a3b02b71Smatthias.ringwald // init
236a3b02b71Smatthias.ringwald void btstack_memory_init(void){
237a3b02b71Smatthias.ringwald #ifdef MAX_NO_HCI_CONNECTIONS
238a3b02b71Smatthias.ringwald     memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t));
239a3b02b71Smatthias.ringwald #endif
240a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_SERVICES
241a3b02b71Smatthias.ringwald     memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t));
242a3b02b71Smatthias.ringwald #endif
243a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_CHANNELS
244a3b02b71Smatthias.ringwald     memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
245a3b02b71Smatthias.ringwald #endif
246a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
247a3b02b71Smatthias.ringwald     memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
248a3b02b71Smatthias.ringwald #endif
249a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_SERVICES
250a3b02b71Smatthias.ringwald     memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
251a3b02b71Smatthias.ringwald #endif
252a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_CHANNELS
253a3b02b71Smatthias.ringwald     memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
254a3b02b71Smatthias.ringwald #endif
255e0e5e285Smatthias.ringwald #ifdef MAX_NO_DB_MEM_DEVICE_NAMES
256e0e5e285Smatthias.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));
257e0e5e285Smatthias.ringwald #endif
258e0e5e285Smatthias.ringwald #ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS
259e0e5e285Smatthias.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));
2601801b596Smatthias.ringwald #endif
2611801b596Smatthias.ringwald #ifdef MAX_NO_DB_MEM_SERVICES
2621801b596Smatthias.ringwald     memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t));
2631801b596Smatthias.ringwald #endif
264a3b02b71Smatthias.ringwald }
265