added free() to AVL tree implementation + use UcxAllocator

Mon, 18 May 2015 18:39:19 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 May 2015 18:39:19 +0200
changeset 196
1b4cdafef2eb
parent 195
bec61f067ea0
child 197
a82f3456b76d

added free() to AVL tree implementation + use UcxAllocator

ucx/avl.c file | annotate | diff | comparison | revisions
ucx/avl.h file | annotate | diff | comparison | revisions
     1.1 --- a/ucx/avl.c	Mon May 18 12:54:18 2015 +0200
     1.2 +++ b/ucx/avl.c	Mon May 18 18:39:19 2015 +0200
     1.3 @@ -107,7 +107,7 @@
     1.4  }
     1.5  
     1.6  UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator) {
     1.7 -    UcxAVLTree *tree = malloc(sizeof(UcxAVLTree));
     1.8 +    UcxAVLTree *tree = almalloc(allocator, sizeof(UcxAVLTree));
     1.9      if (tree) {
    1.10          tree->allocator = allocator;
    1.11          tree->cmpfunc = cmpfunc;
    1.12 @@ -118,6 +118,20 @@
    1.13      return tree;
    1.14  }
    1.15  
    1.16 +static void ucx_avl_free_node(UcxAllocator *al, UcxAVLNode *node) {
    1.17 +    if (node) {
    1.18 +        ucx_avl_free_node(al, node->left);
    1.19 +        ucx_avl_free_node(al, node->right);
    1.20 +        alfree(al, node);
    1.21 +    }
    1.22 +}
    1.23 +
    1.24 +void ucx_avl_free(UcxAVLTree *tree) {
    1.25 +    UcxAllocator *al = tree->allocator;
    1.26 +    ucx_avl_free_node(al, tree->root);
    1.27 +    alfree(al, tree);
    1.28 +}
    1.29 +
    1.30  void *ucx_avl_get(UcxAVLTree *tree, intptr_t key) {
    1.31      UcxAVLNode *n = tree->root;
    1.32      int cmpresult;
    1.33 @@ -143,7 +157,7 @@
    1.34          }
    1.35  
    1.36          if (cmpresult) {
    1.37 -            UcxAVLNode *e = malloc(sizeof(UcxAVLNode));
    1.38 +            UcxAVLNode *e = almalloc(tree->allocator, sizeof(UcxAVLNode));
    1.39              e->key = key; e->value = value; e->height = 1;
    1.40              e->parent = e->left = e->right = NULL;
    1.41              ucx_avl_connect(tree, n, e, 0);
    1.42 @@ -155,7 +169,7 @@
    1.43              return prevval;
    1.44          }
    1.45      } else {
    1.46 -        tree->root = malloc(sizeof(UcxAVLNode));
    1.47 +        tree->root = almalloc(tree->allocator, sizeof(UcxAVLNode));
    1.48          tree->root->key = key; tree->root->value = value;
    1.49          tree->root->height = 1;
    1.50          tree->root->parent = tree->root->left = tree->root->right = NULL;
    1.51 @@ -181,14 +195,15 @@
    1.52              ucx_avl_connect(tree, s->parent, s->right, s->key);
    1.53              n->key = s->key; n->value = s->value;
    1.54              p = s->parent;
    1.55 +            alfree(tree->allocator, s);
    1.56          } else {
    1.57              if (p) {
    1.58                  ucx_avl_connect(tree, p, n->right ? n->right:n->left, n->key);
    1.59              } else {
    1.60                  tree->root = n->right ? n->right : n->left;
    1.61              }
    1.62 +            alfree(tree->allocator, n);
    1.63          }
    1.64 -        // TODO: free the correct memory
    1.65  
    1.66          if (p) {
    1.67              ucx_avl_balance(tree, p);
     2.1 --- a/ucx/avl.h	Mon May 18 12:54:18 2015 +0200
     2.2 +++ b/ucx/avl.h	Mon May 18 18:39:19 2015 +0200
     2.3 @@ -134,6 +134,12 @@
     2.4  UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator);
     2.5  
     2.6  /**
     2.7 + * Destroys an UcxAVLTree.
     2.8 + * @param tree the tree to destroy
     2.9 + */
    2.10 +void ucx_avl_free(UcxAVLTree *tree);
    2.11 +
    2.12 +/**
    2.13   * Macro for initializing a new UcxAVLTree with the default allocator and a
    2.14   * ucx_ptrcmp() compare function.
    2.15   * 

mercurial