xref: /btstack/tool/btstack_memory_generator.py (revision f12924e0a61c0582b548a1e70cea1bd59ba21f1d)
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#include "classic/avdtp_sink.h"
71#include "classic/avdtp_source.h"
72
73// BLE
74#ifdef ENABLE_BLE
75#include "ble/gatt_client.h"
76#include "ble/sm.h"
77#endif
78
79/* API_START */
80
81/**
82 * @brief Initializes BTstack memory pools.
83 */
84void btstack_memory_init(void);
85
86/* API_END */
87"""
88
89hfile_header_end = """
90#if defined __cplusplus
91}
92#endif
93
94#endif // __BTSTACK_MEMORY_H
95"""
96
97cfile_header_begin = """
98/*
99 *  btstack_memory.h
100 *
101 *  @brief BTstack memory management via configurable memory pools
102 *
103 *  @note code generated by tool/btstack_memory_generator.py
104 *
105 */
106
107#include "btstack_memory.h"
108#include "btstack_memory_pool.h"
109
110#include <stdlib.h>
111
112"""
113
114header_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void);
115void   btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);"""
116
117code_template = """
118// MARK: STRUCT_TYPE
119#if !defined(HAVE_MALLOC) && !defined(POOL_COUNT)
120    #if defined(POOL_COUNT_OLD_NO)
121        #error "Deprecated POOL_COUNT_OLD_NO defined instead of POOL_COUNT. Please update your btstack_config.h to use POOL_COUNT."
122    #else
123        #define POOL_COUNT 0
124    #endif
125#endif
126
127#ifdef POOL_COUNT
128#if POOL_COUNT > 0
129static STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT];
130static btstack_memory_pool_t STRUCT_NAME_pool;
131STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
132    return (STRUCT_NAME_t *) btstack_memory_pool_get(&STRUCT_NAME_pool);
133}
134void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
135    btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME);
136}
137#else
138STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
139    return NULL;
140}
141void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
142    // silence compiler warning about unused parameter in a portable way
143    (void) STRUCT_NAME;
144};
145#endif
146#elif defined(HAVE_MALLOC)
147STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
148    return (STRUCT_NAME_t*) malloc(sizeof(STRUCT_TYPE));
149}
150void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
151    free(STRUCT_NAME);
152}
153#endif
154"""
155
156init_template = """#if POOL_COUNT > 0
157    btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE));
158#endif"""
159
160def writeln(f, data):
161    f.write(data + "\n")
162
163def replacePlaceholder(template, struct_name):
164    struct_type = struct_name + '_t'
165    if struct_name.endswith('try'):
166        pool_count = "MAX_NR_" + struct_name.upper()[:-3] + "TRIES"
167    else:
168        pool_count = "MAX_NR_" + struct_name.upper() + "S"
169    pool_count_old_no = pool_count.replace("MAX_NR_", "MAX_NO_")
170    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)
171    return snippet
172
173list_of_structs = [
174    ["hci_connection"],
175    ["l2cap_service", "l2cap_channel"],
176    ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"],
177    ["btstack_link_key_db_memory_entry"],
178    ["bnep_service", "bnep_channel"],
179    ["hfp_connection"],
180    ["service_record_item"],
181    ["avdtp_stream_endpoint"],
182    ["avdtp_connection"]
183]
184list_of_le_structs = [["gatt_client", "whitelist_entry", "sm_lookup_entry"]]
185
186file_name = "../src/btstack_memory"
187
188
189f = open(file_name+".h", "w")
190writeln(f, copyright)
191writeln(f, hfile_header_begin)
192for struct_names in list_of_structs:
193    writeln(f, "// "+ ", ".join(struct_names))
194    for struct_name in struct_names:
195        writeln(f, replacePlaceholder(header_template, struct_name))
196    writeln(f, "")
197writeln(f, "#ifdef ENABLE_BLE")
198for struct_names in list_of_le_structs:
199    writeln(f, "// "+ ", ".join(struct_names))
200    for struct_name in struct_names:
201        writeln(f, replacePlaceholder(header_template, struct_name))
202writeln(f, "#endif")
203writeln(f, hfile_header_end)
204f.close();
205
206
207f = open(file_name+".c", "w")
208writeln(f, copyright)
209writeln(f, cfile_header_begin)
210for struct_names in list_of_structs:
211    for struct_name in struct_names:
212        writeln(f, replacePlaceholder(code_template, struct_name))
213    writeln(f, "")
214writeln(f, "#ifdef ENABLE_BLE")
215for struct_names in list_of_le_structs:
216    for struct_name in struct_names:
217        writeln(f, replacePlaceholder(code_template, struct_name))
218    writeln(f, "")
219writeln(f, "#endif")
220
221
222writeln(f, "// init")
223writeln(f, "void btstack_memory_init(void){")
224for struct_names in list_of_structs:
225    for struct_name in struct_names:
226        writeln(f, replacePlaceholder(init_template, struct_name))
227writeln(f, "#ifdef ENABLE_BLE")
228for struct_names in list_of_le_structs:
229    for struct_name in struct_names:
230        writeln(f, replacePlaceholder(init_template, struct_name))
231writeln(f, "#endif")
232writeln(f, "}")
233f.close();
234
235