57
57
#include <stdbool.h>
58
58
#include <stddef.h>
59
59
60
+ #include "base_alloc.h"
61
+ #include "base_alloc_linear.h"
60
62
#include "critnib.h"
61
63
#include "utils_common.h"
62
64
#include "utils_concurrency.h"
@@ -127,6 +129,10 @@ struct critnib {
127
129
uint64_t remove_count ;
128
130
129
131
struct os_mutex_t * mutex ; /* writes/removes */
132
+
133
+ umf_ba_linear_pool_t * pool_linear ;
134
+ umf_ba_pool_t * pool_nodes ;
135
+ umf_ba_pool_t * pool_leaves ;
130
136
};
131
137
132
138
/*
@@ -176,37 +182,64 @@ static inline unsigned slice_index(word key, sh_t shift) {
176
182
* critnib_new -- allocates a new critnib structure
177
183
*/
178
184
struct critnib * critnib_new (void ) {
179
- struct critnib * c = Zalloc (sizeof (struct critnib ));
180
- if (!c ) {
185
+ umf_ba_linear_pool_t * pool_linear =
186
+ umf_ba_linear_create (0 /* minimal pool size */ );
187
+ if (!pool_linear ) {
181
188
return NULL ;
182
189
}
183
190
184
- c -> mutex = util_mutex_create ();
191
+ struct critnib * c =
192
+ umf_ba_linear_alloc (pool_linear , sizeof (struct critnib ));
193
+ if (!c ) {
194
+ goto err_destroy_pool_linear ;
195
+ }
196
+
197
+ c -> pool_linear = pool_linear ;
198
+
199
+ c -> mutex = util_mutex_init (
200
+ umf_ba_linear_alloc (pool_linear , util_mutex_get_size ()));
185
201
if (!c -> mutex ) {
186
- free (c );
187
- return NULL ;
202
+ goto err_destroy_pool_linear ;
203
+ }
204
+
205
+ c -> pool_nodes = umf_ba_create (sizeof (struct critnib_node ));
206
+ if (!c -> pool_nodes ) {
207
+ goto err_util_mutex_destroy ;
208
+ }
209
+
210
+ c -> pool_leaves = umf_ba_create (sizeof (struct critnib_leaf ));
211
+ if (!c -> pool_leaves ) {
212
+ goto err_destroy_pool_nodes ;
188
213
}
189
214
190
215
VALGRIND_HG_DRD_DISABLE_CHECKING (& c -> root , sizeof (c -> root ));
191
216
VALGRIND_HG_DRD_DISABLE_CHECKING (& c -> remove_count , sizeof (c -> remove_count ));
192
217
193
218
return c ;
219
+
220
+ err_destroy_pool_nodes :
221
+ umf_ba_destroy (c -> pool_nodes );
222
+ err_util_mutex_destroy :
223
+ util_mutex_destroy_not_free (c -> mutex );
224
+ err_destroy_pool_linear :
225
+ umf_ba_linear_destroy (pool_linear ); // free all its allocations and destroy
226
+ return NULL ;
194
227
}
195
228
196
229
/*
197
230
* internal: delete_node -- recursively free (to malloc) a subtree
198
231
*/
199
- static void delete_node (struct critnib_node * __restrict n ) {
232
+ static void delete_node (struct critnib * c , struct critnib_node * __restrict n ) {
200
233
if (is_leaf (n )) {
201
- Free ( to_leaf (n ));
234
+ umf_ba_free ( c -> pool_leaves , to_leaf (n ));
202
235
} else {
203
236
for (int i = 0 ; i < SLNODES ; i ++ ) {
204
237
if (n -> child [i ]) {
205
- delete_node (n -> child [i ]);
238
+ delete_node (c , n -> child [i ]);
206
239
}
207
240
}
208
241
209
- Free ( n );
242
+ umf_ba_free ( c -> pool_nodes , n );
210
243
}
211
244
}
212
245
@@ -215,29 +248,35 @@ static void delete_node(struct critnib_node *__restrict n) {
215
248
*/
216
249
void critnib_delete (struct critnib * c ) {
217
250
if (c -> root ) {
218
- delete_node (c -> root );
251
+ delete_node (c , c -> root );
219
252
}
220
253
221
- util_mutex_destroy (c -> mutex );
254
+ // mutex is freed in umf_ba_linear_destroy(c->pool_linear) at the end
255
+ util_mutex_destroy_not_free (c -> mutex );
222
256
223
257
for (struct critnib_node * m = c -> deleted_node ; m ;) {
224
258
struct critnib_node * mm = m -> child [0 ];
225
- Free ( m );
259
+ umf_ba_free ( c -> pool_nodes , m );
226
260
m = mm ;
227
261
}
228
262
229
263
for (struct critnib_leaf * k = c -> deleted_leaf ; k ;) {
230
264
struct critnib_leaf * kk = k -> value ;
231
- Free ( k );
265
+ umf_ba_free ( c -> pool_leaves , k );
232
266
k = kk ;
233
267
}
234
268
235
269
for (int i = 0 ; i < DELETED_LIFE ; i ++ ) {
236
- Free ( c -> pending_del_nodes [i ]);
237
- Free ( c -> pending_del_leaves [i ]);
270
+ umf_ba_free ( c -> pool_nodes , c -> pending_del_nodes [i ]);
271
+ umf_ba_free ( c -> pool_leaves , c -> pending_del_leaves [i ]);
238
272
}
239
273
240
- Free (c );
274
+ umf_ba_destroy (c -> pool_nodes );
275
+ umf_ba_destroy (c -> pool_leaves );
276
+ umf_ba_linear_destroy (
277
+ c -> pool_linear ); // free all its allocations and destroy
278
+
279
+ // 'c' was freed in umf_ba_linear_destroy(c->pool_linear)
241
280
}
242
281
243
282
/*
@@ -264,7 +303,7 @@ static void free_node(struct critnib *__restrict c,
264
303
*/
265
304
static struct critnib_node * alloc_node (struct critnib * __restrict c ) {
266
305
if (!c -> deleted_node ) {
267
- return Malloc ( sizeof ( struct critnib_node ) );
306
+ return umf_ba_alloc ( c -> pool_nodes );
268
307
}
269
308
270
309
struct critnib_node * n = c -> deleted_node ;
@@ -295,7 +334,7 @@ static void free_leaf(struct critnib *__restrict c,
295
334
*/
296
335
static struct critnib_leaf * alloc_leaf (struct critnib * __restrict c ) {
297
336
if (!c -> deleted_leaf ) {
298
- return Malloc ( sizeof ( struct critnib_leaf ) );
337
+ return umf_ba_alloc ( c -> pool_leaves );
299
338
}
300
339
301
340
struct critnib_leaf * k = c -> deleted_leaf ;
0 commit comments