xref: /btstack/src/btstack_memory.c (revision 1801b59606631b25c1ea9002a24a23686e48310a)
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  *
37a3b02b71Smatthias.ringwald  *  @note code semi-atuomatically generated by btstack_memory_generate.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 
46a3b02b71Smatthias.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 #else
69a3b02b71Smatthias.ringwald #error BTstack needs at least one hci_connection_t, but neither MAX_NO_HCI_CONNECTIONS nor HAVE_MALLOC are defined
70a3b02b71Smatthias.ringwald #endif
71a3b02b71Smatthias.ringwald 
72a3b02b71Smatthias.ringwald 
73a3b02b71Smatthias.ringwald // MARK: l2cap_service_t
74a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_SERVICES
75a3b02b71Smatthias.ringwald static l2cap_service_t l2cap_service_storage[MAX_NO_L2CAP_SERVICES];
76a3b02b71Smatthias.ringwald static memory_pool_t l2cap_service_pool;
77a3b02b71Smatthias.ringwald void * btstack_memory_l2cap_service_get(void){
78a3b02b71Smatthias.ringwald     return memory_pool_get(&l2cap_service_pool);
79a3b02b71Smatthias.ringwald }
80a3b02b71Smatthias.ringwald void   btstack_memory_l2cap_service_free(void *l2cap_service){
81a3b02b71Smatthias.ringwald     memory_pool_free(&l2cap_service_pool, l2cap_service);
82a3b02b71Smatthias.ringwald }
83a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
84a3b02b71Smatthias.ringwald void * btstack_memory_l2cap_service_get(void){
85a3b02b71Smatthias.ringwald     return malloc(sizeof(l2cap_service_t));
86a3b02b71Smatthias.ringwald }
87a3b02b71Smatthias.ringwald void  btstack_memory_l2cap_service_free(void *l2cap_service){
88a3b02b71Smatthias.ringwald     free(l2cap_service);
89a3b02b71Smatthias.ringwald }
90a3b02b71Smatthias.ringwald #else
91a3b02b71Smatthias.ringwald #error BTstack needs at least one l2cap_service_t, but neither MAX_NO_L2CAP_SERVICES nor HAVE_MALLOC are defined
92a3b02b71Smatthias.ringwald #endif
93a3b02b71Smatthias.ringwald 
94a3b02b71Smatthias.ringwald 
95a3b02b71Smatthias.ringwald // MARK: l2cap_channel_t
96a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_CHANNELS
97a3b02b71Smatthias.ringwald static l2cap_channel_t l2cap_channel_storage[MAX_NO_L2CAP_CHANNELS];
98a3b02b71Smatthias.ringwald static memory_pool_t l2cap_channel_pool;
99a3b02b71Smatthias.ringwald void * btstack_memory_l2cap_channel_get(void){
100a3b02b71Smatthias.ringwald     return memory_pool_get(&l2cap_channel_pool);
101a3b02b71Smatthias.ringwald }
102a3b02b71Smatthias.ringwald void   btstack_memory_l2cap_channel_free(void *l2cap_channel){
103a3b02b71Smatthias.ringwald     memory_pool_free(&l2cap_channel_pool, l2cap_channel);
104a3b02b71Smatthias.ringwald }
105a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
106a3b02b71Smatthias.ringwald void * btstack_memory_l2cap_channel_get(void){
107a3b02b71Smatthias.ringwald     return malloc(sizeof(l2cap_channel_t));
108a3b02b71Smatthias.ringwald }
109a3b02b71Smatthias.ringwald void  btstack_memory_l2cap_channel_free(void *l2cap_channel){
110a3b02b71Smatthias.ringwald     free(l2cap_channel);
111a3b02b71Smatthias.ringwald }
112a3b02b71Smatthias.ringwald #else
113a3b02b71Smatthias.ringwald #error BTstack needs at least one l2cap_channel_t, but neither MAX_NO_L2CAP_CHANNELS nor HAVE_MALLOC are defined
114a3b02b71Smatthias.ringwald #endif
115a3b02b71Smatthias.ringwald 
116a3b02b71Smatthias.ringwald 
117a3b02b71Smatthias.ringwald // MARK: rfcomm_multiplexer_t
118a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
119a3b02b71Smatthias.ringwald static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NO_RFCOMM_MULTIPLEXERS];
120a3b02b71Smatthias.ringwald static memory_pool_t rfcomm_multiplexer_pool;
121a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_multiplexer_get(void){
122a3b02b71Smatthias.ringwald     return memory_pool_get(&rfcomm_multiplexer_pool);
123a3b02b71Smatthias.ringwald }
124a3b02b71Smatthias.ringwald void   btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
125a3b02b71Smatthias.ringwald     memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer);
126a3b02b71Smatthias.ringwald }
127a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
128a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_multiplexer_get(void){
129a3b02b71Smatthias.ringwald     return malloc(sizeof(rfcomm_multiplexer_t));
130a3b02b71Smatthias.ringwald }
131a3b02b71Smatthias.ringwald void  btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
132a3b02b71Smatthias.ringwald     free(rfcomm_multiplexer);
133a3b02b71Smatthias.ringwald }
134a3b02b71Smatthias.ringwald #else
135a3b02b71Smatthias.ringwald #error BTstack needs at least one rfcomm_multiplexer_t, but neither MAX_NO_RFCOMM_MULTIPLEXERS nor HAVE_MALLOC are defined
136a3b02b71Smatthias.ringwald #endif
137a3b02b71Smatthias.ringwald 
138a3b02b71Smatthias.ringwald 
139a3b02b71Smatthias.ringwald // MARK: rfcomm_service_t
140a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_SERVICES
141a3b02b71Smatthias.ringwald static rfcomm_service_t rfcomm_service_storage[MAX_NO_RFCOMM_SERVICES];
142a3b02b71Smatthias.ringwald static memory_pool_t rfcomm_service_pool;
143a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_service_get(void){
144a3b02b71Smatthias.ringwald     return memory_pool_get(&rfcomm_service_pool);
145a3b02b71Smatthias.ringwald }
146a3b02b71Smatthias.ringwald void   btstack_memory_rfcomm_service_free(void *rfcomm_service){
147a3b02b71Smatthias.ringwald     memory_pool_free(&rfcomm_service_pool, rfcomm_service);
148a3b02b71Smatthias.ringwald }
149a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
150a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_service_get(void){
151a3b02b71Smatthias.ringwald     return malloc(sizeof(rfcomm_service_t));
152a3b02b71Smatthias.ringwald }
153a3b02b71Smatthias.ringwald void  btstack_memory_rfcomm_service_free(void *rfcomm_service){
154a3b02b71Smatthias.ringwald     free(rfcomm_service);
155a3b02b71Smatthias.ringwald }
156a3b02b71Smatthias.ringwald #else
157a3b02b71Smatthias.ringwald #error BTstack needs at least one rfcomm_service_t, but neither MAX_NO_RFCOMM_SERVICES nor HAVE_MALLOC are defined
158a3b02b71Smatthias.ringwald #endif
159a3b02b71Smatthias.ringwald 
160a3b02b71Smatthias.ringwald 
161a3b02b71Smatthias.ringwald // MARK: rfcomm_channel_t
162a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_CHANNELS
163a3b02b71Smatthias.ringwald static rfcomm_channel_t rfcomm_channel_storage[MAX_NO_RFCOMM_CHANNELS];
164a3b02b71Smatthias.ringwald static memory_pool_t rfcomm_channel_pool;
165a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_channel_get(void){
166a3b02b71Smatthias.ringwald     return memory_pool_get(&rfcomm_channel_pool);
167a3b02b71Smatthias.ringwald }
168a3b02b71Smatthias.ringwald void   btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
169a3b02b71Smatthias.ringwald     memory_pool_free(&rfcomm_channel_pool, rfcomm_channel);
170a3b02b71Smatthias.ringwald }
171a3b02b71Smatthias.ringwald #elif defined(HAVE_MALLOC)
172a3b02b71Smatthias.ringwald void * btstack_memory_rfcomm_channel_get(void){
173a3b02b71Smatthias.ringwald     return malloc(sizeof(rfcomm_channel_t));
174a3b02b71Smatthias.ringwald }
175a3b02b71Smatthias.ringwald void  btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
176a3b02b71Smatthias.ringwald     free(rfcomm_channel);
177a3b02b71Smatthias.ringwald }
178a3b02b71Smatthias.ringwald #else
179a3b02b71Smatthias.ringwald #error BTstack needs at least one rfcomm_channel_t, but neither MAX_NO_RFCOMM_CHANNELS nor HAVE_MALLOC are defined
180a3b02b71Smatthias.ringwald #endif
181a3b02b71Smatthias.ringwald 
182*1801b596Smatthias.ringwald // MARK: db_mem_device_t
183*1801b596Smatthias.ringwald #ifdef MAX_NO_DB_MEM_DEVICES
184*1801b596Smatthias.ringwald static db_mem_device_t db_mem_device_storage[MAX_NO_DB_MEM_DEVICES];
185*1801b596Smatthias.ringwald static memory_pool_t db_mem_device_pool;
186*1801b596Smatthias.ringwald void * btstack_memory_db_mem_device_get(void){
187*1801b596Smatthias.ringwald     return memory_pool_get(&db_mem_device_pool);
188*1801b596Smatthias.ringwald }
189*1801b596Smatthias.ringwald void   btstack_memory_db_mem_device_free(void *db_mem_device){
190*1801b596Smatthias.ringwald     memory_pool_free(&db_mem_device_pool, db_mem_device);
191*1801b596Smatthias.ringwald }
192*1801b596Smatthias.ringwald #elif defined(HAVE_MALLOC)
193*1801b596Smatthias.ringwald void * btstack_memory_db_mem_device_get(void){
194*1801b596Smatthias.ringwald     return malloc(sizeof(db_mem_device_t));
195*1801b596Smatthias.ringwald }
196*1801b596Smatthias.ringwald void  btstack_memory_db_mem_device_free(void *db_mem_device){
197*1801b596Smatthias.ringwald     free(db_mem_device);
198*1801b596Smatthias.ringwald }
199*1801b596Smatthias.ringwald #endif
200*1801b596Smatthias.ringwald 
201*1801b596Smatthias.ringwald 
202*1801b596Smatthias.ringwald // MARK: db_mem_service_t
203*1801b596Smatthias.ringwald #ifdef MAX_NO_DB_MEM_SERVICES
204*1801b596Smatthias.ringwald static db_mem_service_t db_mem_service_storage[MAX_NO_DB_MEM_SERVICES];
205*1801b596Smatthias.ringwald static memory_pool_t db_mem_service_pool;
206*1801b596Smatthias.ringwald void * btstack_memory_db_mem_service_get(void){
207*1801b596Smatthias.ringwald     return memory_pool_get(&db_mem_service_pool);
208*1801b596Smatthias.ringwald }
209*1801b596Smatthias.ringwald void   btstack_memory_db_mem_service_free(void *db_mem_service){
210*1801b596Smatthias.ringwald     memory_pool_free(&db_mem_service_pool, db_mem_service);
211*1801b596Smatthias.ringwald }
212*1801b596Smatthias.ringwald #elif defined(HAVE_MALLOC)
213*1801b596Smatthias.ringwald void * btstack_memory_db_mem_service_get(void){
214*1801b596Smatthias.ringwald     return malloc(sizeof(db_mem_service_t));
215*1801b596Smatthias.ringwald }
216*1801b596Smatthias.ringwald void  btstack_memory_db_mem_service_free(void *db_mem_service){
217*1801b596Smatthias.ringwald     free(db_mem_service);
218*1801b596Smatthias.ringwald }
219*1801b596Smatthias.ringwald #endif
220*1801b596Smatthias.ringwald 
221*1801b596Smatthias.ringwald 
222a3b02b71Smatthias.ringwald // init
223a3b02b71Smatthias.ringwald void btstack_memory_init(void){
224a3b02b71Smatthias.ringwald #ifdef MAX_NO_HCI_CONNECTIONS
225a3b02b71Smatthias.ringwald     memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t));
226a3b02b71Smatthias.ringwald #endif
227a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_SERVICES
228a3b02b71Smatthias.ringwald     memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t));
229a3b02b71Smatthias.ringwald #endif
230a3b02b71Smatthias.ringwald #ifdef MAX_NO_L2CAP_CHANNELS
231a3b02b71Smatthias.ringwald     memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
232a3b02b71Smatthias.ringwald #endif
233a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
234a3b02b71Smatthias.ringwald     memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
235a3b02b71Smatthias.ringwald #endif
236a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_SERVICES
237a3b02b71Smatthias.ringwald     memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
238a3b02b71Smatthias.ringwald #endif
239a3b02b71Smatthias.ringwald #ifdef MAX_NO_RFCOMM_CHANNELS
240a3b02b71Smatthias.ringwald     memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
241a3b02b71Smatthias.ringwald #endif
242*1801b596Smatthias.ringwald #ifdef MAX_NO_DB_MEM_DEVICES
243*1801b596Smatthias.ringwald     memory_pool_create(&db_mem_device_pool, db_mem_device_storage, MAX_NO_DB_MEM_DEVICES, sizeof(db_mem_device_t));
244*1801b596Smatthias.ringwald #endif
245*1801b596Smatthias.ringwald #ifdef MAX_NO_DB_MEM_SERVICES
246*1801b596Smatthias.ringwald     memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t));
247*1801b596Smatthias.ringwald #endif
248a3b02b71Smatthias.ringwald }
249