db_mem_device_t split into two

This commit is contained in:
matthias.ringwald 2011-08-26 22:14:31 +00:00
parent acf1987e2a
commit e0e5e28530
4 changed files with 79 additions and 53 deletions

View File

@ -34,7 +34,7 @@
*
* @brief BTstack memory management via configurable memory pools
*
* @note code semi-atuomatically generated by btstack_memory_generate.py
* @note code semi-atuomatically generated by btstack_memory_generator.py
*
*/
@ -65,8 +65,6 @@ void * btstack_memory_hci_connection_get(void){
void btstack_memory_hci_connection_free(void *hci_connection){
free(hci_connection);
}
#else
#error BTstack needs at least one hci_connection_t, but neither MAX_NO_HCI_CONNECTIONS nor HAVE_MALLOC are defined
#endif
@ -107,8 +105,6 @@ void * btstack_memory_l2cap_channel_get(void){
void btstack_memory_l2cap_channel_free(void *l2cap_channel){
free(l2cap_channel);
}
#else
#error BTstack needs at least one l2cap_channel_t, but neither MAX_NO_L2CAP_CHANNELS nor HAVE_MALLOC are defined
#endif
@ -171,22 +167,43 @@ void btstack_memory_rfcomm_channel_free(void *rfcomm_channel){
}
#endif
// MARK: db_mem_device_t
#ifdef MAX_NO_DB_MEM_DEVICES
static db_mem_device_t db_mem_device_storage[MAX_NO_DB_MEM_DEVICES];
static memory_pool_t db_mem_device_pool;
void * btstack_memory_db_mem_device_get(void){
return memory_pool_get(&db_mem_device_pool);
// MARK: db_mem_device_name_t
#ifdef MAX_NO_DB_MEM_DEVICE_NAMES
static db_mem_device_name_t db_mem_device_name_storage[MAX_NO_DB_MEM_DEVICE_NAMES];
static memory_pool_t db_mem_device_name_pool;
void * btstack_memory_db_mem_device_name_get(void){
return memory_pool_get(&db_mem_device_name_pool);
}
void btstack_memory_db_mem_device_free(void *db_mem_device){
memory_pool_free(&db_mem_device_pool, db_mem_device);
void btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
memory_pool_free(&db_mem_device_name_pool, db_mem_device_name);
}
#elif defined(HAVE_MALLOC)
void * btstack_memory_db_mem_device_get(void){
return malloc(sizeof(db_mem_device_t));
void * btstack_memory_db_mem_device_name_get(void){
return malloc(sizeof(db_mem_device_name_t));
}
void btstack_memory_db_mem_device_free(void *db_mem_device){
free(db_mem_device);
void btstack_memory_db_mem_device_name_free(void *db_mem_device_name){
free(db_mem_device_name);
}
#endif
// MARK: db_mem_device_link_key_t
#ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS
static db_mem_device_link_key_t db_mem_device_link_key_storage[MAX_NO_DB_MEM_DEVICE_LINK_KEYS];
static memory_pool_t db_mem_device_link_key_pool;
void * btstack_memory_db_mem_device_link_key_get(void){
return memory_pool_get(&db_mem_device_link_key_pool);
}
void btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
memory_pool_free(&db_mem_device_link_key_pool, db_mem_device_link_key);
}
#elif defined(HAVE_MALLOC)
void * btstack_memory_db_mem_device_link_key_get(void){
return malloc(sizeof(db_mem_device_link_key_t));
}
void btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){
free(db_mem_device_link_key);
}
#endif
@ -210,7 +227,6 @@ void btstack_memory_db_mem_service_free(void *db_mem_service){
}
#endif
// init
void btstack_memory_init(void){
#ifdef MAX_NO_HCI_CONNECTIONS
@ -231,10 +247,13 @@ void btstack_memory_init(void){
#ifdef MAX_NO_RFCOMM_CHANNELS
memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t));
#endif
#ifdef MAX_NO_DB_MEM_DEVICES
memory_pool_create(&db_mem_device_pool, db_mem_device_storage, MAX_NO_DB_MEM_DEVICES, sizeof(db_mem_device_t));
#ifdef MAX_NO_DB_MEM_DEVICE_NAMES
memory_pool_create(&db_mem_device_name_pool, db_mem_device_name_storage, MAX_NO_DB_MEM_DEVICE_NAMES, sizeof(db_mem_device_name_t));
#endif
#ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS
memory_pool_create(&db_mem_device_link_key_pool, db_mem_device_link_key_storage, MAX_NO_DB_MEM_DEVICE_LINK_KEYS, sizeof(db_mem_device_link_key_t));
#endif
#ifdef MAX_NO_DB_MEM_SERVICES
memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t));
#endif
}
}

View File

@ -52,9 +52,9 @@ void * btstack_memory_rfcomm_service_get(void);
void btstack_memory_rfcomm_service_free(void *rfcomm_service);
void * btstack_memory_rfcomm_channel_get(void);
void btstack_memory_rfcomm_channel_free(void *rfcomm_channel);
void * btstack_memory_db_mem_device_get(void);
void btstack_memory_db_mem_device_free(void *db_mem_device);
void * btstack_memory_db_mem_device_name_get(void);
void btstack_memory_db_mem_device_name_free(void *db_mem_device_name);
void * btstack_memory_db_mem_device_link_key_get(void);
void btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key);
void * btstack_memory_db_mem_service_get(void);
void btstack_memory_db_mem_service_free(void *db_mem_service);

View File

@ -69,10 +69,18 @@ typedef struct {
linked_item_t item;
bd_addr_t bd_addr;
link_key_t link_key;
char device_name[MAX_NAME_LEN];
} db_mem_device_t;
typedef struct {
db_mem_device_t device;
link_key_t link_key;
} db_mem_device_link_key_t;
typedef struct {
db_mem_device_t device;
char device_name[MAX_NAME_LEN];
} db_mem_device_name_t;
typedef struct {
// linked list - assert: first field
linked_item_t item;

View File

@ -37,8 +37,10 @@
#include "debug.h"
#include <btstack/utils.h>
#include <btstack/linked_list.h>
static linked_list_t db_mem_devices = NULL;
static linked_list_t db_mem_link_keys = NULL;
static linked_list_t db_mem_names = NULL;
static linked_list_t db_mem_services = NULL;
// Device info
@ -48,9 +50,9 @@ static void db_open(void){
static void db_close(void){
}
static db_mem_device_t * get_item(bd_addr_t *bd_addr) {
static db_mem_device_t * get_item(linked_list_t list, bd_addr_t *bd_addr) {
linked_item_t *it;
for (it = (linked_item_t *) db_mem_devices; it ; it = it->next){
for (it = (linked_item_t *) list; it ; it = it->next){
db_mem_device_t * item = (db_mem_device_t *) it;
if (BD_ADDR_CMP(item->bd_addr, *bd_addr) == 0) {
return item;
@ -59,19 +61,8 @@ static db_mem_device_t * get_item(bd_addr_t *bd_addr) {
return NULL;
}
static void delete_item(bd_addr_t *bd_addr){
device_name_t device_name;
db_mem_device_t * item = get_item(bd_addr);
if (!item) return;
linked_list_remove(&db_mem_devices, (linked_item_t *) item);
btstack_memory_db_mem_device_free(item);
}
static int get_name(bd_addr_t *bd_addr, device_name_t *device_name) {
db_mem_device_t * item = get_item(bd_addr);
db_mem_device_name_t * item = (db_mem_device_name_t *) get_item(db_mem_names, bd_addr);
if (!item) return 0;
@ -80,7 +71,7 @@ static int get_name(bd_addr_t *bd_addr, device_name_t *device_name) {
}
static int get_link_key(bd_addr_t *bd_addr, link_key_t *link_key) {
db_mem_device_t * item = get_item(bd_addr);
db_mem_device_link_key_t * item = (db_mem_device_link_key_t *) get_item(db_mem_link_keys, bd_addr);
if (!item) return 0;
@ -89,7 +80,12 @@ static int get_link_key(bd_addr_t *bd_addr, link_key_t *link_key) {
}
static void delete_link_key(bd_addr_t *bd_addr){
delete_item(bd_addr);
db_mem_device_t * item = get_item(db_mem_link_keys, bd_addr);
if (!item) return;
linked_list_remove(&db_mem_link_keys, (linked_item_t *) item);
btstack_memory_db_mem_device_link_key_free(item);
}
@ -97,32 +93,35 @@ static void put_link_key(bd_addr_t *bd_addr, link_key_t *link_key){
if ( get_link_key(bd_addr, link_key) ) return;
// Record not found, create new one for this device
db_mem_device_t * newItem = btstack_memory_db_mem_device_get();
db_mem_device_link_key_t * newItem = btstack_memory_db_mem_device_link_key_get();
if (!newItem) return;
memcpy(newItem->bd_addr, bd_addr, sizeof(bd_addr_t));
strncpy(newItem->device_name, "", MAX_NAME_LEN);
memcpy(newItem->device.bd_addr, bd_addr, sizeof(bd_addr_t));
memcpy(newItem->link_key, link_key, LINK_KEY_LEN);
linked_list_add(&db_mem_devices, (linked_item_t *) newItem);
linked_list_add(&db_mem_link_keys, (linked_item_t *) newItem);
}
static void delete_name(bd_addr_t *bd_addr){
delete_item(bd_addr);
db_mem_device_t * item = get_item(db_mem_names, bd_addr);
if (!item) return;
linked_list_remove(&db_mem_names, (linked_item_t *) item);
btstack_memory_db_mem_device_name_free(item);
}
static void put_name(bd_addr_t *bd_addr, device_name_t *device_name){
if (get_name(bd_addr, device_name)) return;
// Record not found, create a new one for this device
db_mem_device_t * newItem = btstack_memory_db_mem_device_get();
db_mem_device_name_t * newItem = btstack_memory_db_mem_device_name_get();
if (!newItem) return;
memcpy(newItem->bd_addr, bd_addr, sizeof(bd_addr_t));
memcpy(newItem->device.bd_addr, bd_addr, sizeof(bd_addr_t));
strncpy(newItem->device_name, (const char*) device_name, MAX_NAME_LEN);
memset(newItem->link_key, 0, LINK_KEY_LEN);
linked_list_add(&db_mem_devices, (linked_item_t *) newItem);
linked_list_add(&db_mem_names, (linked_item_t *) newItem);
}