On 2019.10.17 13:17, Jonathan Tan wrote: > @@ -380,27 +378,37 @@ static void free_base_data(struct base_data *c) > } > } > > -static void prune_base_data(struct base_data *retain) > +static void prune_base_data(struct base_data *youngest_child) > { > struct base_data *b; > struct thread_local *data = get_thread_data(); > - for (b = data->base_cache; > - data->base_cache_used > delta_base_cache_limit && b; > - b = b->child) { > - if (b->data && b != retain) > - free_base_data(b); > + struct base_data **ancestry = NULL; > + size_t nr = 0, alloc = 0; > + ssize_t i; > + > + if (data->base_cache_used <= delta_base_cache_limit) > + return; > + > + /* > + * Free all ancestors of youngest_child until we have enough space, > + * starting with the oldest. (We cannot free youngest_child itself.) > + */ > + for (b = youngest_child->base; b != NULL; b = b->base) { > + ALLOC_GROW(ancestry, nr + 1, alloc); > + ancestry[nr++] = b; > } > + for (i = nr - 1; > + i >= 0 && data->base_cache_used > delta_base_cache_limit; > + i--) { > + if (ancestry[i]->data) > + free_base_data(ancestry[i]); > + } > + free(ancestry); > } I had a small complaint that we're allocating new memory in a function where we are trying to free up space, but in practice it probably doesn't matter. And this is removed in a later patch anyway.