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