1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 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 BLUEKITCHEN GMBH 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 34 * [email protected] 35 * 36 */ 37 38 39 /* 40 * btstack_memory.h 41 * 42 * @brief BTstack memory management via configurable memory pools 43 * 44 * @note code generated by tool/btstack_memory_generator.py 45 * @note returnes buffers are initialized with 0 46 * 47 */ 48 49 #include "btstack_memory.h" 50 #include "btstack_memory_pool.h" 51 52 #include <stdlib.h> 53 54 55 56 // MARK: hci_connection_t 57 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_HCI_CONNECTIONS) 58 #if defined(MAX_NO_HCI_CONNECTIONS) 59 #error "Deprecated MAX_NO_HCI_CONNECTIONS defined instead of MAX_NR_HCI_CONNECTIONS. Please update your btstack_config.h to use MAX_NR_HCI_CONNECTIONS." 60 #else 61 #define MAX_NR_HCI_CONNECTIONS 0 62 #endif 63 #endif 64 65 #ifdef MAX_NR_HCI_CONNECTIONS 66 #if MAX_NR_HCI_CONNECTIONS > 0 67 static hci_connection_t hci_connection_storage[MAX_NR_HCI_CONNECTIONS]; 68 static btstack_memory_pool_t hci_connection_pool; 69 hci_connection_t * btstack_memory_hci_connection_get(void){ 70 void * buffer = btstack_memory_pool_get(&hci_connection_pool); 71 if (buffer){ 72 memset(buffer, 0, sizeof(hci_connection_t)); 73 } 74 return (hci_connection_t *) buffer; 75 } 76 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){ 77 btstack_memory_pool_free(&hci_connection_pool, hci_connection); 78 } 79 #else 80 hci_connection_t * btstack_memory_hci_connection_get(void){ 81 return NULL; 82 } 83 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){ 84 // silence compiler warning about unused parameter in a portable way 85 (void) hci_connection; 86 }; 87 #endif 88 #elif defined(HAVE_MALLOC) 89 hci_connection_t * btstack_memory_hci_connection_get(void){ 90 void * buffer = malloc(sizeof(hci_connection_t)); 91 if (buffer){ 92 memset(buffer, 0, sizeof(hci_connection_t)); 93 } 94 return (hci_connection_t *) buffer; 95 } 96 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){ 97 free(hci_connection); 98 } 99 #endif 100 101 102 103 // MARK: l2cap_service_t 104 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_L2CAP_SERVICES) 105 #if defined(MAX_NO_L2CAP_SERVICES) 106 #error "Deprecated MAX_NO_L2CAP_SERVICES defined instead of MAX_NR_L2CAP_SERVICES. Please update your btstack_config.h to use MAX_NR_L2CAP_SERVICES." 107 #else 108 #define MAX_NR_L2CAP_SERVICES 0 109 #endif 110 #endif 111 112 #ifdef MAX_NR_L2CAP_SERVICES 113 #if MAX_NR_L2CAP_SERVICES > 0 114 static l2cap_service_t l2cap_service_storage[MAX_NR_L2CAP_SERVICES]; 115 static btstack_memory_pool_t l2cap_service_pool; 116 l2cap_service_t * btstack_memory_l2cap_service_get(void){ 117 void * buffer = btstack_memory_pool_get(&l2cap_service_pool); 118 if (buffer){ 119 memset(buffer, 0, sizeof(l2cap_service_t)); 120 } 121 return (l2cap_service_t *) buffer; 122 } 123 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){ 124 btstack_memory_pool_free(&l2cap_service_pool, l2cap_service); 125 } 126 #else 127 l2cap_service_t * btstack_memory_l2cap_service_get(void){ 128 return NULL; 129 } 130 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){ 131 // silence compiler warning about unused parameter in a portable way 132 (void) l2cap_service; 133 }; 134 #endif 135 #elif defined(HAVE_MALLOC) 136 l2cap_service_t * btstack_memory_l2cap_service_get(void){ 137 void * buffer = malloc(sizeof(l2cap_service_t)); 138 if (buffer){ 139 memset(buffer, 0, sizeof(l2cap_service_t)); 140 } 141 return (l2cap_service_t *) buffer; 142 } 143 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){ 144 free(l2cap_service); 145 } 146 #endif 147 148 149 // MARK: l2cap_channel_t 150 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_L2CAP_CHANNELS) 151 #if defined(MAX_NO_L2CAP_CHANNELS) 152 #error "Deprecated MAX_NO_L2CAP_CHANNELS defined instead of MAX_NR_L2CAP_CHANNELS. Please update your btstack_config.h to use MAX_NR_L2CAP_CHANNELS." 153 #else 154 #define MAX_NR_L2CAP_CHANNELS 0 155 #endif 156 #endif 157 158 #ifdef MAX_NR_L2CAP_CHANNELS 159 #if MAX_NR_L2CAP_CHANNELS > 0 160 static l2cap_channel_t l2cap_channel_storage[MAX_NR_L2CAP_CHANNELS]; 161 static btstack_memory_pool_t l2cap_channel_pool; 162 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){ 163 void * buffer = btstack_memory_pool_get(&l2cap_channel_pool); 164 if (buffer){ 165 memset(buffer, 0, sizeof(l2cap_channel_t)); 166 } 167 return (l2cap_channel_t *) buffer; 168 } 169 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){ 170 btstack_memory_pool_free(&l2cap_channel_pool, l2cap_channel); 171 } 172 #else 173 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){ 174 return NULL; 175 } 176 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){ 177 // silence compiler warning about unused parameter in a portable way 178 (void) l2cap_channel; 179 }; 180 #endif 181 #elif defined(HAVE_MALLOC) 182 l2cap_channel_t * btstack_memory_l2cap_channel_get(void){ 183 void * buffer = malloc(sizeof(l2cap_channel_t)); 184 if (buffer){ 185 memset(buffer, 0, sizeof(l2cap_channel_t)); 186 } 187 return (l2cap_channel_t *) buffer; 188 } 189 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){ 190 free(l2cap_channel); 191 } 192 #endif 193 194 195 196 // MARK: rfcomm_multiplexer_t 197 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_RFCOMM_MULTIPLEXERS) 198 #if defined(MAX_NO_RFCOMM_MULTIPLEXERS) 199 #error "Deprecated MAX_NO_RFCOMM_MULTIPLEXERS defined instead of MAX_NR_RFCOMM_MULTIPLEXERS. Please update your btstack_config.h to use MAX_NR_RFCOMM_MULTIPLEXERS." 200 #else 201 #define MAX_NR_RFCOMM_MULTIPLEXERS 0 202 #endif 203 #endif 204 205 #ifdef MAX_NR_RFCOMM_MULTIPLEXERS 206 #if MAX_NR_RFCOMM_MULTIPLEXERS > 0 207 static rfcomm_multiplexer_t rfcomm_multiplexer_storage[MAX_NR_RFCOMM_MULTIPLEXERS]; 208 static btstack_memory_pool_t rfcomm_multiplexer_pool; 209 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){ 210 void * buffer = btstack_memory_pool_get(&rfcomm_multiplexer_pool); 211 if (buffer){ 212 memset(buffer, 0, sizeof(rfcomm_multiplexer_t)); 213 } 214 return (rfcomm_multiplexer_t *) buffer; 215 } 216 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){ 217 btstack_memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer); 218 } 219 #else 220 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){ 221 return NULL; 222 } 223 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){ 224 // silence compiler warning about unused parameter in a portable way 225 (void) rfcomm_multiplexer; 226 }; 227 #endif 228 #elif defined(HAVE_MALLOC) 229 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void){ 230 void * buffer = malloc(sizeof(rfcomm_multiplexer_t)); 231 if (buffer){ 232 memset(buffer, 0, sizeof(rfcomm_multiplexer_t)); 233 } 234 return (rfcomm_multiplexer_t *) buffer; 235 } 236 void btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer){ 237 free(rfcomm_multiplexer); 238 } 239 #endif 240 241 242 // MARK: rfcomm_service_t 243 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_RFCOMM_SERVICES) 244 #if defined(MAX_NO_RFCOMM_SERVICES) 245 #error "Deprecated MAX_NO_RFCOMM_SERVICES defined instead of MAX_NR_RFCOMM_SERVICES. Please update your btstack_config.h to use MAX_NR_RFCOMM_SERVICES." 246 #else 247 #define MAX_NR_RFCOMM_SERVICES 0 248 #endif 249 #endif 250 251 #ifdef MAX_NR_RFCOMM_SERVICES 252 #if MAX_NR_RFCOMM_SERVICES > 0 253 static rfcomm_service_t rfcomm_service_storage[MAX_NR_RFCOMM_SERVICES]; 254 static btstack_memory_pool_t rfcomm_service_pool; 255 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){ 256 void * buffer = btstack_memory_pool_get(&rfcomm_service_pool); 257 if (buffer){ 258 memset(buffer, 0, sizeof(rfcomm_service_t)); 259 } 260 return (rfcomm_service_t *) buffer; 261 } 262 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){ 263 btstack_memory_pool_free(&rfcomm_service_pool, rfcomm_service); 264 } 265 #else 266 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){ 267 return NULL; 268 } 269 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){ 270 // silence compiler warning about unused parameter in a portable way 271 (void) rfcomm_service; 272 }; 273 #endif 274 #elif defined(HAVE_MALLOC) 275 rfcomm_service_t * btstack_memory_rfcomm_service_get(void){ 276 void * buffer = malloc(sizeof(rfcomm_service_t)); 277 if (buffer){ 278 memset(buffer, 0, sizeof(rfcomm_service_t)); 279 } 280 return (rfcomm_service_t *) buffer; 281 } 282 void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service){ 283 free(rfcomm_service); 284 } 285 #endif 286 287 288 // MARK: rfcomm_channel_t 289 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_RFCOMM_CHANNELS) 290 #if defined(MAX_NO_RFCOMM_CHANNELS) 291 #error "Deprecated MAX_NO_RFCOMM_CHANNELS defined instead of MAX_NR_RFCOMM_CHANNELS. Please update your btstack_config.h to use MAX_NR_RFCOMM_CHANNELS." 292 #else 293 #define MAX_NR_RFCOMM_CHANNELS 0 294 #endif 295 #endif 296 297 #ifdef MAX_NR_RFCOMM_CHANNELS 298 #if MAX_NR_RFCOMM_CHANNELS > 0 299 static rfcomm_channel_t rfcomm_channel_storage[MAX_NR_RFCOMM_CHANNELS]; 300 static btstack_memory_pool_t rfcomm_channel_pool; 301 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){ 302 void * buffer = btstack_memory_pool_get(&rfcomm_channel_pool); 303 if (buffer){ 304 memset(buffer, 0, sizeof(rfcomm_channel_t)); 305 } 306 return (rfcomm_channel_t *) buffer; 307 } 308 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){ 309 btstack_memory_pool_free(&rfcomm_channel_pool, rfcomm_channel); 310 } 311 #else 312 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){ 313 return NULL; 314 } 315 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){ 316 // silence compiler warning about unused parameter in a portable way 317 (void) rfcomm_channel; 318 }; 319 #endif 320 #elif defined(HAVE_MALLOC) 321 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void){ 322 void * buffer = malloc(sizeof(rfcomm_channel_t)); 323 if (buffer){ 324 memset(buffer, 0, sizeof(rfcomm_channel_t)); 325 } 326 return (rfcomm_channel_t *) buffer; 327 } 328 void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){ 329 free(rfcomm_channel); 330 } 331 #endif 332 333 334 335 // MARK: btstack_link_key_db_memory_entry_t 336 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES) 337 #if defined(MAX_NO_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES) 338 #error "Deprecated MAX_NO_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES defined instead of MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES. Please update your btstack_config.h to use MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES." 339 #else 340 #define MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES 0 341 #endif 342 #endif 343 344 #ifdef MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES 345 #if MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES > 0 346 static btstack_link_key_db_memory_entry_t btstack_link_key_db_memory_entry_storage[MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES]; 347 static btstack_memory_pool_t btstack_link_key_db_memory_entry_pool; 348 btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){ 349 void * buffer = btstack_memory_pool_get(&btstack_link_key_db_memory_entry_pool); 350 if (buffer){ 351 memset(buffer, 0, sizeof(btstack_link_key_db_memory_entry_t)); 352 } 353 return (btstack_link_key_db_memory_entry_t *) buffer; 354 } 355 void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){ 356 btstack_memory_pool_free(&btstack_link_key_db_memory_entry_pool, btstack_link_key_db_memory_entry); 357 } 358 #else 359 btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){ 360 return NULL; 361 } 362 void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){ 363 // silence compiler warning about unused parameter in a portable way 364 (void) btstack_link_key_db_memory_entry; 365 }; 366 #endif 367 #elif defined(HAVE_MALLOC) 368 btstack_link_key_db_memory_entry_t * btstack_memory_btstack_link_key_db_memory_entry_get(void){ 369 void * buffer = malloc(sizeof(btstack_link_key_db_memory_entry_t)); 370 if (buffer){ 371 memset(buffer, 0, sizeof(btstack_link_key_db_memory_entry_t)); 372 } 373 return (btstack_link_key_db_memory_entry_t *) buffer; 374 } 375 void btstack_memory_btstack_link_key_db_memory_entry_free(btstack_link_key_db_memory_entry_t *btstack_link_key_db_memory_entry){ 376 free(btstack_link_key_db_memory_entry); 377 } 378 #endif 379 380 381 382 // MARK: bnep_service_t 383 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BNEP_SERVICES) 384 #if defined(MAX_NO_BNEP_SERVICES) 385 #error "Deprecated MAX_NO_BNEP_SERVICES defined instead of MAX_NR_BNEP_SERVICES. Please update your btstack_config.h to use MAX_NR_BNEP_SERVICES." 386 #else 387 #define MAX_NR_BNEP_SERVICES 0 388 #endif 389 #endif 390 391 #ifdef MAX_NR_BNEP_SERVICES 392 #if MAX_NR_BNEP_SERVICES > 0 393 static bnep_service_t bnep_service_storage[MAX_NR_BNEP_SERVICES]; 394 static btstack_memory_pool_t bnep_service_pool; 395 bnep_service_t * btstack_memory_bnep_service_get(void){ 396 void * buffer = btstack_memory_pool_get(&bnep_service_pool); 397 if (buffer){ 398 memset(buffer, 0, sizeof(bnep_service_t)); 399 } 400 return (bnep_service_t *) buffer; 401 } 402 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){ 403 btstack_memory_pool_free(&bnep_service_pool, bnep_service); 404 } 405 #else 406 bnep_service_t * btstack_memory_bnep_service_get(void){ 407 return NULL; 408 } 409 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){ 410 // silence compiler warning about unused parameter in a portable way 411 (void) bnep_service; 412 }; 413 #endif 414 #elif defined(HAVE_MALLOC) 415 bnep_service_t * btstack_memory_bnep_service_get(void){ 416 void * buffer = malloc(sizeof(bnep_service_t)); 417 if (buffer){ 418 memset(buffer, 0, sizeof(bnep_service_t)); 419 } 420 return (bnep_service_t *) buffer; 421 } 422 void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){ 423 free(bnep_service); 424 } 425 #endif 426 427 428 // MARK: bnep_channel_t 429 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_BNEP_CHANNELS) 430 #if defined(MAX_NO_BNEP_CHANNELS) 431 #error "Deprecated MAX_NO_BNEP_CHANNELS defined instead of MAX_NR_BNEP_CHANNELS. Please update your btstack_config.h to use MAX_NR_BNEP_CHANNELS." 432 #else 433 #define MAX_NR_BNEP_CHANNELS 0 434 #endif 435 #endif 436 437 #ifdef MAX_NR_BNEP_CHANNELS 438 #if MAX_NR_BNEP_CHANNELS > 0 439 static bnep_channel_t bnep_channel_storage[MAX_NR_BNEP_CHANNELS]; 440 static btstack_memory_pool_t bnep_channel_pool; 441 bnep_channel_t * btstack_memory_bnep_channel_get(void){ 442 void * buffer = btstack_memory_pool_get(&bnep_channel_pool); 443 if (buffer){ 444 memset(buffer, 0, sizeof(bnep_channel_t)); 445 } 446 return (bnep_channel_t *) buffer; 447 } 448 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){ 449 btstack_memory_pool_free(&bnep_channel_pool, bnep_channel); 450 } 451 #else 452 bnep_channel_t * btstack_memory_bnep_channel_get(void){ 453 return NULL; 454 } 455 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){ 456 // silence compiler warning about unused parameter in a portable way 457 (void) bnep_channel; 458 }; 459 #endif 460 #elif defined(HAVE_MALLOC) 461 bnep_channel_t * btstack_memory_bnep_channel_get(void){ 462 void * buffer = malloc(sizeof(bnep_channel_t)); 463 if (buffer){ 464 memset(buffer, 0, sizeof(bnep_channel_t)); 465 } 466 return (bnep_channel_t *) buffer; 467 } 468 void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){ 469 free(bnep_channel); 470 } 471 #endif 472 473 474 475 // MARK: hfp_connection_t 476 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_HFP_CONNECTIONS) 477 #if defined(MAX_NO_HFP_CONNECTIONS) 478 #error "Deprecated MAX_NO_HFP_CONNECTIONS defined instead of MAX_NR_HFP_CONNECTIONS. Please update your btstack_config.h to use MAX_NR_HFP_CONNECTIONS." 479 #else 480 #define MAX_NR_HFP_CONNECTIONS 0 481 #endif 482 #endif 483 484 #ifdef MAX_NR_HFP_CONNECTIONS 485 #if MAX_NR_HFP_CONNECTIONS > 0 486 static hfp_connection_t hfp_connection_storage[MAX_NR_HFP_CONNECTIONS]; 487 static btstack_memory_pool_t hfp_connection_pool; 488 hfp_connection_t * btstack_memory_hfp_connection_get(void){ 489 void * buffer = btstack_memory_pool_get(&hfp_connection_pool); 490 if (buffer){ 491 memset(buffer, 0, sizeof(hfp_connection_t)); 492 } 493 return (hfp_connection_t *) buffer; 494 } 495 void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){ 496 btstack_memory_pool_free(&hfp_connection_pool, hfp_connection); 497 } 498 #else 499 hfp_connection_t * btstack_memory_hfp_connection_get(void){ 500 return NULL; 501 } 502 void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){ 503 // silence compiler warning about unused parameter in a portable way 504 (void) hfp_connection; 505 }; 506 #endif 507 #elif defined(HAVE_MALLOC) 508 hfp_connection_t * btstack_memory_hfp_connection_get(void){ 509 void * buffer = malloc(sizeof(hfp_connection_t)); 510 if (buffer){ 511 memset(buffer, 0, sizeof(hfp_connection_t)); 512 } 513 return (hfp_connection_t *) buffer; 514 } 515 void btstack_memory_hfp_connection_free(hfp_connection_t *hfp_connection){ 516 free(hfp_connection); 517 } 518 #endif 519 520 521 522 // MARK: service_record_item_t 523 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_SERVICE_RECORD_ITEMS) 524 #if defined(MAX_NO_SERVICE_RECORD_ITEMS) 525 #error "Deprecated MAX_NO_SERVICE_RECORD_ITEMS defined instead of MAX_NR_SERVICE_RECORD_ITEMS. Please update your btstack_config.h to use MAX_NR_SERVICE_RECORD_ITEMS." 526 #else 527 #define MAX_NR_SERVICE_RECORD_ITEMS 0 528 #endif 529 #endif 530 531 #ifdef MAX_NR_SERVICE_RECORD_ITEMS 532 #if MAX_NR_SERVICE_RECORD_ITEMS > 0 533 static service_record_item_t service_record_item_storage[MAX_NR_SERVICE_RECORD_ITEMS]; 534 static btstack_memory_pool_t service_record_item_pool; 535 service_record_item_t * btstack_memory_service_record_item_get(void){ 536 void * buffer = btstack_memory_pool_get(&service_record_item_pool); 537 if (buffer){ 538 memset(buffer, 0, sizeof(service_record_item_t)); 539 } 540 return (service_record_item_t *) buffer; 541 } 542 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){ 543 btstack_memory_pool_free(&service_record_item_pool, service_record_item); 544 } 545 #else 546 service_record_item_t * btstack_memory_service_record_item_get(void){ 547 return NULL; 548 } 549 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){ 550 // silence compiler warning about unused parameter in a portable way 551 (void) service_record_item; 552 }; 553 #endif 554 #elif defined(HAVE_MALLOC) 555 service_record_item_t * btstack_memory_service_record_item_get(void){ 556 void * buffer = malloc(sizeof(service_record_item_t)); 557 if (buffer){ 558 memset(buffer, 0, sizeof(service_record_item_t)); 559 } 560 return (service_record_item_t *) buffer; 561 } 562 void btstack_memory_service_record_item_free(service_record_item_t *service_record_item){ 563 free(service_record_item); 564 } 565 #endif 566 567 568 569 // MARK: avdtp_stream_endpoint_t 570 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVDTP_STREAM_ENDPOINTS) 571 #if defined(MAX_NO_AVDTP_STREAM_ENDPOINTS) 572 #error "Deprecated MAX_NO_AVDTP_STREAM_ENDPOINTS defined instead of MAX_NR_AVDTP_STREAM_ENDPOINTS. Please update your btstack_config.h to use MAX_NR_AVDTP_STREAM_ENDPOINTS." 573 #else 574 #define MAX_NR_AVDTP_STREAM_ENDPOINTS 0 575 #endif 576 #endif 577 578 #ifdef MAX_NR_AVDTP_STREAM_ENDPOINTS 579 #if MAX_NR_AVDTP_STREAM_ENDPOINTS > 0 580 static avdtp_stream_endpoint_t avdtp_stream_endpoint_storage[MAX_NR_AVDTP_STREAM_ENDPOINTS]; 581 static btstack_memory_pool_t avdtp_stream_endpoint_pool; 582 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){ 583 void * buffer = btstack_memory_pool_get(&avdtp_stream_endpoint_pool); 584 if (buffer){ 585 memset(buffer, 0, sizeof(avdtp_stream_endpoint_t)); 586 } 587 return (avdtp_stream_endpoint_t *) buffer; 588 } 589 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){ 590 btstack_memory_pool_free(&avdtp_stream_endpoint_pool, avdtp_stream_endpoint); 591 } 592 #else 593 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){ 594 return NULL; 595 } 596 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){ 597 // silence compiler warning about unused parameter in a portable way 598 (void) avdtp_stream_endpoint; 599 }; 600 #endif 601 #elif defined(HAVE_MALLOC) 602 avdtp_stream_endpoint_t * btstack_memory_avdtp_stream_endpoint_get(void){ 603 void * buffer = malloc(sizeof(avdtp_stream_endpoint_t)); 604 if (buffer){ 605 memset(buffer, 0, sizeof(avdtp_stream_endpoint_t)); 606 } 607 return (avdtp_stream_endpoint_t *) buffer; 608 } 609 void btstack_memory_avdtp_stream_endpoint_free(avdtp_stream_endpoint_t *avdtp_stream_endpoint){ 610 free(avdtp_stream_endpoint); 611 } 612 #endif 613 614 615 616 // MARK: avdtp_connection_t 617 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVDTP_CONNECTIONS) 618 #if defined(MAX_NO_AVDTP_CONNECTIONS) 619 #error "Deprecated MAX_NO_AVDTP_CONNECTIONS defined instead of MAX_NR_AVDTP_CONNECTIONS. Please update your btstack_config.h to use MAX_NR_AVDTP_CONNECTIONS." 620 #else 621 #define MAX_NR_AVDTP_CONNECTIONS 0 622 #endif 623 #endif 624 625 #ifdef MAX_NR_AVDTP_CONNECTIONS 626 #if MAX_NR_AVDTP_CONNECTIONS > 0 627 static avdtp_connection_t avdtp_connection_storage[MAX_NR_AVDTP_CONNECTIONS]; 628 static btstack_memory_pool_t avdtp_connection_pool; 629 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){ 630 void * buffer = btstack_memory_pool_get(&avdtp_connection_pool); 631 if (buffer){ 632 memset(buffer, 0, sizeof(avdtp_connection_t)); 633 } 634 return (avdtp_connection_t *) buffer; 635 } 636 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){ 637 btstack_memory_pool_free(&avdtp_connection_pool, avdtp_connection); 638 } 639 #else 640 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){ 641 return NULL; 642 } 643 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){ 644 // silence compiler warning about unused parameter in a portable way 645 (void) avdtp_connection; 646 }; 647 #endif 648 #elif defined(HAVE_MALLOC) 649 avdtp_connection_t * btstack_memory_avdtp_connection_get(void){ 650 void * buffer = malloc(sizeof(avdtp_connection_t)); 651 if (buffer){ 652 memset(buffer, 0, sizeof(avdtp_connection_t)); 653 } 654 return (avdtp_connection_t *) buffer; 655 } 656 void btstack_memory_avdtp_connection_free(avdtp_connection_t *avdtp_connection){ 657 free(avdtp_connection); 658 } 659 #endif 660 661 662 663 // MARK: avrcp_connection_t 664 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVRCP_CONNECTIONS) 665 #if defined(MAX_NO_AVRCP_CONNECTIONS) 666 #error "Deprecated MAX_NO_AVRCP_CONNECTIONS defined instead of MAX_NR_AVRCP_CONNECTIONS. Please update your btstack_config.h to use MAX_NR_AVRCP_CONNECTIONS." 667 #else 668 #define MAX_NR_AVRCP_CONNECTIONS 0 669 #endif 670 #endif 671 672 #ifdef MAX_NR_AVRCP_CONNECTIONS 673 #if MAX_NR_AVRCP_CONNECTIONS > 0 674 static avrcp_connection_t avrcp_connection_storage[MAX_NR_AVRCP_CONNECTIONS]; 675 static btstack_memory_pool_t avrcp_connection_pool; 676 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){ 677 void * buffer = btstack_memory_pool_get(&avrcp_connection_pool); 678 if (buffer){ 679 memset(buffer, 0, sizeof(avrcp_connection_t)); 680 } 681 return (avrcp_connection_t *) buffer; 682 } 683 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){ 684 btstack_memory_pool_free(&avrcp_connection_pool, avrcp_connection); 685 } 686 #else 687 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){ 688 return NULL; 689 } 690 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){ 691 // silence compiler warning about unused parameter in a portable way 692 (void) avrcp_connection; 693 }; 694 #endif 695 #elif defined(HAVE_MALLOC) 696 avrcp_connection_t * btstack_memory_avrcp_connection_get(void){ 697 void * buffer = malloc(sizeof(avrcp_connection_t)); 698 if (buffer){ 699 memset(buffer, 0, sizeof(avrcp_connection_t)); 700 } 701 return (avrcp_connection_t *) buffer; 702 } 703 void btstack_memory_avrcp_connection_free(avrcp_connection_t *avrcp_connection){ 704 free(avrcp_connection); 705 } 706 #endif 707 708 709 710 // MARK: avrcp_browsing_connection_t 711 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_AVRCP_BROWSING_CONNECTIONS) 712 #if defined(MAX_NO_AVRCP_BROWSING_CONNECTIONS) 713 #error "Deprecated MAX_NO_AVRCP_BROWSING_CONNECTIONS defined instead of MAX_NR_AVRCP_BROWSING_CONNECTIONS. Please update your btstack_config.h to use MAX_NR_AVRCP_BROWSING_CONNECTIONS." 714 #else 715 #define MAX_NR_AVRCP_BROWSING_CONNECTIONS 0 716 #endif 717 #endif 718 719 #ifdef MAX_NR_AVRCP_BROWSING_CONNECTIONS 720 #if MAX_NR_AVRCP_BROWSING_CONNECTIONS > 0 721 static avrcp_browsing_connection_t avrcp_browsing_connection_storage[MAX_NR_AVRCP_BROWSING_CONNECTIONS]; 722 static btstack_memory_pool_t avrcp_browsing_connection_pool; 723 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){ 724 void * buffer = btstack_memory_pool_get(&avrcp_browsing_connection_pool); 725 if (buffer){ 726 memset(buffer, 0, sizeof(avrcp_browsing_connection_t)); 727 } 728 return (avrcp_browsing_connection_t *) buffer; 729 } 730 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){ 731 btstack_memory_pool_free(&avrcp_browsing_connection_pool, avrcp_browsing_connection); 732 } 733 #else 734 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){ 735 return NULL; 736 } 737 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){ 738 // silence compiler warning about unused parameter in a portable way 739 (void) avrcp_browsing_connection; 740 }; 741 #endif 742 #elif defined(HAVE_MALLOC) 743 avrcp_browsing_connection_t * btstack_memory_avrcp_browsing_connection_get(void){ 744 void * buffer = malloc(sizeof(avrcp_browsing_connection_t)); 745 if (buffer){ 746 memset(buffer, 0, sizeof(avrcp_browsing_connection_t)); 747 } 748 return (avrcp_browsing_connection_t *) buffer; 749 } 750 void btstack_memory_avrcp_browsing_connection_free(avrcp_browsing_connection_t *avrcp_browsing_connection){ 751 free(avrcp_browsing_connection); 752 } 753 #endif 754 755 756 #ifdef ENABLE_BLE 757 758 // MARK: gatt_client_t 759 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_GATT_CLIENTS) 760 #if defined(MAX_NO_GATT_CLIENTS) 761 #error "Deprecated MAX_NO_GATT_CLIENTS defined instead of MAX_NR_GATT_CLIENTS. Please update your btstack_config.h to use MAX_NR_GATT_CLIENTS." 762 #else 763 #define MAX_NR_GATT_CLIENTS 0 764 #endif 765 #endif 766 767 #ifdef MAX_NR_GATT_CLIENTS 768 #if MAX_NR_GATT_CLIENTS > 0 769 static gatt_client_t gatt_client_storage[MAX_NR_GATT_CLIENTS]; 770 static btstack_memory_pool_t gatt_client_pool; 771 gatt_client_t * btstack_memory_gatt_client_get(void){ 772 void * buffer = btstack_memory_pool_get(&gatt_client_pool); 773 if (buffer){ 774 memset(buffer, 0, sizeof(gatt_client_t)); 775 } 776 return (gatt_client_t *) buffer; 777 } 778 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){ 779 btstack_memory_pool_free(&gatt_client_pool, gatt_client); 780 } 781 #else 782 gatt_client_t * btstack_memory_gatt_client_get(void){ 783 return NULL; 784 } 785 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){ 786 // silence compiler warning about unused parameter in a portable way 787 (void) gatt_client; 788 }; 789 #endif 790 #elif defined(HAVE_MALLOC) 791 gatt_client_t * btstack_memory_gatt_client_get(void){ 792 void * buffer = malloc(sizeof(gatt_client_t)); 793 if (buffer){ 794 memset(buffer, 0, sizeof(gatt_client_t)); 795 } 796 return (gatt_client_t *) buffer; 797 } 798 void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){ 799 free(gatt_client); 800 } 801 #endif 802 803 804 // MARK: whitelist_entry_t 805 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_WHITELIST_ENTRIES) 806 #if defined(MAX_NO_WHITELIST_ENTRIES) 807 #error "Deprecated MAX_NO_WHITELIST_ENTRIES defined instead of MAX_NR_WHITELIST_ENTRIES. Please update your btstack_config.h to use MAX_NR_WHITELIST_ENTRIES." 808 #else 809 #define MAX_NR_WHITELIST_ENTRIES 0 810 #endif 811 #endif 812 813 #ifdef MAX_NR_WHITELIST_ENTRIES 814 #if MAX_NR_WHITELIST_ENTRIES > 0 815 static whitelist_entry_t whitelist_entry_storage[MAX_NR_WHITELIST_ENTRIES]; 816 static btstack_memory_pool_t whitelist_entry_pool; 817 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){ 818 void * buffer = btstack_memory_pool_get(&whitelist_entry_pool); 819 if (buffer){ 820 memset(buffer, 0, sizeof(whitelist_entry_t)); 821 } 822 return (whitelist_entry_t *) buffer; 823 } 824 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){ 825 btstack_memory_pool_free(&whitelist_entry_pool, whitelist_entry); 826 } 827 #else 828 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){ 829 return NULL; 830 } 831 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){ 832 // silence compiler warning about unused parameter in a portable way 833 (void) whitelist_entry; 834 }; 835 #endif 836 #elif defined(HAVE_MALLOC) 837 whitelist_entry_t * btstack_memory_whitelist_entry_get(void){ 838 void * buffer = malloc(sizeof(whitelist_entry_t)); 839 if (buffer){ 840 memset(buffer, 0, sizeof(whitelist_entry_t)); 841 } 842 return (whitelist_entry_t *) buffer; 843 } 844 void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){ 845 free(whitelist_entry); 846 } 847 #endif 848 849 850 // MARK: sm_lookup_entry_t 851 #if !defined(HAVE_MALLOC) && !defined(MAX_NR_SM_LOOKUP_ENTRIES) 852 #if defined(MAX_NO_SM_LOOKUP_ENTRIES) 853 #error "Deprecated MAX_NO_SM_LOOKUP_ENTRIES defined instead of MAX_NR_SM_LOOKUP_ENTRIES. Please update your btstack_config.h to use MAX_NR_SM_LOOKUP_ENTRIES." 854 #else 855 #define MAX_NR_SM_LOOKUP_ENTRIES 0 856 #endif 857 #endif 858 859 #ifdef MAX_NR_SM_LOOKUP_ENTRIES 860 #if MAX_NR_SM_LOOKUP_ENTRIES > 0 861 static sm_lookup_entry_t sm_lookup_entry_storage[MAX_NR_SM_LOOKUP_ENTRIES]; 862 static btstack_memory_pool_t sm_lookup_entry_pool; 863 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){ 864 void * buffer = btstack_memory_pool_get(&sm_lookup_entry_pool); 865 if (buffer){ 866 memset(buffer, 0, sizeof(sm_lookup_entry_t)); 867 } 868 return (sm_lookup_entry_t *) buffer; 869 } 870 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){ 871 btstack_memory_pool_free(&sm_lookup_entry_pool, sm_lookup_entry); 872 } 873 #else 874 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){ 875 return NULL; 876 } 877 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){ 878 // silence compiler warning about unused parameter in a portable way 879 (void) sm_lookup_entry; 880 }; 881 #endif 882 #elif defined(HAVE_MALLOC) 883 sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void){ 884 void * buffer = malloc(sizeof(sm_lookup_entry_t)); 885 if (buffer){ 886 memset(buffer, 0, sizeof(sm_lookup_entry_t)); 887 } 888 return (sm_lookup_entry_t *) buffer; 889 } 890 void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){ 891 free(sm_lookup_entry); 892 } 893 #endif 894 895 896 #endif 897 // init 898 void btstack_memory_init(void){ 899 #if MAX_NR_HCI_CONNECTIONS > 0 900 btstack_memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NR_HCI_CONNECTIONS, sizeof(hci_connection_t)); 901 #endif 902 #if MAX_NR_L2CAP_SERVICES > 0 903 btstack_memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NR_L2CAP_SERVICES, sizeof(l2cap_service_t)); 904 #endif 905 #if MAX_NR_L2CAP_CHANNELS > 0 906 btstack_memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NR_L2CAP_CHANNELS, sizeof(l2cap_channel_t)); 907 #endif 908 #if MAX_NR_RFCOMM_MULTIPLEXERS > 0 909 btstack_memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NR_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t)); 910 #endif 911 #if MAX_NR_RFCOMM_SERVICES > 0 912 btstack_memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NR_RFCOMM_SERVICES, sizeof(rfcomm_service_t)); 913 #endif 914 #if MAX_NR_RFCOMM_CHANNELS > 0 915 btstack_memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NR_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t)); 916 #endif 917 #if MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES > 0 918 btstack_memory_pool_create(&btstack_link_key_db_memory_entry_pool, btstack_link_key_db_memory_entry_storage, MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES, sizeof(btstack_link_key_db_memory_entry_t)); 919 #endif 920 #if MAX_NR_BNEP_SERVICES > 0 921 btstack_memory_pool_create(&bnep_service_pool, bnep_service_storage, MAX_NR_BNEP_SERVICES, sizeof(bnep_service_t)); 922 #endif 923 #if MAX_NR_BNEP_CHANNELS > 0 924 btstack_memory_pool_create(&bnep_channel_pool, bnep_channel_storage, MAX_NR_BNEP_CHANNELS, sizeof(bnep_channel_t)); 925 #endif 926 #if MAX_NR_HFP_CONNECTIONS > 0 927 btstack_memory_pool_create(&hfp_connection_pool, hfp_connection_storage, MAX_NR_HFP_CONNECTIONS, sizeof(hfp_connection_t)); 928 #endif 929 #if MAX_NR_SERVICE_RECORD_ITEMS > 0 930 btstack_memory_pool_create(&service_record_item_pool, service_record_item_storage, MAX_NR_SERVICE_RECORD_ITEMS, sizeof(service_record_item_t)); 931 #endif 932 #if MAX_NR_AVDTP_STREAM_ENDPOINTS > 0 933 btstack_memory_pool_create(&avdtp_stream_endpoint_pool, avdtp_stream_endpoint_storage, MAX_NR_AVDTP_STREAM_ENDPOINTS, sizeof(avdtp_stream_endpoint_t)); 934 #endif 935 #if MAX_NR_AVDTP_CONNECTIONS > 0 936 btstack_memory_pool_create(&avdtp_connection_pool, avdtp_connection_storage, MAX_NR_AVDTP_CONNECTIONS, sizeof(avdtp_connection_t)); 937 #endif 938 #if MAX_NR_AVRCP_CONNECTIONS > 0 939 btstack_memory_pool_create(&avrcp_connection_pool, avrcp_connection_storage, MAX_NR_AVRCP_CONNECTIONS, sizeof(avrcp_connection_t)); 940 #endif 941 #if MAX_NR_AVRCP_BROWSING_CONNECTIONS > 0 942 btstack_memory_pool_create(&avrcp_browsing_connection_pool, avrcp_browsing_connection_storage, MAX_NR_AVRCP_BROWSING_CONNECTIONS, sizeof(avrcp_browsing_connection_t)); 943 #endif 944 #ifdef ENABLE_BLE 945 #if MAX_NR_GATT_CLIENTS > 0 946 btstack_memory_pool_create(&gatt_client_pool, gatt_client_storage, MAX_NR_GATT_CLIENTS, sizeof(gatt_client_t)); 947 #endif 948 #if MAX_NR_WHITELIST_ENTRIES > 0 949 btstack_memory_pool_create(&whitelist_entry_pool, whitelist_entry_storage, MAX_NR_WHITELIST_ENTRIES, sizeof(whitelist_entry_t)); 950 #endif 951 #if MAX_NR_SM_LOOKUP_ENTRIES > 0 952 btstack_memory_pool_create(&sm_lookup_entry_pool, sm_lookup_entry_storage, MAX_NR_SM_LOOKUP_ENTRIES, sizeof(sm_lookup_entry_t)); 953 #endif 954 #endif 955 } 956