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