xref: /btstack/tool/btstack_memory_generator.py (revision 6d34f98e0c6a035be904a5ac00e86a7b4f285631)
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
693edc84c5SMatthias Ringwald#include "classic/bnep.h"
703edc84c5SMatthias Ringwald#include "classic/hfp.h"
71a98592bcSMatthias Ringwald#include "classic/btstack_link_key_db.h"
72a98592bcSMatthias Ringwald#include "classic/btstack_link_key_db_memory.h"
73b3401248SMatthias Ringwald#include "classic/rfcomm.h"
74746ccb7eSMatthias Ringwald#include "classic/sdp_server.h"
75208d3378SMilanka Ringwald#include "classic/avdtp_sink.h"
76208d3378SMilanka Ringwald#include "classic/avdtp_source.h"
77be32e7f1SMilanka Ringwald#include "classic/avrcp.h"
783edc84c5SMatthias Ringwald
793edc84c5SMatthias Ringwald// BLE
80a9a4c409SMatthias Ringwald#ifdef ENABLE_BLE
813edc84c5SMatthias Ringwald#include "ble/gatt_client.h"
823edc84c5SMatthias Ringwald#include "ble/sm.h"
8344c5d856SMatthias Ringwald#endif
8444c5d856SMatthias Ringwald
8544c5d856SMatthias Ringwald#ifdef ENABLE_MESH
8677ba3d3fSMatthias Ringwald#include "mesh/mesh_network.h"
87a5a7b6daSMatthias Ringwald#include "mesh/mesh_keys.h"
88a5a7b6daSMatthias Ringwald#include "mesh/mesh_virtual_addresses.h"
891ca3442bSMatthias Ringwald#endif
901ca3442bSMatthias Ringwald
911ca3442bSMatthias Ringwald/* API_START */
921ca3442bSMatthias Ringwald
931ca3442bSMatthias Ringwald/**
941ca3442bSMatthias Ringwald * @brief Initializes BTstack memory pools.
951ca3442bSMatthias Ringwald */
961ca3442bSMatthias Ringwaldvoid btstack_memory_init(void);
971ca3442bSMatthias Ringwald
98b6269742SMatthias Ringwald/**
99b6269742SMatthias Ringwald * @brief Deinitialize BTstack memory pools
100b6269742SMatthias Ringwald * @note if HAVE_MALLOC is defined, all previously allocated buffers are free'd
101b6269742SMatthias Ringwald */
102b6269742SMatthias Ringwaldvoid btstack_memory_deinit(void);
103b6269742SMatthias Ringwald
1041ca3442bSMatthias Ringwald/* API_END */
1051ca3442bSMatthias Ringwald"""
1061ca3442bSMatthias Ringwald
1071ca3442bSMatthias Ringwaldhfile_header_end = """
1081ca3442bSMatthias Ringwald#if defined __cplusplus
1091ca3442bSMatthias Ringwald}
1101ca3442bSMatthias Ringwald#endif
1111ca3442bSMatthias Ringwald
11280e33422SMatthias Ringwald#endif // BTSTACK_MEMORY_H
1131ca3442bSMatthias Ringwald"""
1141ca3442bSMatthias Ringwald
1151ca3442bSMatthias Ringwaldcfile_header_begin = """
1165c544019SMatthias Ringwald#define BTSTACK_FILE__ "btstack_memory.c"
1175c544019SMatthias Ringwald
1185c544019SMatthias Ringwald
1191ca3442bSMatthias Ringwald/*
1205c544019SMatthias Ringwald *  btstack_memory.c
1211ca3442bSMatthias Ringwald *
1221ca3442bSMatthias Ringwald *  @brief BTstack memory management via configurable memory pools
1231ca3442bSMatthias Ringwald *
124a98592bcSMatthias Ringwald *  @note code generated by tool/btstack_memory_generator.py
125a2673d88SMatthias Ringwald *  @note returnes buffers are initialized with 0
1261ca3442bSMatthias Ringwald *
1271ca3442bSMatthias Ringwald */
1281ca3442bSMatthias Ringwald
1291ca3442bSMatthias Ringwald#include "btstack_memory.h"
130d2e6c4b7SMatthias Ringwald#include "btstack_memory_pool.h"
131b6269742SMatthias Ringwald#include "btstack_debug.h"
1321ca3442bSMatthias Ringwald
1331ca3442bSMatthias Ringwald#include <stdlib.h>
1341ca3442bSMatthias Ringwald
135b6269742SMatthias Ringwald#ifdef HAVE_MALLOC
136b6269742SMatthias Ringwaldtypedef struct btstack_memory_buffer {
137b6269742SMatthias Ringwald    struct btstack_memory_buffer * next;
138b6269742SMatthias Ringwald    struct btstack_memory_buffer * prev;
139b6269742SMatthias Ringwald} btstack_memory_buffer_t;
140b6269742SMatthias Ringwald
141b6269742SMatthias Ringwaldstatic btstack_memory_buffer_t * btstack_memory_malloc_buffers;
142b6269742SMatthias Ringwald
1432a95308bSMatthias Ringwaldstatic void btstack_memory_tracking_add(btstack_memory_buffer_t * buffer){
1442a95308bSMatthias Ringwald    btstack_assert(buffer != NULL);
145b6269742SMatthias Ringwald    btstack_memory_malloc_buffers = buffer;
146b6269742SMatthias Ringwald    buffer->prev = NULL;
147b6269742SMatthias Ringwald    buffer->next = btstack_memory_malloc_buffers;
148b6269742SMatthias Ringwald    btstack_memory_malloc_buffers = buffer;
149b6269742SMatthias Ringwald}
150b6269742SMatthias Ringwald
1512a95308bSMatthias Ringwaldstatic void btstack_memory_tracking_remove(btstack_memory_buffer_t * buffer){
1522a95308bSMatthias Ringwald    btstack_assert(buffer != NULL);
153b6269742SMatthias Ringwald    if (buffer->prev == NULL){
154b6269742SMatthias Ringwald        // first item
155b6269742SMatthias Ringwald        btstack_memory_malloc_buffers = buffer->next;
156b6269742SMatthias Ringwald    } else {
157b6269742SMatthias Ringwald        buffer->prev->next = buffer->next;
158b6269742SMatthias Ringwald    }
159b6269742SMatthias Ringwald    if (buffer->next != NULL){
160b6269742SMatthias Ringwald        buffer->next->prev = buffer->prev;
161b6269742SMatthias Ringwald    }
162b6269742SMatthias Ringwald}
163b6269742SMatthias Ringwald#endif
164b6269742SMatthias Ringwald
165b6269742SMatthias Ringwaldvoid btstack_memory_deinit(void){
166b6269742SMatthias Ringwald#ifdef HAVE_MALLOC
167b6269742SMatthias Ringwald    while (btstack_memory_malloc_buffers != NULL){
168b6269742SMatthias Ringwald        btstack_memory_buffer_t * buffer = btstack_memory_malloc_buffers;
169b6269742SMatthias Ringwald        btstack_memory_malloc_buffers = buffer->next;
170b6269742SMatthias Ringwald        free(buffer);
171b6269742SMatthias Ringwald    }
172b6269742SMatthias Ringwald#endif
173b6269742SMatthias Ringwald}
1741ca3442bSMatthias Ringwald"""
1751ca3442bSMatthias Ringwald
1761ca3442bSMatthias Ringwaldheader_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void);
1771ca3442bSMatthias Ringwaldvoid   btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);"""
1781ca3442bSMatthias Ringwald
1791ca3442bSMatthias Ringwaldcode_template = """
1801ca3442bSMatthias Ringwald// MARK: STRUCT_TYPE
181a265b909SMatthias Ringwald#if !defined(HAVE_MALLOC) && !defined(POOL_COUNT)
182a265b909SMatthias Ringwald    #if defined(POOL_COUNT_OLD_NO)
183a265b909SMatthias Ringwald        #error "Deprecated POOL_COUNT_OLD_NO defined instead of POOL_COUNT. Please update your btstack_config.h to use POOL_COUNT."
184a265b909SMatthias Ringwald    #else
185a265b909SMatthias Ringwald        #define POOL_COUNT 0
186a265b909SMatthias Ringwald    #endif
187a265b909SMatthias Ringwald#endif
188a265b909SMatthias Ringwald
1891ca3442bSMatthias Ringwald#ifdef POOL_COUNT
1901ca3442bSMatthias Ringwald#if POOL_COUNT > 0
1911ca3442bSMatthias Ringwaldstatic STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT];
19229d0c4f7SMatthias Ringwaldstatic btstack_memory_pool_t STRUCT_NAME_pool;
1931ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
194a2673d88SMatthias Ringwald    void * buffer = btstack_memory_pool_get(&STRUCT_NAME_pool);
195a2673d88SMatthias Ringwald    if (buffer){
196a2673d88SMatthias Ringwald        memset(buffer, 0, sizeof(STRUCT_TYPE));
197a2673d88SMatthias Ringwald    }
198a2673d88SMatthias Ringwald    return (STRUCT_NAME_t *) buffer;
1991ca3442bSMatthias Ringwald}
2001ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
20129d0c4f7SMatthias Ringwald    btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME);
2021ca3442bSMatthias Ringwald}
2031ca3442bSMatthias Ringwald#else
2041ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
2051ca3442bSMatthias Ringwald    return NULL;
2061ca3442bSMatthias Ringwald}
2071ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
208b6269742SMatthias Ringwald    UNUSED(STRUCT_NAME);
2091ca3442bSMatthias Ringwald};
2101ca3442bSMatthias Ringwald#endif
2111ca3442bSMatthias Ringwald#elif defined(HAVE_MALLOC)
2122a95308bSMatthias Ringwald
2132a95308bSMatthias Ringwaldtypedef struct {
2142a95308bSMatthias Ringwald    STRUCT_NAME_t data;
2152a95308bSMatthias Ringwald    btstack_memory_buffer_t tracking;
2162a95308bSMatthias Ringwald} btstack_memory_STRUCT_NAME_t;
2172a95308bSMatthias Ringwald
2181ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
2192a95308bSMatthias Ringwald    btstack_memory_STRUCT_NAME_t * buffer = (btstack_memory_STRUCT_NAME_t *) malloc(sizeof(btstack_memory_STRUCT_NAME_t));
220a2673d88SMatthias Ringwald    if (buffer){
2212a95308bSMatthias Ringwald        memset(buffer, 0, sizeof(STRUCT_NAME_t));
2222a95308bSMatthias Ringwald        btstack_memory_tracking_add(&buffer->tracking);
2232a95308bSMatthias Ringwald        return &buffer->data;
224b6269742SMatthias Ringwald    } else {
225b6269742SMatthias Ringwald        return NULL;
226a2673d88SMatthias Ringwald    }
2271ca3442bSMatthias Ringwald}
2281ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
2292a95308bSMatthias Ringwald    btstack_memory_STRUCT_NAME_t * buffer =  (btstack_memory_STRUCT_NAME_t *) STRUCT_NAME;
2302a95308bSMatthias Ringwald    btstack_memory_tracking_remove(&buffer->tracking);
231b6269742SMatthias Ringwald    free(buffer);
2321ca3442bSMatthias Ringwald}
2331ca3442bSMatthias Ringwald#endif
2341ca3442bSMatthias Ringwald"""
2351ca3442bSMatthias Ringwald
2361ca3442bSMatthias Ringwaldinit_template = """#if POOL_COUNT > 0
23729d0c4f7SMatthias Ringwald    btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE));
2381ca3442bSMatthias Ringwald#endif"""
2391ca3442bSMatthias Ringwald
2401ca3442bSMatthias Ringwalddef writeln(f, data):
2411ca3442bSMatthias Ringwald    f.write(data + "\n")
2421ca3442bSMatthias Ringwald
2431ca3442bSMatthias Ringwalddef replacePlaceholder(template, struct_name):
2441ca3442bSMatthias Ringwald    struct_type = struct_name + '_t'
2451ca3442bSMatthias Ringwald    if struct_name.endswith('try'):
246a265b909SMatthias Ringwald        pool_count = "MAX_NR_" + struct_name.upper()[:-3] + "TRIES"
2471ca3442bSMatthias Ringwald    else:
248a265b909SMatthias Ringwald        pool_count = "MAX_NR_" + struct_name.upper() + "S"
249a265b909SMatthias Ringwald    pool_count_old_no = pool_count.replace("MAX_NR_", "MAX_NO_")
250a265b909SMatthias 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)
2511ca3442bSMatthias Ringwald    return snippet
2521ca3442bSMatthias Ringwald
253b3401248SMatthias Ringwaldlist_of_structs = [
254b3401248SMatthias Ringwald    ["hci_connection"],
255b3401248SMatthias Ringwald    ["l2cap_service", "l2cap_channel"],
25644c5d856SMatthias Ringwald]
25744c5d856SMatthias Ringwaldlist_of_classic_structs = [
258b3401248SMatthias Ringwald    ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"],
2592c455dadSMatthias Ringwald    ["btstack_link_key_db_memory_entry"],
260b3401248SMatthias Ringwald    ["bnep_service", "bnep_channel"],
261b3401248SMatthias Ringwald    ["hfp_connection"],
26227faf85aSMilanka Ringwald    ["service_record_item"],
26312e7f38cSMilanka Ringwald    ["avdtp_stream_endpoint"],
26491451a2bSMilanka Ringwald    ["avdtp_connection"],
265f12a3722SMilanka Ringwald    ["avrcp_connection"],
266ebb73e1fSMatthias Ringwald    ["avrcp_browsing_connection"],
267b3401248SMatthias Ringwald]
268ebb73e1fSMatthias Ringwaldlist_of_le_structs = [
269ebb73e1fSMatthias Ringwald    ["gatt_client", "whitelist_entry", "sm_lookup_entry"],
27044c5d856SMatthias Ringwald]
27144c5d856SMatthias Ringwaldlist_of_mesh_structs = [
272039cbf1dSMatthias Ringwald    ['mesh_network_pdu', 'mesh_segmented_pdu', 'mesh_upper_transport_pdu', 'mesh_network_key', 'mesh_transport_key', 'mesh_virtual_address', 'mesh_subnet']
273ebb73e1fSMatthias Ringwald]
2741ca3442bSMatthias Ringwald
275a2673d88SMatthias Ringwaldbtstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
276a2673d88SMatthias Ringwaldfile_name = btstack_root + "/src/btstack_memory"
277a2673d88SMatthias Ringwaldprint ('Generating %s.[h|c]' % file_name)
2781ca3442bSMatthias Ringwald
2791ca3442bSMatthias Ringwaldf = open(file_name+".h", "w")
2801ca3442bSMatthias Ringwaldwriteln(f, copyright)
2811ca3442bSMatthias Ringwaldwriteln(f, hfile_header_begin)
2821ca3442bSMatthias Ringwaldfor struct_names in list_of_structs:
2831ca3442bSMatthias Ringwald    writeln(f, "// "+ ", ".join(struct_names))
2841ca3442bSMatthias Ringwald    for struct_name in struct_names:
2851ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(header_template, struct_name))
2861ca3442bSMatthias Ringwald    writeln(f, "")
28744c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_CLASSIC")
28844c5d856SMatthias Ringwaldfor struct_names in list_of_classic_structs:
28944c5d856SMatthias Ringwald    writeln(f, "// "+ ", ".join(struct_names))
29044c5d856SMatthias Ringwald    for struct_name in struct_names:
29144c5d856SMatthias Ringwald        writeln(f, replacePlaceholder(header_template, struct_name))
29244c5d856SMatthias Ringwald    writeln(f, "")
29344c5d856SMatthias Ringwaldwriteln(f, "#endif")
294a9a4c409SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE")
2951ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs:
2961ca3442bSMatthias Ringwald    writeln(f, "// "+ ", ".join(struct_names))
2971ca3442bSMatthias Ringwald    for struct_name in struct_names:
2981ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(header_template, struct_name))
2991ca3442bSMatthias Ringwaldwriteln(f, "#endif")
30044c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_MESH")
30144c5d856SMatthias Ringwaldfor struct_names in list_of_mesh_structs:
30244c5d856SMatthias Ringwald    writeln(f, "// "+ ", ".join(struct_names))
30344c5d856SMatthias Ringwald    for struct_name in struct_names:
30444c5d856SMatthias Ringwald        writeln(f, replacePlaceholder(header_template, struct_name))
30544c5d856SMatthias Ringwaldwriteln(f, "#endif")
3061ca3442bSMatthias Ringwaldwriteln(f, hfile_header_end)
3071ca3442bSMatthias Ringwaldf.close();
3081ca3442bSMatthias Ringwald
3091ca3442bSMatthias Ringwald
3101ca3442bSMatthias Ringwaldf = open(file_name+".c", "w")
3111ca3442bSMatthias Ringwaldwriteln(f, copyright)
3121ca3442bSMatthias Ringwaldwriteln(f, cfile_header_begin)
3131ca3442bSMatthias Ringwaldfor struct_names in list_of_structs:
3141ca3442bSMatthias Ringwald    for struct_name in struct_names:
3151ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(code_template, struct_name))
3161ca3442bSMatthias Ringwald    writeln(f, "")
31744c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_CLASSIC")
31844c5d856SMatthias Ringwaldfor struct_names in list_of_classic_structs:
31944c5d856SMatthias Ringwald    for struct_name in struct_names:
32044c5d856SMatthias Ringwald        writeln(f, replacePlaceholder(code_template, struct_name))
32144c5d856SMatthias Ringwald    writeln(f, "")
32244c5d856SMatthias Ringwaldwriteln(f, "#endif")
323a9a4c409SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE")
3241ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs:
3251ca3442bSMatthias Ringwald    for struct_name in struct_names:
3261ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(code_template, struct_name))
3271ca3442bSMatthias Ringwald    writeln(f, "")
3281ca3442bSMatthias Ringwaldwriteln(f, "#endif")
32944c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_MESH")
33044c5d856SMatthias Ringwaldfor struct_names in list_of_mesh_structs:
33144c5d856SMatthias Ringwald    for struct_name in struct_names:
33244c5d856SMatthias Ringwald        writeln(f, replacePlaceholder(code_template, struct_name))
33344c5d856SMatthias Ringwald    writeln(f, "")
33444c5d856SMatthias Ringwaldwriteln(f, "#endif")
3351ca3442bSMatthias Ringwald
3361ca3442bSMatthias Ringwald
3371ca3442bSMatthias Ringwaldwriteln(f, "// init")
3381ca3442bSMatthias Ringwaldwriteln(f, "void btstack_memory_init(void){")
3391ca3442bSMatthias Ringwaldfor struct_names in list_of_structs:
3401ca3442bSMatthias Ringwald    for struct_name in struct_names:
3411ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(init_template, struct_name))
34244c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_CLASSIC")
34350dc57fcSMatthias Ringwaldfor struct_names in list_of_classic_structs:
34444c5d856SMatthias Ringwald    for struct_name in struct_names:
34544c5d856SMatthias Ringwald        writeln(f, replacePlaceholder(init_template, struct_name))
34644c5d856SMatthias Ringwaldwriteln(f, "#endif")
347a9a4c409SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE")
3481ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs:
3491ca3442bSMatthias Ringwald    for struct_name in struct_names:
3501ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(init_template, struct_name))
3511ca3442bSMatthias Ringwaldwriteln(f, "#endif")
35244c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_MESH")
35350dc57fcSMatthias Ringwaldfor struct_names in list_of_mesh_structs:
35444c5d856SMatthias Ringwald    for struct_name in struct_names:
35544c5d856SMatthias Ringwald        writeln(f, replacePlaceholder(init_template, struct_name))
35644c5d856SMatthias Ringwaldwriteln(f, "#endif")
3571ca3442bSMatthias Ringwaldwriteln(f, "}")
3581ca3442bSMatthias Ringwaldf.close();
3591ca3442bSMatthias Ringwald
360*6d34f98eSMatthias Ringwald# also generate test code
361*6d34f98eSMatthias Ringwaldtest_header = """
362*6d34f98eSMatthias Ringwald#include <stdint.h>
363*6d34f98eSMatthias Ringwald#include <stdio.h>
364*6d34f98eSMatthias Ringwald#include <stdlib.h>
365*6d34f98eSMatthias Ringwald#include <string.h>
366*6d34f98eSMatthias Ringwald
367*6d34f98eSMatthias Ringwald// malloc hook
368*6d34f98eSMatthias Ringwaldstatic int simulate_no_memory;
369*6d34f98eSMatthias Ringwaldextern "C" void * test_malloc(size_t size);
370*6d34f98eSMatthias Ringwaldvoid * test_malloc(size_t size){
371*6d34f98eSMatthias Ringwald    if (simulate_no_memory) return NULL;
372*6d34f98eSMatthias Ringwald    return malloc(size);
373*6d34f98eSMatthias Ringwald}
374*6d34f98eSMatthias Ringwald
375*6d34f98eSMatthias Ringwald#include "btstack_config.h"
376*6d34f98eSMatthias Ringwald
377*6d34f98eSMatthias Ringwald#include "CppUTest/TestHarness.h"
378*6d34f98eSMatthias Ringwald#include "CppUTest/CommandLineTestRunner.h"
379*6d34f98eSMatthias Ringwald
380*6d34f98eSMatthias Ringwald#include "bluetooth_data_types.h"
381*6d34f98eSMatthias Ringwald#include "btstack_util.h"
382*6d34f98eSMatthias Ringwald#include "btstack_memory.h"
383*6d34f98eSMatthias Ringwald
384*6d34f98eSMatthias Ringwald
385*6d34f98eSMatthias RingwaldTEST_GROUP(btstack_memory){
386*6d34f98eSMatthias Ringwald    void setup(void){
387*6d34f98eSMatthias Ringwald        btstack_memory_init();
388*6d34f98eSMatthias Ringwald        simulate_no_memory = 0;
389*6d34f98eSMatthias Ringwald    }
390*6d34f98eSMatthias Ringwald};
391*6d34f98eSMatthias Ringwald
392*6d34f98eSMatthias Ringwald#ifdef HAVE_MALLOC
393*6d34f98eSMatthias RingwaldTEST(btstack_memory, deinit){
394*6d34f98eSMatthias Ringwald    // alloc buffers 1,2,3
395*6d34f98eSMatthias Ringwald    hci_connection_t * buffer_1 = btstack_memory_hci_connection_get();
396*6d34f98eSMatthias Ringwald    hci_connection_t * buffer_2 = btstack_memory_hci_connection_get();
397*6d34f98eSMatthias Ringwald    hci_connection_t * buffer_3 = btstack_memory_hci_connection_get();
398*6d34f98eSMatthias Ringwald    // free first one in list
399*6d34f98eSMatthias Ringwald    btstack_memory_hci_connection_free(buffer_3);
400*6d34f98eSMatthias Ringwald    // free second one in list
401*6d34f98eSMatthias Ringwald    btstack_memory_hci_connection_free(buffer_1);
402*6d34f98eSMatthias Ringwald    // leave buffer in list
403*6d34f98eSMatthias Ringwald    (void) buffer_2;
404*6d34f98eSMatthias Ringwald    btstack_memory_deinit();
405*6d34f98eSMatthias Ringwald}
406*6d34f98eSMatthias Ringwald#endif
407*6d34f98eSMatthias Ringwald
408*6d34f98eSMatthias Ringwald"""
409*6d34f98eSMatthias Ringwald
410*6d34f98eSMatthias Ringwaldtest_template = """
411*6d34f98eSMatthias Ringwald
412*6d34f98eSMatthias RingwaldTEST(btstack_memory, STRUCT_NAME_GetAndFree){
413*6d34f98eSMatthias Ringwald    STRUCT_NAME_t * context;
414*6d34f98eSMatthias Ringwald#ifdef HAVE_MALLOC
415*6d34f98eSMatthias Ringwald    context = btstack_memory_STRUCT_NAME_get();
416*6d34f98eSMatthias Ringwald    CHECK(context != NULL);
417*6d34f98eSMatthias Ringwald    btstack_memory_STRUCT_NAME_free(context);
418*6d34f98eSMatthias Ringwald#else
419*6d34f98eSMatthias Ringwald#ifdef POOL_COUNT
420*6d34f98eSMatthias Ringwald    // single
421*6d34f98eSMatthias Ringwald    context = btstack_memory_STRUCT_NAME_get();
422*6d34f98eSMatthias Ringwald    CHECK(context != NULL);
423*6d34f98eSMatthias Ringwald    btstack_memory_STRUCT_NAME_free(context);
424*6d34f98eSMatthias Ringwald#else
425*6d34f98eSMatthias Ringwald    // none
426*6d34f98eSMatthias Ringwald    context = btstack_memory_STRUCT_NAME_get();
427*6d34f98eSMatthias Ringwald    CHECK(context == NULL);
428*6d34f98eSMatthias Ringwald    btstack_memory_STRUCT_NAME_free(context);
429*6d34f98eSMatthias Ringwald#endif
430*6d34f98eSMatthias Ringwald#endif
431*6d34f98eSMatthias Ringwald}
432*6d34f98eSMatthias Ringwald
433*6d34f98eSMatthias RingwaldTEST(btstack_memory, STRUCT_NAME_NotEnoughBuffers){
434*6d34f98eSMatthias Ringwald    STRUCT_NAME_t * context;
435*6d34f98eSMatthias Ringwald#ifdef HAVE_MALLOC
436*6d34f98eSMatthias Ringwald    simulate_no_memory = 1;
437*6d34f98eSMatthias Ringwald#else
438*6d34f98eSMatthias Ringwald#ifdef POOL_COUNT
439*6d34f98eSMatthias Ringwald    int i;
440*6d34f98eSMatthias Ringwald    // alloc all static buffers
441*6d34f98eSMatthias Ringwald    for (i = 0; i < POOL_COUNT; i++){
442*6d34f98eSMatthias Ringwald        context = btstack_memory_STRUCT_NAME_get();
443*6d34f98eSMatthias Ringwald        CHECK(context != NULL);
444*6d34f98eSMatthias Ringwald    }
445*6d34f98eSMatthias Ringwald#endif
446*6d34f98eSMatthias Ringwald#endif
447*6d34f98eSMatthias Ringwald    // get one more
448*6d34f98eSMatthias Ringwald    context = btstack_memory_STRUCT_NAME_get();
449*6d34f98eSMatthias Ringwald    CHECK(context == NULL);
450*6d34f98eSMatthias Ringwald}
451*6d34f98eSMatthias Ringwald"""
452*6d34f98eSMatthias Ringwald
453*6d34f98eSMatthias Ringwaldtest_footer = """
454*6d34f98eSMatthias Ringwaldint main (int argc, const char * argv[]){
455*6d34f98eSMatthias Ringwald    return CommandLineTestRunner::RunAllTests(argc, argv);
456*6d34f98eSMatthias Ringwald}
457*6d34f98eSMatthias Ringwald"""
458*6d34f98eSMatthias Ringwald
459*6d34f98eSMatthias Ringwaldfile_name = btstack_root + "/test/btstack_memory/btstack_memory_test.c"
460*6d34f98eSMatthias Ringwaldprint ('Generating %s' % file_name)
461*6d34f98eSMatthias Ringwald
462*6d34f98eSMatthias Ringwaldf = open(file_name, "w")
463*6d34f98eSMatthias Ringwaldwriteln(f, copyright)
464*6d34f98eSMatthias Ringwaldwriteln(f, test_header)
465*6d34f98eSMatthias Ringwaldfor struct_names in list_of_structs:
466*6d34f98eSMatthias Ringwald    for struct_name in struct_names:
467*6d34f98eSMatthias Ringwald        writeln(f, replacePlaceholder(test_template, struct_name))
468*6d34f98eSMatthias Ringwaldwriteln(f, "#ifdef ENABLE_CLASSIC")
469*6d34f98eSMatthias Ringwaldfor struct_names in list_of_classic_structs:
470*6d34f98eSMatthias Ringwald    for struct_name in struct_names:
471*6d34f98eSMatthias Ringwald        writeln(f, replacePlaceholder(test_template, struct_name))
472*6d34f98eSMatthias Ringwaldwriteln(f, "#endif")
473*6d34f98eSMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE")
474*6d34f98eSMatthias Ringwaldfor struct_names in list_of_le_structs:
475*6d34f98eSMatthias Ringwald    for struct_name in struct_names:
476*6d34f98eSMatthias Ringwald        writeln(f, replacePlaceholder(test_template, struct_name))
477*6d34f98eSMatthias Ringwaldwriteln(f, "#endif")
478*6d34f98eSMatthias Ringwaldwriteln(f, "#ifdef ENABLE_MESH")
479*6d34f98eSMatthias Ringwaldfor struct_names in list_of_mesh_structs:
480*6d34f98eSMatthias Ringwald    for struct_name in struct_names:
481*6d34f98eSMatthias Ringwald        writeln(f, replacePlaceholder(test_template, struct_name))
482*6d34f98eSMatthias Ringwaldwriteln(f, "#endif")
483*6d34f98eSMatthias Ringwaldwriteln(f, test_footer)