xref: /btstack/tool/btstack_memory_generator.py (revision 3edc84c5b6b1e23a3d103fe8ce1f6b5ad1df3498)
11ca3442bSMatthias Ringwald#!/usr/bin/env python
21ca3442bSMatthias Ringwald
31ca3442bSMatthias Ringwaldcopyright = """/*
41ca3442bSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH
51ca3442bSMatthias Ringwald *
61ca3442bSMatthias Ringwald * Redistribution and use in source and binary forms, with or without
71ca3442bSMatthias Ringwald * modification, are permitted provided that the following conditions
81ca3442bSMatthias Ringwald * are met:
91ca3442bSMatthias Ringwald *
101ca3442bSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
111ca3442bSMatthias Ringwald *    notice, this list of conditions and the following disclaimer.
121ca3442bSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
131ca3442bSMatthias Ringwald *    notice, this list of conditions and the following disclaimer in the
141ca3442bSMatthias Ringwald *    documentation and/or other materials provided with the distribution.
151ca3442bSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
161ca3442bSMatthias Ringwald *    contributors may be used to endorse or promote products derived
171ca3442bSMatthias Ringwald *    from this software without specific prior written permission.
181ca3442bSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
191ca3442bSMatthias Ringwald *    personal benefit and not for any commercial purpose or for
201ca3442bSMatthias Ringwald *    monetary gain.
211ca3442bSMatthias Ringwald *
221ca3442bSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
231ca3442bSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
241ca3442bSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
251ca3442bSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
261ca3442bSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
271ca3442bSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
281ca3442bSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
291ca3442bSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
301ca3442bSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
311ca3442bSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
321ca3442bSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331ca3442bSMatthias Ringwald * SUCH DAMAGE.
341ca3442bSMatthias Ringwald *
351ca3442bSMatthias Ringwald * Please inquire about commercial licensing options at
361ca3442bSMatthias Ringwald * [email protected]
371ca3442bSMatthias Ringwald *
381ca3442bSMatthias Ringwald */
391ca3442bSMatthias Ringwald"""
401ca3442bSMatthias Ringwald
411ca3442bSMatthias Ringwaldhfile_header_begin = """
421ca3442bSMatthias Ringwald
431ca3442bSMatthias Ringwald/*
441ca3442bSMatthias Ringwald *  btstack_memory.h
451ca3442bSMatthias Ringwald *
461ca3442bSMatthias Ringwald *  @brief BTstack memory management via configurable memory pools
471ca3442bSMatthias Ringwald *
481ca3442bSMatthias Ringwald */
491ca3442bSMatthias Ringwald
501ca3442bSMatthias Ringwald#ifndef __BTSTACK_MEMORY_H
511ca3442bSMatthias Ringwald#define __BTSTACK_MEMORY_H
521ca3442bSMatthias Ringwald
531ca3442bSMatthias Ringwald#if defined __cplusplus
541ca3442bSMatthias Ringwaldextern "C" {
551ca3442bSMatthias Ringwald#endif
561ca3442bSMatthias Ringwald
571ca3442bSMatthias Ringwald#include "btstack-config.h"
581ca3442bSMatthias Ringwald
59*3edc84c5SMatthias Ringwald// Core
601ca3442bSMatthias Ringwald#include "hci.h"
611ca3442bSMatthias Ringwald#include "l2cap.h"
621ca3442bSMatthias Ringwald
63*3edc84c5SMatthias Ringwald// Classic
64*3edc84c5SMatthias Ringwald#include "classic/rfcomm.h"
65*3edc84c5SMatthias Ringwald#include "classic/bnep.h"
66*3edc84c5SMatthias Ringwald#include "classic/hfp.h"
67*3edc84c5SMatthias Ringwald#include "classic/remote_device_db.h"
68*3edc84c5SMatthias Ringwald
69*3edc84c5SMatthias Ringwald// BLE
701ca3442bSMatthias Ringwald#ifdef HAVE_BLE
71*3edc84c5SMatthias Ringwald#include "ble/gatt_client.h"
72*3edc84c5SMatthias Ringwald#include "ble/sm.h"
731ca3442bSMatthias Ringwald#endif
741ca3442bSMatthias Ringwald
751ca3442bSMatthias Ringwald/* API_START */
761ca3442bSMatthias Ringwald
771ca3442bSMatthias Ringwald/**
781ca3442bSMatthias Ringwald * @brief Initializes BTstack memory pools.
791ca3442bSMatthias Ringwald */
801ca3442bSMatthias Ringwaldvoid btstack_memory_init(void);
811ca3442bSMatthias Ringwald
821ca3442bSMatthias Ringwald/* API_END */
831ca3442bSMatthias Ringwald"""
841ca3442bSMatthias Ringwald
851ca3442bSMatthias Ringwaldhfile_header_end = """
861ca3442bSMatthias Ringwald#if defined __cplusplus
871ca3442bSMatthias Ringwald}
881ca3442bSMatthias Ringwald#endif
891ca3442bSMatthias Ringwald
901ca3442bSMatthias Ringwald#endif // __BTSTACK_MEMORY_H
911ca3442bSMatthias Ringwald"""
921ca3442bSMatthias Ringwald
931ca3442bSMatthias Ringwaldcfile_header_begin = """
941ca3442bSMatthias Ringwald/*
951ca3442bSMatthias Ringwald *  btstsack_memory.h
961ca3442bSMatthias Ringwald *
971ca3442bSMatthias Ringwald *  @brief BTstack memory management via configurable memory pools
981ca3442bSMatthias Ringwald *
991ca3442bSMatthias Ringwald *  @note code semi-atuomatically generated by tools/btstack_memory_generator.py
1001ca3442bSMatthias Ringwald *
1011ca3442bSMatthias Ringwald */
1021ca3442bSMatthias Ringwald
1031ca3442bSMatthias Ringwald#include "btstack_memory.h"
1041ca3442bSMatthias Ringwald#include "memory_pool.h"
1051ca3442bSMatthias Ringwald
1061ca3442bSMatthias Ringwald#include <stdlib.h>
1071ca3442bSMatthias Ringwald
1081ca3442bSMatthias Ringwald"""
1091ca3442bSMatthias Ringwald
1101ca3442bSMatthias Ringwaldheader_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void);
1111ca3442bSMatthias Ringwaldvoid   btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);"""
1121ca3442bSMatthias Ringwald
1131ca3442bSMatthias Ringwaldcode_template = """
1141ca3442bSMatthias Ringwald// MARK: STRUCT_TYPE
1151ca3442bSMatthias Ringwald#ifdef POOL_COUNT
1161ca3442bSMatthias Ringwald#if POOL_COUNT > 0
1171ca3442bSMatthias Ringwaldstatic STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT];
1181ca3442bSMatthias Ringwaldstatic memory_pool_t STRUCT_NAME_pool;
1191ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
1201ca3442bSMatthias Ringwald    return (STRUCT_NAME_t *) memory_pool_get(&STRUCT_NAME_pool);
1211ca3442bSMatthias Ringwald}
1221ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
1231ca3442bSMatthias Ringwald    memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME);
1241ca3442bSMatthias Ringwald}
1251ca3442bSMatthias Ringwald#else
1261ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
1271ca3442bSMatthias Ringwald    return NULL;
1281ca3442bSMatthias Ringwald}
1291ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
1301ca3442bSMatthias Ringwald    // silence compiler warning about unused parameter in a portable way
1311ca3442bSMatthias Ringwald    (void) STRUCT_NAME;
1321ca3442bSMatthias Ringwald};
1331ca3442bSMatthias Ringwald#endif
1341ca3442bSMatthias Ringwald#elif defined(HAVE_MALLOC)
1351ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
1361ca3442bSMatthias Ringwald    return (STRUCT_NAME_t*) malloc(sizeof(STRUCT_TYPE));
1371ca3442bSMatthias Ringwald}
1381ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
1391ca3442bSMatthias Ringwald    free(STRUCT_NAME);
1401ca3442bSMatthias Ringwald}
1411ca3442bSMatthias Ringwald#else
1421ca3442bSMatthias Ringwald#error "Neither HAVE_MALLOC nor POOL_COUNT for struct STRUCT_NAME is defined. Please, edit the config file."
1431ca3442bSMatthias Ringwald#endif
1441ca3442bSMatthias Ringwald"""
1451ca3442bSMatthias Ringwald
1461ca3442bSMatthias Ringwaldinit_template = """#if POOL_COUNT > 0
1471ca3442bSMatthias Ringwald    memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE));
1481ca3442bSMatthias Ringwald#endif"""
1491ca3442bSMatthias Ringwald
1501ca3442bSMatthias Ringwalddef writeln(f, data):
1511ca3442bSMatthias Ringwald    f.write(data + "\n")
1521ca3442bSMatthias Ringwald
1531ca3442bSMatthias Ringwalddef replacePlaceholder(template, struct_name):
1541ca3442bSMatthias Ringwald    struct_type = struct_name + '_t'
1551ca3442bSMatthias Ringwald    if struct_name.endswith('try'):
1561ca3442bSMatthias Ringwald        pool_count = "MAX_NO_" + struct_name.upper()[:-3] + "TRIES"
1571ca3442bSMatthias Ringwald    else:
1581ca3442bSMatthias Ringwald        pool_count = "MAX_NO_" + struct_name.upper() + "S"
1591ca3442bSMatthias Ringwald
1601ca3442bSMatthias Ringwald    snippet = template.replace("STRUCT_TYPE", struct_type).replace("STRUCT_NAME", struct_name).replace("POOL_COUNT", pool_count)
1611ca3442bSMatthias Ringwald    return snippet
1621ca3442bSMatthias Ringwald
1631ca3442bSMatthias Ringwaldlist_of_structs = [ ["hci_connection"], ["l2cap_service", "l2cap_channel"], ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"], ["db_mem_device_name", "db_mem_device_link_key", "db_mem_service"], ["bnep_service", "bnep_channel"], ["hfp_connection"]]
1641ca3442bSMatthias Ringwaldlist_of_le_structs = [["gatt_client", "gatt_subclient", "whitelist_entry", "sm_lookup_entry"]]
1651ca3442bSMatthias Ringwald
1661ca3442bSMatthias Ringwaldfile_name = "../src/btstack_memory"
1671ca3442bSMatthias Ringwald
1681ca3442bSMatthias Ringwald
1691ca3442bSMatthias Ringwaldf = open(file_name+".h", "w")
1701ca3442bSMatthias Ringwaldwriteln(f, copyright)
1711ca3442bSMatthias Ringwaldwriteln(f, hfile_header_begin)
1721ca3442bSMatthias Ringwaldfor struct_names in list_of_structs:
1731ca3442bSMatthias Ringwald    writeln(f, "// "+ ", ".join(struct_names))
1741ca3442bSMatthias Ringwald    for struct_name in struct_names:
1751ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(header_template, struct_name))
1761ca3442bSMatthias Ringwald    writeln(f, "")
1771ca3442bSMatthias Ringwaldwriteln(f, "#ifdef HAVE_BLE")
1781ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs:
1791ca3442bSMatthias Ringwald    writeln(f, "// "+ ", ".join(struct_names))
1801ca3442bSMatthias Ringwald    for struct_name in struct_names:
1811ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(header_template, struct_name))
1821ca3442bSMatthias Ringwaldwriteln(f, "#endif")
1831ca3442bSMatthias Ringwaldwriteln(f, hfile_header_end)
1841ca3442bSMatthias Ringwaldf.close();
1851ca3442bSMatthias Ringwald
1861ca3442bSMatthias Ringwald
1871ca3442bSMatthias Ringwaldf = open(file_name+".c", "w")
1881ca3442bSMatthias Ringwaldwriteln(f, copyright)
1891ca3442bSMatthias Ringwaldwriteln(f, cfile_header_begin)
1901ca3442bSMatthias Ringwaldfor struct_names in list_of_structs:
1911ca3442bSMatthias Ringwald    for struct_name in struct_names:
1921ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(code_template, struct_name))
1931ca3442bSMatthias Ringwald    writeln(f, "")
1941ca3442bSMatthias Ringwaldwriteln(f, "#ifdef HAVE_BLE")
1951ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs:
1961ca3442bSMatthias Ringwald    for struct_name in struct_names:
1971ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(code_template, struct_name))
1981ca3442bSMatthias Ringwald    writeln(f, "")
1991ca3442bSMatthias Ringwaldwriteln(f, "#endif")
2001ca3442bSMatthias Ringwald
2011ca3442bSMatthias Ringwald
2021ca3442bSMatthias Ringwaldwriteln(f, "// init")
2031ca3442bSMatthias Ringwaldwriteln(f, "void btstack_memory_init(void){")
2041ca3442bSMatthias Ringwaldfor struct_names in list_of_structs:
2051ca3442bSMatthias Ringwald    for struct_name in struct_names:
2061ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(init_template, struct_name))
2071ca3442bSMatthias Ringwaldwriteln(f, "#ifdef HAVE_BLE")
2081ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs:
2091ca3442bSMatthias Ringwald    for struct_name in struct_names:
2101ca3442bSMatthias Ringwald        writeln(f, replacePlaceholder(init_template, struct_name))
2111ca3442bSMatthias Ringwaldwriteln(f, "#endif")
2121ca3442bSMatthias Ringwaldwriteln(f, "}")
2131ca3442bSMatthias Ringwaldf.close();
2141ca3442bSMatthias Ringwald
215