mem: add basic slab allocator

This commit is contained in:
Michael Shaw 2023-04-20 16:06:49 -04:00
commit 74a58d6641
5 changed files with 117 additions and 0 deletions

View file

@ -93,6 +93,7 @@ uint64_t rsp_read();
void rsp_write(uint64_t rsp); void rsp_write(uint64_t rsp);
void *buddy_alloc(size_t size); void *buddy_alloc(size_t size);
void buddy_free(void *paddr_hhdm); void buddy_free(void *paddr_hhdm);
void kmap(paddr_t paddr, uint64_t vaddr, size_t len, uint64_t attr);
void mem_early_init(char *mem, size_t len); void mem_early_init(char *mem, size_t len);

20
include/kernel/slab.h Normal file
View file

@ -0,0 +1,20 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _SLAB_H_
#define _SLAB_H_
#include <kernel/common.h>
#include <kernel/mem.h>
struct slab {
size_t size; /* size of each object */
size_t num; /* number of objects in this slab */
size_t free; /* number of free objects */
uintptr_t *nextfree;
struct slab *next;
};
struct slab *slab_create(size_t size);
void *slab_alloc(struct slab *slab);
void slab_free(struct slab *slab, void *ptr);
#endif /* _SLAB_H_ */

View file

@ -11,4 +11,16 @@ void bitmap_set(char *bitmap, size_t n);
void bitmap_clear(char *bitmap, size_t n); void bitmap_clear(char *bitmap, size_t n);
uint8_t bitmap_get(char *bitmap, size_t n); uint8_t bitmap_get(char *bitmap, size_t n);
#define MIN(a, b) ({ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
_a < _b ? _a : _b; \
})
#define MAX(a, b) ({ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
_a > _b ? _a : _b; \
})
#endif /* _MATH_H_ */ #endif /* _MATH_H_ */

84
mem/slab.c Normal file
View file

@ -0,0 +1,84 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/mem.h>
#include <kernel/slab.h>
#define SLAB_MIN_COUNT 16
void slab_range(struct slab *slab, uintptr_t *o_start, uintptr_t *o_end)
{
uintptr_t sslab = (uintptr_t)slab;
sslab = (sslab | 7) + 1;
*o_start = sslab;
*o_end = sslab + (slab->size * slab->num);
}
struct slab *slab_create(size_t size)
{
if (size < sizeof(uintptr_t))
return NULL;
/* account for slab header and eight bytes of alignment */
size_t est = npow2(size * SLAB_MIN_COUNT + sizeof(struct slab) + 8);
size_t asize = MAX(est, 0x1000ull);
struct slab *ret = buddy_alloc(asize);
if (ret == NULL)
return NULL;
/* slab area start position */
size_t start = (sizeof(struct slab) | 7) + 1;
ret->nextfree = (uintptr_t *)((uintptr_t)ret + start);
ret->size = size;
uintptr_t *ptr = ret->nextfree;
while (ptr != NULL) {
if (ptr > (uintptr_t *)((uintptr_t)ret + asize - size)) {
*ptr = (uintptr_t)NULL;
ptr = NULL;
} else {
*ptr = (uintptr_t)ptr + size;
ptr = (uintptr_t *)*ptr;
}
}
return ret;
}
void *slab_alloc(struct slab *slab)
{
if (slab->nextfree == NULL) {
if (slab->next == NULL) {
slab->next = slab_create(slab->size);
if (slab->next == NULL)
return NULL;
}
return slab_alloc(slab->next);
}
slab->free--;
uintptr_t *ret = slab->nextfree;
slab->nextfree = (uintptr_t *)*slab->nextfree;
return ret;
}
void slab_free(struct slab *slab, void *ptr)
{
if (slab == NULL)
return;
if (ptr == NULL)
return;
uintptr_t low;
uintptr_t high;
slab_range(slab, &low, &high);
if ((uintptr_t)ptr < low || (uintptr_t)ptr > high)
slab_free(slab->next, ptr);
*(uintptr_t *)ptr = (uintptr_t)slab->nextfree;
slab->nextfree = ptr;
}