Skip to content

Commit

Permalink
Add memory-unsafe insert functions to tree object.
Browse files Browse the repository at this point in the history
  • Loading branch information
jheretic committed Feb 25, 2014
1 parent f6d9ef1 commit b538c96
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
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
18 changes: 18 additions & 0 deletions src/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ co_obj_t *co_tree_delete(co_obj_t *root, const char *key, const size_t klen);
*/
int co_tree_insert(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value);

/**
* @brief insert object into given tree and associate with key, where value is not tied to tree
* @param root tree object
* @param key key to search for
* @param klen length of key
* @param value value object to insert
*/
int co_tree_insert_unsafe(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value);

/**
* @brief insert object into given tree and associate with key (overwrite if it exists)
* @param root tree object
Expand All @@ -125,6 +134,15 @@ int co_tree_insert(co_obj_t *root, const char *key, const size_t klen, co_obj_t
*/
int co_tree_insert_force(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value);

/**
* @brief insert object into given tree and associate with key, where value is not tied to tree (overwrite if it exists)
* @param root tree object
* @param key key to search for
* @param klen length of key
* @param value value object to insert
*/
int co_tree_insert_unsafe_force(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value);

/**
* @brief set value contained in an object in the tree with a specified key (if a string)
* @param root tree object
Expand Down

0 comments on commit b538c96

Please sign in to comment.