xref: /btstack/tool/btstack_memory_generator.py (revision e3c90686fb8588efb93680a309ab21b5e1b1f9a1)
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
301ca3442bSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
311ca3442bSMatthias Ringwald * RINGWALD 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 *
511ca3442bSMatthias Ringwald *  @brief BTstack memory management via 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
69208d3378SMilanka Ringwald#include "classic/avdtp_sink.h"
70208d3378SMilanka Ringwald#include "classic/avdtp_source.h"
71be32e7f1SMilanka Ringwald#include "classic/avrcp.h"
72f399f7fbSMilanka Ringwald#include "classic/bnep.h"
73f399f7fbSMilanka Ringwald#include "classic/btstack_link_key_db.h"
74f399f7fbSMilanka Ringwald#include "classic/btstack_link_key_db_memory.h"
75f399f7fbSMilanka Ringwald#include "classic/hfp.h"
76f399f7fbSMilanka Ringwald#include "classic/hid_host.h"
77f399f7fbSMilanka Ringwald#include "classic/rfcomm.h"
78f399f7fbSMilanka Ringwald#include "classic/sdp_server.h"
793edc84c5SMatthias Ringwald
803edc84c5SMatthias Ringwald// BLE
81a9a4c409SMatthias Ringwald#ifdef ENABLE_BLE
823edc84c5SMatthias Ringwald#include "ble/gatt_client.h"
836bdecec7SMatthias Ringwald#include "ble/gatt-service/battery_service_client.h"
843edc84c5SMatthias Ringwald#include "ble/sm.h"
8544c5d856SMatthias Ringwald#endif
8644c5d856SMatthias Ringwald
8744c5d856SMatthias Ringwald#ifdef ENABLE_MESH
8877ba3d3fSMatthias Ringwald#include "mesh/mesh_network.h"
89a5a7b6daSMatthias Ringwald#include "mesh/mesh_keys.h"
90a5a7b6daSMatthias Ringwald#include "mesh/mesh_virtual_addresses.h"
911ca3442bSMatthias Ringwald#endif
921ca3442bSMatthias Ringwald
931ca3442bSMatthias Ringwald/* API_START */
941ca3442bSMatthias Ringwald
951ca3442bSMatthias Ringwald/**
961ca3442bSMatthias Ringwald * @brief Initializes BTstack memory pools.
971ca3442bSMatthias Ringwald */
981ca3442bSMatthias Ringwaldvoid btstack_memory_init(void);
991ca3442bSMatthias Ringwald
100b6269742SMatthias Ringwald/**
101b6269742SMatthias Ringwald * @brief Deinitialize BTstack memory pools
102b6269742SMatthias Ringwald * @note if HAVE_MALLOC is defined, all previously allocated buffers are free'd
103b6269742SMatthias Ringwald */
104b6269742SMatthias Ringwaldvoid btstack_memory_deinit(void);
105b6269742SMatthias Ringwald
1061ca3442bSMatthias Ringwald/* API_END */
1071ca3442bSMatthias Ringwald"""
1081ca3442bSMatthias Ringwald
1091ca3442bSMatthias Ringwaldhfile_header_end = """
1101ca3442bSMatthias Ringwald#if defined __cplusplus
1111ca3442bSMatthias Ringwald}
1121ca3442bSMatthias Ringwald#endif
1131ca3442bSMatthias Ringwald
11480e33422SMatthias Ringwald#endif // BTSTACK_MEMORY_H
1151ca3442bSMatthias Ringwald"""
1161ca3442bSMatthias Ringwald
1171ca3442bSMatthias Ringwaldcfile_header_begin = """
1185c544019SMatthias Ringwald#define BTSTACK_FILE__ "btstack_memory.c"
1195c544019SMatthias Ringwald
1205c544019SMatthias Ringwald
1211ca3442bSMatthias Ringwald/*
1225c544019SMatthias Ringwald *  btstack_memory.c
1231ca3442bSMatthias Ringwald *
1241ca3442bSMatthias Ringwald *  @brief BTstack memory management via configurable memory pools
1251ca3442bSMatthias Ringwald *
126a98592bcSMatthias Ringwald *  @note code generated by tool/btstack_memory_generator.py
127a2673d88SMatthias Ringwald *  @note returnes buffers are initialized with 0
1281ca3442bSMatthias Ringwald *
1291ca3442bSMatthias Ringwald */
1301ca3442bSMatthias Ringwald
1311ca3442bSMatthias Ringwald#include "btstack_memory.h"
132d2e6c4b7SMatthias Ringwald#include "btstack_memory_pool.h"
133b6269742SMatthias Ringwald#include "btstack_debug.h"
1341ca3442bSMatthias Ringwald
1351ca3442bSMatthias Ringwald#include <stdlib.h>
1361ca3442bSMatthias Ringwald
137*e3c90686SMatthias Ringwald#ifdef ENABLE_MALLOC_TEST
138*e3c90686SMatthias Ringwaldextern "C" void * test_malloc(size_t size);
139*e3c90686SMatthias Ringwald#define malloc test_malloc
140*e3c90686SMatthias Ringwald#endif
141*e3c90686SMatthias Ringwald
142b6269742SMatthias Ringwald#ifdef HAVE_MALLOC
143b6269742SMatthias Ringwaldtypedef struct btstack_memory_buffer {
144b6269742SMatthias Ringwald    struct btstack_memory_buffer * next;
145b6269742SMatthias Ringwald    struct btstack_memory_buffer * prev;
146b6269742SMatthias Ringwald} btstack_memory_buffer_t;
147b6269742SMatthias Ringwald
148798bd46fSMatthias Ringwaldtypedef struct {
149798bd46fSMatthias Ringwald    btstack_memory_buffer_t tracking;
150798bd46fSMatthias Ringwald    void * pointer;
151798bd46fSMatthias Ringwald} test_buffer_t;
152798bd46fSMatthias Ringwald
153b6269742SMatthias Ringwaldstatic btstack_memory_buffer_t * btstack_memory_malloc_buffers;
15419ef97d2SMatthias Ringwaldstatic uint32_t btstack_memory_malloc_counter;
155b6269742SMatthias Ringwald
1562a95308bSMatthias Ringwaldstatic void btstack_memory_tracking_add(btstack_memory_buffer_t * buffer){
1572a95308bSMatthias Ringwald    btstack_assert(buffer != NULL);
15819ef97d2SMatthias Ringwald    if (btstack_memory_malloc_buffers != NULL) {
15919ef97d2SMatthias Ringwald        // let current first item prev point to new first item
16019ef97d2SMatthias Ringwald        btstack_memory_malloc_buffers->prev = buffer;
16119ef97d2SMatthias Ringwald    }
162b6269742SMatthias Ringwald    buffer->prev = NULL;
163b6269742SMatthias Ringwald    buffer->next = btstack_memory_malloc_buffers;
164b6269742SMatthias Ringwald    btstack_memory_malloc_buffers = buffer;
16519ef97d2SMatthias Ringwald
16619ef97d2SMatthias Ringwald    btstack_memory_malloc_counter++;
167b6269742SMatthias Ringwald}
168b6269742SMatthias Ringwald
1692a95308bSMatthias Ringwaldstatic void btstack_memory_tracking_remove(btstack_memory_buffer_t * buffer){
1702a95308bSMatthias Ringwald    btstack_assert(buffer != NULL);
171b6269742SMatthias Ringwald    if (buffer->prev == NULL){
172b6269742SMatthias Ringwald        // first item
173b6269742SMatthias Ringwald        btstack_memory_malloc_buffers = buffer->next;
174b6269742SMatthias Ringwald    } else {
175b6269742SMatthias Ringwald        buffer->prev->next = buffer->next;
176b6269742SMatthias Ringwald    }
177b6269742SMatthias Ringwald    if (buffer->next != NULL){
178b6269742SMatthias Ringwald        buffer->next->prev = buffer->prev;
179b6269742SMatthias Ringwald    }
18019ef97d2SMatthias Ringwald
18119ef97d2SMatthias Ringwald    btstack_memory_malloc_counter--;
182b6269742SMatthias Ringwald}
183b6269742SMatthias Ringwald#endif
184b6269742SMatthias Ringwald
185b6269742SMatthias Ringwaldvoid btstack_memory_deinit(void){
186b6269742SMatthias Ringwald#ifdef HAVE_MALLOC
187b6269742SMatthias Ringwald    while (btstack_memory_malloc_buffers != NULL){
188b6269742SMatthias Ringwald        btstack_memory_buffer_t * buffer = btstack_memory_malloc_buffers;
189b6269742SMatthias Ringwald        btstack_memory_malloc_buffers = buffer->next;
190b6269742SMatthias Ringwald        free(buffer);
191b6269742SMatthias Ringwald    }
19219ef97d2SMatthias Ringwald    btstack_assert(btstack_memory_malloc_counter == 0);
193b6269742SMatthias Ringwald#endif
194b6269742SMatthias Ringwald}
1951ca3442bSMatthias Ringwald"""
1961ca3442bSMatthias Ringwald
1971ca3442bSMatthias Ringwaldheader_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void);
1981ca3442bSMatthias Ringwaldvoid   btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);"""
1991ca3442bSMatthias Ringwald
2001ca3442bSMatthias Ringwaldcode_template = """
2011ca3442bSMatthias Ringwald// MARK: STRUCT_TYPE
202a265b909SMatthias Ringwald#if !defined(HAVE_MALLOC) && !defined(POOL_COUNT)
203a265b909SMatthias Ringwald    #if defined(POOL_COUNT_OLD_NO)
204a265b909SMatthias Ringwald        #error "Deprecated POOL_COUNT_OLD_NO defined instead of POOL_COUNT. Please update your btstack_config.h to use POOL_COUNT."
205a265b909SMatthias Ringwald    #else
206a265b909SMatthias Ringwald        #define POOL_COUNT 0
207a265b909SMatthias Ringwald    #endif
208a265b909SMatthias Ringwald#endif
209a265b909SMatthias Ringwald
2101ca3442bSMatthias Ringwald#ifdef POOL_COUNT
2111ca3442bSMatthias Ringwald#if POOL_COUNT > 0
2121ca3442bSMatthias Ringwaldstatic STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT];
21329d0c4f7SMatthias Ringwaldstatic btstack_memory_pool_t STRUCT_NAME_pool;
2141ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
215a2673d88SMatthias Ringwald    void * buffer = btstack_memory_pool_get(&STRUCT_NAME_pool);
216a2673d88SMatthias Ringwald    if (buffer){
217a2673d88SMatthias Ringwald        memset(buffer, 0, sizeof(STRUCT_TYPE));
218a2673d88SMatthias Ringwald    }
219a2673d88SMatthias Ringwald    return (STRUCT_NAME_t *) buffer;
2201ca3442bSMatthias Ringwald}
2211ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
22229d0c4f7SMatthias Ringwald    btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME);
2231ca3442bSMatthias Ringwald}
2241ca3442bSMatthias Ringwald#else
2251ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
2261ca3442bSMatthias Ringwald    return NULL;
2271ca3442bSMatthias Ringwald}
2281ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
229b6269742SMatthias Ringwald    UNUSED(STRUCT_NAME);
2301ca3442bSMatthias Ringwald};
2311ca3442bSMatthias Ringwald#endif
2321ca3442bSMatthias Ringwald#elif defined(HAVE_MALLOC)
2332a95308bSMatthias Ringwald
2342a95308bSMatthias Ringwaldtypedef struct {
2352a95308bSMatthias Ringwald    btstack_memory_buffer_t tracking;
236798bd46fSMatthias Ringwald    STRUCT_NAME_t data;
2372a95308bSMatthias Ringwald} btstack_memory_STRUCT_NAME_t;
2382a95308bSMatthias Ringwald
2391ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
2402a95308bSMatthias Ringwald    btstack_memory_STRUCT_NAME_t * buffer = (btstack_memory_STRUCT_NAME_t *) malloc(sizeof(btstack_memory_STRUCT_NAME_t));
241a2673d88SMatthias Ringwald    if (buffer){
242798bd46fSMatthias Ringwald        memset(buffer, 0, sizeof(btstack_memory_STRUCT_NAME_t));
2432a95308bSMatthias Ringwald        btstack_memory_tracking_add(&buffer->tracking);
2442a95308bSMatthias Ringwald        return &buffer->data;
245b6269742SMatthias Ringwald    } else {
246b6269742SMatthias Ringwald        return NULL;
247a2673d88SMatthias Ringwald    }
2481ca3442bSMatthias Ringwald}
2491ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
250798bd46fSMatthias Ringwald    // reconstruct buffer start
251798bd46fSMatthias Ringwald    btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) STRUCT_NAME)[-1];
252798bd46fSMatthias Ringwald    btstack_memory_tracking_remove(buffer);
253b6269742SMatthias Ringwald    free(buffer);
2541ca3442bSMatthias Ringwald}
2551ca3442bSMatthias Ringwald#endif
2561ca3442bSMatthias Ringwald"""
257798bd46fSMatthias Ringwaldinit_header = '''
258798bd46fSMatthias Ringwald// init
259798bd46fSMatthias Ringwaldvoid btstack_memory_init(void){
260798bd46fSMatthias Ringwald#ifdef HAVE_MALLOC
261798bd46fSMatthias Ringwald    // assert that there is no unexpected padding for combined buffer
262798bd46fSMatthias Ringwald    btstack_assert(sizeof(test_buffer_t) == sizeof(btstack_memory_buffer_t) + sizeof(void *));
263798bd46fSMatthias Ringwald#endif
264798bd46fSMatthias Ringwald
265798bd46fSMatthias Ringwald'''
2661ca3442bSMatthias Ringwald
2671ca3442bSMatthias Ringwaldinit_template = """#if POOL_COUNT > 0
26829d0c4f7SMatthias Ringwald    btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE));
2691ca3442bSMatthias Ringwald#endif"""
2701ca3442bSMatthias Ringwald
2711ca3442bSMatthias Ringwalddef writeln(f, data):
2721ca3442bSMatthias Ringwald    f.write(data + "\n")
2731ca3442bSMatthias Ringwald
2741ca3442bSMatthias Ringwalddef replacePlaceholder(template, struct_name):
2751ca3442bSMatthias Ringwald    struct_type = struct_name + '_t'
2761ca3442bSMatthias Ringwald    if struct_name.endswith('try'):
277a265b909SMatthias Ringwald        pool_count = "MAX_NR_" + struct_name.upper()[:-3] + "TRIES"
2781ca3442bSMatthias Ringwald    else:
279a265b909SMatthias Ringwald        pool_count = "MAX_NR_" + struct_name.upper() + "S"
280a265b909SMatthias Ringwald    pool_count_old_no = pool_count.replace("MAX_NR_", "MAX_NO_")
281a265b909SMatthias 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)
2821ca3442bSMatthias Ringwald    return snippet
2831ca3442bSMatthias Ringwald
284b3401248SMatthias Ringwaldlist_of_structs = [
285b3401248SMatthias Ringwald    ["hci_connection"],
286b3401248SMatthias Ringwald    ["l2cap_service", "l2cap_channel"],
28744c5d856SMatthias Ringwald]
28844c5d856SMatthias Ringwaldlist_of_classic_structs = [
289b3401248SMatthias Ringwald    ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"],
2902c455dadSMatthias Ringwald    ["btstack_link_key_db_memory_entry"],
291b3401248SMatthias Ringwald    ["bnep_service", "bnep_channel"],
292b3401248SMatthias Ringwald    ["hfp_connection"],
293f399f7fbSMilanka Ringwald    ["hid_host_connection"],
29427faf85aSMilanka Ringwald    ["service_record_item"],
29512e7f38cSMilanka Ringwald    ["avdtp_stream_endpoint"],
29691451a2bSMilanka Ringwald    ["avdtp_connection"],
297f12a3722SMilanka Ringwald    ["avrcp_connection"],
298ebb73e1fSMatthias Ringwald    ["avrcp_browsing_connection"],
299b3401248SMatthias Ringwald]
300ebb73e1fSMatthias Ringwaldlist_of_le_structs = [
301174a0c1cSMilanka Ringwald    ["battery_service_client", "gatt_client", "sm_lookup_entry", "whitelist_entry"],
30244c5d856SMatthias Ringwald]
30344c5d856SMatthias Ringwaldlist_of_mesh_structs = [
304039cbf1dSMatthias Ringwald    ['mesh_network_pdu', 'mesh_segmented_pdu', 'mesh_upper_transport_pdu', 'mesh_network_key', 'mesh_transport_key', 'mesh_virtual_address', 'mesh_subnet']
305ebb73e1fSMatthias Ringwald]
3061ca3442bSMatthias Ringwald
307a2673d88SMatthias Ringwaldbtstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
308a2673d88SMatthias Ringwaldfile_name = btstack_root + "/src/btstack_memory"
309a2673d88SMatthias Ringwaldprint ('Generating %s.[h|c]' % file_name)
3101ca3442bSMatthias Ringwald
3111ca3442bSMatthias Ringwaldf = open(file_name+".h", "w")
3121ca3442bSMatthias Ringwaldwriteln(f, copyright)
3131ca3442bSMatthias Ringwaldwriteln(f, hfile_header_begin)
3141ca3442bSMatthias Ringwaldfor struct_names in list_of_structs:
3151ca3442bSMatthias Ringwald    writeln(f, "// "+ ", ".join(struct_names))
3161ca3442bSMatthias Ringwald    for struct_name in struct_names:
3171ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(header_template, struct_name))
3181ca3442bSMatthias Ringwald    writeln(f, "")
31944c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_CLASSIC")
32044c5d856SMatthias Ringwaldfor struct_names in list_of_classic_structs:
32144c5d856SMatthias Ringwald    writeln(f, "// "+ ", ".join(struct_names))
32244c5d856SMatthias Ringwald    for struct_name in struct_names:
32344c5d856SMatthias Ringwald        writeln(f, replacePlaceholder(header_template, struct_name))
32444c5d856SMatthias Ringwald    writeln(f, "")
32544c5d856SMatthias Ringwaldwriteln(f, "#endif")
326a9a4c409SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE")
3271ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs:
3281ca3442bSMatthias Ringwald    writeln(f, "// "+ ", ".join(struct_names))
3291ca3442bSMatthias Ringwald    for struct_name in struct_names:
3301ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(header_template, struct_name))
3311ca3442bSMatthias Ringwaldwriteln(f, "#endif")
33244c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_MESH")
33344c5d856SMatthias Ringwaldfor struct_names in list_of_mesh_structs:
33444c5d856SMatthias Ringwald    writeln(f, "// "+ ", ".join(struct_names))
33544c5d856SMatthias Ringwald    for struct_name in struct_names:
33644c5d856SMatthias Ringwald        writeln(f, replacePlaceholder(header_template, struct_name))
33744c5d856SMatthias Ringwaldwriteln(f, "#endif")
3381ca3442bSMatthias Ringwaldwriteln(f, hfile_header_end)
3391ca3442bSMatthias Ringwaldf.close();
3401ca3442bSMatthias Ringwald
3411ca3442bSMatthias Ringwald
3421ca3442bSMatthias Ringwaldf = open(file_name+".c", "w")
3431ca3442bSMatthias Ringwaldwriteln(f, copyright)
3441ca3442bSMatthias Ringwaldwriteln(f, cfile_header_begin)
3451ca3442bSMatthias Ringwaldfor struct_names in list_of_structs:
3461ca3442bSMatthias Ringwald    for struct_name in struct_names:
3471ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(code_template, struct_name))
3481ca3442bSMatthias Ringwald    writeln(f, "")
34944c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_CLASSIC")
35044c5d856SMatthias Ringwaldfor struct_names in list_of_classic_structs:
35144c5d856SMatthias Ringwald    for struct_name in struct_names:
35244c5d856SMatthias Ringwald        writeln(f, replacePlaceholder(code_template, struct_name))
35344c5d856SMatthias Ringwald    writeln(f, "")
35444c5d856SMatthias Ringwaldwriteln(f, "#endif")
355a9a4c409SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE")
3561ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs:
3571ca3442bSMatthias Ringwald    for struct_name in struct_names:
3581ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(code_template, struct_name))
3591ca3442bSMatthias Ringwald    writeln(f, "")
3601ca3442bSMatthias Ringwaldwriteln(f, "#endif")
36144c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_MESH")
36244c5d856SMatthias Ringwaldfor struct_names in list_of_mesh_structs:
36344c5d856SMatthias Ringwald    for struct_name in struct_names:
36444c5d856SMatthias Ringwald        writeln(f, replacePlaceholder(code_template, struct_name))
36544c5d856SMatthias Ringwald    writeln(f, "")
36644c5d856SMatthias Ringwaldwriteln(f, "#endif")
3671ca3442bSMatthias Ringwald
368798bd46fSMatthias Ringwaldf.write(init_header)
3691ca3442bSMatthias Ringwaldfor struct_names in list_of_structs:
3701ca3442bSMatthias Ringwald    for struct_name in struct_names:
3711ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(init_template, struct_name))
37244c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_CLASSIC")
37350dc57fcSMatthias Ringwaldfor struct_names in list_of_classic_structs:
37444c5d856SMatthias Ringwald    for struct_name in struct_names:
37544c5d856SMatthias Ringwald        writeln(f, replacePlaceholder(init_template, struct_name))
37644c5d856SMatthias Ringwaldwriteln(f, "#endif")
377a9a4c409SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE")
3781ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs:
3791ca3442bSMatthias Ringwald    for struct_name in struct_names:
3801ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(init_template, struct_name))
3811ca3442bSMatthias Ringwaldwriteln(f, "#endif")
38244c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_MESH")
38350dc57fcSMatthias Ringwaldfor struct_names in list_of_mesh_structs:
38444c5d856SMatthias Ringwald    for struct_name in struct_names:
38544c5d856SMatthias Ringwald        writeln(f, replacePlaceholder(init_template, struct_name))
38644c5d856SMatthias Ringwaldwriteln(f, "#endif")
3871ca3442bSMatthias Ringwaldwriteln(f, "}")
3881ca3442bSMatthias Ringwaldf.close();
3891ca3442bSMatthias Ringwald
3906d34f98eSMatthias Ringwald# also generate test code
3916d34f98eSMatthias Ringwaldtest_header = """
3926d34f98eSMatthias Ringwald#include <stdint.h>
3936d34f98eSMatthias Ringwald#include <stdio.h>
3946d34f98eSMatthias Ringwald#include <stdlib.h>
3956d34f98eSMatthias Ringwald#include <string.h>
3966d34f98eSMatthias Ringwald
3976d34f98eSMatthias Ringwald// malloc hook
3986d34f98eSMatthias Ringwaldstatic int simulate_no_memory;
3996d34f98eSMatthias Ringwaldextern "C" void * test_malloc(size_t size);
4006d34f98eSMatthias Ringwaldvoid * test_malloc(size_t size){
4016d34f98eSMatthias Ringwald    if (simulate_no_memory) return NULL;
4026d34f98eSMatthias Ringwald    return malloc(size);
4036d34f98eSMatthias Ringwald}
4046d34f98eSMatthias Ringwald
4056d34f98eSMatthias Ringwald#include "btstack_config.h"
4066d34f98eSMatthias Ringwald
4076d34f98eSMatthias Ringwald#include "CppUTest/TestHarness.h"
4086d34f98eSMatthias Ringwald#include "CppUTest/CommandLineTestRunner.h"
4096d34f98eSMatthias Ringwald
4106d34f98eSMatthias Ringwald#include "bluetooth_data_types.h"
4116d34f98eSMatthias Ringwald#include "btstack_util.h"
4126d34f98eSMatthias Ringwald#include "btstack_memory.h"
4136d34f98eSMatthias Ringwald
4146d34f98eSMatthias Ringwald
4156d34f98eSMatthias RingwaldTEST_GROUP(btstack_memory){
4166d34f98eSMatthias Ringwald    void setup(void){
4176d34f98eSMatthias Ringwald        btstack_memory_init();
4186d34f98eSMatthias Ringwald        simulate_no_memory = 0;
4196d34f98eSMatthias Ringwald    }
4206d34f98eSMatthias Ringwald};
4216d34f98eSMatthias Ringwald
4226d34f98eSMatthias Ringwald#ifdef HAVE_MALLOC
4236d34f98eSMatthias RingwaldTEST(btstack_memory, deinit){
4246d34f98eSMatthias Ringwald    // alloc buffers 1,2,3
4256d34f98eSMatthias Ringwald    hci_connection_t * buffer_1 = btstack_memory_hci_connection_get();
4266d34f98eSMatthias Ringwald    hci_connection_t * buffer_2 = btstack_memory_hci_connection_get();
4276d34f98eSMatthias Ringwald    hci_connection_t * buffer_3 = btstack_memory_hci_connection_get();
4286d34f98eSMatthias Ringwald    // free first one in list
4296d34f98eSMatthias Ringwald    btstack_memory_hci_connection_free(buffer_3);
4306d34f98eSMatthias Ringwald    // free second one in list
4316d34f98eSMatthias Ringwald    btstack_memory_hci_connection_free(buffer_1);
4326d34f98eSMatthias Ringwald    // leave buffer in list
4336d34f98eSMatthias Ringwald    (void) buffer_2;
4346d34f98eSMatthias Ringwald    btstack_memory_deinit();
4356d34f98eSMatthias Ringwald}
4366d34f98eSMatthias Ringwald#endif
4376d34f98eSMatthias Ringwald
4386d34f98eSMatthias Ringwald"""
4396d34f98eSMatthias Ringwald
4406d34f98eSMatthias Ringwaldtest_template = """
4416d34f98eSMatthias Ringwald
4426d34f98eSMatthias RingwaldTEST(btstack_memory, STRUCT_NAME_GetAndFree){
4436d34f98eSMatthias Ringwald    STRUCT_NAME_t * context;
4446d34f98eSMatthias Ringwald#ifdef HAVE_MALLOC
4456d34f98eSMatthias Ringwald    context = btstack_memory_STRUCT_NAME_get();
4466d34f98eSMatthias Ringwald    CHECK(context != NULL);
4476d34f98eSMatthias Ringwald    btstack_memory_STRUCT_NAME_free(context);
4486d34f98eSMatthias Ringwald#else
4496d34f98eSMatthias Ringwald#ifdef POOL_COUNT
4506d34f98eSMatthias Ringwald    // single
4516d34f98eSMatthias Ringwald    context = btstack_memory_STRUCT_NAME_get();
4526d34f98eSMatthias Ringwald    CHECK(context != NULL);
4536d34f98eSMatthias Ringwald    btstack_memory_STRUCT_NAME_free(context);
4546d34f98eSMatthias Ringwald#else
4556d34f98eSMatthias Ringwald    // none
4566d34f98eSMatthias Ringwald    context = btstack_memory_STRUCT_NAME_get();
4576d34f98eSMatthias Ringwald    CHECK(context == NULL);
4586d34f98eSMatthias Ringwald    btstack_memory_STRUCT_NAME_free(context);
4596d34f98eSMatthias Ringwald#endif
4606d34f98eSMatthias Ringwald#endif
4616d34f98eSMatthias Ringwald}
4626d34f98eSMatthias Ringwald
4636d34f98eSMatthias RingwaldTEST(btstack_memory, STRUCT_NAME_NotEnoughBuffers){
4646d34f98eSMatthias Ringwald    STRUCT_NAME_t * context;
4656d34f98eSMatthias Ringwald#ifdef HAVE_MALLOC
4666d34f98eSMatthias Ringwald    simulate_no_memory = 1;
4676d34f98eSMatthias Ringwald#else
4686d34f98eSMatthias Ringwald#ifdef POOL_COUNT
4696d34f98eSMatthias Ringwald    int i;
4706d34f98eSMatthias Ringwald    // alloc all static buffers
4716d34f98eSMatthias Ringwald    for (i = 0; i < POOL_COUNT; i++){
4726d34f98eSMatthias Ringwald        context = btstack_memory_STRUCT_NAME_get();
4736d34f98eSMatthias Ringwald        CHECK(context != NULL);
4746d34f98eSMatthias Ringwald    }
4756d34f98eSMatthias Ringwald#endif
4766d34f98eSMatthias Ringwald#endif
4776d34f98eSMatthias Ringwald    // get one more
4786d34f98eSMatthias Ringwald    context = btstack_memory_STRUCT_NAME_get();
4796d34f98eSMatthias Ringwald    CHECK(context == NULL);
4806d34f98eSMatthias Ringwald}
4816d34f98eSMatthias Ringwald"""
4826d34f98eSMatthias Ringwald
4836d34f98eSMatthias Ringwaldtest_footer = """
4846d34f98eSMatthias Ringwaldint main (int argc, const char * argv[]){
4856d34f98eSMatthias Ringwald    return CommandLineTestRunner::RunAllTests(argc, argv);
4866d34f98eSMatthias Ringwald}
4876d34f98eSMatthias Ringwald"""
4886d34f98eSMatthias Ringwald
4896d34f98eSMatthias Ringwaldfile_name = btstack_root + "/test/btstack_memory/btstack_memory_test.c"
4906d34f98eSMatthias Ringwaldprint ('Generating %s' % file_name)
4916d34f98eSMatthias Ringwald
4926d34f98eSMatthias Ringwaldf = open(file_name, "w")
4936d34f98eSMatthias Ringwaldwriteln(f, copyright)
4946d34f98eSMatthias Ringwaldwriteln(f, test_header)
4956d34f98eSMatthias Ringwaldfor struct_names in list_of_structs:
4966d34f98eSMatthias Ringwald    for struct_name in struct_names:
4976d34f98eSMatthias Ringwald        writeln(f, replacePlaceholder(test_template, struct_name))
4986d34f98eSMatthias Ringwaldwriteln(f, "#ifdef ENABLE_CLASSIC")
4996d34f98eSMatthias Ringwaldfor struct_names in list_of_classic_structs:
5006d34f98eSMatthias Ringwald    for struct_name in struct_names:
5016d34f98eSMatthias Ringwald        writeln(f, replacePlaceholder(test_template, struct_name))
5026d34f98eSMatthias Ringwaldwriteln(f, "#endif")
5036d34f98eSMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE")
5046d34f98eSMatthias Ringwaldfor struct_names in list_of_le_structs:
5056d34f98eSMatthias Ringwald    for struct_name in struct_names:
5066d34f98eSMatthias Ringwald        writeln(f, replacePlaceholder(test_template, struct_name))
5076d34f98eSMatthias Ringwaldwriteln(f, "#endif")
5086d34f98eSMatthias Ringwaldwriteln(f, "#ifdef ENABLE_MESH")
5096d34f98eSMatthias Ringwaldfor struct_names in list_of_mesh_structs:
5106d34f98eSMatthias Ringwald    for struct_name in struct_names:
5116d34f98eSMatthias Ringwald        writeln(f, replacePlaceholder(test_template, struct_name))
5126d34f98eSMatthias Ringwaldwriteln(f, "#endif")
5136d34f98eSMatthias Ringwaldwriteln(f, test_footer)