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