xref: /btstack/tool/btstack_memory_generator.py (revision bdf6d26abc1b93e8c1e8665fb025fb5d370ff36e)
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/avdtp_sink.h"
70#include "classic/avdtp_source.h"
71#include "classic/avrcp.h"
72#include "classic/bnep.h"
73#include "classic/btstack_link_key_db.h"
74#include "classic/btstack_link_key_db_memory.h"
75#include "classic/hfp.h"
76#include "classic/hid_host.h"
77#include "classic/rfcomm.h"
78#include "classic/sdp_server.h"
79
80// BLE
81#ifdef ENABLE_BLE
82#include "ble/gatt-service/battery_service_client.h"
83#include "ble/gatt-service/hids_client.h"
84#include "ble/gatt-service/scan_parameters_service_client.h"
85#include "ble/gatt_client.h"
86#include "ble/sm.h"
87#endif
88
89#ifdef ENABLE_MESH
90#include "mesh/mesh_network.h"
91#include "mesh/mesh_keys.h"
92#include "mesh/mesh_virtual_addresses.h"
93#endif
94
95/* API_START */
96
97/**
98 * @brief Initializes BTstack memory pools.
99 */
100void btstack_memory_init(void);
101
102/**
103 * @brief Deinitialize BTstack memory pools
104 * @note if HAVE_MALLOC is defined, all previously allocated buffers are free'd
105 */
106void btstack_memory_deinit(void);
107
108/* API_END */
109"""
110
111hfile_header_end = """
112#if defined __cplusplus
113}
114#endif
115
116#endif // BTSTACK_MEMORY_H
117"""
118
119cfile_header_begin = """
120#define BTSTACK_FILE__ "btstack_memory.c"
121
122
123/*
124 *  btstack_memory.c
125 *
126 *  @brief BTstack memory management via configurable memory pools
127 *
128 *  @note code generated by tool/btstack_memory_generator.py
129 *  @note returnes buffers are initialized with 0
130 *
131 */
132
133#include "btstack_memory.h"
134#include "btstack_memory_pool.h"
135#include "btstack_debug.h"
136
137#include <stdlib.h>
138
139#ifdef ENABLE_MALLOC_TEST
140extern "C" void * test_malloc(size_t size);
141#define malloc test_malloc
142#endif
143
144#ifdef HAVE_MALLOC
145typedef struct btstack_memory_buffer {
146    struct btstack_memory_buffer * next;
147    struct btstack_memory_buffer * prev;
148} btstack_memory_buffer_t;
149
150typedef struct {
151    btstack_memory_buffer_t tracking;
152    void * pointer;
153} test_buffer_t;
154
155static btstack_memory_buffer_t * btstack_memory_malloc_buffers;
156static uint32_t btstack_memory_malloc_counter;
157
158static void btstack_memory_tracking_add(btstack_memory_buffer_t * buffer){
159    btstack_assert(buffer != NULL);
160    if (btstack_memory_malloc_buffers != NULL) {
161        // let current first item prev point to new first item
162        btstack_memory_malloc_buffers->prev = buffer;
163    }
164    buffer->prev = NULL;
165    buffer->next = btstack_memory_malloc_buffers;
166    btstack_memory_malloc_buffers = buffer;
167
168    btstack_memory_malloc_counter++;
169}
170
171static void btstack_memory_tracking_remove(btstack_memory_buffer_t * buffer){
172    btstack_assert(buffer != NULL);
173    if (buffer->prev == NULL){
174        // first item
175        btstack_memory_malloc_buffers = buffer->next;
176    } else {
177        buffer->prev->next = buffer->next;
178    }
179    if (buffer->next != NULL){
180        buffer->next->prev = buffer->prev;
181    }
182
183    btstack_memory_malloc_counter--;
184}
185#endif
186
187void btstack_memory_deinit(void){
188#ifdef HAVE_MALLOC
189    while (btstack_memory_malloc_buffers != NULL){
190        btstack_memory_buffer_t * buffer = btstack_memory_malloc_buffers;
191        btstack_memory_malloc_buffers = buffer->next;
192        free(buffer);
193    }
194    btstack_assert(btstack_memory_malloc_counter == 0);
195#endif
196}
197"""
198
199header_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void);
200void   btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);"""
201
202code_template = """
203// MARK: STRUCT_TYPE
204#if !defined(HAVE_MALLOC) && !defined(POOL_COUNT)
205    #if defined(POOL_COUNT_OLD_NO)
206        #error "Deprecated POOL_COUNT_OLD_NO defined instead of POOL_COUNT. Please update your btstack_config.h to use POOL_COUNT."
207    #else
208        #define POOL_COUNT 0
209    #endif
210#endif
211
212#ifdef POOL_COUNT
213#if POOL_COUNT > 0
214static STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT];
215static btstack_memory_pool_t STRUCT_NAME_pool;
216STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
217    void * buffer = btstack_memory_pool_get(&STRUCT_NAME_pool);
218    if (buffer){
219        memset(buffer, 0, sizeof(STRUCT_TYPE));
220    }
221    return (STRUCT_NAME_t *) buffer;
222}
223void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
224    btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME);
225}
226#else
227STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
228    return NULL;
229}
230void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
231    UNUSED(STRUCT_NAME);
232};
233#endif
234#elif defined(HAVE_MALLOC)
235
236typedef struct {
237    btstack_memory_buffer_t tracking;
238    STRUCT_NAME_t data;
239} btstack_memory_STRUCT_NAME_t;
240
241STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
242    btstack_memory_STRUCT_NAME_t * buffer = (btstack_memory_STRUCT_NAME_t *) malloc(sizeof(btstack_memory_STRUCT_NAME_t));
243    if (buffer){
244        memset(buffer, 0, sizeof(btstack_memory_STRUCT_NAME_t));
245        btstack_memory_tracking_add(&buffer->tracking);
246        return &buffer->data;
247    } else {
248        return NULL;
249    }
250}
251void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
252    // reconstruct buffer start
253    btstack_memory_buffer_t * buffer = &((btstack_memory_buffer_t *) STRUCT_NAME)[-1];
254    btstack_memory_tracking_remove(buffer);
255    free(buffer);
256}
257#endif
258"""
259init_header = '''
260// init
261void btstack_memory_init(void){
262#ifdef HAVE_MALLOC
263    // assert that there is no unexpected padding for combined buffer
264    btstack_assert(sizeof(test_buffer_t) == sizeof(btstack_memory_buffer_t) + sizeof(void *));
265#endif
266
267'''
268
269init_template = """#if POOL_COUNT > 0
270    btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE));
271#endif"""
272
273def writeln(f, data):
274    f.write(data + "\n")
275
276def replacePlaceholder(template, struct_name):
277    struct_type = struct_name + '_t'
278    if struct_name.endswith('try'):
279        pool_count = "MAX_NR_" + struct_name.upper()[:-3] + "TRIES"
280    else:
281        pool_count = "MAX_NR_" + struct_name.upper() + "S"
282    pool_count_old_no = pool_count.replace("MAX_NR_", "MAX_NO_")
283    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)
284    return snippet
285
286list_of_structs = [
287    ["hci_connection"],
288    ["l2cap_service", "l2cap_channel"],
289]
290list_of_classic_structs = [
291    ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"],
292    ["btstack_link_key_db_memory_entry"],
293    ["bnep_service", "bnep_channel"],
294    ["hfp_connection"],
295    ["hid_host_connection"],
296    ["service_record_item"],
297    ["avdtp_stream_endpoint"],
298    ["avdtp_connection"],
299    ["avrcp_connection"],
300    ["avrcp_browsing_connection"],
301]
302list_of_le_structs = [
303    ["battery_service_client", "gatt_client", "hids_client", "scan_parameters_service_client", "sm_lookup_entry", "whitelist_entry"],
304]
305list_of_mesh_structs = [
306    ['mesh_network_pdu', 'mesh_segmented_pdu', 'mesh_upper_transport_pdu', 'mesh_network_key', 'mesh_transport_key', 'mesh_virtual_address', 'mesh_subnet']
307]
308
309btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
310file_name = btstack_root + "/src/btstack_memory"
311print ('Generating %s.[h|c]' % file_name)
312
313f = open(file_name+".h", "w")
314writeln(f, copyright)
315writeln(f, hfile_header_begin)
316for struct_names in list_of_structs:
317    writeln(f, "// "+ ", ".join(struct_names))
318    for struct_name in struct_names:
319        writeln(f, replacePlaceholder(header_template, struct_name))
320    writeln(f, "")
321writeln(f, "#ifdef ENABLE_CLASSIC")
322for struct_names in list_of_classic_structs:
323    writeln(f, "// "+ ", ".join(struct_names))
324    for struct_name in struct_names:
325        writeln(f, replacePlaceholder(header_template, struct_name))
326    writeln(f, "")
327writeln(f, "#endif")
328writeln(f, "#ifdef ENABLE_BLE")
329for struct_names in list_of_le_structs:
330    writeln(f, "// "+ ", ".join(struct_names))
331    for struct_name in struct_names:
332        writeln(f, replacePlaceholder(header_template, struct_name))
333writeln(f, "#endif")
334writeln(f, "#ifdef ENABLE_MESH")
335for struct_names in list_of_mesh_structs:
336    writeln(f, "// "+ ", ".join(struct_names))
337    for struct_name in struct_names:
338        writeln(f, replacePlaceholder(header_template, struct_name))
339writeln(f, "#endif")
340writeln(f, hfile_header_end)
341f.close();
342
343
344f = open(file_name+".c", "w")
345writeln(f, copyright)
346writeln(f, cfile_header_begin)
347for struct_names in list_of_structs:
348    for struct_name in struct_names:
349        writeln(f, replacePlaceholder(code_template, struct_name))
350    writeln(f, "")
351writeln(f, "#ifdef ENABLE_CLASSIC")
352for struct_names in list_of_classic_structs:
353    for struct_name in struct_names:
354        writeln(f, replacePlaceholder(code_template, struct_name))
355    writeln(f, "")
356writeln(f, "#endif")
357writeln(f, "#ifdef ENABLE_BLE")
358for struct_names in list_of_le_structs:
359    for struct_name in struct_names:
360        writeln(f, replacePlaceholder(code_template, struct_name))
361    writeln(f, "")
362writeln(f, "#endif")
363writeln(f, "#ifdef ENABLE_MESH")
364for struct_names in list_of_mesh_structs:
365    for struct_name in struct_names:
366        writeln(f, replacePlaceholder(code_template, struct_name))
367    writeln(f, "")
368writeln(f, "#endif")
369
370f.write(init_header)
371for struct_names in list_of_structs:
372    for struct_name in struct_names:
373        writeln(f, replacePlaceholder(init_template, struct_name))
374writeln(f, "#ifdef ENABLE_CLASSIC")
375for struct_names in list_of_classic_structs:
376    for struct_name in struct_names:
377        writeln(f, replacePlaceholder(init_template, struct_name))
378writeln(f, "#endif")
379writeln(f, "#ifdef ENABLE_BLE")
380for struct_names in list_of_le_structs:
381    for struct_name in struct_names:
382        writeln(f, replacePlaceholder(init_template, struct_name))
383writeln(f, "#endif")
384writeln(f, "#ifdef ENABLE_MESH")
385for struct_names in list_of_mesh_structs:
386    for struct_name in struct_names:
387        writeln(f, replacePlaceholder(init_template, struct_name))
388writeln(f, "#endif")
389writeln(f, "}")
390f.close();
391
392# also generate test code
393test_header = """
394#include <stdint.h>
395#include <stdio.h>
396#include <stdlib.h>
397#include <string.h>
398
399// malloc hook
400static int simulate_no_memory;
401extern "C" void * test_malloc(size_t size);
402void * test_malloc(size_t size){
403    if (simulate_no_memory) return NULL;
404    return malloc(size);
405}
406
407#include "btstack_config.h"
408
409#include "CppUTest/TestHarness.h"
410#include "CppUTest/CommandLineTestRunner.h"
411
412#include "bluetooth_data_types.h"
413#include "btstack_util.h"
414#include "btstack_memory.h"
415
416
417TEST_GROUP(btstack_memory){
418    void setup(void){
419        btstack_memory_init();
420        simulate_no_memory = 0;
421    }
422};
423
424#ifdef HAVE_MALLOC
425TEST(btstack_memory, deinit){
426    // alloc buffers 1,2,3
427    hci_connection_t * buffer_1 = btstack_memory_hci_connection_get();
428    hci_connection_t * buffer_2 = btstack_memory_hci_connection_get();
429    hci_connection_t * buffer_3 = btstack_memory_hci_connection_get();
430    // free first one in list
431    btstack_memory_hci_connection_free(buffer_3);
432    // free second one in list
433    btstack_memory_hci_connection_free(buffer_1);
434    // leave buffer in list
435    (void) buffer_2;
436    btstack_memory_deinit();
437}
438#endif
439
440"""
441
442test_template = """
443
444TEST(btstack_memory, STRUCT_NAME_GetAndFree){
445    STRUCT_NAME_t * context;
446#ifdef HAVE_MALLOC
447    context = btstack_memory_STRUCT_NAME_get();
448    CHECK(context != NULL);
449    btstack_memory_STRUCT_NAME_free(context);
450#else
451#ifdef POOL_COUNT
452    // single
453    context = btstack_memory_STRUCT_NAME_get();
454    CHECK(context != NULL);
455    btstack_memory_STRUCT_NAME_free(context);
456#else
457    // none
458    context = btstack_memory_STRUCT_NAME_get();
459    CHECK(context == NULL);
460    btstack_memory_STRUCT_NAME_free(context);
461#endif
462#endif
463}
464
465TEST(btstack_memory, STRUCT_NAME_NotEnoughBuffers){
466    STRUCT_NAME_t * context;
467#ifdef HAVE_MALLOC
468    simulate_no_memory = 1;
469#else
470#ifdef POOL_COUNT
471    int i;
472    // alloc all static buffers
473    for (i = 0; i < POOL_COUNT; i++){
474        context = btstack_memory_STRUCT_NAME_get();
475        CHECK(context != NULL);
476    }
477#endif
478#endif
479    // get one more
480    context = btstack_memory_STRUCT_NAME_get();
481    CHECK(context == NULL);
482}
483"""
484
485test_footer = """
486int main (int argc, const char * argv[]){
487    return CommandLineTestRunner::RunAllTests(argc, argv);
488}
489"""
490
491file_name = btstack_root + "/test/btstack_memory/btstack_memory_test.c"
492print ('Generating %s' % file_name)
493
494f = open(file_name, "w")
495writeln(f, copyright)
496writeln(f, test_header)
497for struct_names in list_of_structs:
498    for struct_name in struct_names:
499        writeln(f, replacePlaceholder(test_template, struct_name))
500writeln(f, "#ifdef ENABLE_CLASSIC")
501for struct_names in list_of_classic_structs:
502    for struct_name in struct_names:
503        writeln(f, replacePlaceholder(test_template, struct_name))
504writeln(f, "#endif")
505writeln(f, "#ifdef ENABLE_BLE")
506for struct_names in list_of_le_structs:
507    for struct_name in struct_names:
508        writeln(f, replacePlaceholder(test_template, struct_name))
509writeln(f, "#endif")
510writeln(f, "#ifdef ENABLE_MESH")
511for struct_names in list_of_mesh_structs:
512    for struct_name in struct_names:
513        writeln(f, replacePlaceholder(test_template, struct_name))
514writeln(f, "#endif")
515writeln(f, test_footer)