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 tools/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 "btstack-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 #if MAX_NO_HCI_CONNECTIONS > 0 59 static hci_connection_t hci_connection_storage[MAX_NO_HCI_CONNECTIONS]; 60 static memory_pool_t hci_connection_pool; 61 hci_connection_t * btstack_memory_hci_connection_get(void){ 62 return memory_pool_get(&hci_connection_pool); 63 } 64 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){ 65 memory_pool_free(&hci_connection_pool, hci_connection); 66 } 67 #else 68 hci_connection_t * btstack_memory_hci_connection_get(void){ 69 return NULL; 70 } 71 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){ 72 // silence compiler warning about unused parameter in a portable way 73 hci_connection; 74 }; 75 #endif 76 #elif defined(HAVE_MALLOC) 77 hci_connection_t * btstack_memory_hci_connection_get(void){ 78 return (hci_connection_t*) malloc(sizeof(hci_connection_t)); 79 } 80 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){ 81 free(hci_connection); 82 } 83 #else 84 #error "Neither HAVE_MALLOC nor MAX_NO_HCI_CONNECTIONS for struct hci_connection is defined. Please, edit the config file." 85 #endif 86 87 88 // MARK: l2cap_service_t 89 #ifdef MAX_NO_L2CAP_SERVICES 90 #if MAX_NO_L2CAP_SERVICES > 0 91 static l2cap_service_t l2cap_service_storage[MAX_NO_L2CAP_SERVICES]; 92 static memory_pool_t l2cap_service_pool; 93 l2cap_service_t * btstack_memory_l2cap_service_get(void){ 94 return memory_pool_get(&l2cap_service_pool); 95 } 96 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){ 97 memory_pool_free(&l2cap_service_pool, l2cap_service); 98 } 99 #else 100 l2cap_service_t * btstack_memory_l2cap_service_get(void){ 101 return NULL; 102 } 103 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){ 104 // silence compiler warning about unused parameter in a portable way 105 (void) l2cap_service; 106 }; 107 #endif 108 #elif defined(HAVE_MALLOC) 109 l2cap_service_t * btstack_memory_l2cap_service_get(void){ 110 return (l2cap_service_t*) malloc(sizeof(l2cap_service_t)); 111 } 112 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){ 113 free(l2cap_service); 114 } 115 #else 116 #error "Neither HAVE_MALLOC nor MAX_NO_L2CAP_SERVICES for struct l2cap_service is defined. Please, edit the config file." 117 #endif 118 119 120 // MARK: l2cap_channel_t 121 #ifdef MAX_NO_L2CAP_CHANNELS 122 #if MAX_NO_L2CAP_CHANNELS > 0 123 static l2cap_channel_t l2cap_channel_storage[MAX_NO_L2CAP_CHANNELS]; 124 static memory_pool_t l2cap_channel_pool; 125 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){ 126 return memory_pool_get(&l2cap_channel_pool); 127 } 128 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){ 129 memory_pool_free(&l2cap_channel_pool, l2cap_channel); 130 } 131 #else 132 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){ 133 return NULL; 134 } 135 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){ 136 // silence compiler warning about unused parameter in a portable way 137 (void) l2cap_channel; 138 }; 139 #endif 140 #elif defined(HAVE_MALLOC) 141 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){ 142 return (l2cap_channel_t*) malloc(sizeof(l2cap_channel_t)); 143 } 144 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){ 145 free(l2cap_channel); 146 } 147 #else 148 #error "Neither HAVE_MALLOC nor MAX_NO_L2CAP_CHANNELS for struct l2cap_channel is defined. Please, edit the config file." 149 #endif 150 151 152 // MARK: rfcomm_multiplexer_t 153 #ifdef MAX_NO_RFCOMM_MULTIPLEXERS 154 #if MAX_NO_RFCOMM_MULTIPLEXERS > 0 155 static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NO_RFCOMM_MULTIPLEXERS]; 156 static memory_pool_t rfcomm_multiplexer_pool; 157 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){ 158 return memory_pool_get(&rfcomm_multiplexer_pool); 159 } 160 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){ 161 memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer); 162 } 163 #else 164 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){ 165 return NULL; 166 } 167 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){ 168 // silence compiler warning about unused parameter in a portable way 169 (void) rfcomm_multiplexer; 170 }; 171 #endif 172 #elif defined(HAVE_MALLOC) 173 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){ 174 return (rfcomm_multiplexer_t*) malloc(sizeof(rfcomm_multiplexer_t)); 175 } 176 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){ 177 free(rfcomm_multiplexer); 178 } 179 #else 180 #error "Neither HAVE_MALLOC nor MAX_NO_RFCOMM_MULTIPLEXERS for struct rfcomm_multiplexer is defined. Please, edit the config file." 181 #endif 182 183 184 // MARK: rfcomm_service_t 185 #ifdef MAX_NO_RFCOMM_SERVICES 186 #if MAX_NO_RFCOMM_SERVICES > 0 187 static rfcomm_service_t rfcomm_service_storage[MAX_NO_RFCOMM_SERVICES]; 188 static memory_pool_t rfcomm_service_pool; 189 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){ 190 return memory_pool_get(&rfcomm_service_pool); 191 } 192 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){ 193 memory_pool_free(&rfcomm_service_pool, rfcomm_service); 194 } 195 #else 196 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){ 197 return NULL; 198 } 199 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){ 200 // silence compiler warning about unused parameter in a portable way 201 (void) rfcomm_service; 202 }; 203 #endif 204 #elif defined(HAVE_MALLOC) 205 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){ 206 return (rfcomm_service_t*) malloc(sizeof(rfcomm_service_t)); 207 } 208 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){ 209 free(rfcomm_service); 210 } 211 #else 212 #error "Neither HAVE_MALLOC nor MAX_NO_RFCOMM_SERVICES for struct rfcomm_service is defined. Please, edit the config file." 213 #endif 214 215 216 // MARK: rfcomm_channel_t 217 #ifdef MAX_NO_RFCOMM_CHANNELS 218 #if MAX_NO_RFCOMM_CHANNELS > 0 219 static rfcomm_channel_t rfcomm_channel_storage[MAX_NO_RFCOMM_CHANNELS]; 220 static memory_pool_t rfcomm_channel_pool; 221 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){ 222 return memory_pool_get(&rfcomm_channel_pool); 223 } 224 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){ 225 memory_pool_free(&rfcomm_channel_pool, rfcomm_channel); 226 } 227 #else 228 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){ 229 return NULL; 230 } 231 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){ 232 // silence compiler warning about unused parameter in a portable way 233 (void) rfcomm_channel; 234 }; 235 #endif 236 #elif defined(HAVE_MALLOC) 237 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){ 238 return (rfcomm_channel_t*) malloc(sizeof(rfcomm_channel_t)); 239 } 240 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){ 241 free(rfcomm_channel); 242 } 243 #else 244 #error "Neither HAVE_MALLOC nor MAX_NO_RFCOMM_CHANNELS for struct rfcomm_channel is defined. Please, edit the config file." 245 #endif 246 247 248 // MARK: db_mem_device_name_t 249 #ifdef MAX_NO_DB_MEM_DEVICE_NAMES 250 #if MAX_NO_DB_MEM_DEVICE_NAMES > 0 251 static db_mem_device_name_t db_mem_device_name_storage[MAX_NO_DB_MEM_DEVICE_NAMES]; 252 static memory_pool_t db_mem_device_name_pool; 253 db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void){ 254 return memory_pool_get(&db_mem_device_name_pool); 255 } 256 void btstack_memory_db_mem_device_name_free(db_mem_device_name_t *db_mem_device_name){ 257 memory_pool_free(&db_mem_device_name_pool, db_mem_device_name); 258 } 259 #else 260 db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void){ 261 return NULL; 262 } 263 void btstack_memory_db_mem_device_name_free(db_mem_device_name_t * db_mem_device_name){ 264 // silence compiler warning about unused parameter in a portable way 265 (void) db_mem_device_name; 266 }; 267 #endif 268 #elif defined(HAVE_MALLOC) 269 db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void){ 270 return (db_mem_device_name_t*) malloc(sizeof(db_mem_device_name_t)); 271 } 272 void btstack_memory_db_mem_device_name_free(db_mem_device_name_t *db_mem_device_name){ 273 free(db_mem_device_name); 274 } 275 #else 276 #error "Neither HAVE_MALLOC nor MAX_NO_DB_MEM_DEVICE_NAMES for struct db_mem_device_name is defined. Please, edit the config file." 277 #endif 278 279 280 // MARK: db_mem_device_link_key_t 281 #ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS 282 #if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0 283 static db_mem_device_link_key_t db_mem_device_link_key_storage[MAX_NO_DB_MEM_DEVICE_LINK_KEYS]; 284 static memory_pool_t db_mem_device_link_key_pool; 285 db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void){ 286 return memory_pool_get(&db_mem_device_link_key_pool); 287 } 288 void btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key){ 289 memory_pool_free(&db_mem_device_link_key_pool, db_mem_device_link_key); 290 } 291 #else 292 db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void){ 293 return NULL; 294 } 295 void btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key){ 296 // silence compiler warning about unused parameter in a portable way 297 (void) db_mem_device_link_key; 298 }; 299 #endif 300 #elif defined(HAVE_MALLOC) 301 db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void){ 302 return (db_mem_device_link_key_t*) malloc(sizeof(db_mem_device_link_key_t)); 303 } 304 void btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key){ 305 free(db_mem_device_link_key); 306 } 307 #else 308 #error "Neither HAVE_MALLOC nor MAX_NO_DB_MEM_DEVICE_LINK_KEYS for struct db_mem_device_link_key is defined. Please, edit the config file." 309 #endif 310 311 312 // MARK: db_mem_service_t 313 #ifdef MAX_NO_DB_MEM_SERVICES 314 #if MAX_NO_DB_MEM_SERVICES > 0 315 static db_mem_service_t db_mem_service_storage[MAX_NO_DB_MEM_SERVICES]; 316 static memory_pool_t db_mem_service_pool; 317 db_mem_service_t * btstack_memory_db_mem_service_get(void){ 318 return memory_pool_get(&db_mem_service_pool); 319 } 320 void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service){ 321 memory_pool_free(&db_mem_service_pool, db_mem_service); 322 } 323 #else 324 db_mem_service_t * btstack_memory_db_mem_service_get(void){ 325 return NULL; 326 } 327 void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service){ 328 // silence compiler warning about unused parameter in a portable way 329 (void) db_mem_service; 330 }; 331 #endif 332 #elif defined(HAVE_MALLOC) 333 db_mem_service_t * btstack_memory_db_mem_service_get(void){ 334 return (db_mem_service_t*) malloc(sizeof(db_mem_service_t)); 335 } 336 void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service){ 337 free(db_mem_service); 338 } 339 #else 340 #error "Neither HAVE_MALLOC nor MAX_NO_DB_MEM_SERVICES for struct db_mem_service is defined. Please, edit the config file." 341 #endif 342 343 // MARK: gatt_client_t 344 #ifdef HAVE_BLE 345 #ifdef MAX_NO_GATT_CLIENTS 346 #if MAX_NO_GATT_CLIENTS > 0 347 static gatt_client_t gatt_client_storage[MAX_NO_GATT_CLIENTS]; 348 static memory_pool_t gatt_client_pool; 349 gatt_client_t * btstack_memory_gatt_client_get(void){ 350 return memory_pool_get(&gatt_client_pool); 351 } 352 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){ 353 memory_pool_free(&gatt_client_pool, gatt_client); 354 } 355 #else 356 gatt_client_t * btstack_memory_gatt_client_get(void){ 357 return NULL; 358 } 359 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){ 360 // silence compiler warning about unused parameter in a portable way 361 (void) gatt_client; 362 }; 363 #endif 364 #elif defined(HAVE_MALLOC) 365 gatt_client_t * btstack_memory_gatt_client_get(void){ 366 return (gatt_client_t*) malloc(sizeof(gatt_client_t)); 367 } 368 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){ 369 free(gatt_client); 370 } 371 #else 372 #error "Neither HAVE_MALLOC nor MAX_NO_GATT_CLIENTS for struct gatt_client is defined. Please, edit the config file." 373 #endif 374 #endif 375 // init 376 void btstack_memory_init(void){ 377 #if MAX_NO_HCI_CONNECTIONS > 0 378 memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t)); 379 #endif 380 #if MAX_NO_L2CAP_SERVICES > 0 381 memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t)); 382 #endif 383 #if MAX_NO_L2CAP_CHANNELS > 0 384 memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t)); 385 #endif 386 #if MAX_NO_RFCOMM_MULTIPLEXERS > 0 387 memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t)); 388 #endif 389 #if MAX_NO_RFCOMM_SERVICES > 0 390 memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t)); 391 #endif 392 #if MAX_NO_RFCOMM_CHANNELS > 0 393 memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t)); 394 #endif 395 #if MAX_NO_DB_MEM_DEVICE_NAMES > 0 396 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)); 397 #endif 398 #if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0 399 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)); 400 #endif 401 #if MAX_NO_DB_MEM_SERVICES > 0 402 memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t)); 403 #endif 404 #ifdef HAVE_BLE 405 #if MAX_NO_GATT_CLIENTS > 0 406 memory_pool_create(&gatt_client_pool, gatt_client_storage, MAX_NO_GATT_CLIENTS, sizeof(gatt_client_t)); 407 #endif 408 #endif 409 } 410 411