1#!/usr/bin/env python3 2import os 3import sys 4 5import os 6import sys 7 8copyright = """/* 9 * Copyright (C) 2014 BlueKitchen GmbH 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the copyright holders nor the names of 21 * contributors may be used to endorse or promote products derived 22 * from this software without specific prior written permission. 23 * 4. Any redistribution, use, or modification is done solely for 24 * personal benefit and not for any commercial purpose or for 25 * monetary gain. 26 * 27 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 31 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 35 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 37 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * Please inquire about commercial licensing options at 41 * [email protected] 42 * 43 */ 44""" 45 46hfile_header_begin = """ 47 48/* 49 * btstack_memory.h 50 * 51 * @brief BTstack memory management via configurable memory pools 52 * 53 */ 54 55#ifndef BTSTACK_MEMORY_H 56#define BTSTACK_MEMORY_H 57 58#if defined __cplusplus 59extern "C" { 60#endif 61 62#include "btstack_config.h" 63 64// Core 65#include "hci.h" 66#include "l2cap.h" 67 68// Classic 69#include "classic/avdtp_sink.h" 70#include "classic/avdtp_source.h" 71#include "classic/avrcp.h" 72#include "classic/bnep.h" 73#include "classic/btstack_link_key_db.h" 74#include "classic/btstack_link_key_db_memory.h" 75#include "classic/hfp.h" 76#include "classic/hid_host.h" 77#include "classic/rfcomm.h" 78#include "classic/sdp_server.h" 79 80// BLE 81#ifdef ENABLE_BLE 82#include "ble/gatt_client.h" 83#include "ble/gatt-service/battery_service_client.h" 84#include "ble/sm.h" 85#endif 86 87#ifdef ENABLE_MESH 88#include "mesh/mesh_network.h" 89#include "mesh/mesh_keys.h" 90#include "mesh/mesh_virtual_addresses.h" 91#endif 92 93/* API_START */ 94 95/** 96 * @brief Initializes BTstack memory pools. 97 */ 98void btstack_memory_init(void); 99 100/** 101 * @brief Deinitialize BTstack memory pools 102 * @note if HAVE_MALLOC is defined, all previously allocated buffers are free'd 103 */ 104void btstack_memory_deinit(void); 105 106/* API_END */ 107""" 108 109hfile_header_end = """ 110#if defined __cplusplus 111} 112#endif 113 114#endif // BTSTACK_MEMORY_H 115""" 116 117cfile_header_begin = """ 118#define BTSTACK_FILE__ "btstack_memory.c" 119 120 121/* 122 * btstack_memory.c 123 * 124 * @brief BTstack memory management via configurable memory pools 125 * 126 * @note code generated by tool/btstack_memory_generator.py 127 * @note returnes buffers are initialized with 0 128 * 129 */ 130 131#include "btstack_memory.h" 132#include "btstack_memory_pool.h" 133#include "btstack_debug.h" 134 135#include <stdlib.h> 136 137#ifdef ENABLE_MALLOC_TEST 138extern "C" void * test_malloc(size_t size); 139#define malloc test_malloc 140#endif 141 142#ifdef HAVE_MALLOC 143typedef struct btstack_memory_buffer { 144 struct btstack_memory_buffer * next; 145 struct btstack_memory_buffer * prev; 146} btstack_memory_buffer_t; 147 148typedef struct { 149 btstack_memory_buffer_t tracking; 150 void * pointer; 151} test_buffer_t; 152 153static btstack_memory_buffer_t * btstack_memory_malloc_buffers; 154static uint32_t btstack_memory_malloc_counter; 155 156static void btstack_memory_tracking_add(btstack_memory_buffer_t * buffer){ 157 btstack_assert(buffer != NULL); 158 if (btstack_memory_malloc_buffers != NULL) { 159 // let current first item prev point to new first item 160 btstack_memory_malloc_buffers->prev = buffer; 161 } 162 buffer->prev = NULL; 163 buffer->next = btstack_memory_malloc_buffers; 164 btstack_memory_malloc_buffers = buffer; 165 166 btstack_memory_malloc_counter++; 167} 168 169static void btstack_memory_tracking_remove(btstack_memory_buffer_t * buffer){ 170 btstack_assert(buffer != NULL); 171 if (buffer->prev == NULL){ 172 // first item 173 btstack_memory_malloc_buffers = buffer->next; 174 } else { 175 buffer->prev->next = buffer->next; 176 } 177 if (buffer->next != NULL){ 178 buffer->next->prev = buffer->prev; 179 } 180 181 btstack_memory_malloc_counter--; 182} 183#endif 184 185void btstack_memory_deinit(void){ 186#ifdef HAVE_MALLOC 187 while (btstack_memory_malloc_buffers != NULL){ 188 btstack_memory_buffer_t * buffer = btstack_memory_malloc_buffers; 189 btstack_memory_malloc_buffers = buffer->next; 190 free(buffer); 191 } 192 btstack_assert(btstack_memory_malloc_counter == 0); 193#endif 194} 195""" 196 197header_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void); 198void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);""" 199 200code_template = """ 201// MARK: STRUCT_TYPE 202#if !defined(HAVE_MALLOC) && !defined(POOL_COUNT) 203 #if defined(POOL_COUNT_OLD_NO) 204 #error "Deprecated POOL_COUNT_OLD_NO defined instead of POOL_COUNT. Please update your btstack_config.h to use POOL_COUNT." 205 #else 206 #define POOL_COUNT 0 207 #endif 208#endif 209 210#ifdef POOL_COUNT 211#if POOL_COUNT > 0 212static STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT]; 213static btstack_memory_pool_t STRUCT_NAME_pool; 214STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 215 void * buffer = btstack_memory_pool_get(&STRUCT_NAME_pool); 216 if (buffer){ 217 memset(buffer, 0, sizeof(STRUCT_TYPE)); 218 } 219 return (STRUCT_NAME_t *) buffer; 220} 221void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 222 btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME); 223} 224#else 225STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 226 return NULL; 227} 228void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 229 UNUSED(STRUCT_NAME); 230}; 231#endif 232#elif defined(HAVE_MALLOC) 233 234typedef struct { 235 btstack_memory_buffer_t tracking; 236 STRUCT_NAME_t data; 237} btstack_memory_STRUCT_NAME_t; 238 239STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 240 btstack_memory_STRUCT_NAME_t * buffer = (btstack_memory_STRUCT_NAME_t *) malloc(sizeof(btstack_memory_STRUCT_NAME_t)); 241 if (buffer){ 242 memset(buffer, 0, sizeof(btstack_memory_STRUCT_NAME_t)); 243 btstack_memory_tracking_add(&buffer->tracking); 244 return &buffer->data; 245 } else { 246 return NULL; 247 } 248} 249void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 250 // reconstruct buffer start 251 btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) STRUCT_NAME)[-1]; 252 btstack_memory_tracking_remove(buffer); 253 free(buffer); 254} 255#endif 256""" 257init_header = ''' 258// init 259void btstack_memory_init(void){ 260#ifdef HAVE_MALLOC 261 // assert that there is no unexpected padding for combined buffer 262 btstack_assert(sizeof(test_buffer_t) == sizeof(btstack_memory_buffer_t) + sizeof(void *)); 263#endif 264 265''' 266 267init_template = """#if POOL_COUNT > 0 268 btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE)); 269#endif""" 270 271def writeln(f, data): 272 f.write(data + "\n") 273 274def replacePlaceholder(template, struct_name): 275 struct_type = struct_name + '_t' 276 if struct_name.endswith('try'): 277 pool_count = "MAX_NR_" + struct_name.upper()[:-3] + "TRIES" 278 else: 279 pool_count = "MAX_NR_" + struct_name.upper() + "S" 280 pool_count_old_no = pool_count.replace("MAX_NR_", "MAX_NO_") 281 snippet = template.replace("STRUCT_TYPE", struct_type).replace("STRUCT_NAME", struct_name).replace("POOL_COUNT_OLD_NO", pool_count_old_no).replace("POOL_COUNT", pool_count) 282 return snippet 283 284list_of_structs = [ 285 ["hci_connection"], 286 ["l2cap_service", "l2cap_channel"], 287] 288list_of_classic_structs = [ 289 ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"], 290 ["btstack_link_key_db_memory_entry"], 291 ["bnep_service", "bnep_channel"], 292 ["hfp_connection"], 293 ["hid_host_connection"], 294 ["service_record_item"], 295 ["avdtp_stream_endpoint"], 296 ["avdtp_connection"], 297 ["avrcp_connection"], 298 ["avrcp_browsing_connection"], 299] 300list_of_le_structs = [ 301 ["battery_service_client", "gatt_client", "sm_lookup_entry", "whitelist_entry"], 302] 303list_of_mesh_structs = [ 304 ['mesh_network_pdu', 'mesh_segmented_pdu', 'mesh_upper_transport_pdu', 'mesh_network_key', 'mesh_transport_key', 'mesh_virtual_address', 'mesh_subnet'] 305] 306 307btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..') 308file_name = btstack_root + "/src/btstack_memory" 309print ('Generating %s.[h|c]' % file_name) 310 311f = open(file_name+".h", "w") 312writeln(f, copyright) 313writeln(f, hfile_header_begin) 314for struct_names in list_of_structs: 315 writeln(f, "// "+ ", ".join(struct_names)) 316 for struct_name in struct_names: 317 writeln(f, replacePlaceholder(header_template, struct_name)) 318 writeln(f, "") 319writeln(f, "#ifdef ENABLE_CLASSIC") 320for struct_names in list_of_classic_structs: 321 writeln(f, "// "+ ", ".join(struct_names)) 322 for struct_name in struct_names: 323 writeln(f, replacePlaceholder(header_template, struct_name)) 324 writeln(f, "") 325writeln(f, "#endif") 326writeln(f, "#ifdef ENABLE_BLE") 327for struct_names in list_of_le_structs: 328 writeln(f, "// "+ ", ".join(struct_names)) 329 for struct_name in struct_names: 330 writeln(f, replacePlaceholder(header_template, struct_name)) 331writeln(f, "#endif") 332writeln(f, "#ifdef ENABLE_MESH") 333for struct_names in list_of_mesh_structs: 334 writeln(f, "// "+ ", ".join(struct_names)) 335 for struct_name in struct_names: 336 writeln(f, replacePlaceholder(header_template, struct_name)) 337writeln(f, "#endif") 338writeln(f, hfile_header_end) 339f.close(); 340 341 342f = open(file_name+".c", "w") 343writeln(f, copyright) 344writeln(f, cfile_header_begin) 345for struct_names in list_of_structs: 346 for struct_name in struct_names: 347 writeln(f, replacePlaceholder(code_template, struct_name)) 348 writeln(f, "") 349writeln(f, "#ifdef ENABLE_CLASSIC") 350for struct_names in list_of_classic_structs: 351 for struct_name in struct_names: 352 writeln(f, replacePlaceholder(code_template, struct_name)) 353 writeln(f, "") 354writeln(f, "#endif") 355writeln(f, "#ifdef ENABLE_BLE") 356for struct_names in list_of_le_structs: 357 for struct_name in struct_names: 358 writeln(f, replacePlaceholder(code_template, struct_name)) 359 writeln(f, "") 360writeln(f, "#endif") 361writeln(f, "#ifdef ENABLE_MESH") 362for struct_names in list_of_mesh_structs: 363 for struct_name in struct_names: 364 writeln(f, replacePlaceholder(code_template, struct_name)) 365 writeln(f, "") 366writeln(f, "#endif") 367 368f.write(init_header) 369for struct_names in list_of_structs: 370 for struct_name in struct_names: 371 writeln(f, replacePlaceholder(init_template, struct_name)) 372writeln(f, "#ifdef ENABLE_CLASSIC") 373for struct_names in list_of_classic_structs: 374 for struct_name in struct_names: 375 writeln(f, replacePlaceholder(init_template, struct_name)) 376writeln(f, "#endif") 377writeln(f, "#ifdef ENABLE_BLE") 378for struct_names in list_of_le_structs: 379 for struct_name in struct_names: 380 writeln(f, replacePlaceholder(init_template, struct_name)) 381writeln(f, "#endif") 382writeln(f, "#ifdef ENABLE_MESH") 383for struct_names in list_of_mesh_structs: 384 for struct_name in struct_names: 385 writeln(f, replacePlaceholder(init_template, struct_name)) 386writeln(f, "#endif") 387writeln(f, "}") 388f.close(); 389 390# also generate test code 391test_header = """ 392#include <stdint.h> 393#include <stdio.h> 394#include <stdlib.h> 395#include <string.h> 396 397// malloc hook 398static int simulate_no_memory; 399extern "C" void * test_malloc(size_t size); 400void * test_malloc(size_t size){ 401 if (simulate_no_memory) return NULL; 402 return malloc(size); 403} 404 405#include "btstack_config.h" 406 407#include "CppUTest/TestHarness.h" 408#include "CppUTest/CommandLineTestRunner.h" 409 410#include "bluetooth_data_types.h" 411#include "btstack_util.h" 412#include "btstack_memory.h" 413 414 415TEST_GROUP(btstack_memory){ 416 void setup(void){ 417 btstack_memory_init(); 418 simulate_no_memory = 0; 419 } 420}; 421 422#ifdef HAVE_MALLOC 423TEST(btstack_memory, deinit){ 424 // alloc buffers 1,2,3 425 hci_connection_t * buffer_1 = btstack_memory_hci_connection_get(); 426 hci_connection_t * buffer_2 = btstack_memory_hci_connection_get(); 427 hci_connection_t * buffer_3 = btstack_memory_hci_connection_get(); 428 // free first one in list 429 btstack_memory_hci_connection_free(buffer_3); 430 // free second one in list 431 btstack_memory_hci_connection_free(buffer_1); 432 // leave buffer in list 433 (void) buffer_2; 434 btstack_memory_deinit(); 435} 436#endif 437 438""" 439 440test_template = """ 441 442TEST(btstack_memory, STRUCT_NAME_GetAndFree){ 443 STRUCT_NAME_t * context; 444#ifdef HAVE_MALLOC 445 context = btstack_memory_STRUCT_NAME_get(); 446 CHECK(context != NULL); 447 btstack_memory_STRUCT_NAME_free(context); 448#else 449#ifdef POOL_COUNT 450 // single 451 context = btstack_memory_STRUCT_NAME_get(); 452 CHECK(context != NULL); 453 btstack_memory_STRUCT_NAME_free(context); 454#else 455 // none 456 context = btstack_memory_STRUCT_NAME_get(); 457 CHECK(context == NULL); 458 btstack_memory_STRUCT_NAME_free(context); 459#endif 460#endif 461} 462 463TEST(btstack_memory, STRUCT_NAME_NotEnoughBuffers){ 464 STRUCT_NAME_t * context; 465#ifdef HAVE_MALLOC 466 simulate_no_memory = 1; 467#else 468#ifdef POOL_COUNT 469 int i; 470 // alloc all static buffers 471 for (i = 0; i < POOL_COUNT; i++){ 472 context = btstack_memory_STRUCT_NAME_get(); 473 CHECK(context != NULL); 474 } 475#endif 476#endif 477 // get one more 478 context = btstack_memory_STRUCT_NAME_get(); 479 CHECK(context == NULL); 480} 481""" 482 483test_footer = """ 484int main (int argc, const char * argv[]){ 485 return CommandLineTestRunner::RunAllTests(argc, argv); 486} 487""" 488 489file_name = btstack_root + "/test/btstack_memory/btstack_memory_test.c" 490print ('Generating %s' % file_name) 491 492f = open(file_name, "w") 493writeln(f, copyright) 494writeln(f, test_header) 495for struct_names in list_of_structs: 496 for struct_name in struct_names: 497 writeln(f, replacePlaceholder(test_template, struct_name)) 498writeln(f, "#ifdef ENABLE_CLASSIC") 499for struct_names in list_of_classic_structs: 500 for struct_name in struct_names: 501 writeln(f, replacePlaceholder(test_template, struct_name)) 502writeln(f, "#endif") 503writeln(f, "#ifdef ENABLE_BLE") 504for struct_names in list_of_le_structs: 505 for struct_name in struct_names: 506 writeln(f, replacePlaceholder(test_template, struct_name)) 507writeln(f, "#endif") 508writeln(f, "#ifdef ENABLE_MESH") 509for struct_names in list_of_mesh_structs: 510 for struct_name in struct_names: 511 writeln(f, replacePlaceholder(test_template, struct_name)) 512writeln(f, "#endif") 513writeln(f, test_footer)