Initialize the heap and file descriptors during CRT startup
authorcoderain <coderain@sdf.org>
Sat, 28 Oct 2017 21:45:59 +0000 (23:45 +0200)
committercoderain <coderain@sdf.org>
Sat, 28 Oct 2017 21:45:59 +0000 (23:45 +0200)
crt/src/crt0.c
crt/src/malloc.c

index 15f3b1fc31c033800a8b95d57b190297b9891f85..e8a5e7a89debdda3a2f72bc4027a03e52ff9e530 100644 (file)
@@ -21,6 +21,8 @@
 #include <string.h>
 #include <monolithium.h>
 
+int __crt_initialize_heap(void);
+int __crt_initialize_files(process_params_t *params);
 int main(int argc, char **argv);
 
 void process_startup(process_params_t *params)
@@ -29,6 +31,9 @@ void process_startup(process_params_t *params)
     char *ptr;
     int space = 1;
 
+    if (__crt_initialize_heap() < 0) syscall_terminate(INVALID_HANDLE, 255);
+    if (__crt_initialize_files(params) < 0) syscall_terminate(INVALID_HANDLE, 255);
+
     for (ptr = params->command_line; *ptr; ptr++)
     {
         if (space != (isspace(*ptr) != 0))
index 40d5cb4f20cbf4b31e8c6a766822eedaaadbfe2a..30fa14296d43094744561913492032d346f11d8d 100644 (file)
@@ -343,3 +343,40 @@ void *calloc(size_t nmemb, size_t size)
     memset(ptr, 0, nmemb * size);
     return ptr;
 }
+
+static void __crt_heap_lock(void *mutex)
+{
+    syscall_wait_mutex((handle_t)mutex, NO_TIMEOUT);
+}
+
+static void __crt_heap_unlock(void *mutex)
+{
+    syscall_release_mutex((handle_t)mutex);
+}
+
+static void __crt_heap_problem(int problem)
+{
+    // TODO
+}
+
+int __crt_initialize_heap(void)
+{
+    static __crt_heap_t heap;
+
+    heap.magic             = 0x70616548;
+    heap.base              = NULL;
+    heap.size              = 0x10000000;
+    heap.flags             = 0;
+    heap.next_offset       = 0;
+    heap.lock_mutex_proc   = __crt_heap_lock;
+    heap.unlock_mutex_proc = __crt_heap_unlock;
+    heap.problem           = __crt_heap_problem;
+
+    sysret_t status = syscall_create_mutex(NULL, TRUE, (handle_t*)&heap.mutex);
+    if (status != ERR_SUCCESS) return -1;
+
+    status = syscall_alloc_memory(INVALID_HANDLE, &heap.base, heap.size, MEMORY_BLOCK_ACCESSIBLE | MEMORY_BLOCK_WRITABLE);
+    if (status != ERR_SUCCESS) return -1;
+
+    return 0;
+}