sandbox: moved all sources to main kernel tree

This commit is contained in:
2023-02-03 20:43:38 +00:00
parent e714d619ba
commit 40f83922da
18 changed files with 0 additions and 9 deletions

73
vm/kmalloc.c Normal file
View File

@@ -0,0 +1,73 @@
#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)
{
if (!count) {
return NULL;
}
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)
{
phys_addr_t phys = vm_virt_to_phys(p);
vm_page_t *pg = vm_page_get(phys);
if (!pg || !pg->p_slab) {
return;
}
vm_cache_free(pg->p_slab->s_cache, p);
}