xref: /btstack/tool/btstack_memory_generator.py (revision 898500f031ad1a9e2b47e464fc0a657f7ae298ac)
1#!/usr/bin/env python
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 "ble/mesh/mesh_network.h"
87#endif
88
89/* API_START */
90
91/**
92 * @brief Initializes BTstack memory pools.
93 */
94void btstack_memory_init(void);
95
96/* API_END */
97"""
98
99hfile_header_end = """
100#if defined __cplusplus
101}
102#endif
103
104#endif // BTSTACK_MEMORY_H
105"""
106
107cfile_header_begin = """
108/*
109 *  btstack_memory.h
110 *
111 *  @brief BTstack memory management via configurable memory pools
112 *
113 *  @note code generated by tool/btstack_memory_generator.py
114 *  @note returnes buffers are initialized with 0
115 *
116 */
117
118#include "btstack_memory.h"
119#include "btstack_memory_pool.h"
120
121#include <stdlib.h>
122
123"""
124
125header_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void);
126void   btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);"""
127
128code_template = """
129// MARK: STRUCT_TYPE
130#if !defined(HAVE_MALLOC) && !defined(POOL_COUNT)
131    #if defined(POOL_COUNT_OLD_NO)
132        #error "Deprecated POOL_COUNT_OLD_NO defined instead of POOL_COUNT. Please update your btstack_config.h to use POOL_COUNT."
133    #else
134        #define POOL_COUNT 0
135    #endif
136#endif
137
138#ifdef POOL_COUNT
139#if POOL_COUNT > 0
140static STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT];
141static btstack_memory_pool_t STRUCT_NAME_pool;
142STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
143    void * buffer = btstack_memory_pool_get(&STRUCT_NAME_pool);
144    if (buffer){
145        memset(buffer, 0, sizeof(STRUCT_TYPE));
146    }
147    return (STRUCT_NAME_t *) buffer;
148}
149void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
150    btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME);
151}
152#else
153STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
154    return NULL;
155}
156void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
157    // silence compiler warning about unused parameter in a portable way
158    (void) STRUCT_NAME;
159};
160#endif
161#elif defined(HAVE_MALLOC)
162STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
163    void * buffer = malloc(sizeof(STRUCT_TYPE));
164    if (buffer){
165        memset(buffer, 0, sizeof(STRUCT_TYPE));
166    }
167    return (STRUCT_NAME_t *) buffer;
168}
169void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
170    free(STRUCT_NAME);
171}
172#endif
173"""
174
175init_template = """#if POOL_COUNT > 0
176    btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE));
177#endif"""
178
179def writeln(f, data):
180    f.write(data + "\n")
181
182def replacePlaceholder(template, struct_name):
183    struct_type = struct_name + '_t'
184    if struct_name.endswith('try'):
185        pool_count = "MAX_NR_" + struct_name.upper()[:-3] + "TRIES"
186    else:
187        pool_count = "MAX_NR_" + struct_name.upper() + "S"
188    pool_count_old_no = pool_count.replace("MAX_NR_", "MAX_NO_")
189    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)
190    return snippet
191
192list_of_structs = [
193    ["hci_connection"],
194    ["l2cap_service", "l2cap_channel"],
195]
196list_of_classic_structs = [
197    ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"],
198    ["btstack_link_key_db_memory_entry"],
199    ["bnep_service", "bnep_channel"],
200    ["hfp_connection"],
201    ["service_record_item"],
202    ["avdtp_stream_endpoint"],
203    ["avdtp_connection"],
204    ["avrcp_connection"],
205    ["avrcp_browsing_connection"],
206]
207list_of_le_structs = [
208    ["gatt_client", "whitelist_entry", "sm_lookup_entry"],
209]
210list_of_mesh_structs = [
211    ['mesh_network_pdu', 'mesh_access_pdu', 'mesh_network_key']
212]
213
214btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
215file_name = btstack_root + "/src/btstack_memory"
216print ('Generating %s.[h|c]' % file_name)
217
218f = open(file_name+".h", "w")
219writeln(f, copyright)
220writeln(f, hfile_header_begin)
221for struct_names in list_of_structs:
222    writeln(f, "// "+ ", ".join(struct_names))
223    for struct_name in struct_names:
224        writeln(f, replacePlaceholder(header_template, struct_name))
225    writeln(f, "")
226writeln(f, "#ifdef ENABLE_CLASSIC")
227for struct_names in list_of_classic_structs:
228    writeln(f, "// "+ ", ".join(struct_names))
229    for struct_name in struct_names:
230        writeln(f, replacePlaceholder(header_template, struct_name))
231    writeln(f, "")
232writeln(f, "#endif")
233writeln(f, "#ifdef ENABLE_BLE")
234for struct_names in list_of_le_structs:
235    writeln(f, "// "+ ", ".join(struct_names))
236    for struct_name in struct_names:
237        writeln(f, replacePlaceholder(header_template, struct_name))
238writeln(f, "#endif")
239writeln(f, "#ifdef ENABLE_MESH")
240for struct_names in list_of_mesh_structs:
241    writeln(f, "// "+ ", ".join(struct_names))
242    for struct_name in struct_names:
243        writeln(f, replacePlaceholder(header_template, struct_name))
244writeln(f, "#endif")
245writeln(f, hfile_header_end)
246f.close();
247
248
249f = open(file_name+".c", "w")
250writeln(f, copyright)
251writeln(f, cfile_header_begin)
252for struct_names in list_of_structs:
253    for struct_name in struct_names:
254        writeln(f, replacePlaceholder(code_template, struct_name))
255    writeln(f, "")
256writeln(f, "#ifdef ENABLE_CLASSIC")
257for struct_names in list_of_classic_structs:
258    for struct_name in struct_names:
259        writeln(f, replacePlaceholder(code_template, struct_name))
260    writeln(f, "")
261writeln(f, "#endif")
262writeln(f, "#ifdef ENABLE_BLE")
263for struct_names in list_of_le_structs:
264    for struct_name in struct_names:
265        writeln(f, replacePlaceholder(code_template, struct_name))
266    writeln(f, "")
267writeln(f, "#endif")
268writeln(f, "#ifdef ENABLE_MESH")
269for struct_names in list_of_mesh_structs:
270    for struct_name in struct_names:
271        writeln(f, replacePlaceholder(code_template, struct_name))
272    writeln(f, "")
273writeln(f, "#endif")
274
275
276writeln(f, "// init")
277writeln(f, "void btstack_memory_init(void){")
278for struct_names in list_of_structs:
279    for struct_name in struct_names:
280        writeln(f, replacePlaceholder(init_template, struct_name))
281writeln(f, "#ifdef ENABLE_CLASSIC")
282for struct_names in list_of_le_structs:
283    for struct_name in struct_names:
284        writeln(f, replacePlaceholder(init_template, struct_name))
285writeln(f, "#endif")
286writeln(f, "#ifdef ENABLE_BLE")
287for struct_names in list_of_le_structs:
288    for struct_name in struct_names:
289        writeln(f, replacePlaceholder(init_template, struct_name))
290writeln(f, "#endif")
291writeln(f, "#ifdef ENABLE_MESH")
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, "}")
297f.close();
298
299