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 "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 void * btstack_memory_hci_connection_get(void){ 62 return memory_pool_get(&hci_connection_pool); 63 } 64 void btstack_memory_hci_connection_free(void *hci_connection){ 65 memory_pool_free(&hci_connection_pool, hci_connection); 66 } 67 #else 68 void * btstack_memory_hci_connection_get(void){ 69 return NULL; 70 } 71 void btstack_memory_hci_connection_free(void *hci_connection){ 72 }; 73 #endif 74 #elif defined(HAVE_MALLOC) 75 void * btstack_memory_hci_connection_get(void){ 76 return malloc(sizeof(hci_connection_t)); 77 } 78 void btstack_memory_hci_connection_free(void *hci_connection){ 79 free(hci_connection); 80 } 81 #else 82 #error "The struct hci_connection has neither HAVE_MALLOC nor MAX_NO_HCI_CONNECTIONS 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 void * btstack_memory_l2cap_service_get(void){ 92 return memory_pool_get(&l2cap_service_pool); 93 } 94 void btstack_memory_l2cap_service_free(void *l2cap_service){ 95 memory_pool_free(&l2cap_service_pool, l2cap_service); 96 } 97 #else 98 void * btstack_memory_l2cap_service_get(void){ 99 return NULL; 100 } 101 void btstack_memory_l2cap_service_free(void *l2cap_service){ 102 }; 103 #endif 104 #elif defined(HAVE_MALLOC) 105 void * btstack_memory_l2cap_service_get(void){ 106 return malloc(sizeof(l2cap_service_t)); 107 } 108 void btstack_memory_l2cap_service_free(void *l2cap_service){ 109 free(l2cap_service); 110 } 111 #else 112 #error "The struct l2cap_service has neither HAVE_MALLOC nor MAX_NO_L2CAP_SERVICES 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 void * btstack_memory_l2cap_channel_get(void){ 122 return memory_pool_get(&l2cap_channel_pool); 123 } 124 void btstack_memory_l2cap_channel_free(void *l2cap_channel){ 125 memory_pool_free(&l2cap_channel_pool, l2cap_channel); 126 } 127 #else 128 void * btstack_memory_l2cap_channel_get(void){ 129 return NULL; 130 } 131 void btstack_memory_l2cap_channel_free(void *l2cap_channel){ 132 }; 133 #endif 134 #elif defined(HAVE_MALLOC) 135 void * btstack_memory_l2cap_channel_get(void){ 136 return malloc(sizeof(l2cap_channel_t)); 137 } 138 void btstack_memory_l2cap_channel_free(void *l2cap_channel){ 139 free(l2cap_channel); 140 } 141 #else 142 #error "The struct l2cap_channel has neither HAVE_MALLOC nor MAX_NO_L2CAP_CHANNELS 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 void * btstack_memory_rfcomm_multiplexer_get(void){ 152 return memory_pool_get(&rfcomm_multiplexer_pool); 153 } 154 void btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){ 155 memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer); 156 } 157 #else 158 void * btstack_memory_rfcomm_multiplexer_get(void){ 159 return NULL; 160 } 161 void btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){ 162 }; 163 #endif 164 #elif defined(HAVE_MALLOC) 165 void * btstack_memory_rfcomm_multiplexer_get(void){ 166 return malloc(sizeof(rfcomm_multiplexer_t)); 167 } 168 void btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){ 169 free(rfcomm_multiplexer); 170 } 171 #else 172 #error "The struct rfcomm_multiplexer has neither HAVE_MALLOC nor MAX_NO_RFCOMM_MULTIPLEXERS 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 void * btstack_memory_rfcomm_service_get(void){ 182 return memory_pool_get(&rfcomm_service_pool); 183 } 184 void btstack_memory_rfcomm_service_free(void *rfcomm_service){ 185 memory_pool_free(&rfcomm_service_pool, rfcomm_service); 186 } 187 #else 188 void * btstack_memory_rfcomm_service_get(void){ 189 return NULL; 190 } 191 void btstack_memory_rfcomm_service_free(void *rfcomm_service){ 192 }; 193 #endif 194 #elif defined(HAVE_MALLOC) 195 void * btstack_memory_rfcomm_service_get(void){ 196 return malloc(sizeof(rfcomm_service_t)); 197 } 198 void btstack_memory_rfcomm_service_free(void *rfcomm_service){ 199 free(rfcomm_service); 200 } 201 #else 202 #error "The struct rfcomm_service has neither HAVE_MALLOC nor MAX_NO_RFCOMM_SERVICES 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 void * btstack_memory_rfcomm_channel_get(void){ 212 return memory_pool_get(&rfcomm_channel_pool); 213 } 214 void btstack_memory_rfcomm_channel_free(void *rfcomm_channel){ 215 memory_pool_free(&rfcomm_channel_pool, rfcomm_channel); 216 } 217 #else 218 void * btstack_memory_rfcomm_channel_get(void){ 219 return NULL; 220 } 221 void btstack_memory_rfcomm_channel_free(void *rfcomm_channel){ 222 }; 223 #endif 224 #elif defined(HAVE_MALLOC) 225 void * btstack_memory_rfcomm_channel_get(void){ 226 return malloc(sizeof(rfcomm_channel_t)); 227 } 228 void btstack_memory_rfcomm_channel_free(void *rfcomm_channel){ 229 free(rfcomm_channel); 230 } 231 #else 232 #error "The struct rfcomm_channel has neither HAVE_MALLOC nor MAX_NO_RFCOMM_CHANNELS 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 void * 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(void *db_mem_device_name){ 245 memory_pool_free(&db_mem_device_name_pool, db_mem_device_name); 246 } 247 #else 248 void * btstack_memory_db_mem_device_name_get(void){ 249 return NULL; 250 } 251 void btstack_memory_db_mem_device_name_free(void *db_mem_device_name){ 252 }; 253 #endif 254 #elif defined(HAVE_MALLOC) 255 void * btstack_memory_db_mem_device_name_get(void){ 256 return malloc(sizeof(db_mem_device_name_t)); 257 } 258 void btstack_memory_db_mem_device_name_free(void *db_mem_device_name){ 259 free(db_mem_device_name); 260 } 261 #else 262 #error "The struct db_mem_device_name has neither HAVE_MALLOC nor MAX_NO_DB_MEM_DEVICE_NAMES 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 void * 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(void *db_mem_device_link_key){ 275 memory_pool_free(&db_mem_device_link_key_pool, db_mem_device_link_key); 276 } 277 #else 278 void * btstack_memory_db_mem_device_link_key_get(void){ 279 return NULL; 280 } 281 void btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){ 282 }; 283 #endif 284 #elif defined(HAVE_MALLOC) 285 void * btstack_memory_db_mem_device_link_key_get(void){ 286 return malloc(sizeof(db_mem_device_link_key_t)); 287 } 288 void btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){ 289 free(db_mem_device_link_key); 290 } 291 #else 292 #error "The struct db_mem_device_link_key has neither HAVE_MALLOC nor MAX_NO_DB_MEM_DEVICE_LINK_KEYS 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 void * 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(void *db_mem_service){ 305 memory_pool_free(&db_mem_service_pool, db_mem_service); 306 } 307 #else 308 void * btstack_memory_db_mem_service_get(void){ 309 return NULL; 310 } 311 void btstack_memory_db_mem_service_free(void *db_mem_service){ 312 }; 313 #endif 314 #elif defined(HAVE_MALLOC) 315 void * btstack_memory_db_mem_service_get(void){ 316 return malloc(sizeof(db_mem_service_t)); 317 } 318 void btstack_memory_db_mem_service_free(void *db_mem_service){ 319 free(db_mem_service); 320 } 321 #else 322 #error "The struct db_mem_service has neither HAVE_MALLOC nor MAX_NO_DB_MEM_SERVICES defined. Please, edit the config file." 323 #endif 324 325 // init 326 void btstack_memory_init(void){ 327 #if MAX_NO_HCI_CONNECTIONS > 0 328 memory_pool_create(&hci_connection_pool, hci_connection_storage, MAX_NO_HCI_CONNECTIONS, sizeof(hci_connection_t)); 329 #endif 330 #if MAX_NO_L2CAP_SERVICES > 0 331 memory_pool_create(&l2cap_service_pool, l2cap_service_storage, MAX_NO_L2CAP_SERVICES, sizeof(l2cap_service_t)); 332 #endif 333 #if MAX_NO_L2CAP_CHANNELS > 0 334 memory_pool_create(&l2cap_channel_pool, l2cap_channel_storage, MAX_NO_L2CAP_CHANNELS, sizeof(l2cap_channel_t)); 335 #endif 336 #if MAX_NO_RFCOMM_MULTIPLEXERS > 0 337 memory_pool_create(&rfcomm_multiplexer_pool, rfcomm_multiplexer_storage, MAX_NO_RFCOMM_MULTIPLEXERS, sizeof(rfcomm_multiplexer_t)); 338 #endif 339 #if MAX_NO_RFCOMM_SERVICES > 0 340 memory_pool_create(&rfcomm_service_pool, rfcomm_service_storage, MAX_NO_RFCOMM_SERVICES, sizeof(rfcomm_service_t)); 341 #endif 342 #if MAX_NO_RFCOMM_CHANNELS > 0 343 memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t)); 344 #endif 345 #if MAX_NO_DB_MEM_DEVICE_NAMES > 0 346 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)); 347 #endif 348 #if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0 349 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)); 350 #endif 351 #if MAX_NO_DB_MEM_SERVICES > 0 352 memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t)); 353 #endif 354 } 355 356