sandbox: vm: add cache allocator, generic size-N caches for kmalloc()
This commit is contained in:
63
sandbox/vm/kmalloc.c
Normal file
63
sandbox/vm/kmalloc.c
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <socks/vm.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SIZE_N_CACHE(s) \
|
||||
{ .c_name = "size-" # s, .c_obj_size = s, .c_page_order = VM_PAGE_16K }
|
||||
|
||||
/* reserve space for the size-N caches: */
|
||||
static vm_cache_t size_n_caches[] = {
|
||||
SIZE_N_CACHE(16),
|
||||
SIZE_N_CACHE(32),
|
||||
SIZE_N_CACHE(48),
|
||||
SIZE_N_CACHE(64),
|
||||
SIZE_N_CACHE(96),
|
||||
SIZE_N_CACHE(128),
|
||||
SIZE_N_CACHE(160),
|
||||
SIZE_N_CACHE(256),
|
||||
SIZE_N_CACHE(388),
|
||||
SIZE_N_CACHE(512),
|
||||
SIZE_N_CACHE(576),
|
||||
SIZE_N_CACHE(768),
|
||||
SIZE_N_CACHE(1024),
|
||||
SIZE_N_CACHE(1664),
|
||||
SIZE_N_CACHE(2048),
|
||||
SIZE_N_CACHE(3072),
|
||||
SIZE_N_CACHE(4096),
|
||||
};
|
||||
static const size_t nr_size_n_caches = sizeof size_n_caches / sizeof size_n_caches[0];
|
||||
|
||||
void *kmalloc(size_t count, vm_flags_t flags)
|
||||
{
|
||||
vm_cache_t *best_fit = NULL;
|
||||
for (size_t i = 0; i < nr_size_n_caches; i++) {
|
||||
if (size_n_caches[i].c_obj_size >= count) {
|
||||
best_fit = &size_n_caches[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!best_fit) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!VM_CACHE_INITIALISED(best_fit)) {
|
||||
vm_cache_init(best_fit);
|
||||
}
|
||||
|
||||
return vm_cache_alloc(best_fit, flags);
|
||||
}
|
||||
|
||||
void *kzalloc(size_t count, vm_flags_t flags)
|
||||
{
|
||||
void *p = kmalloc(count, flags);
|
||||
if (p) {
|
||||
memset(p, 0x0, count);
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void kfree(void *p)
|
||||
{
|
||||
/* TODO */
|
||||
}
|
||||
Reference in New Issue
Block a user