xref: /btstack/src/btstack_memory.c (revision 6b64433e6a17b046d431031fa3995f31c29aa188)
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 static hci_connection_t hci_connection_storage[MAX_NO_HCI_CONNECTIONS];
59 static memory_pool_t hci_connection_pool;
60 void * btstack_memory_hci_connection_get(void){
61     return memory_pool_get(&hci_connection_pool);
62 }
63 void   btstack_memory_hci_connection_free(void *hci_connection){
64     memory_pool_free(&hci_connection_pool, hci_connection);
65 }
66 #elif defined(HAVE_MALLOC)
67 void * btstack_memory_hci_connection_get(void){
68     return malloc(sizeof(hci_connection_t));
69 }
70 void  btstack_memory_hci_connection_free(void *hci_connection){
71     free(hci_connection);
72 }
73 #endif
74 
75 
76 // MARK: l2cap_service_t
77 #ifdef MAX_NO_L2CAP_SERVICES
78 static l2cap_service_t l2cap_service_storage[MAX_NO_L2CAP_SERVICES];
79 static memory_pool_t l2cap_service_pool;
80 void * btstack_memory_l2cap_service_get(void){
81     return memory_pool_get(&l2cap_service_pool);
82 }
83 void   btstack_memory_l2cap_service_free(void *l2cap_service){
84     memory_pool_free(&l2cap_service_pool, l2cap_service);
85 }
86 #elif defined(HAVE_MALLOC)
87 void * btstack_memory_l2cap_service_get(void){
88     return malloc(sizeof(l2cap_service_t));
89 }
90 void  btstack_memory_l2cap_service_free(void *l2cap_service){
91     free(l2cap_service);
92 }
93 #endif
94 
95 
96 // MARK: l2cap_channel_t
97 #ifdef MAX_NO_L2CAP_CHANNELS
98 static l2cap_channel_t l2cap_channel_storage[MAX_NO_L2CAP_CHANNELS];
99 static memory_pool_t l2cap_channel_pool;
100 void * btstack_memory_l2cap_channel_get(void){
101     return memory_pool_get(&l2cap_channel_pool);
102 }
103 void   btstack_memory_l2cap_channel_free(void *l2cap_channel){
104     memory_pool_free(&l2cap_channel_pool, l2cap_channel);
105 }
106 #elif defined(HAVE_MALLOC)
107 void * btstack_memory_l2cap_channel_get(void){
108     return malloc(sizeof(l2cap_channel_t));
109 }
110 void  btstack_memory_l2cap_channel_free(void *l2cap_channel){
111     free(l2cap_channel);
112 }
113 #endif
114 
115 
116 // MARK: rfcomm_multiplexer_t
117 #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
118 static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NO_RFCOMM_MULTIPLEXERS];
119 static memory_pool_t rfcomm_multiplexer_pool;
120 void * btstack_memory_rfcomm_multiplexer_get(void){
121     return memory_pool_get(&rfcomm_multiplexer_pool);
122 }
123 void   btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
124     memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer);
125 }
126 #elif defined(HAVE_MALLOC)
127 void * btstack_memory_rfcomm_multiplexer_get(void){
128     return malloc(sizeof(rfcomm_multiplexer_t));
129 }
130 void  btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){
131     free(rfcomm_multiplexer);
132 }
133 #endif
134 
135 
136 // MARK: rfcomm_service_t
137 #ifdef MAX_NO_RFCOMM_SERVICES
138 static rfcomm_service_t rfcomm_service_storage[MAX_NO_RFCOMM_SERVICES];
139 static memory_pool_t rfcomm_service_pool;
140 void * btstack_memory_rfcomm_service_get(void){
141     return memory_pool_get(&rfcomm_service_pool);
142 }
143 void   btstack_memory_rfcomm_service_free(void *rfcomm_service){
144     memory_pool_free(&rfcomm_service_pool, rfcomm_service);
145 }
146 #elif defined(HAVE_MALLOC)
147 void * btstack_memory_rfcomm_service_get(void){
148     return malloc(sizeof(rfcomm_service_t));
149 }
150 void  btstack_memory_rfcomm_service_free(void *rfcomm_service){
151     free(rfcomm_service);
152 }
153 #endif
154 
155 
156 // MARK: rfcomm_channel_t
157 #ifdef MAX_NO_RFCOMM_CHANNELS
158 static rfcomm_channel_t rfcomm_channel_storage[MAX_NO_RFCOMM_CHANNELS];
159 static memory_pool_t rfcomm_channel_pool;
160 void * btstack_memory_rfcomm_channel_get(void){
161     return memory_pool_get(&rfcomm_channel_pool);
162 }
163 void   btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
164     memory_pool_free(&rfcomm_channel_pool, rfcomm_channel);
165 }
166 #elif defined(HAVE_MALLOC)
167 void * btstack_memory_rfcomm_channel_get(void){
168     return malloc(sizeof(rfcomm_channel_t));
169 }
170 void  btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
171     free(rfcomm_channel);
172 }
173 #endif
174 
175 
176 // MARK: db_mem_device_name_t
177 #ifdef MAX_NO_DB_MEM_DEVICE_NAMES
178 static db_mem_device_name_t db_mem_device_name_storage[MAX_NO_DB_MEM_DEVICE_NAMES];
179 static memory_pool_t db_mem_device_name_pool;
180 void * btstack_memory_db_mem_device_name_get(void){
181     return memory_pool_get(&db_mem_device_name_pool);
182 }
183 void   btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
184     memory_pool_free(&db_mem_device_name_pool, db_mem_device_name);
185 }
186 #elif defined(HAVE_MALLOC)
187 void * btstack_memory_db_mem_device_name_get(void){
188     return malloc(sizeof(db_mem_device_name_t));
189 }
190 void  btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
191     free(db_mem_device_name);
192 }
193 #endif
194 
195 
196 // MARK: db_mem_device_link_key_t
197 #ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS
198 static db_mem_device_link_key_t db_mem_device_link_key_storage[MAX_NO_DB_MEM_DEVICE_LINK_KEYS];
199 static memory_pool_t db_mem_device_link_key_pool;
200 void * btstack_memory_db_mem_device_link_key_get(void){
201     return memory_pool_get(&db_mem_device_link_key_pool);
202 }
203 void   btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
204     memory_pool_free(&db_mem_device_link_key_pool, db_mem_device_link_key);
205 }
206 #elif defined(HAVE_MALLOC)
207 void * btstack_memory_db_mem_device_link_key_get(void){
208     return malloc(sizeof(db_mem_device_link_key_t));
209 }
210 void  btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
211     free(db_mem_device_link_key);
212 }
213 #endif
214 
215 
216 // MARK: db_mem_service_t
217 #ifdef MAX_NO_DB_MEM_SERVICES
218 static db_mem_service_t db_mem_service_storage[MAX_NO_DB_MEM_SERVICES];
219 static memory_pool_t db_mem_service_pool;
220 void * btstack_memory_db_mem_service_get(void){
221     return memory_pool_get(&db_mem_service_pool);
222 }
223 void   btstack_memory_db_mem_service_free(void *db_mem_service){
224     memory_pool_free(&db_mem_service_pool, db_mem_service);
225 }
226 #elif defined(HAVE_MALLOC)
227 void * btstack_memory_db_mem_service_get(void){
228     return malloc(sizeof(db_mem_service_t));
229 }
230 void  btstack_memory_db_mem_service_free(void *db_mem_service){
231     free(db_mem_service);
232 }
233 #endif
234 
235 // init
236 void btstack_memory_init(void){
237 #ifdef MAX_NO_HCI_CONNECTIONS
238     memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t));
239 #endif
240 #ifdef MAX_NO_L2CAP_SERVICES
241     memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t));
242 #endif
243 #ifdef MAX_NO_L2CAP_CHANNELS
244     memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t));
245 #endif
246 #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
247     memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t));
248 #endif
249 #ifdef MAX_NO_RFCOMM_SERVICES
250     memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t));
251 #endif
252 #ifdef MAX_NO_RFCOMM_CHANNELS
253     memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
254 #endif
255 #ifdef MAX_NO_DB_MEM_DEVICE_NAMES
256     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));
257 #endif
258 #ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS
259     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));
260 #endif
261 #ifdef MAX_NO_DB_MEM_SERVICES
262     memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t));
263 #endif
264 }
265