mem: improve slabbing to include flags
This commit is contained in:
parent
1fe7b002cf
commit
e1108b3493
6 changed files with 30 additions and 14 deletions
|
|
@ -5,10 +5,14 @@
|
|||
#include <kernel/common.h>
|
||||
#include <kernel/mem.h>
|
||||
|
||||
#define SLAB_PAGE_ALIGN 0x01
|
||||
|
||||
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 */
|
||||
uint64_t flags;
|
||||
size_t align;
|
||||
uintptr_t *nextfree;
|
||||
struct slab *next;
|
||||
struct slab *prev;
|
||||
|
|
@ -21,7 +25,7 @@ struct kmap_entry {
|
|||
struct kmap_entry *next;
|
||||
};
|
||||
|
||||
struct slab *slab_create(size_t size);
|
||||
struct slab *slab_create(size_t size, uint64_t flags);
|
||||
void *slab_alloc(struct slab *slab);
|
||||
void slab_free(struct slab *slab, void *ptr);
|
||||
|
||||
|
|
|
|||
11
init/main.c
11
init/main.c
|
|
@ -62,12 +62,15 @@ void kmain(void)
|
|||
char kmem[one_gb] ALIGN(0x1000);
|
||||
mem_early_init(kmem, one_gb);
|
||||
kmalloc_init();
|
||||
|
||||
_pic_init();
|
||||
|
||||
printf("\n# ");
|
||||
|
||||
void *kstack = kmalloc(KSTACK_SIZE);
|
||||
void *kstack = buddy_alloc(KSTACK_SIZE);
|
||||
uintptr_t ptr = (uintptr_t)kstack + KSTACK_SIZE - 8;
|
||||
stack_init(ptr, ptr);
|
||||
}
|
||||
|
||||
void kmain_post_stack_init()
|
||||
{
|
||||
printf("\n\n# ");
|
||||
yield();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ void *memset(void *str, int c, size_t n)
|
|||
ptr += 8;
|
||||
}
|
||||
|
||||
if(n & 7) {
|
||||
if (n & 7) {
|
||||
ptr = (uintptr_t)str + (n & (~7ull));
|
||||
for (size_t i = 0; i < (n & 7); i++) {
|
||||
*(char *)ptr = c;
|
||||
|
|
|
|||
|
|
@ -392,7 +392,7 @@ void mem_early_init(char *mem, size_t len)
|
|||
/* copy the regions to the permanent regions table */
|
||||
memcpy(mem_regions, regions, sizeof(struct mem_region) * num_regions);
|
||||
|
||||
kmap_slab = slab_create(0x1000);
|
||||
kmap_slab = slab_create(0x1000, SLAB_PAGE_ALIGN);
|
||||
|
||||
/* initialize and populate the new permanent kernel page tables */
|
||||
alloc_page = kmap_alloc_page;
|
||||
|
|
|
|||
|
|
@ -33,4 +33,6 @@ rbp_write:
|
|||
stack_init:
|
||||
movq %rdi, %rsp
|
||||
movq %rsi, %rbp
|
||||
call yield
|
||||
__stack_init_no_escape:
|
||||
call kmain_post_stack_init
|
||||
jmp __stack_init_no_escape
|
||||
|
|
|
|||
19
mem/slab.c
19
mem/slab.c
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#define SLAB_MIN_COUNT 256
|
||||
#define SLAB_MIN_SIZE 0x10000ull /* 64 KiB */
|
||||
#define SLAB_ALIGN 7
|
||||
|
||||
size_t slab_sizes[] = { 16, 32, 64, 128, 256, 512, 1024, 2048 };
|
||||
struct slab *slab_cache[ARRAY_SIZE(slab_sizes)];
|
||||
|
|
@ -15,13 +16,13 @@ struct kmap_entry *kmem_map = NULL;
|
|||
static inline void slab_range(struct slab *slab, uintptr_t *o_start, uintptr_t *o_end)
|
||||
{
|
||||
uintptr_t sslab = (uintptr_t)slab;
|
||||
sslab = (sslab | npow2(slab->size)) + 1;
|
||||
sslab = (sslab | slab->align) + 1;
|
||||
|
||||
*o_start = sslab;
|
||||
*o_end = sslab + (slab->size * slab->num);
|
||||
}
|
||||
|
||||
struct slab *slab_create(size_t size)
|
||||
struct slab *slab_create(size_t size, uint64_t flags)
|
||||
{
|
||||
if (size < sizeof(uintptr_t))
|
||||
return NULL;
|
||||
|
|
@ -39,13 +40,19 @@ struct slab *slab_create(size_t size)
|
|||
uintptr_t aret = (uintptr_t)ret;
|
||||
uintptr_t start = (uintptr_t)ret + sizeof(struct slab);
|
||||
|
||||
start |= (npow2(size) - 1);
|
||||
if (flags & SLAB_PAGE_ALIGN)
|
||||
ret->align = 0xFFF;
|
||||
else
|
||||
ret->align = SLAB_ALIGN;
|
||||
|
||||
start |= ret->align;
|
||||
start += 1;
|
||||
|
||||
ret->num = (((aret + asize) - start) / size);
|
||||
ret->size = size;
|
||||
ret->next = NULL;
|
||||
ret->prev = NULL;
|
||||
ret->flags = flags;
|
||||
ret->nextfree = (uintptr_t *)start;
|
||||
ret->free = ret->num;
|
||||
|
||||
|
|
@ -67,7 +74,7 @@ void *slab_alloc(struct slab *slab)
|
|||
|
||||
if (slab->nextfree == NULL) {
|
||||
if (slab->next == NULL) {
|
||||
slab->next = slab_create(slab->size);
|
||||
slab->next = slab_create(slab->size, slab->flags);
|
||||
if (slab->next == NULL)
|
||||
return NULL;
|
||||
slab->next->prev = slab;
|
||||
|
|
@ -120,8 +127,8 @@ void slab_free(struct slab *slab, void *ptr)
|
|||
void kmalloc_init()
|
||||
{
|
||||
for (size_t i = 0; i < ARRAY_SIZE(slab_sizes); i++)
|
||||
slab_cache[i] = slab_create(slab_sizes[i]);
|
||||
kmalloc_slab = slab_create(sizeof(struct kmap_entry));
|
||||
slab_cache[i] = slab_create(slab_sizes[i], 0);
|
||||
kmalloc_slab = slab_create(sizeof(struct kmap_entry), 0);
|
||||
}
|
||||
|
||||
void *kmalloc(size_t size)
|
||||
|
|
|
|||
Loading…
Reference in a new issue