20
20
21
21
#include "hash.h"
22
22
#include "../hash_func.h"
23
+ #include "../mem/rpm_mem.h"
23
24
24
- gen_hash_t * hash_init ( unsigned int size )
25
+ int hash_init_locks ( gen_hash_t * h )
25
26
{
26
27
unsigned int n ;
27
- gen_hash_t * h ;
28
28
gen_lock_set_t * locks = NULL ;
29
29
30
+ for (n = h -> size ; n >= 1 && !locks ; n /=2 ) {
31
+ locks = lock_set_alloc (n );
32
+ if (locks && lock_set_init (locks ))
33
+ break ;
34
+ }
35
+
36
+ if (!locks ) {
37
+ LM_ERR ("could not allocate hash locks\n" );
38
+ return -1 ;
39
+ }
40
+ h -> locks = locks ;
41
+ h -> locks_no = n ;
42
+ return 0 ;
43
+ }
44
+
45
+ gen_hash_t * hash_init_flags (unsigned int size , unsigned int flags )
46
+ {
47
+ unsigned int n ;
48
+ gen_hash_t * h ;
49
+
30
50
/* initialized the hash table */
31
51
for (n = 0 ; n < (8 * sizeof (n ) - 1 ) ; n ++ ) {
32
52
if (size == (1 <<n ))
@@ -39,34 +59,45 @@ gen_hash_t *hash_init(unsigned int size)
39
59
break ;
40
60
}
41
61
}
42
- for (n = size ; n >= 1 && !locks ; n /=2 ) {
43
- locks = lock_set_alloc (n );
44
- if (locks && lock_set_init (locks ))
45
- break ;
46
- }
47
62
48
- if (!locks ) {
49
- LM_ERR ("could not allocate hash locks\n" );
50
- return NULL ;
51
- }
52
- h = shm_malloc (sizeof * h + size * sizeof (* h -> entries ));
63
+ if (flags & HASH_MAP_PERSIST )
64
+ h = rpm_malloc (sizeof * h + size * sizeof (* h -> entries ));
65
+ else
66
+ h = shm_malloc (sizeof * h + size * sizeof (* h -> entries ));
53
67
if (!h ) {
54
68
LM_ERR ("could not alloc hash of %d elements!\n" , size );
55
69
return NULL ;
56
70
}
71
+ memset (h , 0 , sizeof * h + size * sizeof (* h -> entries ));
57
72
h -> entries = (map_t * )(h + 1 );
58
73
h -> size = size ;
59
- h -> locks = locks ;
60
- h -> locks_no = n ;
74
+ h -> flags = flags ;
75
+ if (hash_init_locks (h ) < 0 ) {
76
+ LM_ERR ("could not alloc locks for hash!\n" );
77
+ goto error ;
78
+ }
79
+
61
80
for (n = 0 ; n < h -> size ; n ++ ) {
62
- h -> entries [n ] = map_create (AVLMAP_SHARED );
81
+ h -> entries [n ] = map_create (
82
+ ((flags & HASH_MAP_PERSIST )?AVLMAP_PERSISTENT :AVLMAP_SHARED ));
63
83
if (!h -> entries [n ]) {
64
84
h -> size = n ; /* we only account for what we've already created */
65
- hash_destroy (h , NULL );
66
- return NULL ;
85
+ goto error ;
67
86
}
68
87
}
69
88
return h ;
89
+ error :
90
+ hash_destroy (h , NULL );
91
+ return NULL ;
92
+ }
93
+
94
+ void hash_destroy_locks (gen_hash_t * hash )
95
+ {
96
+ if (!hash -> locks )
97
+ return ;
98
+ lock_set_destroy (hash -> locks );
99
+ lock_set_dealloc (hash -> locks );
100
+ hash -> locks = NULL ;
70
101
}
71
102
72
103
void hash_destroy (gen_hash_t * hash , hash_destroy_func destroy )
@@ -76,11 +107,14 @@ void hash_destroy(gen_hash_t *hash, hash_destroy_func destroy)
76
107
if (!hash )
77
108
return ;
78
109
79
- lock_set_destroy (hash -> locks );
80
- lock_set_dealloc (hash -> locks );
110
+ hash_destroy_locks (hash );
81
111
82
112
for (n = 0 ; n < hash -> size ; n ++ )
83
- map_destroy (hash -> entries [n ], destroy );
113
+ if (hash -> entries [n ])
114
+ map_destroy (hash -> entries [n ], destroy );
84
115
85
- shm_free (hash );
116
+ if (hash -> flags & HASH_MAP_PERSIST )
117
+ rpm_free (hash );
118
+ else
119
+ shm_free (hash );
86
120
}
0 commit comments