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