Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unsafe tree and list insert functions to commotiond and use where appropriate. #94

Merged
merged 3 commits into from
Feb 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ _cmd_help_i(co_obj_t *data, co_obj_t *current, void *context)
size_t cmd_len = 0;
CHECK((cmd_len = co_obj_data(&cmd_name, ((co_cmd_t *)current)->name)) > 0, "Failed to read command name.");
DEBUG("Command: %s, Length: %d", cmd_name, (int)cmd_len);
co_tree_insert((co_obj_t *)context, cmd_name, cmd_len, ((co_cmd_t *)current)->usage);
co_tree_insert_unsafe((co_obj_t *)context, cmd_name, cmd_len, ((co_cmd_t *)current)->usage);
return NULL;
error:
return NULL;
Expand All @@ -126,7 +126,7 @@ CMD(help)
size_t clen = co_obj_data(&cstr, cmd);
if(clen > 0)
{
co_tree_insert(*output, cstr, clen, co_cmd_desc(cmd));
co_tree_insert_unsafe(*output, cstr, clen, co_cmd_desc(cmd));
return 1;
}
}
Expand All @@ -140,7 +140,7 @@ _cmd_profiles_i(co_obj_t *data, co_obj_t *current, void *context)
{
char *name = NULL;
size_t nlen = co_obj_data(&name, ((co_profile_t *)current)->name);
co_tree_insert((co_obj_t *)context, name, nlen, ((co_profile_t *)current)->name);
co_tree_insert_unsafe((co_obj_t *)context, name, nlen, ((co_profile_t *)current)->name);
return NULL;
}

Expand Down Expand Up @@ -292,6 +292,9 @@ CMD(state)
CHECK(co_profile_get_str(prof, &ip, "ip", sizeof("ip")) > 0, "Attempting to generate IP but ip not set.");
CHECK(co_generate_ip(ip, ipgenmask, co_id_get(), address, t), "Failed to generate IP.");
object = co_str8_create(address, sizeof(address), 0);
CHECK(object != NULL, "Failed to get property.");
co_tree_insert(*output, propname, proplen, object);
return 1;
}
else
{
Expand Down Expand Up @@ -322,6 +325,9 @@ CMD(state)
CHECK(snprintfcat(bssidstr, BSSID_STR_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x", bssid[0] & 0xff, bssid[1] & 0xff, bssid[2] & 0xff, bssid[3] & 0xff, bssid[4] & 0xff, bssid[5] & 0xff) > 0, "Failed to convert BSSID.");
DEBUG("BSSID: %s", bssidstr);
object = co_str8_create(bssidstr, strlen(bssidstr) + 1, 0);
CHECK(object != NULL, "Failed to get property.");
co_tree_insert(*output, propname, proplen, object);
return 1;
}
else
{
Expand All @@ -339,7 +345,7 @@ CMD(state)
}

CHECK(object != NULL, "Failed to get property.");
co_tree_insert(*output, propname, proplen, object);
co_tree_insert_unsafe(*output, propname, proplen, object);
//co_obj_free(p);
return 1;
error:
Expand Down Expand Up @@ -561,7 +567,7 @@ CMD(get)

co_obj_t *value = co_profile_get(prof, kobj);
CHECK(value != NULL, "Invalid value.");
co_tree_insert(*output, kstr, klen, value);
co_tree_insert_unsafe(*output, kstr, klen, value);
return 1;

error:
Expand Down
104 changes: 98 additions & 6 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ _DEFINE_LIST(16);
_DEFINE_LIST(32);

static _listnode_t *
_listnode_create(co_obj_t *value)
_listnode_create(co_obj_t *value, bool safe)
{
_listnode_t *ret = h_calloc(1, sizeof(_listnode_t));
_LIST_PREV(ret) = NULL;
_LIST_NEXT(ret) = NULL;
if(value != NULL)
{
ret->value = value;
hattach(value, ret);
if(safe) hattach(value, ret);
}
else
ret->value = NULL;
Expand Down Expand Up @@ -279,7 +279,37 @@ specified list.");
CHECK(!co_list_contains(list, new_obj), "New node already in specified \
list.");
_listnode_t *adjacent = _LIST_PREV(this_node);
_listnode_t *new_node = _listnode_create(new_obj);
_listnode_t *new_node = _listnode_create(new_obj, true);
hattach(new_node, list);
if(adjacent == NULL)
{
/* First in list. */
_co_list_set_first(list, new_node);
}
else
{
_LIST_NEXT(adjacent) = new_node;
_LIST_PREV(new_node) = adjacent;
}
_LIST_NEXT(new_node) = this_node;
_LIST_PREV(this_node) = new_node;
_co_list_increment(list);
return 1;
error:
return 0;
}

int /* Done */
co_list_insert_before_unsafe(co_obj_t *list, co_obj_t *new_obj, co_obj_t *this_obj)
{
CHECK(IS_LIST(list), "Not a list object.");
_listnode_t *this_node = _co_list_find_node(list, this_obj);
CHECK(this_node != NULL, "Unable to find existing node in \
specified list.");
CHECK(!co_list_contains(list, new_obj), "New node already in specified \
list.");
_listnode_t *adjacent = _LIST_PREV(this_node);
_listnode_t *new_node = _listnode_create(new_obj, false);
hattach(new_node, list);
if(adjacent == NULL)
{
Expand Down Expand Up @@ -309,7 +339,37 @@ specified list.");
CHECK(!co_list_contains(list, new_obj), "New node already in specified \
list.");
_listnode_t *adjacent = _LIST_NEXT(this_node);
_listnode_t *new_node = _listnode_create(new_obj);
_listnode_t *new_node = _listnode_create(new_obj, true);
hattach(new_node, list);
if(adjacent == NULL)
{
/* Last in list. */
_co_list_set_last(list, new_node);
}
else
{
_LIST_PREV(adjacent) = new_node;
_LIST_NEXT(new_node) = adjacent;
}
_LIST_PREV(new_node) = this_node;
_LIST_NEXT(this_node) = new_node;
_co_list_increment(list);
return 1;
error:
return 0;
}

int /* Done */
co_list_insert_after_unsafe(co_obj_t *list, co_obj_t *new_obj, co_obj_t *this_obj)
{
CHECK(IS_LIST(list), "Not a list object.");
_listnode_t *this_node = _co_list_find_node(list, this_obj);
CHECK(this_node != NULL, "Unable to find existing node in \
specified list.");
CHECK(!co_list_contains(list, new_obj), "New node already in specified \
list.");
_listnode_t *adjacent = _LIST_NEXT(this_node);
_listnode_t *new_node = _listnode_create(new_obj, false);
hattach(new_node, list);
if(adjacent == NULL)
{
Expand All @@ -335,7 +395,23 @@ co_list_prepend(co_obj_t *list, co_obj_t *new_obj)
if(co_list_length(list) == 0)
{
/* First item in list. */
_listnode_t *new_node = _listnode_create(new_obj);
_listnode_t *new_node = _listnode_create(new_obj, true);
_co_list_set_first(list, new_node);
_co_list_set_last(list, new_node);
hattach(new_node, list);
_co_list_increment(list);
return 1;
}
return co_list_insert_before(list, new_obj, (_co_list_get_first_node(list))->value);
}

int /* Done */
co_list_prepend_unsafe(co_obj_t *list, co_obj_t *new_obj)
{
if(co_list_length(list) == 0)
{
/* First item in list. */
_listnode_t *new_node = _listnode_create(new_obj, false);
_co_list_set_first(list, new_node);
_co_list_set_last(list, new_node);
hattach(new_node, list);
Expand All @@ -351,7 +427,23 @@ co_list_append(co_obj_t *list, co_obj_t *new_obj)
if(co_list_length(list) == 0)
{
/* First item in list. */
_listnode_t *new_node = _listnode_create(new_obj);
_listnode_t *new_node = _listnode_create(new_obj, true);
_co_list_set_first(list, new_node);
_co_list_set_last(list, new_node);
hattach(new_node, list);
_co_list_increment(list);
return 1;
}
return co_list_insert_after(list, new_obj, (_co_list_get_last_node(list))->value);
}

int /* Done */
co_list_append_unsafe(co_obj_t *list, co_obj_t *new_obj)
{
if(co_list_length(list) == 0)
{
/* First item in list. */
_listnode_t *new_node = _listnode_create(new_obj, false);
_co_list_set_first(list, new_node);
_co_list_set_last(list, new_node);
hattach(new_node, list);
Expand Down
31 changes: 31 additions & 0 deletions src/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ int co_list_contains(co_obj_t *list, co_obj_t *item);
int co_list_insert_before(co_obj_t *list, co_obj_t *new_obj, \
co_obj_t *this_obj);

/**
* @brief insert new item in list before specified item without the list managing the item's memory
* @param list list object to process
* @param new_obj item to insert
* @param this_obj item to insert before
*/
int co_list_insert_before_unsafe(co_obj_t *list, co_obj_t *new_obj, \
co_obj_t *this_obj);

/**
* @brief insert new item in list after specified item
* @param list list object to process
Expand All @@ -97,20 +106,42 @@ int co_list_insert_before(co_obj_t *list, co_obj_t *new_obj, \
*/
int co_list_insert_after(co_obj_t *list, co_obj_t *new_obj, co_obj_t *this_obj);

/**
* @brief insert new item in list after specified item without the list managing the item's memory
* @param list list object to process
* @param new_obj item to insert
* @param this_obj item to insert after
*/
int co_list_insert_after_unsafe(co_obj_t *list, co_obj_t *new_obj, co_obj_t *this_obj);

/**
* @brief insert new item at beginning of list
* @param list list object to process
* @param new_obj item to insert
*/
int co_list_prepend(co_obj_t *list, co_obj_t *new_obj);

/**
* @brief insert new item at beginning of list without the list managing the item's memory
* @param list list object to process
* @param new_obj item to insert
*/
int co_list_prepend_unsafe(co_obj_t *list, co_obj_t *new_obj);

/**
* @brief insert new item at end of list
* @param list list object to process
* @param new_obj item to insert
*/
int co_list_append(co_obj_t *list, co_obj_t *new_obj);

/**
* @brief insert new item at end of list without the list managing the item's memory
* @param list list object to process
* @param new_obj item to insert
*/
int co_list_append_unsafe(co_obj_t *list, co_obj_t *new_obj);

/**
* @brief delete specified item from list
* @param list list object to process
Expand Down
41 changes: 30 additions & 11 deletions src/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ co_tree_delete(co_obj_t *root, const char *key, const size_t klen)
}

static inline _treenode_t *
_co_tree_insert_r(_treenode_t *root, _treenode_t *current, const char *orig_key, const size_t orig_klen, const char *key, const size_t klen, co_obj_t *value)
_co_tree_insert_r(_treenode_t *root, _treenode_t *current, const char *orig_key, const size_t orig_klen, const char *key, const size_t klen, co_obj_t *value, bool safe)
{
if (current == NULL)
{
Expand All @@ -264,14 +264,14 @@ _co_tree_insert_r(_treenode_t *root, _treenode_t *current, const char *orig_key,

if (*key < current->splitchar)
{
current->low = _co_tree_insert_r(root, current->low, orig_key, orig_klen, key, klen, value);
current->low = _co_tree_insert_r(root, current->low, orig_key, orig_klen, key, klen, value, safe);
}
else if (*key == current->splitchar)
{
if (klen > 1)
{
// not done yet, keep going but one less
current->equal = _co_tree_insert_r(root, current->equal, orig_key, orig_klen, key+1, klen - 1, value);
current->equal = _co_tree_insert_r(root, current->equal, orig_key, orig_klen, key+1, klen - 1, value, safe);
}
else
{
Expand All @@ -286,34 +286,37 @@ _co_tree_insert_r(_treenode_t *root, _treenode_t *current, const char *orig_key,
current->value = value;
current->key = co_str8_create(orig_key, orig_klen, 0);
hattach(current->key, current);
hattach(current->value, current);
current->key->_ref++;
current->value->_ref++;
if(safe)
{
hattach(current->value, current);
current->value->_ref++;
}
}
}
else
{
current->high = _co_tree_insert_r(root, current->high, orig_key, orig_klen, key, klen, value);
current->high = _co_tree_insert_r(root, current->high, orig_key, orig_klen, key, klen, value, safe);
}

return current;
}

static int
_co_tree_insert(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value)
_co_tree_insert(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value, bool safe)
{
_treenode_t *n = NULL;
if(CO_TYPE(root) == _tree16)
{
((co_tree16_t *)root)->root = _co_tree_insert_r(((co_tree16_t *)root)->root, \
((co_tree16_t *)root)->root, key, klen, key, klen, value);
((co_tree16_t *)root)->root, key, klen, key, klen, value, safe);
n = ((co_tree16_t *)root)->root;
hattach(n, root);
}
else if(CO_TYPE(root) == _tree32)
{
((co_tree32_t *)root)->root = _co_tree_insert_r(((co_tree32_t *)root)->root, \
((co_tree32_t *)root)->root, key, klen, key, klen, value);
((co_tree32_t *)root)->root, key, klen, key, klen, value, safe);
n = ((co_tree32_t *)root)->root;
hattach(n, root);
}
Expand All @@ -330,15 +333,31 @@ co_tree_insert(co_obj_t *root, const char *key, const size_t klen, co_obj_t *val
{
_treenode_t *n = co_tree_find_node(co_tree_root(root), key, klen);
CHECK(n == NULL, "Key exists.");
return _co_tree_insert(root, key, klen, value);
return _co_tree_insert(root, key, klen, value, true);
error:
return 0;
}

int
co_tree_insert_unsafe(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value)
{
_treenode_t *n = co_tree_find_node(co_tree_root(root), key, klen);
CHECK(n == NULL, "Key exists.");
return _co_tree_insert(root, key, klen, value, false);
error:
return 0;
}

int
co_tree_insert_force(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value)
{
return _co_tree_insert(root, key, klen, value);
return _co_tree_insert(root, key, klen, value, true);
}

int
co_tree_insert_unsafe_force(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value)
{
return _co_tree_insert(root, key, klen, value, false);
}

static int
Expand Down