code generator for fixed size memory pool allocation

This commit is contained in:
matthias.ringwald 2011-07-24 08:49:48 +00:00
parent 73cf2b3d2a
commit b30da82314

56
src/btstack_memory_generator.py Executable file
View File

@ -0,0 +1,56 @@
#!/usr/bin/env python
header_template = """void * btstack_memory_STRUCT_NAME_get(void);
void btstack_memory_STRUCT_NAME_free(void *STRUCT_NAME);"""
code_template = """
// MARK: STRUCT_TYPE
#ifdef POOL_COUNT
static STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT];
static memory_pool_t STRUCT_NAME_pool;
void * btstack_memory_STRUCT_NAME_get(void){
return memory_pool_get(&STRUCT_NAME_pool);
}
void btstack_memory_STRUCT_NAME_free(void *STRUCT_NAME){
memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME);
}
#elif defined(HAVE_MALLOC)
void * btstack_memory_STRUCT_NAME_get(void){
return malloc(sizeof(STRUCT_TYPE));
}
void btstack_memory_STRUCT_NAME_free(void *STRUCT_NAME){
free(STRUCT_NAME);
}
#else
#error BTstack needs at least one STRUCT_TYPE, but neither POOL_COUNT nor HAVE_MALLOC are defined
#endif
"""
init_template = """#ifdef POOL_COUNT
memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE));
#endif"""
def replacePlaceholder(template, struct_name):
struct_type = struct_name + '_t'
pool_count = "MAX_NO_" + struct_name.upper() + "S"
snippet = template.replace("STRUCT_TYPE", struct_type).replace("STRUCT_NAME", struct_name).replace("POOL_COUNT", pool_count)
return snippet
list_of_structs = [ "hci_connection", "l2cap_service", "l2cap_channel", "rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel" ]
print "// header file"
for struct_name in list_of_structs:
print replacePlaceholder(header_template, struct_name)
print "// template code"
for struct_name in list_of_structs:
print replacePlaceholder(code_template, struct_name)
print "// init"
print "void btstack_memory_init(void){"
for struct_name in list_of_structs:
print replacePlaceholder(init_template, struct_name)
print "}"