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