Skip to content

Commit 9bd8b9b

Browse files
authored
Merge pull request #178 from ldorau/Use_base_allocator_in_critnib
Use base allocator in critnib
2 parents d663f0e + 5714aea commit 9bd8b9b

File tree

3 files changed

+71
-44
lines changed

3 files changed

+71
-44
lines changed

cmake/helpers.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function(add_umf_library)
104104
target_link_libraries(${ARG_NAME} PRIVATE ${ARG_LIBS})
105105
target_include_directories(${ARG_NAME} PRIVATE
106106
${UMF_CMAKE_SOURCE_DIR}/include
107-
${UMF_CMAKE_SOURCE_DIR}/src/common)
107+
${UMF_CMAKE_SOURCE_DIR}/src/base_alloc)
108108
add_umf_target_compile_options(${ARG_NAME})
109109
add_umf_target_link_options(${ARG_NAME})
110110
endfunction()

src/critnib/critnib.c

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
#include <stdbool.h>
5858
#include <stddef.h>
5959

60+
#include "base_alloc.h"
61+
#include "base_alloc_linear.h"
6062
#include "critnib.h"
6163
#include "utils_common.h"
6264
#include "utils_concurrency.h"
@@ -126,7 +128,11 @@ struct critnib {
126128

127129
uint64_t remove_count;
128130

129-
struct os_mutex_t *mutex; /* writes/removes */
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;
130136
};
131137

132138
/*
@@ -176,37 +182,63 @@ static inline unsigned slice_index(word key, sh_t shift) {
176182
* critnib_new -- allocates a new critnib structure
177183
*/
178184
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) {
181188
return NULL;
182189
}
183190

184-
c->mutex = util_mutex_create();
185-
if (!c->mutex) {
186-
free(c);
187-
return NULL;
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+
void *mutex_ptr = util_mutex_init(&c->mutex);
200+
if (!mutex_ptr) {
201+
goto err_destroy_pool_linear;
202+
}
203+
204+
c->pool_nodes = umf_ba_create(sizeof(struct critnib_node));
205+
if (!c->pool_nodes) {
206+
goto err_util_mutex_destroy;
207+
}
208+
209+
c->pool_leaves = umf_ba_create(sizeof(struct critnib_leaf));
210+
if (!c->pool_leaves) {
211+
goto err_destroy_pool_nodes;
188212
}
189213

190214
VALGRIND_HG_DRD_DISABLE_CHECKING(&c->root, sizeof(c->root));
191215
VALGRIND_HG_DRD_DISABLE_CHECKING(&c->remove_count, sizeof(c->remove_count));
192216

193217
return c;
218+
219+
err_destroy_pool_nodes:
220+
umf_ba_destroy(c->pool_nodes);
221+
err_util_mutex_destroy:
222+
util_mutex_destroy_not_free(&c->mutex);
223+
err_destroy_pool_linear:
224+
umf_ba_linear_destroy(pool_linear); // free all its allocations and destroy
225+
return NULL;
194226
}
195227

196228
/*
197229
* internal: delete_node -- recursively free (to malloc) a subtree
198230
*/
199-
static void delete_node(struct critnib_node *__restrict n) {
231+
static void delete_node(struct critnib *c, struct critnib_node *__restrict n) {
200232
if (is_leaf(n)) {
201-
Free(to_leaf(n));
233+
umf_ba_free(c->pool_leaves, to_leaf(n));
202234
} else {
203235
for (int i = 0; i < SLNODES; i++) {
204236
if (n->child[i]) {
205-
delete_node(n->child[i]);
237+
delete_node(c, n->child[i]);
206238
}
207239
}
208240

209-
Free(n);
241+
umf_ba_free(c->pool_nodes, n);
210242
}
211243
}
212244

@@ -215,29 +247,35 @@ static void delete_node(struct critnib_node *__restrict n) {
215247
*/
216248
void critnib_delete(struct critnib *c) {
217249
if (c->root) {
218-
delete_node(c->root);
250+
delete_node(c, c->root);
219251
}
220252

221-
util_mutex_destroy(c->mutex);
253+
// mutex is freed in umf_ba_linear_destroy(c->pool_linear) at the end
254+
util_mutex_destroy_not_free(&c->mutex);
222255

223256
for (struct critnib_node *m = c->deleted_node; m;) {
224257
struct critnib_node *mm = m->child[0];
225-
Free(m);
258+
umf_ba_free(c->pool_nodes, m);
226259
m = mm;
227260
}
228261

229262
for (struct critnib_leaf *k = c->deleted_leaf; k;) {
230263
struct critnib_leaf *kk = k->value;
231-
Free(k);
264+
umf_ba_free(c->pool_leaves, k);
232265
k = kk;
233266
}
234267

235268
for (int i = 0; i < DELETED_LIFE; i++) {
236-
Free(c->pending_del_nodes[i]);
237-
Free(c->pending_del_leaves[i]);
269+
umf_ba_free(c->pool_nodes, c->pending_del_nodes[i]);
270+
umf_ba_free(c->pool_leaves, c->pending_del_leaves[i]);
238271
}
239272

240-
Free(c);
273+
umf_ba_destroy(c->pool_nodes);
274+
umf_ba_destroy(c->pool_leaves);
275+
umf_ba_linear_destroy(
276+
c->pool_linear); // free all its allocations and destroy
277+
278+
// 'c' was freed in umf_ba_linear_destroy(c->pool_linear)
241279
}
242280

243281
/*
@@ -264,7 +302,7 @@ static void free_node(struct critnib *__restrict c,
264302
*/
265303
static struct critnib_node *alloc_node(struct critnib *__restrict c) {
266304
if (!c->deleted_node) {
267-
return Malloc(sizeof(struct critnib_node));
305+
return umf_ba_alloc(c->pool_nodes);
268306
}
269307

270308
struct critnib_node *n = c->deleted_node;
@@ -295,7 +333,7 @@ static void free_leaf(struct critnib *__restrict c,
295333
*/
296334
static struct critnib_leaf *alloc_leaf(struct critnib *__restrict c) {
297335
if (!c->deleted_leaf) {
298-
return Malloc(sizeof(struct critnib_leaf));
336+
return umf_ba_alloc(c->pool_leaves);
299337
}
300338

301339
struct critnib_leaf *k = c->deleted_leaf;
@@ -317,11 +355,11 @@ static struct critnib_leaf *alloc_leaf(struct critnib *__restrict c) {
317355
* Takes a global write lock but doesn't stall any readers.
318356
*/
319357
int critnib_insert(struct critnib *c, word key, void *value, int update) {
320-
util_mutex_lock(c->mutex);
358+
util_mutex_lock(&c->mutex);
321359

322360
struct critnib_leaf *k = alloc_leaf(c);
323361
if (!k) {
324-
util_mutex_unlock(c->mutex);
362+
util_mutex_unlock(&c->mutex);
325363

326364
return ENOMEM;
327365
}
@@ -337,7 +375,7 @@ int critnib_insert(struct critnib *c, word key, void *value, int update) {
337375
if (!n) {
338376
c->root = kn;
339377

340-
util_mutex_unlock(c->mutex);
378+
util_mutex_unlock(&c->mutex);
341379

342380
return 0;
343381
}
@@ -355,7 +393,7 @@ int critnib_insert(struct critnib *c, word key, void *value, int update) {
355393
n = prev;
356394
store(&n->child[slice_index(key, n->shift)], kn);
357395

358-
util_mutex_unlock(c->mutex);
396+
util_mutex_unlock(&c->mutex);
359397

360398
return 0;
361399
}
@@ -369,10 +407,10 @@ int critnib_insert(struct critnib *c, word key, void *value, int update) {
369407

370408
if (update) {
371409
to_leaf(n)->value = value;
372-
util_mutex_unlock(c->mutex);
410+
util_mutex_unlock(&c->mutex);
373411
return 0;
374412
} else {
375-
util_mutex_unlock(c->mutex);
413+
util_mutex_unlock(&c->mutex);
376414
return EEXIST;
377415
}
378416
}
@@ -384,7 +422,7 @@ int critnib_insert(struct critnib *c, word key, void *value, int update) {
384422
if (!m) {
385423
free_leaf(c, to_leaf(kn));
386424

387-
util_mutex_unlock(c->mutex);
425+
util_mutex_unlock(&c->mutex);
388426

389427
return ENOMEM;
390428
}
@@ -400,7 +438,7 @@ int critnib_insert(struct critnib *c, word key, void *value, int update) {
400438
m->path = key & path_mask(sh);
401439
store(parent, m);
402440

403-
util_mutex_unlock(c->mutex);
441+
util_mutex_unlock(&c->mutex);
404442

405443
return 0;
406444
}
@@ -412,7 +450,7 @@ void *critnib_remove(struct critnib *c, word key) {
412450
struct critnib_leaf *k;
413451
void *value = NULL;
414452

415-
util_mutex_lock(c->mutex);
453+
util_mutex_lock(&c->mutex);
416454

417455
struct critnib_node *n = c->root;
418456
if (!n) {
@@ -482,7 +520,7 @@ void *critnib_remove(struct critnib *c, word key) {
482520
c->pending_del_leaves[del] = k;
483521

484522
not_found:
485-
util_mutex_unlock(c->mutex);
523+
util_mutex_unlock(&c->mutex);
486524
return value;
487525
}
488526

@@ -805,9 +843,9 @@ static int iter(struct critnib_node *__restrict n, word min, word max,
805843
void critnib_iter(critnib *c, uintptr_t min, uintptr_t max,
806844
int (*func)(uintptr_t key, void *value, void *privdata),
807845
void *privdata) {
808-
util_mutex_lock(c->mutex);
846+
util_mutex_lock(&c->mutex);
809847
if (c->root) {
810848
iter(c->root, min, max, func, privdata);
811849
}
812-
util_mutex_unlock(c->mutex);
850+
util_mutex_unlock(&c->mutex);
813851
}

src/utils/utils_common.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@ extern "C" {
2525
#define __TLS __thread
2626
#endif
2727

28-
#define Malloc malloc
29-
#define Free free
30-
31-
static inline void *Zalloc(size_t s) {
32-
void *m = Malloc(s);
33-
if (m) {
34-
memset(m, 0, s);
35-
}
36-
return m;
37-
}
38-
3928
#define NOFUNCTION \
4029
do { \
4130
} while (0)

0 commit comments

Comments
 (0)