Initial commit - slab allocator, kmalloc, other re

factors
This commit is contained in:
ssimnb 2026-01-22 08:05:47 +01:00
parent 1dd7b8b07f
commit 4e40a040dd
39 changed files with 863 additions and 412 deletions

57
src/mm/slab.h Normal file
View file

@ -0,0 +1,57 @@
#include <stdatomic.h>
#include <stddef.h>
#include <stdint.h>
#include <error.h>
#include <stdbool.h>
#pragma once
#define KCACHE_NAME_LEN 16
struct ma_bufctl {
struct ma_bufctl *next;
size_t *startaddr;
};
struct ma_slab {
struct ma_kcache *cache;
struct ma_slab *next;
struct ma_slab *prev;
uint32_t refcount; // The amount of active (not free) objects in the slabs
struct ma_bufctl *free; // Linked list of free buffers in the slab. Is equal to NULL once there are no more free objects
};
/* objrefs are used to be able to quickly find out which slab and cache a object belongs to. objrefs belonging to the same slab are kept in one page, there is no mixing. */
struct ma_objref {
struct ma_objref *next;
struct ma_objref *prev;
void *addr; // Addr of the object
struct ma_slab *slab; // The slab which the obj belongs to
struct ma_kcache *kcache; // The cache which the obj belongs to
};
struct ma_kcache {
struct ma_kcache *next;
struct ma_kcache *prev;
uint32_t objsize; // Size of the object which the cache stores
uint16_t flags; // Not useful yet
uint32_t num; // Number of objects per slab
uint32_t slabsize; // How many pages does a single slab take up. Useful for objects > PAGE_SIZE
struct ma_slab *slabs_free;
struct ma_slab *slabs_partial;
struct ma_slab *slabs_used;
atomic_flag lock;
char name[KCACHE_NAME_LEN];
};
void *ma_cache_alloc(struct ma_kcache *kcache, uint32_t flags);
kstatus ma_cache_dealloc(void *object);
struct ma_kcache *ma_cache_create(char *name, size_t size, uint32_t flags, void (*constructor)(void *, size_t), void (*destructor)(void *, size_t));
void cache_info(struct ma_kcache *cache);