mem: add kmalloc and kfree
This commit is contained in:
parent
74a58d6641
commit
577779f8ae
4 changed files with 104 additions and 1 deletions
|
|
@ -14,6 +14,11 @@
|
||||||
#include <kernel/bitwise.h>
|
#include <kernel/bitwise.h>
|
||||||
|
|
||||||
#define PACKED __attribute__((packed))
|
#define PACKED __attribute__((packed))
|
||||||
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
||||||
|
|
||||||
|
#define KB 1024
|
||||||
|
#define MB (1024 * KB)
|
||||||
|
#define GB (1024 * MB)
|
||||||
|
|
||||||
typedef uintptr_t paddr_t;
|
typedef uintptr_t paddr_t;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,19 @@ struct slab {
|
||||||
struct slab *next;
|
struct slab *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct kmap_entry {
|
||||||
|
uintptr_t *ptr;
|
||||||
|
size_t size;
|
||||||
|
struct slab *slab;
|
||||||
|
struct kmap_entry *next;
|
||||||
|
};
|
||||||
|
|
||||||
struct slab *slab_create(size_t size);
|
struct slab *slab_create(size_t size);
|
||||||
void *slab_alloc(struct slab *slab);
|
void *slab_alloc(struct slab *slab);
|
||||||
void slab_free(struct slab *slab, void *ptr);
|
void slab_free(struct slab *slab, void *ptr);
|
||||||
|
|
||||||
|
void kmalloc_init();
|
||||||
|
void *kmalloc(size_t size);
|
||||||
|
void kfree(void *ptr);
|
||||||
|
|
||||||
#endif /* _SLAB_H_ */
|
#endif /* _SLAB_H_ */
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include <kernel/gdt.h>
|
#include <kernel/gdt.h>
|
||||||
#include <kernel/idt.h>
|
#include <kernel/idt.h>
|
||||||
#include <kernel/mem.h>
|
#include <kernel/mem.h>
|
||||||
|
#include <kernel/slab.h>
|
||||||
#include <kernel/pio.h>
|
#include <kernel/pio.h>
|
||||||
|
|
||||||
#include <dev/pic.h>
|
#include <dev/pic.h>
|
||||||
|
|
@ -43,6 +44,7 @@ void kmain(void)
|
||||||
const uintptr_t one_gb = 0x40000000;
|
const uintptr_t one_gb = 0x40000000;
|
||||||
char kmem[one_gb] ALIGN(0x1000);
|
char kmem[one_gb] ALIGN(0x1000);
|
||||||
mem_early_init(kmem, one_gb);
|
mem_early_init(kmem, one_gb);
|
||||||
|
kmalloc_init();
|
||||||
|
|
||||||
cli();
|
cli();
|
||||||
pic_mask(4, 0);
|
pic_mask(4, 0);
|
||||||
|
|
|
||||||
87
mem/slab.c
87
mem/slab.c
|
|
@ -5,6 +5,12 @@
|
||||||
|
|
||||||
#define SLAB_MIN_COUNT 16
|
#define SLAB_MIN_COUNT 16
|
||||||
|
|
||||||
|
size_t slab_sizes[] = { 16, 32, 64, 128, 256, 512, 1024, 2048 };
|
||||||
|
struct slab *slab_cache[ARRAY_SIZE(slab_sizes)];
|
||||||
|
|
||||||
|
struct slab *kmap_slab = NULL;
|
||||||
|
struct kmap_entry *kmem_map = NULL;
|
||||||
|
|
||||||
void slab_range(struct slab *slab, uintptr_t *o_start, uintptr_t *o_end)
|
void slab_range(struct slab *slab, uintptr_t *o_start, uintptr_t *o_end)
|
||||||
{
|
{
|
||||||
uintptr_t sslab = (uintptr_t)slab;
|
uintptr_t sslab = (uintptr_t)slab;
|
||||||
|
|
@ -49,6 +55,9 @@ struct slab *slab_create(size_t size)
|
||||||
|
|
||||||
void *slab_alloc(struct slab *slab)
|
void *slab_alloc(struct slab *slab)
|
||||||
{
|
{
|
||||||
|
if (slab == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (slab->nextfree == NULL) {
|
if (slab->nextfree == NULL) {
|
||||||
if (slab->next == NULL) {
|
if (slab->next == NULL) {
|
||||||
slab->next = slab_create(slab->size);
|
slab->next = slab_create(slab->size);
|
||||||
|
|
@ -76,9 +85,85 @@ void slab_free(struct slab *slab, void *ptr)
|
||||||
uintptr_t high;
|
uintptr_t high;
|
||||||
slab_range(slab, &low, &high);
|
slab_range(slab, &low, &high);
|
||||||
|
|
||||||
if ((uintptr_t)ptr < low || (uintptr_t)ptr > high)
|
if ((uintptr_t)ptr < low || (uintptr_t)ptr > high) {
|
||||||
slab_free(slab->next, ptr);
|
slab_free(slab->next, ptr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
*(uintptr_t *)ptr = (uintptr_t)slab->nextfree;
|
*(uintptr_t *)ptr = (uintptr_t)slab->nextfree;
|
||||||
slab->nextfree = ptr;
|
slab->nextfree = ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void kmalloc_init()
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < ARRAY_SIZE(slab_sizes); i++)
|
||||||
|
slab_cache[i] = slab_create(slab_sizes[i]);
|
||||||
|
kmap_slab = slab_create(sizeof(struct kmap_entry));
|
||||||
|
}
|
||||||
|
|
||||||
|
void *kmalloc(size_t size)
|
||||||
|
{
|
||||||
|
void *ret = NULL;
|
||||||
|
struct slab *slab = NULL;
|
||||||
|
|
||||||
|
if (size > slab_sizes[ARRAY_SIZE(slab_sizes) - 1]) {
|
||||||
|
ret = buddy_alloc(npow2(size));
|
||||||
|
} else {
|
||||||
|
for (size_t i = 0; i < ARRAY_SIZE(slab_sizes); i++) {
|
||||||
|
if (size <= slab_sizes[i]) {
|
||||||
|
size = slab_sizes[i];
|
||||||
|
slab = slab_cache[i];
|
||||||
|
ret = slab_alloc(slab);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
struct kmap_entry *entry = slab_alloc(kmap_slab);
|
||||||
|
if (entry == NULL) {
|
||||||
|
slab_free(slab, ret);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
entry->ptr = ret;
|
||||||
|
entry->slab = slab;
|
||||||
|
entry->size = size;
|
||||||
|
entry->next = kmem_map;
|
||||||
|
|
||||||
|
kmem_map = entry;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void kfree(void *ptr)
|
||||||
|
{
|
||||||
|
if (ptr == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
struct kmap_entry *entry = kmem_map;
|
||||||
|
struct kmap_entry *prev = NULL;
|
||||||
|
|
||||||
|
while (entry != NULL) {
|
||||||
|
if (entry->ptr == ptr) {
|
||||||
|
if (prev == NULL)
|
||||||
|
kmem_map = entry->next;
|
||||||
|
else
|
||||||
|
prev->next = entry->next;
|
||||||
|
|
||||||
|
if (entry->slab == NULL)
|
||||||
|
buddy_free(entry->ptr);
|
||||||
|
else
|
||||||
|
slab_free(entry->slab, entry->ptr);
|
||||||
|
|
||||||
|
slab_free(kmap_slab, entry);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
prev = entry;
|
||||||
|
entry = entry->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue