1998-05-04 00:43:39 +08:00
|
|
|
/*
|
2000-03-06 06:48:30 +08:00
|
|
|
* BIRD Resource Manager -- A SLAB-like Memory Allocator
|
1998-05-04 00:43:39 +08:00
|
|
|
*
|
2000-03-06 06:48:30 +08:00
|
|
|
* Heavily inspired by the original SLAB paper by Jeff Bonwick.
|
|
|
|
*
|
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
2020-07-22 06:09:15 +08:00
|
|
|
* (c) 2020 Maria Matejka <mq@jmq.cz>
|
1998-05-04 00:43:39 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-06-05 19:41:41 +08:00
|
|
|
/**
|
|
|
|
* DOC: Slabs
|
|
|
|
*
|
|
|
|
* Slabs are collections of memory blocks of a fixed size.
|
|
|
|
* They support very fast allocation and freeing of such blocks, prevent memory
|
|
|
|
* fragmentation and optimize L2 cache usage. Slabs have been invented by Jeff Bonwick
|
|
|
|
* and published in USENIX proceedings as `The Slab Allocator: An Object-Caching Kernel
|
|
|
|
* Memory Allocator'. Our implementation follows this article except that we don't use
|
|
|
|
* constructors and destructors.
|
|
|
|
*
|
|
|
|
* When the |DEBUGGING| switch is turned on, we automatically fill all
|
2000-06-07 21:25:53 +08:00
|
|
|
* newly allocated and freed blocks with a special pattern to make detection
|
2000-06-05 19:41:41 +08:00
|
|
|
* of use of uninitialized or already freed memory easier.
|
|
|
|
*
|
2000-06-07 21:25:53 +08:00
|
|
|
* Example: Nodes of a FIB are allocated from a per-FIB Slab.
|
2000-06-05 19:41:41 +08:00
|
|
|
*/
|
|
|
|
|
1998-05-04 00:43:39 +08:00
|
|
|
#include <stdlib.h>
|
2009-09-17 23:52:36 +08:00
|
|
|
#include <stdint.h>
|
1998-05-04 00:43:39 +08:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "lib/resource.h"
|
2000-04-01 07:30:21 +08:00
|
|
|
#include "lib/string.h"
|
1998-05-04 00:43:39 +08:00
|
|
|
|
2000-03-06 06:48:30 +08:00
|
|
|
#undef FAKE_SLAB /* Turn on if you want to debug memory allocations */
|
|
|
|
|
2000-05-16 21:51:31 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
#define POISON /* Poison all regions after they are freed */
|
|
|
|
#endif
|
|
|
|
|
2000-03-06 06:48:30 +08:00
|
|
|
static void slab_free(resource *r);
|
|
|
|
static void slab_dump(resource *r);
|
2000-05-09 06:33:38 +08:00
|
|
|
static resource *slab_lookup(resource *r, unsigned long addr);
|
2010-06-03 04:20:40 +08:00
|
|
|
static size_t slab_memsize(resource *r);
|
2000-03-06 06:48:30 +08:00
|
|
|
|
|
|
|
#ifdef FAKE_SLAB
|
|
|
|
|
1998-05-04 00:43:39 +08:00
|
|
|
/*
|
2000-03-06 06:48:30 +08:00
|
|
|
* Fake version used for debugging.
|
1998-05-04 00:43:39 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct slab {
|
|
|
|
resource r;
|
2015-05-19 14:53:34 +08:00
|
|
|
uint size;
|
1998-05-04 00:43:39 +08:00
|
|
|
list objs;
|
|
|
|
};
|
|
|
|
|
1998-05-24 22:46:20 +08:00
|
|
|
static struct resclass sl_class = {
|
2000-03-06 06:48:30 +08:00
|
|
|
"FakeSlab",
|
1998-05-04 00:43:39 +08:00
|
|
|
sizeof(struct slab),
|
|
|
|
slab_free,
|
2010-06-03 04:20:40 +08:00
|
|
|
slab_dump,
|
2012-01-03 07:42:25 +08:00
|
|
|
NULL,
|
2010-06-03 04:20:40 +08:00
|
|
|
slab_memsize
|
1998-05-04 00:43:39 +08:00
|
|
|
};
|
|
|
|
|
2000-03-06 06:48:30 +08:00
|
|
|
struct sl_obj {
|
|
|
|
node n;
|
2009-07-07 01:07:01 +08:00
|
|
|
uintptr_t data_align[0];
|
2000-03-06 06:48:30 +08:00
|
|
|
byte data[0];
|
|
|
|
};
|
|
|
|
|
1998-05-04 00:43:39 +08:00
|
|
|
slab *
|
2015-05-19 14:53:34 +08:00
|
|
|
sl_new(pool *p, uint size)
|
1998-05-04 00:43:39 +08:00
|
|
|
{
|
|
|
|
slab *s = ralloc(p, &sl_class);
|
|
|
|
s->size = size;
|
|
|
|
init_list(&s->objs);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
sl_alloc(slab *s)
|
|
|
|
{
|
|
|
|
struct sl_obj *o = xmalloc(sizeof(struct sl_obj) + s->size);
|
|
|
|
|
|
|
|
add_tail(&s->objs, &o->n);
|
|
|
|
return o->data;
|
|
|
|
}
|
|
|
|
|
2020-11-24 09:32:13 +08:00
|
|
|
void *
|
|
|
|
sl_allocz(slab *s)
|
|
|
|
{
|
|
|
|
void *obj = sl_alloc(s);
|
|
|
|
memset(obj, 0, s->size);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
1998-05-04 00:43:39 +08:00
|
|
|
void
|
|
|
|
sl_free(slab *s, void *oo)
|
|
|
|
{
|
|
|
|
struct sl_obj *o = SKIP_BACK(struct sl_obj, data, oo);
|
|
|
|
|
|
|
|
rem_node(&o->n);
|
|
|
|
xfree(o);
|
|
|
|
}
|
|
|
|
|
1998-05-24 22:46:20 +08:00
|
|
|
static void
|
1998-05-04 00:43:39 +08:00
|
|
|
slab_free(resource *r)
|
|
|
|
{
|
|
|
|
slab *s = (slab *) r;
|
|
|
|
struct sl_obj *o, *p;
|
|
|
|
|
|
|
|
for(o = HEAD(s->objs); p = (struct sl_obj *) o->n.next; o = p)
|
|
|
|
xfree(o);
|
|
|
|
}
|
|
|
|
|
1998-05-24 22:46:20 +08:00
|
|
|
static void
|
1998-05-04 00:43:39 +08:00
|
|
|
slab_dump(resource *r)
|
|
|
|
{
|
|
|
|
slab *s = (slab *) r;
|
|
|
|
int cnt = 0;
|
|
|
|
struct sl_obj *o;
|
|
|
|
|
|
|
|
WALK_LIST(o, s->objs)
|
|
|
|
cnt++;
|
|
|
|
debug("(%d objects per %d bytes)\n", cnt, s->size);
|
|
|
|
}
|
2000-03-06 06:48:30 +08:00
|
|
|
|
2010-06-03 04:20:40 +08:00
|
|
|
static size_t
|
|
|
|
slab_memsize(resource *r)
|
|
|
|
{
|
|
|
|
slab *s = (slab *) r;
|
2014-06-30 17:55:22 +08:00
|
|
|
size_t cnt = 0;
|
2010-06-03 04:20:40 +08:00
|
|
|
struct sl_obj *o;
|
|
|
|
|
|
|
|
WALK_LIST(o, s->objs)
|
|
|
|
cnt++;
|
|
|
|
|
|
|
|
return ALLOC_OVERHEAD + sizeof(struct slab) + cnt * (ALLOC_OVERHEAD + s->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-06 06:48:30 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Real efficient version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_EMPTY_HEADS 1
|
|
|
|
|
|
|
|
struct slab {
|
|
|
|
resource r;
|
2021-09-01 06:46:46 +08:00
|
|
|
pool *p;
|
2020-07-22 06:09:15 +08:00
|
|
|
uint obj_size, head_size, head_bitfield_len;
|
|
|
|
uint objs_per_slab, num_empty_heads, data_size;
|
2000-03-06 06:48:30 +08:00
|
|
|
list empty_heads, partial_heads, full_heads;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct resclass sl_class = {
|
|
|
|
"Slab",
|
|
|
|
sizeof(struct slab),
|
|
|
|
slab_free,
|
2000-05-09 06:33:38 +08:00
|
|
|
slab_dump,
|
2010-06-03 04:20:40 +08:00
|
|
|
slab_lookup,
|
|
|
|
slab_memsize
|
2000-03-06 06:48:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct sl_head {
|
|
|
|
node n;
|
2020-07-22 06:09:15 +08:00
|
|
|
u32 num_full;
|
|
|
|
u32 used_bits[0];
|
2000-03-06 06:48:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct sl_alignment { /* Magic structure for testing of alignment */
|
|
|
|
byte data;
|
|
|
|
int x[0];
|
|
|
|
};
|
|
|
|
|
2020-07-22 06:09:15 +08:00
|
|
|
#define SL_GET_HEAD(x) ((struct sl_head *) (((uintptr_t) (x)) & ~(get_page_size()-1)))
|
|
|
|
|
2000-06-05 19:41:41 +08:00
|
|
|
/**
|
|
|
|
* sl_new - create a new Slab
|
|
|
|
* @p: resource pool
|
|
|
|
* @size: block size
|
|
|
|
*
|
|
|
|
* This function creates a new Slab resource from which
|
|
|
|
* objects of size @size can be allocated.
|
|
|
|
*/
|
2000-03-06 06:48:30 +08:00
|
|
|
slab *
|
2015-05-19 14:53:34 +08:00
|
|
|
sl_new(pool *p, uint size)
|
2000-03-06 06:48:30 +08:00
|
|
|
{
|
|
|
|
slab *s = ralloc(p, &sl_class);
|
2021-09-01 06:46:46 +08:00
|
|
|
s->p = p;
|
2015-05-19 14:53:34 +08:00
|
|
|
uint align = sizeof(struct sl_alignment);
|
2000-03-06 06:48:30 +08:00
|
|
|
if (align < sizeof(int))
|
|
|
|
align = sizeof(int);
|
2000-05-16 23:02:33 +08:00
|
|
|
s->data_size = size;
|
2000-03-06 06:48:30 +08:00
|
|
|
size = (size + align - 1) / align * align;
|
|
|
|
s->obj_size = size;
|
2020-07-22 06:09:15 +08:00
|
|
|
|
|
|
|
s->head_size = sizeof(struct sl_head);
|
|
|
|
|
|
|
|
do {
|
|
|
|
s->objs_per_slab = (page_size - s->head_size) / size;
|
|
|
|
s->head_bitfield_len = (s->objs_per_slab + 31) / 32;
|
|
|
|
s->head_size = (
|
|
|
|
sizeof(struct sl_head)
|
|
|
|
+ sizeof(u32) * s->head_bitfield_len
|
|
|
|
+ align - 1)
|
|
|
|
/ align * align;
|
|
|
|
} while (s->objs_per_slab * size + s->head_size > page_size);
|
|
|
|
|
2000-03-06 06:48:30 +08:00
|
|
|
if (!s->objs_per_slab)
|
|
|
|
bug("Slab: object too large");
|
|
|
|
s->num_empty_heads = 0;
|
2020-07-22 06:09:15 +08:00
|
|
|
|
2000-03-06 06:48:30 +08:00
|
|
|
init_list(&s->empty_heads);
|
|
|
|
init_list(&s->partial_heads);
|
|
|
|
init_list(&s->full_heads);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2000-06-05 19:41:41 +08:00
|
|
|
/**
|
|
|
|
* sl_alloc - allocate an object from Slab
|
|
|
|
* @s: slab
|
|
|
|
*
|
|
|
|
* sl_alloc() allocates space for a single object from the
|
|
|
|
* Slab and returns a pointer to the object.
|
|
|
|
*/
|
2000-03-06 06:48:30 +08:00
|
|
|
void *
|
|
|
|
sl_alloc(slab *s)
|
|
|
|
{
|
|
|
|
struct sl_head *h;
|
|
|
|
|
|
|
|
redo:
|
|
|
|
h = HEAD(s->partial_heads);
|
|
|
|
if (!h->n.next)
|
|
|
|
goto no_partial;
|
|
|
|
okay:
|
2020-07-22 06:09:15 +08:00
|
|
|
for (uint i=0; i<s->head_bitfield_len; i++)
|
|
|
|
if (~h->used_bits[i])
|
|
|
|
{
|
|
|
|
uint pos = u32_ctz(~h->used_bits[i]);
|
|
|
|
if (i * 32 + pos >= s->objs_per_slab)
|
|
|
|
break;
|
|
|
|
|
|
|
|
h->used_bits[i] |= 1 << pos;
|
|
|
|
h->num_full++;
|
|
|
|
|
|
|
|
void *out = ((void *) h) + s->head_size + (i * 32 + pos) * s->obj_size;
|
2000-05-16 23:02:33 +08:00
|
|
|
#ifdef POISON
|
2020-07-22 06:09:15 +08:00
|
|
|
memset(out, 0xcd, s->data_size);
|
2000-05-16 23:02:33 +08:00
|
|
|
#endif
|
2020-07-22 06:09:15 +08:00
|
|
|
return out;
|
|
|
|
}
|
2000-03-06 06:48:30 +08:00
|
|
|
|
|
|
|
rem_node(&h->n);
|
|
|
|
add_tail(&s->full_heads, &h->n);
|
|
|
|
goto redo;
|
|
|
|
|
|
|
|
no_partial:
|
|
|
|
h = HEAD(s->empty_heads);
|
|
|
|
if (h->n.next)
|
|
|
|
{
|
|
|
|
rem_node(&h->n);
|
|
|
|
add_head(&s->partial_heads, &h->n);
|
|
|
|
s->num_empty_heads--;
|
|
|
|
goto okay;
|
|
|
|
}
|
2021-09-01 06:46:46 +08:00
|
|
|
h = alloc_page(s->p);
|
2021-05-27 16:35:33 +08:00
|
|
|
#ifdef POISON
|
2021-09-01 06:46:46 +08:00
|
|
|
memset(h, 0xba, page_size);
|
2021-05-27 16:35:33 +08:00
|
|
|
#endif
|
2020-07-22 06:09:15 +08:00
|
|
|
ASSERT_DIE(SL_GET_HEAD(h) == h);
|
|
|
|
memset(h, 0, s->head_size);
|
2000-03-06 06:48:30 +08:00
|
|
|
add_head(&s->partial_heads, &h->n);
|
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
2020-11-24 09:32:13 +08:00
|
|
|
/**
|
|
|
|
* sl_allocz - allocate an object from Slab and zero it
|
|
|
|
* @s: slab
|
|
|
|
*
|
|
|
|
* sl_allocz() allocates space for a single object from the
|
|
|
|
* Slab and returns a pointer to the object after zeroing out
|
|
|
|
* the object memory.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
sl_allocz(slab *s)
|
|
|
|
{
|
|
|
|
void *obj = sl_alloc(s);
|
|
|
|
memset(obj, 0, s->data_size);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2000-06-05 19:41:41 +08:00
|
|
|
/**
|
|
|
|
* sl_free - return a free object back to a Slab
|
|
|
|
* @s: slab
|
|
|
|
* @oo: object returned by sl_alloc()
|
|
|
|
*
|
|
|
|
* This function frees memory associated with the object @oo
|
|
|
|
* and returns it back to the Slab @s.
|
|
|
|
*/
|
2000-03-06 06:48:30 +08:00
|
|
|
void
|
|
|
|
sl_free(slab *s, void *oo)
|
|
|
|
{
|
2020-07-22 06:09:15 +08:00
|
|
|
struct sl_head *h = SL_GET_HEAD(oo);
|
2000-03-06 06:48:30 +08:00
|
|
|
|
2000-05-16 21:51:31 +08:00
|
|
|
#ifdef POISON
|
2000-05-16 23:02:33 +08:00
|
|
|
memset(oo, 0xdb, s->data_size);
|
2000-05-16 21:51:31 +08:00
|
|
|
#endif
|
2020-07-22 06:09:15 +08:00
|
|
|
|
|
|
|
uint offset = oo - ((void *) h) - s->head_size;
|
|
|
|
ASSERT_DIE(offset % s->obj_size == 0);
|
|
|
|
uint pos = offset / s->obj_size;
|
|
|
|
ASSERT_DIE(pos < s->objs_per_slab);
|
|
|
|
|
|
|
|
h->used_bits[pos / 32] &= ~(1 << (pos % 32));
|
|
|
|
|
|
|
|
if (h->num_full-- == s->objs_per_slab)
|
|
|
|
{
|
|
|
|
rem_node(&h->n);
|
|
|
|
add_head(&s->partial_heads, &h->n);
|
|
|
|
}
|
|
|
|
else if (!h->num_full)
|
2000-03-06 06:48:30 +08:00
|
|
|
{
|
|
|
|
rem_node(&h->n);
|
|
|
|
if (s->num_empty_heads >= MAX_EMPTY_HEADS)
|
2021-05-27 16:35:33 +08:00
|
|
|
{
|
|
|
|
#ifdef POISON
|
2021-09-01 06:46:46 +08:00
|
|
|
memset(h, 0xde, page_size);
|
2021-05-27 16:35:33 +08:00
|
|
|
#endif
|
2021-09-01 06:46:46 +08:00
|
|
|
free_page(s->p, h);
|
2021-05-27 16:35:33 +08:00
|
|
|
}
|
2000-03-06 06:48:30 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
add_head(&s->empty_heads, &h->n);
|
|
|
|
s->num_empty_heads++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
slab_free(resource *r)
|
|
|
|
{
|
|
|
|
slab *s = (slab *) r;
|
|
|
|
struct sl_head *h, *g;
|
|
|
|
|
|
|
|
WALK_LIST_DELSAFE(h, g, s->empty_heads)
|
2021-09-01 06:46:46 +08:00
|
|
|
free_page(s->p, h);
|
2000-03-06 06:48:30 +08:00
|
|
|
WALK_LIST_DELSAFE(h, g, s->partial_heads)
|
2021-09-01 06:46:46 +08:00
|
|
|
free_page(s->p, h);
|
2000-03-06 06:48:30 +08:00
|
|
|
WALK_LIST_DELSAFE(h, g, s->full_heads)
|
2021-09-01 06:46:46 +08:00
|
|
|
free_page(s->p, h);
|
2000-03-06 06:48:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
slab_dump(resource *r)
|
|
|
|
{
|
|
|
|
slab *s = (slab *) r;
|
|
|
|
int ec=0, pc=0, fc=0;
|
|
|
|
struct sl_head *h;
|
|
|
|
|
|
|
|
WALK_LIST(h, s->empty_heads)
|
|
|
|
ec++;
|
|
|
|
WALK_LIST(h, s->partial_heads)
|
|
|
|
pc++;
|
|
|
|
WALK_LIST(h, s->full_heads)
|
|
|
|
fc++;
|
|
|
|
debug("(%de+%dp+%df blocks per %d objs per %d bytes)\n", ec, pc, fc, s->objs_per_slab, s->obj_size);
|
|
|
|
}
|
|
|
|
|
2010-06-03 04:20:40 +08:00
|
|
|
static size_t
|
|
|
|
slab_memsize(resource *r)
|
|
|
|
{
|
|
|
|
slab *s = (slab *) r;
|
2014-06-30 17:55:22 +08:00
|
|
|
size_t heads = 0;
|
2010-06-03 04:20:40 +08:00
|
|
|
struct sl_head *h;
|
|
|
|
|
|
|
|
WALK_LIST(h, s->empty_heads)
|
|
|
|
heads++;
|
|
|
|
WALK_LIST(h, s->partial_heads)
|
|
|
|
heads++;
|
|
|
|
WALK_LIST(h, s->full_heads)
|
|
|
|
heads++;
|
|
|
|
|
2021-09-01 06:46:46 +08:00
|
|
|
// return ALLOC_OVERHEAD + sizeof(struct slab) + heads * (ALLOC_OVERHEAD + page_size);
|
|
|
|
return ALLOC_OVERHEAD + sizeof(struct slab); /* The page sizes are accounted for in the pool */
|
2010-06-03 04:20:40 +08:00
|
|
|
}
|
|
|
|
|
2000-05-09 06:33:38 +08:00
|
|
|
static resource *
|
|
|
|
slab_lookup(resource *r, unsigned long a)
|
|
|
|
{
|
|
|
|
slab *s = (slab *) r;
|
|
|
|
struct sl_head *h;
|
|
|
|
|
|
|
|
WALK_LIST(h, s->partial_heads)
|
2021-09-01 06:46:46 +08:00
|
|
|
if ((unsigned long) h < a && (unsigned long) h + page_size < a)
|
2000-05-09 06:33:38 +08:00
|
|
|
return r;
|
|
|
|
WALK_LIST(h, s->full_heads)
|
2021-09-01 06:46:46 +08:00
|
|
|
if ((unsigned long) h < a && (unsigned long) h + page_size < a)
|
2000-05-09 06:33:38 +08:00
|
|
|
return r;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-03-06 06:48:30 +08:00
|
|
|
#endif
|