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