xref: /btstack/tool/btstack_memory_generator.py (revision 42a2bbebe0f5cb756f37d635be1a160ee231a9a2)
15c544019SMatthias Ringwald#!/usr/bin/env python3
2c0a711d9SMatthias Ringwaldimport os
3c0a711d9SMatthias Ringwaldimport sys
41ca3442bSMatthias Ringwald
5a2673d88SMatthias Ringwaldimport os
6a2673d88SMatthias Ringwaldimport sys
7a2673d88SMatthias Ringwald
81ca3442bSMatthias Ringwaldcopyright = """/*
91ca3442bSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH
101ca3442bSMatthias Ringwald *
111ca3442bSMatthias Ringwald * Redistribution and use in source and binary forms, with or without
121ca3442bSMatthias Ringwald * modification, are permitted provided that the following conditions
131ca3442bSMatthias Ringwald * are met:
141ca3442bSMatthias Ringwald *
151ca3442bSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
161ca3442bSMatthias Ringwald *    notice, this list of conditions and the following disclaimer.
171ca3442bSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
181ca3442bSMatthias Ringwald *    notice, this list of conditions and the following disclaimer in the
191ca3442bSMatthias Ringwald *    documentation and/or other materials provided with the distribution.
201ca3442bSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
211ca3442bSMatthias Ringwald *    contributors may be used to endorse or promote products derived
221ca3442bSMatthias Ringwald *    from this software without specific prior written permission.
231ca3442bSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
241ca3442bSMatthias Ringwald *    personal benefit and not for any commercial purpose or for
251ca3442bSMatthias Ringwald *    monetary gain.
261ca3442bSMatthias Ringwald *
271ca3442bSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
281ca3442bSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
291ca3442bSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30154c56ceSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
31154c56ceSMatthias Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
321ca3442bSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
331ca3442bSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
341ca3442bSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
351ca3442bSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
361ca3442bSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
371ca3442bSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
381ca3442bSMatthias Ringwald * SUCH DAMAGE.
391ca3442bSMatthias Ringwald *
401ca3442bSMatthias Ringwald * Please inquire about commercial licensing options at
411ca3442bSMatthias Ringwald * [email protected]
421ca3442bSMatthias Ringwald *
431ca3442bSMatthias Ringwald */
441ca3442bSMatthias Ringwald"""
451ca3442bSMatthias Ringwald
461ca3442bSMatthias Ringwaldhfile_header_begin = """
471ca3442bSMatthias Ringwald
481ca3442bSMatthias Ringwald/*
491ca3442bSMatthias Ringwald * btstack_memory.h
501ca3442bSMatthias Ringwald *
51b45b7749SMilanka Ringwald * @brief BTstack memory management using configurable memory pools
521ca3442bSMatthias Ringwald *
531ca3442bSMatthias Ringwald */
541ca3442bSMatthias Ringwald
5580e33422SMatthias Ringwald#ifndef BTSTACK_MEMORY_H
5680e33422SMatthias Ringwald#define BTSTACK_MEMORY_H
571ca3442bSMatthias Ringwald
581ca3442bSMatthias Ringwald#if defined __cplusplus
591ca3442bSMatthias Ringwaldextern "C" {
601ca3442bSMatthias Ringwald#endif
611ca3442bSMatthias Ringwald
627907f069SMatthias Ringwald#include "btstack_config.h"
631ca3442bSMatthias Ringwald
643edc84c5SMatthias Ringwald// Core
651ca3442bSMatthias Ringwald#include "hci.h"
661ca3442bSMatthias Ringwald#include "l2cap.h"
671ca3442bSMatthias Ringwald
683edc84c5SMatthias Ringwald// Classic
69ef83f5c4SMatthias Ringwald#ifdef ENABLE_CLASSIC
70208d3378SMilanka Ringwald#include "classic/avdtp_sink.h"
71208d3378SMilanka Ringwald#include "classic/avdtp_source.h"
72be32e7f1SMilanka Ringwald#include "classic/avrcp.h"
73f399f7fbSMilanka Ringwald#include "classic/bnep.h"
74f399f7fbSMilanka Ringwald#include "classic/btstack_link_key_db.h"
75f399f7fbSMilanka Ringwald#include "classic/btstack_link_key_db_memory.h"
76b8c00949SMatthias Ringwald#include "classic/goep_server.h"
77f399f7fbSMilanka Ringwald#include "classic/hfp.h"
78f399f7fbSMilanka Ringwald#include "classic/hid_host.h"
79f399f7fbSMilanka Ringwald#include "classic/rfcomm.h"
80f399f7fbSMilanka Ringwald#include "classic/sdp_server.h"
81ef83f5c4SMatthias Ringwald#endif
823edc84c5SMatthias Ringwald
833edc84c5SMatthias Ringwald// BLE
84a9a4c409SMatthias Ringwald#ifdef ENABLE_BLE
856bdecec7SMatthias Ringwald#include "ble/gatt-service/battery_service_client.h"
86cf26c8fbSMilanka Ringwald#include "ble/gatt-service/hids_client.h"
8778ae886bSMilanka Ringwald#include "ble/gatt-service/scan_parameters_service_client.h"
88cf26c8fbSMilanka Ringwald#include "ble/gatt_client.h"
89cf26c8fbSMilanka Ringwald#include "ble/sm.h"
9044c5d856SMatthias Ringwald#endif
9144c5d856SMatthias Ringwald
9244c5d856SMatthias Ringwald#ifdef ENABLE_MESH
9377ba3d3fSMatthias Ringwald#include "mesh/mesh_network.h"
94a5a7b6daSMatthias Ringwald#include "mesh/mesh_keys.h"
95a5a7b6daSMatthias Ringwald#include "mesh/mesh_virtual_addresses.h"
961ca3442bSMatthias Ringwald#endif
971ca3442bSMatthias Ringwald
981ca3442bSMatthias Ringwald/* API_START */
991ca3442bSMatthias Ringwald
1001ca3442bSMatthias Ringwald/**
1011ca3442bSMatthias Ringwald * @brief Initializes BTstack memory pools.
1021ca3442bSMatthias Ringwald */
1031ca3442bSMatthias Ringwaldvoid btstack_memory_init(void);
1041ca3442bSMatthias Ringwald
105b6269742SMatthias Ringwald/**
106b6269742SMatthias Ringwald * @brief Deinitialize BTstack memory pools
107b6269742SMatthias Ringwald * @note if HAVE_MALLOC is defined, all previously allocated buffers are free'd
108b6269742SMatthias Ringwald */
109b6269742SMatthias Ringwaldvoid btstack_memory_deinit(void);
110b6269742SMatthias Ringwald
1111ca3442bSMatthias Ringwald/* API_END */
1121ca3442bSMatthias Ringwald"""
1131ca3442bSMatthias Ringwald
1141ca3442bSMatthias Ringwaldhfile_header_end = """
1151ca3442bSMatthias Ringwald#if defined __cplusplus
1161ca3442bSMatthias Ringwald}
1171ca3442bSMatthias Ringwald#endif
1181ca3442bSMatthias Ringwald
11980e33422SMatthias Ringwald#endif // BTSTACK_MEMORY_H
1201ca3442bSMatthias Ringwald"""
1211ca3442bSMatthias Ringwald
1221ca3442bSMatthias Ringwaldcfile_header_begin = """
1235c544019SMatthias Ringwald#define BTSTACK_FILE__ "btstack_memory.c"
1245c544019SMatthias Ringwald
1255c544019SMatthias Ringwald
1261ca3442bSMatthias Ringwald/*
1275c544019SMatthias Ringwald *  btstack_memory.c
1281ca3442bSMatthias Ringwald *
1291ca3442bSMatthias Ringwald *  @brief BTstack memory management via configurable memory pools
1301ca3442bSMatthias Ringwald *
131a98592bcSMatthias Ringwald *  @note code generated by tool/btstack_memory_generator.py
132a2673d88SMatthias Ringwald *  @note returnes buffers are initialized with 0
1331ca3442bSMatthias Ringwald *
1341ca3442bSMatthias Ringwald */
1351ca3442bSMatthias Ringwald
1361ca3442bSMatthias Ringwald#include "btstack_memory.h"
137d2e6c4b7SMatthias Ringwald#include "btstack_memory_pool.h"
138b6269742SMatthias Ringwald#include "btstack_debug.h"
1391ca3442bSMatthias Ringwald
1401ca3442bSMatthias Ringwald#include <stdlib.h>
1411ca3442bSMatthias Ringwald
142e3c90686SMatthias Ringwald#ifdef ENABLE_MALLOC_TEST
1434490dd1dSMatthias Ringwaldvoid * test_malloc(size_t size);
144e3c90686SMatthias Ringwald#define malloc test_malloc
145e3c90686SMatthias Ringwald#endif
146e3c90686SMatthias Ringwald
147b6269742SMatthias Ringwald#ifdef HAVE_MALLOC
148b6269742SMatthias Ringwaldtypedef struct btstack_memory_buffer {
149b6269742SMatthias Ringwald    struct btstack_memory_buffer * next;
150b6269742SMatthias Ringwald    struct btstack_memory_buffer * prev;
151b6269742SMatthias Ringwald} btstack_memory_buffer_t;
152b6269742SMatthias Ringwald
153798bd46fSMatthias Ringwaldtypedef struct {
154798bd46fSMatthias Ringwald    btstack_memory_buffer_t tracking;
155798bd46fSMatthias Ringwald    void * pointer;
156798bd46fSMatthias Ringwald} test_buffer_t;
157798bd46fSMatthias Ringwald
158b6269742SMatthias Ringwaldstatic btstack_memory_buffer_t * btstack_memory_malloc_buffers;
15919ef97d2SMatthias Ringwaldstatic uint32_t btstack_memory_malloc_counter;
160b6269742SMatthias Ringwald
1612a95308bSMatthias Ringwaldstatic void btstack_memory_tracking_add(btstack_memory_buffer_t * buffer){
1622a95308bSMatthias Ringwald    btstack_assert(buffer != NULL);
16319ef97d2SMatthias Ringwald    if (btstack_memory_malloc_buffers != NULL) {
16419ef97d2SMatthias Ringwald        // let current first item prev point to new first item
16519ef97d2SMatthias Ringwald        btstack_memory_malloc_buffers->prev = buffer;
16619ef97d2SMatthias Ringwald    }
167b6269742SMatthias Ringwald    buffer->prev = NULL;
168b6269742SMatthias Ringwald    buffer->next = btstack_memory_malloc_buffers;
169b6269742SMatthias Ringwald    btstack_memory_malloc_buffers = buffer;
17019ef97d2SMatthias Ringwald
17119ef97d2SMatthias Ringwald    btstack_memory_malloc_counter++;
172b6269742SMatthias Ringwald}
173b6269742SMatthias Ringwald
1742a95308bSMatthias Ringwaldstatic void btstack_memory_tracking_remove(btstack_memory_buffer_t * buffer){
1752a95308bSMatthias Ringwald    btstack_assert(buffer != NULL);
176b6269742SMatthias Ringwald    if (buffer->prev == NULL){
177b6269742SMatthias Ringwald        // first item
178b6269742SMatthias Ringwald        btstack_memory_malloc_buffers = buffer->next;
179b6269742SMatthias Ringwald    } else {
180b6269742SMatthias Ringwald        buffer->prev->next = buffer->next;
181b6269742SMatthias Ringwald    }
182b6269742SMatthias Ringwald    if (buffer->next != NULL){
183b6269742SMatthias Ringwald        buffer->next->prev = buffer->prev;
184b6269742SMatthias Ringwald    }
18519ef97d2SMatthias Ringwald
18619ef97d2SMatthias Ringwald    btstack_memory_malloc_counter--;
187b6269742SMatthias Ringwald}
188b6269742SMatthias Ringwald#endif
189b6269742SMatthias Ringwald
190b6269742SMatthias Ringwaldvoid btstack_memory_deinit(void){
191b6269742SMatthias Ringwald#ifdef HAVE_MALLOC
192b6269742SMatthias Ringwald    while (btstack_memory_malloc_buffers != NULL){
193b6269742SMatthias Ringwald        btstack_memory_buffer_t * buffer = btstack_memory_malloc_buffers;
194b6269742SMatthias Ringwald        btstack_memory_malloc_buffers = buffer->next;
195b6269742SMatthias Ringwald        free(buffer);
196154c56ceSMatthias Ringwald        btstack_memory_malloc_counter--;
197b6269742SMatthias Ringwald    }
19819ef97d2SMatthias Ringwald    btstack_assert(btstack_memory_malloc_counter == 0);
199b6269742SMatthias Ringwald#endif
200b6269742SMatthias Ringwald}
2011ca3442bSMatthias Ringwald"""
2021ca3442bSMatthias Ringwald
2031ca3442bSMatthias Ringwaldheader_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void);
2041ca3442bSMatthias Ringwaldvoid   btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);"""
2051ca3442bSMatthias Ringwald
2061ca3442bSMatthias Ringwaldcode_template = """
2071ca3442bSMatthias Ringwald// MARK: STRUCT_TYPE
208a265b909SMatthias Ringwald#if !defined(HAVE_MALLOC) && !defined(POOL_COUNT)
209a265b909SMatthias Ringwald    #if defined(POOL_COUNT_OLD_NO)
210a265b909SMatthias Ringwald        #error "Deprecated POOL_COUNT_OLD_NO defined instead of POOL_COUNT. Please update your btstack_config.h to use POOL_COUNT."
211a265b909SMatthias Ringwald    #else
212a265b909SMatthias Ringwald        #define POOL_COUNT 0
213a265b909SMatthias Ringwald    #endif
214a265b909SMatthias Ringwald#endif
215a265b909SMatthias Ringwald
2161ca3442bSMatthias Ringwald#ifdef POOL_COUNT
2171ca3442bSMatthias Ringwald#if POOL_COUNT > 0
2181ca3442bSMatthias Ringwaldstatic STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT];
21929d0c4f7SMatthias Ringwaldstatic btstack_memory_pool_t STRUCT_NAME_pool;
2201ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
221a2673d88SMatthias Ringwald    void * buffer = btstack_memory_pool_get(&STRUCT_NAME_pool);
222a2673d88SMatthias Ringwald    if (buffer){
223a2673d88SMatthias Ringwald        memset(buffer, 0, sizeof(STRUCT_TYPE));
224a2673d88SMatthias Ringwald    }
225a2673d88SMatthias Ringwald    return (STRUCT_NAME_t *) buffer;
2261ca3442bSMatthias Ringwald}
2271ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
22829d0c4f7SMatthias Ringwald    btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME);
2291ca3442bSMatthias Ringwald}
2301ca3442bSMatthias Ringwald#else
2311ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
2321ca3442bSMatthias Ringwald    return NULL;
2331ca3442bSMatthias Ringwald}
2341ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
235b6269742SMatthias Ringwald    UNUSED(STRUCT_NAME);
236*42a2bbebSMatthias Ringwald}
2371ca3442bSMatthias Ringwald#endif
2381ca3442bSMatthias Ringwald#elif defined(HAVE_MALLOC)
2392a95308bSMatthias Ringwald
2402a95308bSMatthias Ringwaldtypedef struct {
2412a95308bSMatthias Ringwald    btstack_memory_buffer_t tracking;
242798bd46fSMatthias Ringwald    STRUCT_NAME_t data;
2432a95308bSMatthias Ringwald} btstack_memory_STRUCT_NAME_t;
2442a95308bSMatthias Ringwald
2451ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
2462a95308bSMatthias Ringwald    btstack_memory_STRUCT_NAME_t * buffer = (btstack_memory_STRUCT_NAME_t *) malloc(sizeof(btstack_memory_STRUCT_NAME_t));
247a2673d88SMatthias Ringwald    if (buffer){
248798bd46fSMatthias Ringwald        memset(buffer, 0, sizeof(btstack_memory_STRUCT_NAME_t));
2492a95308bSMatthias Ringwald        btstack_memory_tracking_add(&buffer->tracking);
2502a95308bSMatthias Ringwald        return &buffer->data;
251b6269742SMatthias Ringwald    } else {
252b6269742SMatthias Ringwald        return NULL;
253a2673d88SMatthias Ringwald    }
2541ca3442bSMatthias Ringwald}
2551ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
256798bd46fSMatthias Ringwald    // reconstruct buffer start
257798bd46fSMatthias Ringwald    btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) STRUCT_NAME)[-1];
258798bd46fSMatthias Ringwald    btstack_memory_tracking_remove(buffer);
259b6269742SMatthias Ringwald    free(buffer);
2601ca3442bSMatthias Ringwald}
2611ca3442bSMatthias Ringwald#endif
2621ca3442bSMatthias Ringwald"""
263798bd46fSMatthias Ringwaldinit_header = '''
264798bd46fSMatthias Ringwald// init
265798bd46fSMatthias Ringwaldvoid btstack_memory_init(void){
266798bd46fSMatthias Ringwald#ifdef HAVE_MALLOC
267798bd46fSMatthias Ringwald    // assert that there is no unexpected padding for combined buffer
268798bd46fSMatthias Ringwald    btstack_assert(sizeof(test_buffer_t) == sizeof(btstack_memory_buffer_t) + sizeof(void *));
269798bd46fSMatthias Ringwald#endif
270798bd46fSMatthias Ringwald
271798bd46fSMatthias Ringwald'''
2721ca3442bSMatthias Ringwald
2731ca3442bSMatthias Ringwaldinit_template = """#if POOL_COUNT > 0
27429d0c4f7SMatthias Ringwald    btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE));
2751ca3442bSMatthias Ringwald#endif"""
2761ca3442bSMatthias Ringwald
277b3401248SMatthias Ringwaldlist_of_structs = [
278b3401248SMatthias Ringwald    ["hci_connection"],
279b3401248SMatthias Ringwald    ["l2cap_service", "l2cap_channel"],
28044c5d856SMatthias Ringwald]
28144c5d856SMatthias Ringwaldlist_of_classic_structs = [
282b3401248SMatthias Ringwald    ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"],
2832c455dadSMatthias Ringwald    ["btstack_link_key_db_memory_entry"],
284b3401248SMatthias Ringwald    ["bnep_service", "bnep_channel"],
285b8c00949SMatthias Ringwald    ["goep_server_service", "goep_server_connection"],
286b3401248SMatthias Ringwald    ["hfp_connection"],
287f399f7fbSMilanka Ringwald    ["hid_host_connection"],
28827faf85aSMilanka Ringwald    ["service_record_item"],
28912e7f38cSMilanka Ringwald    ["avdtp_stream_endpoint"],
29091451a2bSMilanka Ringwald    ["avdtp_connection"],
291f12a3722SMilanka Ringwald    ["avrcp_connection"],
292ebb73e1fSMatthias Ringwald    ["avrcp_browsing_connection"],
293b3401248SMatthias Ringwald]
294ebb73e1fSMatthias Ringwaldlist_of_le_structs = [
295dbca66ffSMatthias Ringwald    ["battery_service_client", "gatt_client", "hids_client", "scan_parameters_service_client", "sm_lookup_entry", "whitelist_entry", "periodic_advertiser_list_entry"],
29644c5d856SMatthias Ringwald]
29744c5d856SMatthias Ringwaldlist_of_mesh_structs = [
298039cbf1dSMatthias Ringwald    ['mesh_network_pdu', 'mesh_segmented_pdu', 'mesh_upper_transport_pdu', 'mesh_network_key', 'mesh_transport_key', 'mesh_virtual_address', 'mesh_subnet']
299ebb73e1fSMatthias Ringwald]
300b6ac006aSMatthias Ringwaldlist_of_iso_structs = [
301b6ac006aSMatthias Ringwald    ['hci_iso_stream']
302b6ac006aSMatthias Ringwald]
3031ca3442bSMatthias Ringwald
3042281ada7SMatthias Ringwalddef writeln(f, data):
3052281ada7SMatthias Ringwald    f.write(data + "\n")
3062281ada7SMatthias Ringwald
3072281ada7SMatthias Ringwalddef replacePlaceholder(template, struct_name):
3082281ada7SMatthias Ringwald    struct_type = struct_name + '_t'
3092281ada7SMatthias Ringwald    if struct_name.endswith('try'):
3102281ada7SMatthias Ringwald        pool_count = "MAX_NR_" + struct_name.upper()[:-3] + "TRIES"
3112281ada7SMatthias Ringwald    else:
3122281ada7SMatthias Ringwald        pool_count = "MAX_NR_" + struct_name.upper() + "S"
3132281ada7SMatthias Ringwald    pool_count_old_no = pool_count.replace("MAX_NR_", "MAX_NO_")
3142281ada7SMatthias Ringwald    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)
3152281ada7SMatthias Ringwald    return snippet
3162281ada7SMatthias Ringwald
3172281ada7SMatthias Ringwalddef add_struct(f, guard, template, structs):
3182281ada7SMatthias Ringwald    if not guard == "":
3192281ada7SMatthias Ringwald        writeln(f, "#ifdef " + guard)
3202281ada7SMatthias Ringwald    for struct_names in structs:
3212281ada7SMatthias Ringwald        # writeln(f, "// "+ ", ".join(struct_names))
3222281ada7SMatthias Ringwald        for struct_name in struct_names:
3232281ada7SMatthias Ringwald            writeln(f, replacePlaceholder(template, struct_name))
3242281ada7SMatthias Ringwald        writeln(f, "")
3252281ada7SMatthias Ringwald    if not guard == "":
3262281ada7SMatthias Ringwald        writeln(f, "#endif")
3272281ada7SMatthias Ringwald
3282281ada7SMatthias Ringwalddef add_structs(f, template):
3292281ada7SMatthias Ringwald    add_struct(f, "",                               template, list_of_structs)
3302281ada7SMatthias Ringwald    add_struct(f, "ENABLE_CLASSIC",                 template, list_of_classic_structs)
3312281ada7SMatthias Ringwald    add_struct(f, "ENABLE_BLE",                     template, list_of_le_structs)
3322281ada7SMatthias Ringwald    add_struct(f, "ENABLE_MESH",                    template, list_of_mesh_structs)
333b6ac006aSMatthias Ringwald    add_struct(f, "ENABLE_LE_ISOCHRONOUS_STREAMS",  template, list_of_iso_structs)
3342281ada7SMatthias Ringwald
335a2673d88SMatthias Ringwaldbtstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
336a2673d88SMatthias Ringwaldfile_name = btstack_root + "/src/btstack_memory"
337a2673d88SMatthias Ringwaldprint ('Generating %s.[h|c]' % file_name)
3381ca3442bSMatthias Ringwald
3391ca3442bSMatthias Ringwaldf = open(file_name+".h", "w")
3401ca3442bSMatthias Ringwaldwriteln(f, copyright)
3411ca3442bSMatthias Ringwaldwriteln(f, hfile_header_begin)
3422281ada7SMatthias Ringwaldadd_structs(f, header_template)
3431ca3442bSMatthias Ringwaldwriteln(f, hfile_header_end)
3441ca3442bSMatthias Ringwaldf.close();
3451ca3442bSMatthias Ringwald
3461ca3442bSMatthias Ringwald
3471ca3442bSMatthias Ringwaldf = open(file_name+".c", "w")
3481ca3442bSMatthias Ringwaldwriteln(f, copyright)
3491ca3442bSMatthias Ringwaldwriteln(f, cfile_header_begin)
3502281ada7SMatthias Ringwaldadd_structs(f, code_template)
3511ca3442bSMatthias Ringwald
352798bd46fSMatthias Ringwaldf.write(init_header)
3532281ada7SMatthias Ringwaldadd_structs(f, init_template)
3541ca3442bSMatthias Ringwaldwriteln(f, "}")
3551ca3442bSMatthias Ringwaldf.close();
3561ca3442bSMatthias Ringwald
3576d34f98eSMatthias Ringwald# also generate test code
3586d34f98eSMatthias Ringwaldtest_header = """
3596d34f98eSMatthias Ringwald#include <stdint.h>
3606d34f98eSMatthias Ringwald#include <stdio.h>
3616d34f98eSMatthias Ringwald#include <stdlib.h>
3626d34f98eSMatthias Ringwald#include <string.h>
3636d34f98eSMatthias Ringwald
3646d34f98eSMatthias Ringwald// malloc hook
3656d34f98eSMatthias Ringwaldstatic int simulate_no_memory;
3666d34f98eSMatthias Ringwaldextern "C" void * test_malloc(size_t size);
3676d34f98eSMatthias Ringwaldvoid * test_malloc(size_t size){
3686d34f98eSMatthias Ringwald    if (simulate_no_memory) return NULL;
3696d34f98eSMatthias Ringwald    return malloc(size);
3706d34f98eSMatthias Ringwald}
3716d34f98eSMatthias Ringwald
3726d34f98eSMatthias Ringwald#include "btstack_config.h"
3736d34f98eSMatthias Ringwald
3746d34f98eSMatthias Ringwald#include "CppUTest/TestHarness.h"
3756d34f98eSMatthias Ringwald#include "CppUTest/CommandLineTestRunner.h"
3766d34f98eSMatthias Ringwald
3776d34f98eSMatthias Ringwald#include "bluetooth_data_types.h"
3786d34f98eSMatthias Ringwald#include "btstack_util.h"
3796d34f98eSMatthias Ringwald#include "btstack_memory.h"
3806d34f98eSMatthias Ringwald
3816d34f98eSMatthias Ringwald
3826d34f98eSMatthias RingwaldTEST_GROUP(btstack_memory){
3836d34f98eSMatthias Ringwald    void setup(void){
3846d34f98eSMatthias Ringwald        btstack_memory_init();
3856d34f98eSMatthias Ringwald        simulate_no_memory = 0;
3866d34f98eSMatthias Ringwald    }
3876d34f98eSMatthias Ringwald};
3886d34f98eSMatthias Ringwald
3896d34f98eSMatthias Ringwald#ifdef HAVE_MALLOC
3906d34f98eSMatthias RingwaldTEST(btstack_memory, deinit){
3916d34f98eSMatthias Ringwald    // alloc buffers 1,2,3
3926d34f98eSMatthias Ringwald    hci_connection_t * buffer_1 = btstack_memory_hci_connection_get();
3936d34f98eSMatthias Ringwald    hci_connection_t * buffer_2 = btstack_memory_hci_connection_get();
3946d34f98eSMatthias Ringwald    hci_connection_t * buffer_3 = btstack_memory_hci_connection_get();
3956d34f98eSMatthias Ringwald    // free first one in list
3966d34f98eSMatthias Ringwald    btstack_memory_hci_connection_free(buffer_3);
3976d34f98eSMatthias Ringwald    // free second one in list
3986d34f98eSMatthias Ringwald    btstack_memory_hci_connection_free(buffer_1);
3996d34f98eSMatthias Ringwald    // leave buffer in list
4006d34f98eSMatthias Ringwald    (void) buffer_2;
4016d34f98eSMatthias Ringwald    btstack_memory_deinit();
4026d34f98eSMatthias Ringwald}
4036d34f98eSMatthias Ringwald#endif
4046d34f98eSMatthias Ringwald
4056d34f98eSMatthias Ringwald"""
4066d34f98eSMatthias Ringwald
4076d34f98eSMatthias Ringwaldtest_template = """
4086d34f98eSMatthias Ringwald
4096d34f98eSMatthias RingwaldTEST(btstack_memory, STRUCT_NAME_GetAndFree){
4106d34f98eSMatthias Ringwald    STRUCT_NAME_t * context;
4116d34f98eSMatthias Ringwald#ifdef HAVE_MALLOC
4126d34f98eSMatthias Ringwald    context = btstack_memory_STRUCT_NAME_get();
4136d34f98eSMatthias Ringwald    CHECK(context != NULL);
4146d34f98eSMatthias Ringwald    btstack_memory_STRUCT_NAME_free(context);
4156d34f98eSMatthias Ringwald#else
4166d34f98eSMatthias Ringwald#ifdef POOL_COUNT
4176d34f98eSMatthias Ringwald    // single
4186d34f98eSMatthias Ringwald    context = btstack_memory_STRUCT_NAME_get();
4196d34f98eSMatthias Ringwald    CHECK(context != NULL);
4206d34f98eSMatthias Ringwald    btstack_memory_STRUCT_NAME_free(context);
4216d34f98eSMatthias Ringwald#else
4226d34f98eSMatthias Ringwald    // none
4236d34f98eSMatthias Ringwald    context = btstack_memory_STRUCT_NAME_get();
4246d34f98eSMatthias Ringwald    CHECK(context == NULL);
4256d34f98eSMatthias Ringwald    btstack_memory_STRUCT_NAME_free(context);
4266d34f98eSMatthias Ringwald#endif
4276d34f98eSMatthias Ringwald#endif
4286d34f98eSMatthias Ringwald}
4296d34f98eSMatthias Ringwald
4306d34f98eSMatthias RingwaldTEST(btstack_memory, STRUCT_NAME_NotEnoughBuffers){
4316d34f98eSMatthias Ringwald    STRUCT_NAME_t * context;
4326d34f98eSMatthias Ringwald#ifdef HAVE_MALLOC
4336d34f98eSMatthias Ringwald    simulate_no_memory = 1;
4346d34f98eSMatthias Ringwald#else
4356d34f98eSMatthias Ringwald#ifdef POOL_COUNT
4366d34f98eSMatthias Ringwald    int i;
4376d34f98eSMatthias Ringwald    // alloc all static buffers
4386d34f98eSMatthias Ringwald    for (i = 0; i < POOL_COUNT; i++){
4396d34f98eSMatthias Ringwald        context = btstack_memory_STRUCT_NAME_get();
4406d34f98eSMatthias Ringwald        CHECK(context != NULL);
4416d34f98eSMatthias Ringwald    }
4426d34f98eSMatthias Ringwald#endif
4436d34f98eSMatthias Ringwald#endif
4446d34f98eSMatthias Ringwald    // get one more
4456d34f98eSMatthias Ringwald    context = btstack_memory_STRUCT_NAME_get();
4466d34f98eSMatthias Ringwald    CHECK(context == NULL);
4476d34f98eSMatthias Ringwald}
4486d34f98eSMatthias Ringwald"""
4496d34f98eSMatthias Ringwald
4506d34f98eSMatthias Ringwaldtest_footer = """
4516d34f98eSMatthias Ringwaldint main (int argc, const char * argv[]){
4526d34f98eSMatthias Ringwald    return CommandLineTestRunner::RunAllTests(argc, argv);
4536d34f98eSMatthias Ringwald}
4546d34f98eSMatthias Ringwald"""
4556d34f98eSMatthias Ringwald
4562281ada7SMatthias Ringwaldfile_name = btstack_root + "/test/btstack_memory/btstack_memory_test.cpp"
4576d34f98eSMatthias Ringwaldprint ('Generating %s' % file_name)
4586d34f98eSMatthias Ringwald
4596d34f98eSMatthias Ringwaldf = open(file_name, "w")
4606d34f98eSMatthias Ringwaldwriteln(f, copyright)
4616d34f98eSMatthias Ringwaldwriteln(f, test_header)
4622281ada7SMatthias Ringwaldadd_structs(f, test_template)
4636d34f98eSMatthias Ringwaldwriteln(f, test_footer)
464