]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Adjust initial LST pivot stack size in accordance with initial size (#4213)
authorJames Jones <jejones3141@gmail.com>
Fri, 27 Aug 2021 15:44:41 +0000 (10:44 -0500)
committerGitHub <noreply@github.com>
Fri, 27 Aug 2021 15:44:41 +0000 (10:44 -0500)
It's now initially max(1, log2(initial array size)).

src/lib/util/lst.c
src/lib/util/lst_tests.c

index e36190fe2ab80ea612c349315e7a70cea2ce4132..f5d930b34f12db300b93e3137ce9147e85abe6d8 100644 (file)
@@ -46,7 +46,6 @@ RCSID("$Id$")
  * the pivot stack may be a rare event.
  */
 #define INITIAL_CAPACITY       2048
-#define INITIAL_STACK_CAPACITY 32
 
 #define is_power_of_2(_n)      ((_n) && (((_n) & ((_n) - 1)) == 0))
 
@@ -224,6 +223,7 @@ fr_lst_t *_fr_lst_alloc(TALLOC_CTX *ctx, fr_lst_cmp_t cmp, char const *type, siz
 {
        fr_lst_t        *lst;
        pivot_stack_t   *s;
+       unsigned int    initial_stack_capacity;
 
        if (!init) {
                init = INITIAL_CAPACITY;
@@ -231,6 +231,8 @@ fr_lst_t *_fr_lst_alloc(TALLOC_CTX *ctx, fr_lst_cmp_t cmp, char const *type, siz
                init = 1 << fr_high_bit_pos(init);
        }
 
+       for (initial_stack_capacity = 1; (1U << initial_stack_capacity) < init; initial_stack_capacity++) ;
+
        /*
         *      Pre-allocate stack memory as it is
         *      unlikely to need to grow in practice.
@@ -243,7 +245,7 @@ fr_lst_t *_fr_lst_alloc(TALLOC_CTX *ctx, fr_lst_cmp_t cmp, char const *type, siz
         *      Pre-allocating three chunks appears to be
         *      the optimum.
         */
-       lst = talloc_zero_pooled_object(ctx, fr_lst_t, 3, (INITIAL_STACK_CAPACITY * sizeof(fr_lst_index_t)));
+       lst = talloc_zero_pooled_object(ctx, fr_lst_t, 3, (initial_stack_capacity * sizeof(fr_lst_index_t)));
        if (unlikely(!lst)) return NULL;
 
        lst->capacity = init;
@@ -258,10 +260,10 @@ fr_lst_t *_fr_lst_alloc(TALLOC_CTX *ctx, fr_lst_cmp_t cmp, char const *type, siz
         *      Allocate the initial stack
         */
        s = &lst->s;
-       s->data = talloc_array(lst, fr_lst_index_t, INITIAL_STACK_CAPACITY);
+       s->data = talloc_array(lst, fr_lst_index_t, initial_stack_capacity);
        if (unlikely(!s->data)) goto cleanup;
        s->depth = 0;
-       s->size = INITIAL_STACK_CAPACITY;
+       s->size = initial_stack_capacity;
 
        /* Initially the LST is empty and we start at the beginning of the array */
        stack_push(lst, &lst->s, 0);
index 15686aadd487f90a05d03156ea6f7d345d979924..68830728e6028c5297c5a99a5c26303082cbb925 100644 (file)
@@ -260,7 +260,8 @@ static void lst_burn_in(void)
        array = calloc(BURN_IN_OPS, sizeof(lst_thing));
        for (unsigned int i = 0; i < BURN_IN_OPS; i++) array[i].data = rand() % 65537;
 
-       lst = fr_lst_alloc(NULL, lst_cmp, lst_thing, idx, 0);
+       /* Make init small to exercise growing the pivot stack. */
+       lst = fr_lst_alloc(NULL, lst_cmp, lst_thing, idx, 32);
 
        for (unsigned int i = 0; i < BURN_IN_OPS; i++) {
                lst_thing       *ret_thing = NULL;