Implement (half) simple semaphore & mutex lock, and ...
[lunaix-os.git] / lunaix-os / kernel / mm / kalloc.c
index 675a51f4033fe0191a5fba16c46e0f558e22f40e..16c1d8286ba9164c7a5a0753b4a5853a2de01557 100644 (file)
 #include <lunaix/mm/kalloc.h>
 #include <lunaix/mm/dmm.h>
 
-#include <lunaix/constants.h>
+#include <lunaix/common.h>
 #include <lunaix/spike.h>
 
-#include <libc/string.h>
+#include <klibc/string.h>
 
 #include <stdint.h>
 
 extern uint8_t __kernel_heap_start;
 
-heap_context_t __kalloc_kheap;
+// FIXME: This should go to PCB once we're started to support multitasking
+static heap_context_t __kalloc_kheap;
 
 void*
 lx_malloc_internal(heap_context_t* heap, size_t size);
@@ -82,13 +83,20 @@ lxmalloc(size_t size) {
 }
 
 void*
-lxcalloc(size_t size) {
-    void* ptr = lxmalloc(size);
+lxcalloc(size_t n, size_t elem) {
+    size_t pd = n * elem;
+
+    // overflow detection
+    if (pd < elem || pd < n) {
+        return NULL;
+    }
+
+    void* ptr = lxmalloc(pd);
     if (!ptr) {
         return NULL;
     }
 
-    return memset(ptr, 0, size);
+    return memset(ptr, 0, pd);
 }
 
 void