forked from PoliEcho-holding-org/kern
basic heap scafolding
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
#ifndef _KMALLOC_H
|
||||
#define _KMALLOC_H
|
||||
|
||||
#include "types.h"
|
||||
|
||||
// This function allocates size bytes of contiguous memory.
|
||||
// Returns a pointer to allocated memory or NULL.
|
||||
void* kmalloc(size_t size);
|
||||
|
||||
// This function allocates size bytes of contiguous, zeroed-out memory.
|
||||
// Returns a pointer to allocated memory or NULL.
|
||||
void* kzalloc(size_t size);
|
||||
|
||||
// This function allocates memory for count elements each of size bytes, zeroed-out memory.
|
||||
// Returns a pointer to allocated memory or NULL.
|
||||
void* kcalloc(size_t count, size_t size);
|
||||
#endif
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "types.h"
|
||||
#include <kmalloc.h>
|
||||
#include <string.h>
|
||||
// this file contains SLUB inspired HEAP allocator
|
||||
void* freelist = NULL;
|
||||
|
||||
void* kcalloc(size_t count, size_t size) {
|
||||
size_t size_to_alloc;
|
||||
__asm__ volatile goto(".intel_syntax noprefix\n\t"
|
||||
"mov rax, %1\n\t"
|
||||
"mov rdx, %2\n\t"
|
||||
"mul rdx\n\t"
|
||||
"jo %l[overflow]\n\t"
|
||||
"mov %0, rax\n\t"
|
||||
".att_syntax prefix\n\t"
|
||||
: "=r"(size_to_alloc)
|
||||
: "r"(count), "r"(size)
|
||||
: "rax", "rdx", "cc"
|
||||
: overflow);
|
||||
return kzalloc(size_to_alloc);
|
||||
|
||||
overflow:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* kzalloc(size_t size) {
|
||||
void* allocated = kmalloc(size);
|
||||
if (!allocated) {
|
||||
return NULL;
|
||||
}
|
||||
memset(allocated, 0, size);
|
||||
return allocated;
|
||||
}
|
||||
|
||||
void* kmalloc(size_t size) {}
|
||||
Reference in New Issue
Block a user