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 // MARK: bnepservice_t 248 #ifdef MAX_NO_BNEP_SERVICES 249 #if MAX_NO_BNEP_SERVICES > 0 250 static bnep_service_t bnep_service_storage[MAX_NO_BNEP_SERVICES]; 251 static memory_pool_t bnep_service_pool; 252 bnep_service_t * btstack_memory_bnep_service_get(void){ 253 return memory_pool_get(&bnep_service_pool); 254 } 255 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){ 256 memory_pool_free(&bnep_service_pool, bnep_service); 257 } 258 #else 259 bnep_service_t * btstack_memory_bnep_service_get(void){ 260 return NULL; 261 } 262 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){ 263 }; 264 #endif 265 #elif defined(HAVE_MALLOC) 266 bnep_service_t * btstack_memory_bnep_service_get(void){ 267 return (bnep_service_t*) malloc(sizeof(bnep_service_t)); 268 } 269 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){ 270 free(bnep_service); 271 } 272 #else 273 #error "Neither HAVE_MALLOC nor MAX_NO_BNEP_SERVICES for struct bnep_service is defined. Please, edit the config file." 274 #endif 275 276 277 // MARK: bnep_channel_t 278 #ifdef MAX_NO_BNEP_CHANNELS 279 #if MAX_NO_BNEP_CHANNELS > 0 280 static bnep_channel_t bnep_channel_storage[MAX_NO_BNEP_CHANNELS]; 281 static memory_pool_t bnep_channel_pool; 282 bnep_channel_t * btstack_memory_bnep_channel_get(void){ 283 return memory_pool_get(&bnep_channel_pool); 284 } 285 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){ 286 memory_pool_free(&bnep_channel_pool, bnep_channel); 287 } 288 #else 289 bnep_channel_t * btstack_memory_bnep_channel_get(void){ 290 return NULL; 291 } 292 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){ 293 }; 294 #endif 295 #elif defined(HAVE_MALLOC) 296 bnep_channel_t * btstack_memory_bnep_channel_get(void){ 297 return (bnep_channel_t*) malloc(sizeof(bnep_channel_t)); 298 } 299 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){ 300 free(bnep_channel); 301 } 302 #else 303 #error "Neither HAVE_MALLOC nor MAX_NO_BNEP_CHANNELS for struct bnep_channel is defined. Please, edit the config file." 304 #endif 305 306 307 // MARK: db_mem_device_name_t 308 #ifdef MAX_NO_DB_MEM_DEVICE_NAMES 309 #if MAX_NO_DB_MEM_DEVICE_NAMES > 0 310 static db_mem_device_name_t db_mem_device_name_storage[MAX_NO_DB_MEM_DEVICE_NAMES]; 311 static memory_pool_t db_mem_device_name_pool; 312 db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void){ 313 return memory_pool_get(&db_mem_device_name_pool); 314 } 315 void btstack_memory_db_mem_device_name_free(db_mem_device_name_t *db_mem_device_name){ 316 memory_pool_free(&db_mem_device_name_pool, db_mem_device_name); 317 } 318 #else 319 db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void){ 320 return NULL; 321 } 322 void btstack_memory_db_mem_device_name_free(db_mem_device_name_t * db_mem_device_name){ 323 // silence compiler warning about unused parameter in a portable way 324 (void) db_mem_device_name; 325 }; 326 #endif 327 #elif defined(HAVE_MALLOC) 328 db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void){ 329 return (db_mem_device_name_t*) malloc(sizeof(db_mem_device_name_t)); 330 } 331 void btstack_memory_db_mem_device_name_free(db_mem_device_name_t *db_mem_device_name){ 332 free(db_mem_device_name); 333 } 334 #else 335 #error "Neither HAVE_MALLOC nor MAX_NO_DB_MEM_DEVICE_NAMES for struct db_mem_device_name is defined. Please, edit the config file." 336 #endif 337 338 339 // MARK: db_mem_device_link_key_t 340 #ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS 341 #if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0 342 static db_mem_device_link_key_t db_mem_device_link_key_storage[MAX_NO_DB_MEM_DEVICE_LINK_KEYS]; 343 static memory_pool_t db_mem_device_link_key_pool; 344 db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void){ 345 return memory_pool_get(&db_mem_device_link_key_pool); 346 } 347 void btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key){ 348 memory_pool_free(&db_mem_device_link_key_pool, db_mem_device_link_key); 349 } 350 #else 351 db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void){ 352 return NULL; 353 } 354 void btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key){ 355 // silence compiler warning about unused parameter in a portable way 356 (void) db_mem_device_link_key; 357 }; 358 #endif 359 #elif defined(HAVE_MALLOC) 360 db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void){ 361 return (db_mem_device_link_key_t*) malloc(sizeof(db_mem_device_link_key_t)); 362 } 363 void btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key){ 364 free(db_mem_device_link_key); 365 } 366 #else 367 #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." 368 #endif 369 370 371 // MARK: db_mem_service_t 372 #ifdef MAX_NO_DB_MEM_SERVICES 373 #if MAX_NO_DB_MEM_SERVICES > 0 374 static db_mem_service_t db_mem_service_storage[MAX_NO_DB_MEM_SERVICES]; 375 static memory_pool_t db_mem_service_pool; 376 db_mem_service_t * btstack_memory_db_mem_service_get(void){ 377 return memory_pool_get(&db_mem_service_pool); 378 } 379 void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service){ 380 memory_pool_free(&db_mem_service_pool, db_mem_service); 381 } 382 #else 383 db_mem_service_t * btstack_memory_db_mem_service_get(void){ 384 return NULL; 385 } 386 void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service){ 387 // silence compiler warning about unused parameter in a portable way 388 (void) db_mem_service; 389 }; 390 #endif 391 #elif defined(HAVE_MALLOC) 392 db_mem_service_t * btstack_memory_db_mem_service_get(void){ 393 return (db_mem_service_t*) malloc(sizeof(db_mem_service_t)); 394 } 395 void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service){ 396 free(db_mem_service); 397 } 398 #else 399 #error "Neither HAVE_MALLOC nor MAX_NO_DB_MEM_SERVICES for struct db_mem_service is defined. Please, edit the config file." 400 #endif 401 402 // MARK: gatt_client_t 403 #ifdef HAVE_BLE 404 #ifdef MAX_NO_GATT_CLIENTS 405 #if MAX_NO_GATT_CLIENTS > 0 406 static gatt_client_t gatt_client_storage[MAX_NO_GATT_CLIENTS]; 407 static memory_pool_t gatt_client_pool; 408 gatt_client_t * btstack_memory_gatt_client_get(void){ 409 return memory_pool_get(&gatt_client_pool); 410 } 411 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){ 412 memory_pool_free(&gatt_client_pool, gatt_client); 413 } 414 #else 415 gatt_client_t * btstack_memory_gatt_client_get(void){ 416 return NULL; 417 } 418 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){ 419 // silence compiler warning about unused parameter in a portable way 420 (void) gatt_client; 421 }; 422 #endif 423 #elif defined(HAVE_MALLOC) 424 gatt_client_t * btstack_memory_gatt_client_get(void){ 425 return (gatt_client_t*) malloc(sizeof(gatt_client_t)); 426 } 427 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){ 428 free(gatt_client); 429 } 430 #else 431 #error "Neither HAVE_MALLOC nor MAX_NO_GATT_CLIENTS for struct gatt_client is defined. Please, edit the config file." 432 #endif 433 #endif 434 435 // init 436 void btstack_memory_init(void){ 437 #if MAX_NO_HCI_CONNECTIONS > 0 438 memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t)); 439 #endif 440 #if MAX_NO_L2CAP_SERVICES > 0 441 memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t)); 442 #endif 443 #if MAX_NO_L2CAP_CHANNELS > 0 444 memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t)); 445 #endif 446 #if MAX_NO_RFCOMM_MULTIPLEXERS > 0 447 memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t)); 448 #endif 449 #if MAX_NO_RFCOMM_SERVICES > 0 450 memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t)); 451 #endif 452 #if MAX_NO_RFCOMM_CHANNELS > 0 453 memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t)); 454 #endif 455 #if MAX_NO_DB_MEM_DEVICE_NAMES > 0 456 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)); 457 #endif 458 #if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0 459 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)); 460 #endif 461 #if MAX_NO_DB_MEM_SERVICES > 0 462 memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t)); 463 #endif 464 #ifdef HAVE_BLE 465 #if MAX_NO_GATT_CLIENTS > 0 466 memory_pool_create(&gatt_client_pool, gatt_client_storage, MAX_NO_GATT_CLIENTS, sizeof(gatt_client_t)); 467 #endif 468 #endif 469 } 470 471