Refactored the code, removed void pointer arithmetic, and fixed invalid list removal after freeing of memb object.
This commit is contained in:
parent
f3f6a0de24
commit
75b624eb71
1 changed files with 43 additions and 45 deletions
|
@ -58,13 +58,15 @@ LIST(neighbor_attrs);
|
||||||
static struct neighbor_addr *
|
static struct neighbor_addr *
|
||||||
neighbor_addr_get(const rimeaddr_t *addr)
|
neighbor_addr_get(const rimeaddr_t *addr)
|
||||||
{
|
{
|
||||||
/* check if addr is derived from table, inside memb */
|
struct neighbor_addr *item;
|
||||||
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);
|
|
||||||
|
|
||||||
|
/* 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) {
|
while(item != NULL) {
|
||||||
if(rimeaddr_cmp(addr, &item->addr)) {
|
if(rimeaddr_cmp(addr, &item->addr)) {
|
||||||
return item;
|
return item;
|
||||||
|
@ -80,22 +82,28 @@ neighbor_attr_list_neighbors(void)
|
||||||
return list_head(neighbor_addrs);
|
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
|
int
|
||||||
neighbor_attr_register(struct neighbor_attr *def)
|
neighbor_attr_register(struct neighbor_attr *def)
|
||||||
{
|
{
|
||||||
list_push(neighbor_attrs, def);
|
struct neighbor_addr *addr;
|
||||||
/* set default values for already existing neighbors */
|
|
||||||
struct neighbor_addr *addr = list_head(neighbor_addrs);
|
|
||||||
|
|
||||||
while(addr != NULL) {
|
list_push(neighbor_attrs, def);
|
||||||
if(def->default_value != NULL) {
|
|
||||||
memcpy(def->data + addr->index * def->size, def->default_value,
|
/* set default values for already existing neighbors */
|
||||||
def->size);
|
for(addr = list_head(neighbor_addrs); addr != NULL; addr = addr->next) {
|
||||||
} else {
|
set_attr(def, addr->index);
|
||||||
/* fill with zeroes */
|
|
||||||
memset(def->data + addr->index * def->size, 0, def->size);
|
|
||||||
}
|
|
||||||
addr = addr->next;
|
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -109,12 +117,16 @@ neighbor_attr_has_neighbor(const rimeaddr_t *addr)
|
||||||
int
|
int
|
||||||
neighbor_attr_add_neighbor(const rimeaddr_t *addr)
|
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)) {
|
if(neighbor_attr_has_neighbor(addr)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct neighbor_addr *item = memb_alloc(&neighbor_addr_mem);
|
item = memb_alloc(&neighbor_addr_mem);
|
||||||
|
|
||||||
if(item == NULL) {
|
if(item == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -125,9 +137,7 @@ neighbor_attr_add_neighbor(const rimeaddr_t *addr)
|
||||||
rimeaddr_copy(&item->addr, addr);
|
rimeaddr_copy(&item->addr, addr);
|
||||||
|
|
||||||
/* look up index and set default values */
|
/* look up index and set default values */
|
||||||
uint16_t i;
|
ptr = neighbor_addr_mem.mem;
|
||||||
struct neighbor_addr *ptr = neighbor_addr_mem.mem;
|
|
||||||
|
|
||||||
for(i = 0; i < neighbor_addr_mem.num; ++i) {
|
for(i = 0; i < neighbor_addr_mem.num; ++i) {
|
||||||
if(&ptr[i] == item) {
|
if(&ptr[i] == item) {
|
||||||
break;
|
break;
|
||||||
|
@ -136,16 +146,8 @@ neighbor_attr_add_neighbor(const rimeaddr_t *addr)
|
||||||
|
|
||||||
item->index = i;
|
item->index = i;
|
||||||
|
|
||||||
struct neighbor_attr *def = list_head(neighbor_attrs);
|
for(def = list_head(neighbor_attrs); def != NULL; def = def->next) {
|
||||||
|
set_attr(def, i);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -154,16 +156,12 @@ neighbor_attr_add_neighbor(const rimeaddr_t *addr)
|
||||||
int
|
int
|
||||||
neighbor_attr_remove_neighbor(const rimeaddr_t *addr)
|
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(item != NULL) {
|
||||||
if(rimeaddr_cmp(&item->addr, addr)) {
|
list_remove(neighbor_addrs, item);
|
||||||
memb_free(&neighbor_addr_mem, item);
|
memb_free(&neighbor_addr_mem, item);
|
||||||
list_remove(neighbor_addrs, item);
|
return 0;
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
item = item->next;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return -1;
|
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);
|
struct neighbor_addr *attr = neighbor_addr_get(addr);
|
||||||
|
|
||||||
if(attr != NULL) {
|
if(attr != NULL) {
|
||||||
return (void *)(def->data + attr->index * def->size);
|
return (char *)def->data + attr->index * def->size;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -192,7 +190,7 @@ neighbor_attr_set_data(struct neighbor_attr *def, const rimeaddr_t *addr,
|
||||||
}
|
}
|
||||||
if(attr != NULL) {
|
if(attr != NULL) {
|
||||||
attr->time = 0;
|
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 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -228,8 +226,8 @@ timeout_check(void *ptr)
|
||||||
if(item->time >= timeout) {
|
if(item->time >= timeout) {
|
||||||
struct neighbor_addr *next_item = item->next;
|
struct neighbor_addr *next_item = item->next;
|
||||||
|
|
||||||
memb_free(&neighbor_addr_mem, item);
|
|
||||||
list_remove(neighbor_addrs, item);
|
list_remove(neighbor_addrs, item);
|
||||||
|
memb_free(&neighbor_addr_mem, item);
|
||||||
item = next_item;
|
item = next_item;
|
||||||
} else {
|
} else {
|
||||||
item = item->next;
|
item = item->next;
|
||||||
|
|
Loading…
Reference in a new issue