Skip to content

Commit 67d7ab4

Browse files
author
Georgy Kirichenko
committed
Free all slabs on region reset
Free all slabs on region_reset. Freed slabs are going to be used as a cache for future allocation. (cherry picked from commit 50069ca)
1 parent 76be096 commit 67d7ab4

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

small/region.h

+14-4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ region_reserve(struct region *region, size_t size)
156156
slab.next_in_list);
157157
if (size <= rslab_unused(slab))
158158
return (char *) rslab_data(slab) + slab->used;
159+
/* Try to get a slab from the region cache. */
160+
slab = rlist_last_entry(&region->slabs.slabs,
161+
struct rslab,
162+
slab.next_in_list);
163+
if (slab->used == 0 && size <= rslab_unused(slab)) {
164+
/* Move this slab to the head. */
165+
slab_list_del(&region->slabs, &slab->slab, next_in_list);
166+
slab_list_add(&region->slabs, &slab->slab, next_in_list);
167+
return (char *) rslab_data(slab);
168+
}
159169
}
160170
return region_reserve_slow(region, size);
161171
}
@@ -212,14 +222,14 @@ region_aligned_alloc(struct region *region, size_t size, size_t alignment)
212222

213223
/**
214224
* Mark region as empty, but keep the blocks.
225+
* Do not change the first slab and use previous slabs as a cache to
226+
* use for future allocations.
215227
*/
216228
static inline void
217229
region_reset(struct region *region)
218230
{
219-
if (! rlist_empty(&region->slabs.slabs)) {
220-
struct rslab *slab = rlist_first_entry(&region->slabs.slabs,
221-
struct rslab,
222-
slab.next_in_list);
231+
struct rslab *slab;
232+
rlist_foreach_entry(slab, &region->slabs.slabs, slab.next_in_list) {
223233
region->slabs.stats.used -= slab->used;
224234
slab->used = 0;
225235
}

0 commit comments

Comments
 (0)