28
28
#include <stdlib.h>
29
29
#include <string.h>
30
30
#include "str.h"
31
+ #include "map.h"
31
32
32
33
#include "mem/mem.h"
33
34
#include "mem/shm_mem.h"
34
- #include "mem/rpm_mem.h"
35
- #include "map.h"
35
+
36
+ #define avl_malloc (dest ,size ,flags ) do \
37
+ { \
38
+ if(flags & AVLMAP_SHARED) \
39
+ (dest) = shm_malloc(size); \
40
+ else \
41
+ (dest) = pkg_malloc(size); \
42
+ } while(0)
43
+
44
+ #define avl_free (dest ,flags ) do \
45
+ { \
46
+ if(flags & AVLMAP_SHARED) \
47
+ shm_free(dest); \
48
+ else \
49
+ pkg_free(dest); \
50
+ } while(0)
51
+
36
52
37
53
#define min (a ,b ) ((a)<(b))?(a):(b)
38
54
@@ -59,24 +75,11 @@ static int str_cmp(str s1, str s2)
59
75
map_t map_create (enum map_flags flags )
60
76
{
61
77
map_t tree ;
62
- osips_malloc_f m ;
63
- osips_free_f f ;
64
-
65
- if (flags & AVLMAP_PERSISTENT ) {
66
- m = rpm_malloc_func ;
67
- f = rpm_free_func ;
68
- } else if (flags & AVLMAP_SHARED ) {
69
- m = shm_malloc_func ;
70
- f = shm_free_func ;
71
- } else {
72
- m = pkg_malloc_func ;
73
- f = pkg_free_func ;
74
- }
75
- tree = func_malloc (m , sizeof * tree );
78
+
79
+ avl_malloc (tree , sizeof * tree , flags );
80
+
76
81
if (tree == NULL )
77
82
return NULL ;
78
- tree -> malloc = m ;
79
- tree -> free = f ;
80
83
81
84
tree -> avl_root = NULL ;
82
85
tree -> flags = flags ;
@@ -133,7 +136,7 @@ void ** map_get( map_t tree, str key)
133
136
y = p ;
134
137
}
135
138
136
- n = func_malloc ( tree -> malloc , sizeof * n );
139
+ avl_malloc ( n , sizeof * n , tree -> flags );
137
140
138
141
if (n == NULL )
139
142
return NULL ;
@@ -144,7 +147,7 @@ void ** map_get( map_t tree, str key)
144
147
145
148
if ( !( tree -> flags & AVLMAP_NO_DUPLICATE ) )
146
149
{
147
- key_copy .s = func_malloc ( tree -> malloc , key . len );
150
+ avl_malloc ( key_copy .s , key . len , tree -> flags );
148
151
if (!key_copy .s )
149
152
return NULL ;
150
153
@@ -327,9 +330,9 @@ void * delete_node(map_t tree, struct avl_node * p)
327
330
}
328
331
329
332
if (!( tree -> flags & AVLMAP_NO_DUPLICATE ) )
330
- func_free ( tree -> free , p -> key .s );
333
+ avl_free ( p -> key .s , tree -> flags );
331
334
332
- func_free ( tree -> free , p );
335
+ avl_free ( p , tree -> flags );
333
336
334
337
while (q != (struct avl_node * ) & tree -> avl_root ) {
335
338
struct avl_node * y = q ;
@@ -481,15 +484,15 @@ void map_destroy( map_t tree, value_destroy_func destroy)
481
484
if (destroy != NULL && p -> val != NULL )
482
485
destroy (p -> val );
483
486
if ( !(tree -> flags & AVLMAP_NO_DUPLICATE ) )
484
- func_free ( tree -> free , p -> key .s );
485
- func_free ( tree -> free , p );
487
+ avl_free ( p -> key .s , tree -> flags );
488
+ avl_free ( p , tree -> flags );
486
489
} else {
487
490
q = p -> avl_link [0 ];
488
491
p -> avl_link [0 ] = q -> avl_link [1 ];
489
492
q -> avl_link [1 ] = p ;
490
493
}
491
494
492
- func_free ( tree -> free , tree );
495
+ avl_free ( tree , tree -> flags );
493
496
}
494
497
495
498
int map_size ( map_t tree )
0 commit comments