* 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))
{
fr_lst_t *lst;
pivot_stack_t *s;
+ unsigned int initial_stack_capacity;
if (!init) {
init = INITIAL_CAPACITY;
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.
* 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;
* 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);
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;