Redesign syscalls
All checks were successful
Build documentation / build-and-deploy (push) Successful in 40s

This commit is contained in:
2026-01-20 20:46:34 +01:00
parent a29233f853
commit fff51321bc
15 changed files with 198 additions and 357 deletions

View File

@@ -2,37 +2,29 @@
#pragma clang optimize off
#include <alloc/liballoc.h>
#include <m/proc.h>
#include <m/resource_buffer.h>
#include <m/system.h>
static int liballoc_mutex;
static uintptr_t liballoc_map_base = PROC_MAP_BASE;
static int mem_rid_base = 1000000;
void liballoc_init (void) {
liballoc_mutex = proc_create_resource (100, PR_MUTEX, RV_PRIVATE, NULL);
}
void liballoc_init (void) {}
int liballoc_lock (void) {
proc_mutex_lock (liballoc_mutex, RV_PRIVATE);
return 0;
}
int liballoc_lock (void) { return 0; }
int liballoc_unlock (void) {
proc_mutex_unlock (liballoc_mutex, RV_PRIVATE);
return 0;
}
int liballoc_unlock (void) { return 0; }
void* liballoc_alloc (int pages) {
void* liballoc_alloc (int pages, int* mem_rid) {
uintptr_t current_base = liballoc_map_base;
struct resource_buffer mem_init = {.type = PR_MEM, .u = {.mem = {.pages = (size_t)pages}}};
int mem_rid = proc_create_resource (mem_rid_base++, PR_MEM, RV_PRIVATE, &mem_init);
if (mem_rid < 0)
*mem_rid = create_mem (mem_rid_base++, RV_PRIVATE, pages);
if (*mem_rid < 0) {
return NULL;
}
proc_map (mem_init.u.mem.paddr, current_base, (size_t)pages, PM_PRESENT | PM_RW | PM_USER);
if (map (*mem_rid, RV_PRIVATE, current_base, MAP_FLAGS | MAP_RW) < 0) {
unlink_mem (*mem_rid, RV_PRIVATE, pages);
return NULL;
}
uintptr_t old_base = current_base;
current_base += pages * PAGE_SIZE;
@@ -40,7 +32,11 @@ void* liballoc_alloc (int pages) {
return (void*)old_base;
}
int liballoc_free (void* ptr, int pages) { return 0; }
int liballoc_free (void* ptr, int pages, int mem_rid) {
unmap ((uintptr_t)ptr, pages);
unlink_mem (mem_rid, RV_PRIVATE, pages);
return 0;
}
/** Durand's Ridiculously Amazing Super Duper Memory functions. */
@@ -207,6 +203,7 @@ static struct boundary_tag* allocate_new_tag (unsigned int size) {
unsigned int pages;
unsigned int usage;
struct boundary_tag* tag;
int mem_rid;
// This is how much space is required.
usage = size + sizeof (struct boundary_tag);
@@ -220,7 +217,7 @@ static struct boundary_tag* allocate_new_tag (unsigned int size) {
if (pages < (unsigned int)l_pageCount)
pages = l_pageCount;
tag = (struct boundary_tag*)liballoc_alloc (pages);
tag = (struct boundary_tag*)liballoc_alloc (pages, &mem_rid);
if (tag == NULL)
return NULL; // uh oh, we ran out of memory.
@@ -229,6 +226,7 @@ static struct boundary_tag* allocate_new_tag (unsigned int size) {
tag->size = size;
tag->real_size = pages * l_pageSize;
tag->index = -1;
tag->mem_rid = mem_rid;
tag->next = NULL;
tag->prev = NULL;
@@ -351,7 +349,7 @@ void free (void* ptr) {
if (pages < (unsigned int)l_pageCount)
pages = l_pageCount;
liballoc_free (tag, pages);
liballoc_free (tag, pages, tag->mem_rid);
liballoc_unlock ();
return;

View File

@@ -41,6 +41,8 @@ struct boundary_tag {
struct boundary_tag* next; //< Linked list info.
struct boundary_tag* prev; //< Linked list info.
int mem_rid;
};
/** This function is supposed to lock the memory data structures. It
@@ -67,7 +69,7 @@ extern int liballoc_unlock (void);
* \return NULL if the pages were not allocated.
* \return A pointer to the allocated memory.
*/
extern void* liballoc_alloc (int);
extern void* liballoc_alloc (int pages, int* mem_rid);
/** This frees previously allocated memory. The void* parameter passed
* to the function is the exact same value returned from a previous
@@ -77,7 +79,7 @@ extern void* liballoc_alloc (int);
*
* \return 0 if the memory was successfully freed.
*/
extern int liballoc_free (void*, int);
extern int liballoc_free (void* ptr, int pages, int mem_rid);
void* malloc (size_t); //< The standard function.
void* realloc (void*, size_t); //< The standard function.