xref: /btstack/tool/btstack_memory_generator.py (revision 2c455dad9a589f886e28f6a5d10bb735305e4f1b)
1#!/usr/bin/env python
2
3copyright = """/*
4 * Copyright (C) 2014 BlueKitchen GmbH
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the copyright holders nor the names of
16 *    contributors may be used to endorse or promote products derived
17 *    from this software without specific prior written permission.
18 * 4. Any redistribution, use, or modification is done solely for
19 *    personal benefit and not for any commercial purpose or for
20 *    monetary gain.
21 *
22 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
26 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
32 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * Please inquire about commercial licensing options at
36 * [email protected]
37 *
38 */
39"""
40
41hfile_header_begin = """
42
43/*
44 *  btstack_memory.h
45 *
46 *  @brief BTstack memory management via configurable memory pools
47 *
48 */
49
50#ifndef __BTSTACK_MEMORY_H
51#define __BTSTACK_MEMORY_H
52
53#if defined __cplusplus
54extern "C" {
55#endif
56
57#include "btstack_config.h"
58
59// Core
60#include "hci.h"
61#include "l2cap.h"
62
63// Classic
64#include "classic/bnep.h"
65#include "classic/hfp.h"
66#include "classic/btstack_link_key_db.h"
67#include "classic/btstack_link_key_db_memory.h"
68#include "classic/rfcomm.h"
69#include "classic/sdp_server.h"
70
71// BLE
72#ifdef ENABLE_BLE
73#include "ble/gatt_client.h"
74#include "ble/sm.h"
75#endif
76
77/* API_START */
78
79/**
80 * @brief Initializes BTstack memory pools.
81 */
82void btstack_memory_init(void);
83
84/* API_END */
85"""
86
87hfile_header_end = """
88#if defined __cplusplus
89}
90#endif
91
92#endif // __BTSTACK_MEMORY_H
93"""
94
95cfile_header_begin = """
96/*
97 *  btstack_memory.h
98 *
99 *  @brief BTstack memory management via configurable memory pools
100 *
101 *  @note code generated by tool/btstack_memory_generator.py
102 *
103 */
104
105#include "btstack_memory.h"
106#include "btstack_memory_pool.h"
107
108#include <stdlib.h>
109
110"""
111
112header_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void);
113void   btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);"""
114
115code_template = """
116// MARK: STRUCT_TYPE
117#ifdef POOL_COUNT
118#if POOL_COUNT > 0
119static STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT];
120static btstack_memory_pool_t STRUCT_NAME_pool;
121STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
122    return (STRUCT_NAME_t *) btstack_memory_pool_get(&STRUCT_NAME_pool);
123}
124void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
125    btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME);
126}
127#else
128STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
129    return NULL;
130}
131void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
132    // silence compiler warning about unused parameter in a portable way
133    (void) STRUCT_NAME;
134};
135#endif
136#elif defined(HAVE_MALLOC)
137STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
138    return (STRUCT_NAME_t*) malloc(sizeof(STRUCT_TYPE));
139}
140void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
141    free(STRUCT_NAME);
142}
143#else
144#error "Neither HAVE_MALLOC nor POOL_COUNT for struct STRUCT_NAME is defined. Please, edit the config file."
145#endif
146"""
147
148init_template = """#if POOL_COUNT > 0
149    btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE));
150#endif"""
151
152def writeln(f, data):
153    f.write(data + "\n")
154
155def replacePlaceholder(template, struct_name):
156    struct_type = struct_name + '_t'
157    if struct_name.endswith('try'):
158        pool_count = "MAX_NO_" + struct_name.upper()[:-3] + "TRIES"
159    else:
160        pool_count = "MAX_NO_" + struct_name.upper() + "S"
161
162    snippet = template.replace("STRUCT_TYPE", struct_type).replace("STRUCT_NAME", struct_name).replace("POOL_COUNT", pool_count)
163    return snippet
164
165list_of_structs = [
166    ["hci_connection"],
167    ["l2cap_service", "l2cap_channel"],
168    ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"],
169    ["btstack_link_key_db_memory_entry"],
170    ["bnep_service", "bnep_channel"],
171    ["hfp_connection"],
172    ["service_record_item"]
173]
174list_of_le_structs = [["gatt_client", "whitelist_entry", "sm_lookup_entry"]]
175
176file_name = "../src/btstack_memory"
177
178
179f = open(file_name+".h", "w")
180writeln(f, copyright)
181writeln(f, hfile_header_begin)
182for struct_names in list_of_structs:
183    writeln(f, "// "+ ", ".join(struct_names))
184    for struct_name in struct_names:
185        writeln(f, replacePlaceholder(header_template, struct_name))
186    writeln(f, "")
187writeln(f, "#ifdef ENABLE_BLE")
188for struct_names in list_of_le_structs:
189    writeln(f, "// "+ ", ".join(struct_names))
190    for struct_name in struct_names:
191        writeln(f, replacePlaceholder(header_template, struct_name))
192writeln(f, "#endif")
193writeln(f, hfile_header_end)
194f.close();
195
196
197f = open(file_name+".c", "w")
198writeln(f, copyright)
199writeln(f, cfile_header_begin)
200for struct_names in list_of_structs:
201    for struct_name in struct_names:
202        writeln(f, replacePlaceholder(code_template, struct_name))
203    writeln(f, "")
204writeln(f, "#ifdef ENABLE_BLE")
205for struct_names in list_of_le_structs:
206    for struct_name in struct_names:
207        writeln(f, replacePlaceholder(code_template, struct_name))
208    writeln(f, "")
209writeln(f, "#endif")
210
211
212writeln(f, "// init")
213writeln(f, "void btstack_memory_init(void){")
214for struct_names in list_of_structs:
215    for struct_name in struct_names:
216        writeln(f, replacePlaceholder(init_template, struct_name))
217writeln(f, "#ifdef ENABLE_BLE")
218for struct_names in list_of_le_structs:
219    for struct_name in struct_names:
220        writeln(f, replacePlaceholder(init_template, struct_name))
221writeln(f, "#endif")
222writeln(f, "}")
223f.close();
224
225