7 #include <freeradius-devel/util/acutest.h>
8 #include <freeradius-devel/util/acutest_helpers.h>
32 .interval.value = 1 *
NSEC
57 test_slab_list_t *test_slab_list;
64 test_slab_list = test_slab_list_alloc(NULL, NULL, &
def_slab_config, NULL, NULL, NULL,
true,
false);
66 if (!test_slab_list)
return;
68 test_elements[0] = test_slab_reserve(test_slab_list);
77 if (test_elements[0]) test_elements[0]->
name = talloc_strdup(test_elements[0],
"Hello there");
78 if (test_elements[0]) test_slab_element_set_destructor(test_elements[0],
test_element_free, &test_uctx);
80 test_elements[1] = test_slab_reserve(test_slab_list);
82 TEST_CHECK(test_elements[1] != test_elements[0]);
87 test_elements[2] = test_slab_reserve(test_slab_list);
89 if (test_elements[2]) test_elements[2]->
name = talloc_strdup(test_elements[2],
"Hello there testing");
90 if (test_elements[2]) test_slab_element_set_destructor(test_elements[2],
test_element_free, &test_uctx2);
94 test_elements[3] = test_slab_reserve(test_slab_list);
100 test_elements[4] = test_slab_reserve(test_slab_list);
106 if (test_elements[0]) test_slab_release(test_elements[0]);
111 if (test_elements[1]) test_slab_release(test_elements[1]);
115 if (test_elements[2]) test_slab_release(test_elements[2]);
128 test_slab_list_t *test_slab_list;
136 test_slab_list = test_slab_list_alloc(NULL, NULL, &slab_config, NULL, NULL, NULL,
true,
false);
138 if (!test_slab_list)
return;
140 test_elements[0] = test_slab_reserve(test_slab_list);
145 test_elements[1] = test_slab_reserve(test_slab_list);
146 test_elements[2] = test_slab_reserve(test_slab_list);
147 test_elements[3] = test_slab_reserve(test_slab_list);
153 test_elements[4] = test_slab_reserve(test_slab_list);
166 test_slab_list_t *test_slab_list;
170 test_slab_list = test_slab_list_alloc(NULL, NULL, &
def_slab_config, NULL, NULL, NULL,
true,
false);
172 if (!test_slab_list)
return;
174 test_elements[0] = test_slab_reserve(test_slab_list);
181 if (test_elements[0]) test_elements[0]->
name = talloc_strdup(test_elements[0],
"Hello there");
182 if (test_elements[0]) test_slab_element_set_destructor(test_elements[0],
test_element_free, &test_uctx);
184 test_elements[1] = test_slab_reserve(test_slab_list);
186 TEST_CHECK(test_elements[1] != test_elements[0]);
188 test_elements[2] = test_slab_reserve(test_slab_list);
191 test_elements[3] = test_slab_reserve(test_slab_list);
194 if (test_elements[0]) test_slab_release(test_elements[0]);
202 test_elements[4] = test_slab_reserve(test_slab_list);
204 TEST_CHECK(test_elements[4] == test_elements[0]);
214 if (test_elements[4]) test_elements[4]->
name = talloc_strdup(test_elements[4],
"Different length string");
215 if (test_elements[4]) test_slab_release(test_elements[4]);
226 test_slab_list_t *test_slab_list;
230 test_slab_list = test_slab_list_alloc(NULL, NULL, &
def_slab_config, NULL, NULL, NULL,
false,
false);
232 if (!test_slab_list)
return;
234 test_elements[0] = test_slab_reserve(test_slab_list);
241 if (test_elements[0]) test_elements[0]->
name = talloc_strdup(test_elements[0],
"Hello there");
242 if (test_elements[0]) test_slab_element_set_destructor(test_elements[0],
test_element_free, &test_uctx);
244 test_elements[1] = test_slab_reserve(test_slab_list);
246 TEST_CHECK(test_elements[1] != test_elements[0]);
248 if (test_elements[0]) test_slab_release(test_elements[0]);
256 test_elements[2] = test_slab_reserve(test_slab_list);
258 TEST_CHECK(test_elements[2] == test_elements[0]);
259 if (test_elements[0] && test_elements[2])
TEST_CHECK(test_elements[2]->
name == test_elements[0]->
name);
266 if (test_elements[2]) test_elements[2]->
name = talloc_strdup(test_elements[2],
"Different length string");
267 if (test_elements[2]) test_slab_release(test_elements[2]);
279 test_slab_list_t *test_slab_list;
286 test_slab_list = test_slab_list_alloc(NULL, NULL, &
def_slab_config, NULL, NULL, NULL,
true,
false);
288 if (!test_slab_list)
return;
290 test_elements[0] = test_slab_reserve(test_slab_list);
293 if (test_elements[0]) test_slab_release(test_elements[0]);
295 test_elements[1] = test_slab_reserve(test_slab_list);
297 TEST_CHECK(test_elements[0] != test_elements[1]);
304 test_slab_list = test_slab_list_alloc(NULL, NULL, &
def_slab_config, NULL, NULL, NULL,
true,
true);
306 if (!test_slab_list)
return;
308 test_elements[0] = test_slab_reserve(test_slab_list);
311 if (test_elements[0]) test_slab_release(test_elements[0]);
313 test_elements[1] = test_slab_reserve(test_slab_list);
315 TEST_CHECK(test_elements[0] == test_elements[1]);
325 test_slab_list_t *test_slab_list;
329 test_slab_list = test_slab_list_alloc(NULL, NULL, &
def_slab_config, NULL, NULL, NULL,
true,
false);
331 if (!test_slab_list)
return;
333 test_element = test_slab_reserve(test_slab_list);
338 if (test_element) test_element->
name = talloc_strdup(test_element,
"Hello there");
339 if (test_element) test_slab_element_set_destructor(test_element,
test_element_free, &test_uctx);
359 test_slab_list_t *test_slab_list;
365 test_slab_list = test_slab_list_alloc(NULL, NULL, &slab_config,
test_element_alloc, NULL, &test_conf,
false,
false);
367 if (!test_slab_list)
return;
369 test_elements[0] = test_slab_reserve(test_slab_list);
371 TEST_CHECK(test_elements[0] && (test_elements[0]->num == 10));
376 if (test_elements[0]) {
377 test_elements[0]->
num = 5;
378 test_slab_release(test_elements[0]);
384 test_elements[1] = test_slab_reserve(test_slab_list);
386 TEST_CHECK(test_elements[1] == test_elements[0]);
387 if (test_elements[1])
TEST_CHECK(test_elements[1]->num == 5);
397 test_slab_list_t *test_slab_list;
403 test_slab_list = test_slab_list_alloc(NULL, NULL, &slab_config, NULL,
test_element_alloc, &test_conf,
false,
false);
405 if (!test_slab_list)
return;
407 test_elements[0] = test_slab_reserve(test_slab_list);
409 TEST_CHECK(test_elements[0] && (test_elements[0]->num == 10));
414 if (test_elements[0]) {
415 test_elements[0]->
num = 5;
416 test_slab_release(test_elements[0]);
422 test_elements[1] = test_slab_reserve(test_slab_list);
424 TEST_CHECK(test_elements[1] == test_elements[0]);
425 if (test_elements[1])
TEST_CHECK(test_elements[1]->num == 10);
442 test_slab_list_t *test_slab_list;
450 if (!test_slab_list)
return;
452 test_elements[0] = test_slab_reserve(test_slab_list);
454 TEST_CHECK(test_elements[0] && (test_elements[0]->num == 20));
459 if (test_elements[0]) {
460 test_elements[0]->
num = 5;
461 test_slab_release(test_elements[0]);
467 test_elements[1] = test_slab_reserve(test_slab_list);
469 TEST_CHECK(test_elements[1] == test_elements[0]);
470 if (test_elements[1])
TEST_CHECK(test_elements[1]->num == 20);
482 test_slab_list_t *test_slab_list;
491 test_slab_list = test_slab_list_alloc(NULL,
el, &slab_config, NULL, NULL, NULL,
true,
false);
493 if (!test_slab_list)
return;
498 for (i = 0; i < 6; i++) {
499 test_elements[i] = test_slab_reserve(test_slab_list);
508 for (i = 0; i < 4; i++) {
509 test_slab_release(test_elements[i]);
536 test_slab_list_t *test_slab_list;
546 test_slab_list = test_slab_list_alloc(NULL,
el, &slab_config, NULL, NULL, NULL,
true,
false);
548 if (!test_slab_list)
return;
553 for (i = 0; i < 20; i++) {
554 test_elements[i] = test_slab_reserve(test_slab_list);
563 for (i = 0; i < 20; i++) {
564 test_slab_release(test_elements[i]);
601 test_slab_list_t *test_slab_list;
611 test_slab_list = test_slab_list_alloc(NULL,
el, &slab_config, NULL, NULL, NULL,
true,
false);
613 if (!test_slab_list)
return;
618 for (i = 0; i < 20; i++) {
619 test_elements[i] = test_slab_reserve(test_slab_list);
628 for (i = 0; i < 20; i++) {
629 test_slab_release(test_elements[i]);
695 test_slab_list_t *test_slab_list;
705 test_slab_list = test_slab_list_alloc(NULL,
el, &slab_config, NULL, NULL, NULL,
true,
false);
707 if (!test_slab_list)
return;
712 for (i = 0; i < 20; i++) {
713 test_elements[i] = test_slab_reserve(test_slab_list);
722 for (i = 0; i < 20; i++) {
723 test_slab_release(test_elements[i]);
742 for (i = 0; i < 20; i++) {
743 test_elements[i] = test_slab_reserve(test_slab_list);
755 test_slab_list_t *test_slab_list;
762 test_slab_list = test_slab_list_alloc(NULL, NULL, &slab_config, NULL, NULL, NULL,
false,
false);
764 if (!test_slab_list)
return;
766 test_elements[0] = test_slab_reserve(test_slab_list);
768 test_elements[1] = test_slab_reserve(test_slab_list);
778 if (test_elements[0]) {
779 test_elements[0]->
name = talloc_strdup(test_elements[0],
"Hello there");
780 TEST_CHECK((
void *)test_elements[0]->
name > (
void *)test_elements[0]);
781 if (test_elements[1] && (test_elements[1] > test_elements[0])) {
782 TEST_CHECK((
void *)test_elements[0]->
name < (
void *)test_elements[1]);
783 TEST_MSG(
"element 0: %p, name %p, element 1: %p",
784 test_elements[0], test_elements[0]->
name, test_elements[1]);
fr_dcursor_eval_t void const * uctx
TEST_CHECK_RET((int) count,(int) 3)
void fr_event_service(fr_event_list_t *el)
Service any outstanding timer or file descriptor events.
fr_event_list_t * fr_event_list_alloc(TALLOC_CTX *ctx, fr_event_status_cb_t status, void *status_uctx)
Initialise a new event list.
void fr_event_list_set_time_func(fr_event_list_t *el, fr_event_time_source_t func)
Override event list time source.
int fr_event_corral(fr_event_list_t *el, fr_time_t now, bool wait)
Gather outstanding timer and file descriptor events.
Stores all information relating to an event list.
static fr_event_list_t * events
Resource pool management using slabs of memory.
#define FR_SLAB_FUNCS(_name, _type)
Define type specific wrapper functions for slabs and slab elements.
#define FR_SLAB_TYPES(_name, _type)
Define type specific wrapper structs for slabs and slab elements.
unsigned int min_elements
Minimum number of elements to keep allocated.
unsigned int max_elements
Maximum number of elements to allocate using slabs.
bool at_max_fail
Should requests for additional elements fail when the number in use has reached max_elements.
unsigned int elements_per_slab
Number of elements to allocate per slab.
unsigned int num_children
How many child allocations are expected off each element.
size_t child_pool_size
Size of pool space to be allocated to each element.
Tuneable parameters for slabs.
static fr_time_t test_time(void)
static void test_clearup_3(void)
Test that repeated clearing frees more slabs.
static void test_reserve(void)
Test that a reserve callback correctly initialises slab elements.
static fr_time_t test_time_base
static void test_init_reserve(void)
Test that reserve callback runs after init callback.
static fr_slab_config_t def_slab_config
static void test_reserve_mru(void)
Test that setting reserve_mru to true works.
static void test_clearup_2(void)
Test that slab clearing does not go beyond the minimum.
static void test_reuse_noreset(void)
Test that freeing an element makes it available for reuse with the element not reset between uses.
static void test_child_alloc(void)
static int test_element_reserve(test_element_t *elem, void *uctx)
static void test_clearup_1(void)
Test of clearing unused slabs.
static void test_free(void)
Test that talloc freeing an element results in destructor being called.
static void test_alloc(void)
Test basic allocation and reservation of elements.
static void test_reuse_reset(void)
Test that freeing an element makes it available for reuse with the element reset between uses.
static int test_element_free(test_element_t *elem, void *uctx)
static void test_init(void)
Test that a callback correctly initialises slab elements on first use.
static void test_alloc_fail(void)
Test allocation beyond max fails correctly.
static int test_element_alloc(test_element_t *elem, void *uctx)
static void test_realloc(void)
Test that reserving after clearup results in new slab allocation.
static TALLOC_CTX * talloc_init_const(char const *name)
Allocate a top level chunk with a constant name.
static fr_time_t fr_time_add_time_delta(fr_time_t a, fr_time_delta_t b)
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
#define fr_time_wrap(_time)
static fr_event_list_t * el