Refactored the code, removed void pointer arithmetic, and fixed invalid list removal after freeing of memb object.

This commit is contained in:
nvt-se 2010-06-11 12:32:48 +00:00
parent f3f6a0de24
commit 75b624eb71

View file

@ -58,13 +58,15 @@ LIST(neighbor_attrs);
static struct neighbor_addr *
neighbor_addr_get(const rimeaddr_t *addr)
{
/* check if addr is derived from table, inside memb */
if(memb_inmemb(&neighbor_addr_mem, (struct queuebuf *)addr)) {
return (struct neighbor_addr *)
(((void *)addr) - offsetof(struct neighbor_addr, addr));
}
struct neighbor_addr *item = list_head(neighbor_addrs);
struct neighbor_addr *item;
/* check if addr is derived from table, inside memb */
if(memb_inmemb(&neighbor_addr_mem, (char *)addr)) {
return (struct neighbor_addr *)
(((char *)addr) - offsetof(struct neighbor_addr, addr));
}
item = list_head(neighbor_addrs);
while(item != NULL) {
if(rimeaddr_cmp(addr, &item->addr)) {
return item;
@ -80,22 +82,28 @@ neighbor_attr_list_neighbors(void)
return list_head(neighbor_addrs);
}
/*---------------------------------------------------------------------------*/
static void
set_attr(struct neighbor_attr *attr, uint16_t index)
{
if(attr->default_value != NULL) {
memcpy((char *)attr->data + index * attr->size,
attr->default_value, attr->size);
} else {
/* fill with zeroes */
memset((char *)attr->data + index * attr->size, 0, attr->size);
}
}
/*---------------------------------------------------------------------------*/
int
neighbor_attr_register(struct neighbor_attr *def)
{
list_push(neighbor_attrs, def);
/* set default values for already existing neighbors */
struct neighbor_addr *addr = list_head(neighbor_addrs);
struct neighbor_addr *addr;
while(addr != NULL) {
if(def->default_value != NULL) {
memcpy(def->data + addr->index * def->size, def->default_value,
def->size);
} else {
/* fill with zeroes */
memset(def->data + addr->index * def->size, 0, def->size);
}
addr = addr->next;
list_push(neighbor_attrs, def);
/* set default values for already existing neighbors */
for(addr = list_head(neighbor_addrs); addr != NULL; addr = addr->next) {
set_attr(def, addr->index);
}
return 1;
}
@ -109,12 +117,16 @@ neighbor_attr_has_neighbor(const rimeaddr_t *addr)
int
neighbor_attr_add_neighbor(const rimeaddr_t *addr)
{
struct neighbor_attr *def;
struct neighbor_addr *item;
struct neighbor_addr *ptr;
uint16_t i;
if(neighbor_attr_has_neighbor(addr)) {
return 0;
}
struct neighbor_addr *item = memb_alloc(&neighbor_addr_mem);
item = memb_alloc(&neighbor_addr_mem);
if(item == NULL) {
return -1;
}
@ -125,9 +137,7 @@ neighbor_attr_add_neighbor(const rimeaddr_t *addr)
rimeaddr_copy(&item->addr, addr);
/* look up index and set default values */
uint16_t i;
struct neighbor_addr *ptr = neighbor_addr_mem.mem;
ptr = neighbor_addr_mem.mem;
for(i = 0; i < neighbor_addr_mem.num; ++i) {
if(&ptr[i] == item) {
break;
@ -136,16 +146,8 @@ neighbor_attr_add_neighbor(const rimeaddr_t *addr)
item->index = i;
struct neighbor_attr *def = list_head(neighbor_attrs);
while(def != NULL) {
if(def->default_value != NULL) {
memcpy(def->data + i * def->size, def->default_value, def->size);
} else {
/* fill with zeroes */
memset(def->data + i * def->size, 0, def->size);
}
def = def->next;
for(def = list_head(neighbor_attrs); def != NULL; def = def->next) {
set_attr(def, i);
}
return 1;
@ -154,16 +156,12 @@ neighbor_attr_add_neighbor(const rimeaddr_t *addr)
int
neighbor_attr_remove_neighbor(const rimeaddr_t *addr)
{
struct neighbor_addr *item = neighbor_attr_list_neighbors();
struct neighbor_addr *item = neighbor_addr_get(addr);
while(item != NULL) {
if(rimeaddr_cmp(&item->addr, addr)) {
memb_free(&neighbor_addr_mem, item);
list_remove(neighbor_addrs, item);
return 0;
} else {
item = item->next;
}
if(item != NULL) {
list_remove(neighbor_addrs, item);
memb_free(&neighbor_addr_mem, item);
return 0;
}
return -1;
}
@ -174,7 +172,7 @@ neighbor_attr_get_data(struct neighbor_attr *def, const rimeaddr_t *addr)
struct neighbor_addr *attr = neighbor_addr_get(addr);
if(attr != NULL) {
return (void *)(def->data + attr->index * def->size);
return (char *)def->data + attr->index * def->size;
}
return NULL;
}
@ -192,7 +190,7 @@ neighbor_attr_set_data(struct neighbor_attr *def, const rimeaddr_t *addr,
}
if(attr != NULL) {
attr->time = 0;
memcpy((void *)(def->data + attr->index * def->size), data, def->size);
memcpy((char *)def->data + attr->index * def->size, data, def->size);
return 1;
}
return 0;
@ -228,8 +226,8 @@ timeout_check(void *ptr)
if(item->time >= timeout) {
struct neighbor_addr *next_item = item->next;
memb_free(&neighbor_addr_mem, item);
list_remove(neighbor_addrs, item);
memb_free(&neighbor_addr_mem, item);
item = next_item;
} else {
item = item->next;