btstack/tools/btstack_memory_generator.py

64 lines
2.0 KiB
Python
Raw Normal View History

#!/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
2012-05-21 18:24:37 +00:00
#if POOL_COUNT > 0
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);
}
2012-05-21 18:24:37 +00:00
void btstack_memory_STRUCT_NAME_free(void *STRUCT_NAME){
memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME);
}
2012-05-21 18:24:37 +00:00
#else
void * btstack_memory_STRUCT_NAME_get(void){
return NULL;
}
void btstack_memory_STRUCT_NAME_free(void *STRUCT_NAME){
};
#endif
#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);
}
2012-11-07 17:31:37 +00:00
#else
2013-03-01 00:18:13 +00:00
#error "Neither HAVE_MALLOC nor POOL_COUNT for struct STRUCT_NAME is defined. Please, edit the config file."
#endif
"""
2012-05-21 18:24:37 +00:00
init_template = """#if POOL_COUNT > 0
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
2011-08-26 22:14:05 +00:00
list_of_structs = [ "hci_connection", "l2cap_service", "l2cap_channel", "rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel", "db_mem_device_name", "db_mem_device_link_key", "db_mem_service"]
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 "}"