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 BLUEKITCHEN 31 * GMBH 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 using 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#ifdef ENABLE_CLASSIC 70#include "classic/avdtp_sink.h" 71#include "classic/avdtp_source.h" 72#include "classic/avrcp.h" 73#include "classic/bnep.h" 74#include "classic/btstack_link_key_db.h" 75#include "classic/btstack_link_key_db_memory.h" 76#include "classic/hfp.h" 77#include "classic/hid_host.h" 78#include "classic/rfcomm.h" 79#include "classic/sdp_server.h" 80#endif 81 82// BLE 83#ifdef ENABLE_BLE 84#include "ble/gatt-service/battery_service_client.h" 85#include "ble/gatt-service/hids_client.h" 86#include "ble/gatt-service/scan_parameters_service_client.h" 87#include "ble/gatt_client.h" 88#include "ble/sm.h" 89#endif 90 91#ifdef ENABLE_MESH 92#include "mesh/mesh_network.h" 93#include "mesh/mesh_keys.h" 94#include "mesh/mesh_virtual_addresses.h" 95#endif 96 97/* API_START */ 98 99/** 100 * @brief Initializes BTstack memory pools. 101 */ 102void btstack_memory_init(void); 103 104/** 105 * @brief Deinitialize BTstack memory pools 106 * @note if HAVE_MALLOC is defined, all previously allocated buffers are free'd 107 */ 108void btstack_memory_deinit(void); 109 110/* API_END */ 111""" 112 113hfile_header_end = """ 114#if defined __cplusplus 115} 116#endif 117 118#endif // BTSTACK_MEMORY_H 119""" 120 121cfile_header_begin = """ 122#define BTSTACK_FILE__ "btstack_memory.c" 123 124 125/* 126 * btstack_memory.c 127 * 128 * @brief BTstack memory management via configurable memory pools 129 * 130 * @note code generated by tool/btstack_memory_generator.py 131 * @note returnes buffers are initialized with 0 132 * 133 */ 134 135#include "btstack_memory.h" 136#include "btstack_memory_pool.h" 137#include "btstack_debug.h" 138 139#include <stdlib.h> 140 141#ifdef ENABLE_MALLOC_TEST 142void * test_malloc(size_t size); 143#define malloc test_malloc 144#endif 145 146#ifdef HAVE_MALLOC 147typedef struct btstack_memory_buffer { 148 struct btstack_memory_buffer * next; 149 struct btstack_memory_buffer * prev; 150} btstack_memory_buffer_t; 151 152typedef struct { 153 btstack_memory_buffer_t tracking; 154 void * pointer; 155} test_buffer_t; 156 157static btstack_memory_buffer_t * btstack_memory_malloc_buffers; 158static uint32_t btstack_memory_malloc_counter; 159 160static void btstack_memory_tracking_add(btstack_memory_buffer_t * buffer){ 161 btstack_assert(buffer != NULL); 162 if (btstack_memory_malloc_buffers != NULL) { 163 // let current first item prev point to new first item 164 btstack_memory_malloc_buffers->prev = buffer; 165 } 166 buffer->prev = NULL; 167 buffer->next = btstack_memory_malloc_buffers; 168 btstack_memory_malloc_buffers = buffer; 169 170 btstack_memory_malloc_counter++; 171} 172 173static void btstack_memory_tracking_remove(btstack_memory_buffer_t * buffer){ 174 btstack_assert(buffer != NULL); 175 if (buffer->prev == NULL){ 176 // first item 177 btstack_memory_malloc_buffers = buffer->next; 178 } else { 179 buffer->prev->next = buffer->next; 180 } 181 if (buffer->next != NULL){ 182 buffer->next->prev = buffer->prev; 183 } 184 185 btstack_memory_malloc_counter--; 186} 187#endif 188 189void btstack_memory_deinit(void){ 190#ifdef HAVE_MALLOC 191 while (btstack_memory_malloc_buffers != NULL){ 192 btstack_memory_buffer_t * buffer = btstack_memory_malloc_buffers; 193 btstack_memory_malloc_buffers = buffer->next; 194 free(buffer); 195 btstack_memory_malloc_counter--; 196 } 197 btstack_assert(btstack_memory_malloc_counter == 0); 198#endif 199} 200""" 201 202header_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void); 203void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);""" 204 205code_template = """ 206// MARK: STRUCT_TYPE 207#if !defined(HAVE_MALLOC) && !defined(POOL_COUNT) 208 #if defined(POOL_COUNT_OLD_NO) 209 #error "Deprecated POOL_COUNT_OLD_NO defined instead of POOL_COUNT. Please update your btstack_config.h to use POOL_COUNT." 210 #else 211 #define POOL_COUNT 0 212 #endif 213#endif 214 215#ifdef POOL_COUNT 216#if POOL_COUNT > 0 217static STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT]; 218static btstack_memory_pool_t STRUCT_NAME_pool; 219STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 220 void * buffer = btstack_memory_pool_get(&STRUCT_NAME_pool); 221 if (buffer){ 222 memset(buffer, 0, sizeof(STRUCT_TYPE)); 223 } 224 return (STRUCT_NAME_t *) buffer; 225} 226void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 227 btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME); 228} 229#else 230STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 231 return NULL; 232} 233void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 234 UNUSED(STRUCT_NAME); 235}; 236#endif 237#elif defined(HAVE_MALLOC) 238 239typedef struct { 240 btstack_memory_buffer_t tracking; 241 STRUCT_NAME_t data; 242} btstack_memory_STRUCT_NAME_t; 243 244STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 245 btstack_memory_STRUCT_NAME_t * buffer = (btstack_memory_STRUCT_NAME_t *) malloc(sizeof(btstack_memory_STRUCT_NAME_t)); 246 if (buffer){ 247 memset(buffer, 0, sizeof(btstack_memory_STRUCT_NAME_t)); 248 btstack_memory_tracking_add(&buffer->tracking); 249 return &buffer->data; 250 } else { 251 return NULL; 252 } 253} 254void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 255 // reconstruct buffer start 256 btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) STRUCT_NAME)[-1]; 257 btstack_memory_tracking_remove(buffer); 258 free(buffer); 259} 260#endif 261""" 262init_header = ''' 263// init 264void btstack_memory_init(void){ 265#ifdef HAVE_MALLOC 266 // assert that there is no unexpected padding for combined buffer 267 btstack_assert(sizeof(test_buffer_t) == sizeof(btstack_memory_buffer_t) + sizeof(void *)); 268#endif 269 270''' 271 272init_template = """#if POOL_COUNT > 0 273 btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE)); 274#endif""" 275 276def writeln(f, data): 277 f.write(data + "\n") 278 279def replacePlaceholder(template, struct_name): 280 struct_type = struct_name + '_t' 281 if struct_name.endswith('try'): 282 pool_count = "MAX_NR_" + struct_name.upper()[:-3] + "TRIES" 283 else: 284 pool_count = "MAX_NR_" + struct_name.upper() + "S" 285 pool_count_old_no = pool_count.replace("MAX_NR_", "MAX_NO_") 286 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) 287 return snippet 288 289list_of_structs = [ 290 ["hci_connection"], 291 ["l2cap_service", "l2cap_channel"], 292] 293list_of_classic_structs = [ 294 ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"], 295 ["btstack_link_key_db_memory_entry"], 296 ["bnep_service", "bnep_channel"], 297 ["hfp_connection"], 298 ["hid_host_connection"], 299 ["service_record_item"], 300 ["avdtp_stream_endpoint"], 301 ["avdtp_connection"], 302 ["avrcp_connection"], 303 ["avrcp_browsing_connection"], 304] 305list_of_le_structs = [ 306 ["battery_service_client", "gatt_client", "hids_client", "scan_parameters_service_client", "sm_lookup_entry", "whitelist_entry", "periodic_advertiser_list_entry"], 307] 308list_of_mesh_structs = [ 309 ['mesh_network_pdu', 'mesh_segmented_pdu', 'mesh_upper_transport_pdu', 'mesh_network_key', 'mesh_transport_key', 'mesh_virtual_address', 'mesh_subnet'] 310] 311 312btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..') 313file_name = btstack_root + "/src/btstack_memory" 314print ('Generating %s.[h|c]' % file_name) 315 316f = open(file_name+".h", "w") 317writeln(f, copyright) 318writeln(f, hfile_header_begin) 319for struct_names in list_of_structs: 320 writeln(f, "// "+ ", ".join(struct_names)) 321 for struct_name in struct_names: 322 writeln(f, replacePlaceholder(header_template, struct_name)) 323 writeln(f, "") 324writeln(f, "#ifdef ENABLE_CLASSIC") 325for struct_names in list_of_classic_structs: 326 writeln(f, "// "+ ", ".join(struct_names)) 327 for struct_name in struct_names: 328 writeln(f, replacePlaceholder(header_template, struct_name)) 329 writeln(f, "") 330writeln(f, "#endif") 331writeln(f, "#ifdef ENABLE_BLE") 332for struct_names in list_of_le_structs: 333 writeln(f, "// "+ ", ".join(struct_names)) 334 for struct_name in struct_names: 335 writeln(f, replacePlaceholder(header_template, struct_name)) 336writeln(f, "#endif") 337writeln(f, "#ifdef ENABLE_MESH") 338for struct_names in list_of_mesh_structs: 339 writeln(f, "// "+ ", ".join(struct_names)) 340 for struct_name in struct_names: 341 writeln(f, replacePlaceholder(header_template, struct_name)) 342writeln(f, "#endif") 343writeln(f, hfile_header_end) 344f.close(); 345 346 347f = open(file_name+".c", "w") 348writeln(f, copyright) 349writeln(f, cfile_header_begin) 350for struct_names in list_of_structs: 351 for struct_name in struct_names: 352 writeln(f, replacePlaceholder(code_template, struct_name)) 353 writeln(f, "") 354writeln(f, "#ifdef ENABLE_CLASSIC") 355for struct_names in list_of_classic_structs: 356 for struct_name in struct_names: 357 writeln(f, replacePlaceholder(code_template, struct_name)) 358 writeln(f, "") 359writeln(f, "#endif") 360writeln(f, "#ifdef ENABLE_BLE") 361for struct_names in list_of_le_structs: 362 for struct_name in struct_names: 363 writeln(f, replacePlaceholder(code_template, struct_name)) 364 writeln(f, "") 365writeln(f, "#endif") 366writeln(f, "#ifdef ENABLE_MESH") 367for struct_names in list_of_mesh_structs: 368 for struct_name in struct_names: 369 writeln(f, replacePlaceholder(code_template, struct_name)) 370 writeln(f, "") 371writeln(f, "#endif") 372 373f.write(init_header) 374for struct_names in list_of_structs: 375 for struct_name in struct_names: 376 writeln(f, replacePlaceholder(init_template, struct_name)) 377writeln(f, "#ifdef ENABLE_CLASSIC") 378for struct_names in list_of_classic_structs: 379 for struct_name in struct_names: 380 writeln(f, replacePlaceholder(init_template, struct_name)) 381writeln(f, "#endif") 382writeln(f, "#ifdef ENABLE_BLE") 383for struct_names in list_of_le_structs: 384 for struct_name in struct_names: 385 writeln(f, replacePlaceholder(init_template, struct_name)) 386writeln(f, "#endif") 387writeln(f, "#ifdef ENABLE_MESH") 388for struct_names in list_of_mesh_structs: 389 for struct_name in struct_names: 390 writeln(f, replacePlaceholder(init_template, struct_name)) 391writeln(f, "#endif") 392writeln(f, "}") 393f.close(); 394 395# also generate test code 396test_header = """ 397#include <stdint.h> 398#include <stdio.h> 399#include <stdlib.h> 400#include <string.h> 401 402// malloc hook 403static int simulate_no_memory; 404extern "C" void * test_malloc(size_t size); 405void * test_malloc(size_t size){ 406 if (simulate_no_memory) return NULL; 407 return malloc(size); 408} 409 410#include "btstack_config.h" 411 412#include "CppUTest/TestHarness.h" 413#include "CppUTest/CommandLineTestRunner.h" 414 415#include "bluetooth_data_types.h" 416#include "btstack_util.h" 417#include "btstack_memory.h" 418 419 420TEST_GROUP(btstack_memory){ 421 void setup(void){ 422 btstack_memory_init(); 423 simulate_no_memory = 0; 424 } 425}; 426 427#ifdef HAVE_MALLOC 428TEST(btstack_memory, deinit){ 429 // alloc buffers 1,2,3 430 hci_connection_t * buffer_1 = btstack_memory_hci_connection_get(); 431 hci_connection_t * buffer_2 = btstack_memory_hci_connection_get(); 432 hci_connection_t * buffer_3 = btstack_memory_hci_connection_get(); 433 // free first one in list 434 btstack_memory_hci_connection_free(buffer_3); 435 // free second one in list 436 btstack_memory_hci_connection_free(buffer_1); 437 // leave buffer in list 438 (void) buffer_2; 439 btstack_memory_deinit(); 440} 441#endif 442 443""" 444 445test_template = """ 446 447TEST(btstack_memory, STRUCT_NAME_GetAndFree){ 448 STRUCT_NAME_t * context; 449#ifdef HAVE_MALLOC 450 context = btstack_memory_STRUCT_NAME_get(); 451 CHECK(context != NULL); 452 btstack_memory_STRUCT_NAME_free(context); 453#else 454#ifdef POOL_COUNT 455 // single 456 context = btstack_memory_STRUCT_NAME_get(); 457 CHECK(context != NULL); 458 btstack_memory_STRUCT_NAME_free(context); 459#else 460 // none 461 context = btstack_memory_STRUCT_NAME_get(); 462 CHECK(context == NULL); 463 btstack_memory_STRUCT_NAME_free(context); 464#endif 465#endif 466} 467 468TEST(btstack_memory, STRUCT_NAME_NotEnoughBuffers){ 469 STRUCT_NAME_t * context; 470#ifdef HAVE_MALLOC 471 simulate_no_memory = 1; 472#else 473#ifdef POOL_COUNT 474 int i; 475 // alloc all static buffers 476 for (i = 0; i < POOL_COUNT; i++){ 477 context = btstack_memory_STRUCT_NAME_get(); 478 CHECK(context != NULL); 479 } 480#endif 481#endif 482 // get one more 483 context = btstack_memory_STRUCT_NAME_get(); 484 CHECK(context == NULL); 485} 486""" 487 488test_footer = """ 489int main (int argc, const char * argv[]){ 490 return CommandLineTestRunner::RunAllTests(argc, argv); 491} 492""" 493 494file_name = btstack_root + "/test/btstack_memory/btstack_memory_test.c" 495print ('Generating %s' % file_name) 496 497f = open(file_name, "w") 498writeln(f, copyright) 499writeln(f, test_header) 500for struct_names in list_of_structs: 501 for struct_name in struct_names: 502 writeln(f, replacePlaceholder(test_template, struct_name)) 503writeln(f, "#ifdef ENABLE_CLASSIC") 504for struct_names in list_of_classic_structs: 505 for struct_name in struct_names: 506 writeln(f, replacePlaceholder(test_template, struct_name)) 507writeln(f, "#endif") 508writeln(f, "#ifdef ENABLE_BLE") 509for struct_names in list_of_le_structs: 510 for struct_name in struct_names: 511 writeln(f, replacePlaceholder(test_template, struct_name)) 512writeln(f, "#endif") 513writeln(f, "#ifdef ENABLE_MESH") 514for struct_names in list_of_mesh_structs: 515 for struct_name in struct_names: 516 writeln(f, replacePlaceholder(test_template, struct_name)) 517writeln(f, "#endif") 518writeln(f, test_footer)