xref: /btstack/tool/btstack_memory_generator.py (revision 039cbf1d6ce84311e8e410ce991e37f8e522b370)
1#!/usr/bin/env python3
2import os
3import sys
4
5import os
6import sys
7
8copyright = """/*
9 * Copyright (C) 2014 BlueKitchen GmbH
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the copyright holders nor the names of
21 *    contributors may be used to endorse or promote products derived
22 *    from this software without specific prior written permission.
23 * 4. Any redistribution, use, or modification is done solely for
24 *    personal benefit and not for any commercial purpose or for
25 *    monetary gain.
26 *
27 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
31 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
35 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
37 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * Please inquire about commercial licensing options at
41 * [email protected]
42 *
43 */
44"""
45
46hfile_header_begin = """
47
48/*
49 *  btstack_memory.h
50 *
51 *  @brief BTstack memory management via configurable memory pools
52 *
53 */
54
55#ifndef BTSTACK_MEMORY_H
56#define BTSTACK_MEMORY_H
57
58#if defined __cplusplus
59extern "C" {
60#endif
61
62#include "btstack_config.h"
63
64// Core
65#include "hci.h"
66#include "l2cap.h"
67
68// Classic
69#include "classic/bnep.h"
70#include "classic/hfp.h"
71#include "classic/btstack_link_key_db.h"
72#include "classic/btstack_link_key_db_memory.h"
73#include "classic/rfcomm.h"
74#include "classic/sdp_server.h"
75#include "classic/avdtp_sink.h"
76#include "classic/avdtp_source.h"
77#include "classic/avrcp.h"
78
79// BLE
80#ifdef ENABLE_BLE
81#include "ble/gatt_client.h"
82#include "ble/sm.h"
83#endif
84
85#ifdef ENABLE_MESH
86#include "mesh/mesh_network.h"
87#include "mesh/mesh_keys.h"
88#include "mesh/mesh_virtual_addresses.h"
89#endif
90
91/* API_START */
92
93/**
94 * @brief Initializes BTstack memory pools.
95 */
96void btstack_memory_init(void);
97
98/* API_END */
99"""
100
101hfile_header_end = """
102#if defined __cplusplus
103}
104#endif
105
106#endif // BTSTACK_MEMORY_H
107"""
108
109cfile_header_begin = """
110#define BTSTACK_FILE__ "btstack_memory.c"
111
112
113/*
114 *  btstack_memory.c
115 *
116 *  @brief BTstack memory management via configurable memory pools
117 *
118 *  @note code generated by tool/btstack_memory_generator.py
119 *  @note returnes buffers are initialized with 0
120 *
121 */
122
123#include "btstack_memory.h"
124#include "btstack_memory_pool.h"
125
126#include <stdlib.h>
127
128"""
129
130header_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void);
131void   btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);"""
132
133code_template = """
134// MARK: STRUCT_TYPE
135#if !defined(HAVE_MALLOC) && !defined(POOL_COUNT)
136    #if defined(POOL_COUNT_OLD_NO)
137        #error "Deprecated POOL_COUNT_OLD_NO defined instead of POOL_COUNT. Please update your btstack_config.h to use POOL_COUNT."
138    #else
139        #define POOL_COUNT 0
140    #endif
141#endif
142
143#ifdef POOL_COUNT
144#if POOL_COUNT > 0
145static STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT];
146static btstack_memory_pool_t STRUCT_NAME_pool;
147STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
148    void * buffer = btstack_memory_pool_get(&STRUCT_NAME_pool);
149    if (buffer){
150        memset(buffer, 0, sizeof(STRUCT_TYPE));
151    }
152    return (STRUCT_NAME_t *) buffer;
153}
154void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
155    btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME);
156}
157#else
158STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
159    return NULL;
160}
161void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
162    // silence compiler warning about unused parameter in a portable way
163    (void) STRUCT_NAME;
164};
165#endif
166#elif defined(HAVE_MALLOC)
167STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
168    void * buffer = malloc(sizeof(STRUCT_TYPE));
169    if (buffer){
170        memset(buffer, 0, sizeof(STRUCT_TYPE));
171    }
172    return (STRUCT_NAME_t *) buffer;
173}
174void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
175    free(STRUCT_NAME);
176}
177#endif
178"""
179
180init_template = """#if POOL_COUNT > 0
181    btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE));
182#endif"""
183
184def writeln(f, data):
185    f.write(data + "\n")
186
187def replacePlaceholder(template, struct_name):
188    struct_type = struct_name + '_t'
189    if struct_name.endswith('try'):
190        pool_count = "MAX_NR_" + struct_name.upper()[:-3] + "TRIES"
191    else:
192        pool_count = "MAX_NR_" + struct_name.upper() + "S"
193    pool_count_old_no = pool_count.replace("MAX_NR_", "MAX_NO_")
194    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)
195    return snippet
196
197list_of_structs = [
198    ["hci_connection"],
199    ["l2cap_service", "l2cap_channel"],
200]
201list_of_classic_structs = [
202    ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"],
203    ["btstack_link_key_db_memory_entry"],
204    ["bnep_service", "bnep_channel"],
205    ["hfp_connection"],
206    ["service_record_item"],
207    ["avdtp_stream_endpoint"],
208    ["avdtp_connection"],
209    ["avrcp_connection"],
210    ["avrcp_browsing_connection"],
211]
212list_of_le_structs = [
213    ["gatt_client", "whitelist_entry", "sm_lookup_entry"],
214]
215list_of_mesh_structs = [
216    ['mesh_network_pdu', 'mesh_segmented_pdu', 'mesh_upper_transport_pdu', 'mesh_network_key', 'mesh_transport_key', 'mesh_virtual_address', 'mesh_subnet']
217]
218
219btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
220file_name = btstack_root + "/src/btstack_memory"
221print ('Generating %s.[h|c]' % file_name)
222
223f = open(file_name+".h", "w")
224writeln(f, copyright)
225writeln(f, hfile_header_begin)
226for struct_names in list_of_structs:
227    writeln(f, "// "+ ", ".join(struct_names))
228    for struct_name in struct_names:
229        writeln(f, replacePlaceholder(header_template, struct_name))
230    writeln(f, "")
231writeln(f, "#ifdef ENABLE_CLASSIC")
232for struct_names in list_of_classic_structs:
233    writeln(f, "// "+ ", ".join(struct_names))
234    for struct_name in struct_names:
235        writeln(f, replacePlaceholder(header_template, struct_name))
236    writeln(f, "")
237writeln(f, "#endif")
238writeln(f, "#ifdef ENABLE_BLE")
239for struct_names in list_of_le_structs:
240    writeln(f, "// "+ ", ".join(struct_names))
241    for struct_name in struct_names:
242        writeln(f, replacePlaceholder(header_template, struct_name))
243writeln(f, "#endif")
244writeln(f, "#ifdef ENABLE_MESH")
245for struct_names in list_of_mesh_structs:
246    writeln(f, "// "+ ", ".join(struct_names))
247    for struct_name in struct_names:
248        writeln(f, replacePlaceholder(header_template, struct_name))
249writeln(f, "#endif")
250writeln(f, hfile_header_end)
251f.close();
252
253
254f = open(file_name+".c", "w")
255writeln(f, copyright)
256writeln(f, cfile_header_begin)
257for struct_names in list_of_structs:
258    for struct_name in struct_names:
259        writeln(f, replacePlaceholder(code_template, struct_name))
260    writeln(f, "")
261writeln(f, "#ifdef ENABLE_CLASSIC")
262for struct_names in list_of_classic_structs:
263    for struct_name in struct_names:
264        writeln(f, replacePlaceholder(code_template, struct_name))
265    writeln(f, "")
266writeln(f, "#endif")
267writeln(f, "#ifdef ENABLE_BLE")
268for struct_names in list_of_le_structs:
269    for struct_name in struct_names:
270        writeln(f, replacePlaceholder(code_template, struct_name))
271    writeln(f, "")
272writeln(f, "#endif")
273writeln(f, "#ifdef ENABLE_MESH")
274for struct_names in list_of_mesh_structs:
275    for struct_name in struct_names:
276        writeln(f, replacePlaceholder(code_template, struct_name))
277    writeln(f, "")
278writeln(f, "#endif")
279
280
281writeln(f, "// init")
282writeln(f, "void btstack_memory_init(void){")
283for struct_names in list_of_structs:
284    for struct_name in struct_names:
285        writeln(f, replacePlaceholder(init_template, struct_name))
286writeln(f, "#ifdef ENABLE_CLASSIC")
287for struct_names in list_of_classic_structs:
288    for struct_name in struct_names:
289        writeln(f, replacePlaceholder(init_template, struct_name))
290writeln(f, "#endif")
291writeln(f, "#ifdef ENABLE_BLE")
292for struct_names in list_of_le_structs:
293    for struct_name in struct_names:
294        writeln(f, replacePlaceholder(init_template, struct_name))
295writeln(f, "#endif")
296writeln(f, "#ifdef ENABLE_MESH")
297for struct_names in list_of_mesh_structs:
298    for struct_name in struct_names:
299        writeln(f, replacePlaceholder(init_template, struct_name))
300writeln(f, "#endif")
301writeln(f, "}")
302f.close();
303
304