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);