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