Skip to content

Commit e9106a5

Browse files
committed
Use base allocator in critnib
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 729dbe0 commit e9106a5

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
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: 61 additions & 18 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"
@@ -127,6 +129,10 @@ struct critnib {
127129
uint64_t remove_count;
128130

129131
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,68 @@ 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();
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 = umf_ba_linear_alloc(pool_linear, util_mutex_get_size());
200+
if (!mutex_ptr) {
201+
goto err_destroy_pool_linear;
202+
}
203+
204+
c->mutex = util_mutex_init(mutex_ptr);
185205
if (!c->mutex) {
186-
free(c);
187-
return NULL;
206+
goto err_destroy_pool_linear;
207+
}
208+
209+
c->pool_nodes = umf_ba_create(sizeof(struct critnib_node));
210+
if (!c->pool_nodes) {
211+
goto err_util_mutex_destroy;
212+
}
213+
214+
c->pool_leaves = umf_ba_create(sizeof(struct critnib_leaf));
215+
if (!c->pool_leaves) {
216+
goto err_destroy_pool_nodes;
188217
}
189218

190219
VALGRIND_HG_DRD_DISABLE_CHECKING(&c->root, sizeof(c->root));
191220
VALGRIND_HG_DRD_DISABLE_CHECKING(&c->remove_count, sizeof(c->remove_count));
192221

193222
return c;
223+
224+
err_destroy_pool_nodes:
225+
umf_ba_destroy(c->pool_nodes);
226+
err_util_mutex_destroy:
227+
util_mutex_destroy_not_free(c->mutex);
228+
err_destroy_pool_linear:
229+
umf_ba_linear_destroy(pool_linear); // free all its allocations and destroy
230+
return NULL;
194231
}
195232

196233
/*
197234
* internal: delete_node -- recursively free (to malloc) a subtree
198235
*/
199-
static void delete_node(struct critnib_node *__restrict n) {
236+
static void delete_node(struct critnib *c, struct critnib_node *__restrict n) {
200237
if (is_leaf(n)) {
201-
Free(to_leaf(n));
238+
umf_ba_free(c->pool_leaves, to_leaf(n));
202239
} else {
203240
for (int i = 0; i < SLNODES; i++) {
204241
if (n->child[i]) {
205-
delete_node(n->child[i]);
242+
delete_node(c, n->child[i]);
206243
}
207244
}
208245

209-
Free(n);
246+
umf_ba_free(c->pool_nodes, n);
210247
}
211248
}
212249

@@ -215,29 +252,35 @@ static void delete_node(struct critnib_node *__restrict n) {
215252
*/
216253
void critnib_delete(struct critnib *c) {
217254
if (c->root) {
218-
delete_node(c->root);
255+
delete_node(c, c->root);
219256
}
220257

221-
util_mutex_destroy(c->mutex);
258+
// mutex is freed in umf_ba_linear_destroy(c->pool_linear) at the end
259+
util_mutex_destroy_not_free(c->mutex);
222260

223261
for (struct critnib_node *m = c->deleted_node; m;) {
224262
struct critnib_node *mm = m->child[0];
225-
Free(m);
263+
umf_ba_free(c->pool_nodes, m);
226264
m = mm;
227265
}
228266

229267
for (struct critnib_leaf *k = c->deleted_leaf; k;) {
230268
struct critnib_leaf *kk = k->value;
231-
Free(k);
269+
umf_ba_free(c->pool_leaves, k);
232270
k = kk;
233271
}
234272

235273
for (int i = 0; i < DELETED_LIFE; i++) {
236-
Free(c->pending_del_nodes[i]);
237-
Free(c->pending_del_leaves[i]);
274+
umf_ba_free(c->pool_nodes, c->pending_del_nodes[i]);
275+
umf_ba_free(c->pool_leaves, c->pending_del_leaves[i]);
238276
}
239277

240-
Free(c);
278+
umf_ba_destroy(c->pool_nodes);
279+
umf_ba_destroy(c->pool_leaves);
280+
umf_ba_linear_destroy(
281+
c->pool_linear); // free all its allocations and destroy
282+
283+
// 'c' was freed in umf_ba_linear_destroy(c->pool_linear)
241284
}
242285

243286
/*
@@ -264,7 +307,7 @@ static void free_node(struct critnib *__restrict c,
264307
*/
265308
static struct critnib_node *alloc_node(struct critnib *__restrict c) {
266309
if (!c->deleted_node) {
267-
return Malloc(sizeof(struct critnib_node));
310+
return umf_ba_alloc(c->pool_nodes);
268311
}
269312

270313
struct critnib_node *n = c->deleted_node;
@@ -295,7 +338,7 @@ static void free_leaf(struct critnib *__restrict c,
295338
*/
296339
static struct critnib_leaf *alloc_leaf(struct critnib *__restrict c) {
297340
if (!c->deleted_leaf) {
298-
return Malloc(sizeof(struct critnib_leaf));
341+
return umf_ba_alloc(c->pool_leaves);
299342
}
300343

301344
struct critnib_leaf *k = c->deleted_leaf;

0 commit comments

Comments
 (0)