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