Skip to content

Commit

Permalink
do not keep a slab in cache if new allocation does not fit
Browse files Browse the repository at this point in the history
Do not allocate a new slab if there is a free slab in a region cache.
If a cached slab is not big enough then free it.

Follow up 67d7ab4

Fixes #12
  • Loading branch information
Georgy Kirichenko committed Jan 27, 2020
1 parent 50cb787 commit b4c1131
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions small/region.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,20 @@ region_reserve(struct region *region, size_t size)
if (size <= rslab_unused(slab))
return (char *) rslab_data(slab) + slab->used;
/* Try to get a slab from the region cache. */
slab = rlist_last_entry(&region->slabs.slabs,
struct rslab,
slab.next_in_list);
if (slab->used == 0 && size <= rslab_unused(slab)) {
/* Move this slab to the head. */
while ((slab = rlist_last_entry(&region->slabs.slabs,
struct rslab,
slab.next_in_list))->used == 0) {
slab_list_del(&region->slabs, &slab->slab, next_in_list);
slab_list_add(&region->slabs, &slab->slab, next_in_list);
return (char *) rslab_data(slab);
if (size <= rslab_unused(slab)) {
/* Move this slab to the head. */
slab_list_add(&region->slabs, &slab->slab, next_in_list);
return (char *) rslab_data(slab);
}
/*
* This cached slab could not be used so free it
* and try to use the next one.
*/
slab_put(region->cache, (struct slab *)slab);
}
}
return region_reserve_slow(region, size);
Expand Down

0 comments on commit b4c1131

Please sign in to comment.