]> git.ozlabs.org Git - ccan/blobdiff - ccan/rbtree/rbtree.c
tdb2: implement tdb_chainlock_read/tdb_chainunlock_read.
[ccan] / ccan / rbtree / rbtree.c
index a6a7d38b95d3eab5883f12872ef3802c9dadd16d..56fa3f155c7e87c1ee83c067c00cf4685bf798ee 100644 (file)
 #include <ccan/rbtree/rbtree.h>
 
 static void
-tree_destructor_traverse_node(TALLOC_CTX *mem_ctx, trbt_node_t *node)
+tree_destructor_traverse_node(trbt_node_t *node)
 {
        talloc_set_destructor(node, NULL);
        if (node->left) {
-               tree_destructor_traverse_node(mem_ctx, node->left);
+               tree_destructor_traverse_node(node->left);
        }
        if (node->right) {
-               tree_destructor_traverse_node(mem_ctx, node->right);
+               tree_destructor_traverse_node(node->right);
        }
-       talloc_steal(mem_ctx, node);
+       talloc_free(node);
 }
 
 /*
@@ -36,7 +36,6 @@ tree_destructor_traverse_node(TALLOC_CTX *mem_ctx, trbt_node_t *node)
  */
 static int tree_destructor(trbt_tree_t *tree)
 {
-       TALLOC_CTX *tmp_ctx;
        trbt_node_t *node;
 
        if (tree == NULL) {
@@ -48,17 +47,14 @@ static int tree_destructor(trbt_tree_t *tree)
                return 0;
        }
 
-       /* traverse the tree and remove the node destructor and steal
-          the node to the temporary context.
-          we dont want to use the existing destructor for the node
+       /* traverse the tree and remove the node destructor then delete it.
+          we don't want to use the existing destructor for the node
           since that will remove the nodes one by one from the tree.
-          since the entire tree will be completely destroyed we dont care
+          since the entire tree will be completely destroyed we don't care
           if it is inconsistent or unbalanced while freeing the
           individual nodes
        */
-       tmp_ctx = talloc_new(NULL);
-       tree_destructor_traverse_node(tmp_ctx, node);
-       talloc_free(tmp_ctx);
+       tree_destructor_traverse_node(node);
 
        return 0;
 }
@@ -442,7 +438,7 @@ trbt_delete_case1(trbt_node_t *node)
 }
 
 static void
-delete_node(trbt_node_t *node, int from_destructor)
+delete_node(trbt_node_t *node)
 {
        trbt_node_t *parent, *child, dc;
        trbt_node_t *temp = NULL;
@@ -477,7 +473,7 @@ delete_node(trbt_node_t *node, int from_destructor)
                /* then delete the temp node.
                   this node is guaranteed to have at least one leaf
                   child */
-               delete_node(temp, from_destructor);
+               delete_node(temp);
                goto finished;
        }
 
@@ -493,7 +489,7 @@ delete_node(trbt_node_t *node, int from_destructor)
           Once the delete of the node is finished, we remove this dummy
           node, which is simple to do since it is guaranteed that it will
           still not have any children after the delete operation.
-          This is because we dont represent the leaf-nodes as actual nodes
+          This is because we don't represent the leaf-nodes as actual nodes
           in this implementation.
         */
        if (!child) {
@@ -532,7 +528,7 @@ delete_node(trbt_node_t *node, int from_destructor)
           This is simple since this dummy node originally had no children
           and we are guaranteed that it will also not have any children
           after the node has been deleted and any possible rotations
-          have occured.
+          have occurred.
 
           The only special case is if this was the last node of the tree
           in which case we have to reset the root to NULL as well.
@@ -550,15 +546,11 @@ delete_node(trbt_node_t *node, int from_destructor)
        }
 
 finished:
-       if (!from_destructor) {
-               talloc_free(node);
-       }
-
        /* if we came from a destructor and temp!=NULL  this means we
           did the node-swap but now the tree still contains the old
           node  which was freed in the destructor. Not good.
        */
-       if (from_destructor && temp) {
+       if (temp) {
                temp->key32    = node->key32;
                temp->rb_color = node->rb_color;
 
@@ -601,7 +593,7 @@ finished:
  */
 static int node_destructor(trbt_node_t *node)
 {
-       delete_node(node, 1);
+       delete_node(node);
 
        return 0;
 }
@@ -679,6 +671,8 @@ trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data)
                                trbt_node_t *new_node;
 
                                new_node = trbt_create_node(tree, node, key, data);
+                               if (!new_node)
+                                       return NULL;
                                node->left=new_node;
                                node=new_node;
 
@@ -693,6 +687,8 @@ trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data)
                                trbt_node_t *new_node;
 
                                new_node = trbt_create_node(tree, node, key, data);
+                               if (!new_node)
+                                       return NULL;
                                node->right=new_node;
                                node=new_node;
                                break;
@@ -744,7 +740,7 @@ trbt_delete32(trbt_tree_t *tree, uint32_t key)
 
        while(node){
                if(key==node->key32){
-                       delete_node(node, 0);
+                       talloc_free(node);
                        return;
                }
                if(key<node->key32){