Support to multi-threading and pthread interface (POSIX.1-2008) (#23)
[lunaix-os.git] / lunaix-os / kernel / mm / valloc.c
index 4ff4794fd3e0da1a9543f07efbbec432ddbb66c6..113b7c659c15c0a72f8e6889be9c54dab00ae53f 100644 (file)
@@ -1,59 +1,69 @@
 #include <klibc/string.h>
 #include <lunaix/mm/cake.h>
 #include <lunaix/mm/valloc.h>
 #include <klibc/string.h>
 #include <lunaix/mm/cake.h>
 #include <lunaix/mm/valloc.h>
-
-#define MAX_CLASS 6
-
-static char piles_names[MAX_CLASS][PILE_NAME_MAXLEN] = {
-    "valloc_16",  "valloc_32",  "valloc_64",
-    "valloc_128", "valloc_256", "valloc_512"
-};
-
-static char piles_names_dma[MAX_CLASS][PILE_NAME_MAXLEN] = {
-    "valloc_dma_128", "valloc_dma_512", "valloc_dma_512",
-    "valloc_dma_1k",  "valloc_dma_2k",  "valloc_dma_4k"
-};
-
-static struct cake_pile* piles[MAX_CLASS];
-static struct cake_pile* piles_dma[MAX_CLASS];
+#include <lunaix/spike.h>
+
+#define CLASS_LEN(class) (sizeof(class) / sizeof(class[0]))
+
+static char piles_names[][PILE_NAME_MAXLEN] = {"valloc_8",
+                                               "valloc_16",
+                                               "valloc_32",
+                                               "valloc_64",
+                                               "valloc_128",
+                                               "valloc_256",
+                                               "valloc_512",
+                                               "valloc_1k",
+                                               "valloc_2k",
+                                               "valloc_4k",
+                                               "valloc_8k"};
+
+static char piles_names_dma[][PILE_NAME_MAXLEN] = {"valloc_dma_128",
+                                                   "valloc_dma_256",
+                                                   "valloc_dma_512",
+                                                   "valloc_dma_1k",
+                                                   "valloc_dma_2k",
+                                                   "valloc_dma_4k"};
+
+static struct cake_pile* piles[CLASS_LEN(piles_names)];
+static struct cake_pile* piles_dma[CLASS_LEN(piles_names_dma)];
 
 void
 valloc_init()
 {
 
 void
 valloc_init()
 {
-    for (size_t i = 0; i < MAX_CLASS; i++) {
-        int size = 1 << (i + 4);
-        piles[i] = cake_new_pile(&piles_names[i], size, 1, 0);
+    for (size_t i = 0; i < CLASS_LEN(piles_names); i++) {
+        int size = 1 << (i + 3);
+        piles[i] = cake_new_pile(piles_names[i], size, size > 1024 ? 8 : 1, 0);
     }
 
     // DMA 内存保证128字节对齐
     }
 
     // DMA 内存保证128字节对齐
-    for (size_t i = 0; i < MAX_CLASS; i++) {
+    for (size_t i = 0; i < CLASS_LEN(piles_names_dma); i++) {
         int size = 1 << (i + 7);
         piles_dma[i] = cake_new_pile(
         int size = 1 << (i + 7);
         piles_dma[i] = cake_new_pile(
-          &piles_names_dma[i], size, size > 1024 ? 8 : 1, PILE_CACHELINE);
+            piles_names_dma[i], size, size > 1024 ? 4 : 1, PILE_ALIGN_CACHE);
     }
 }
 
 void*
     }
 }
 
 void*
-__valloc(unsigned int size, struct cake_pile** segregate_list)
+__valloc(unsigned int size,
+         struct cake_pile** segregate_list,
+         size_t len,
+         size_t boffset)
 {
 {
-    size_t i = 0;
-    for (; i < MAX_CLASS; i++) {
-        if (segregate_list[i]->piece_size >= size) {
-            goto found_class;
-        }
-    }
+    size_t i = ILOG2(size);
+    i += (size - (1 << i) != 0);
+    i -= boffset;
 
 
-    return NULL;
+    if (i >= len)
+        i = 0;
 
 
-found_class:
     return cake_grab(segregate_list[i]);
 }
 
 void
     return cake_grab(segregate_list[i]);
 }
 
 void
-__vfree(void* ptr, struct cake_pile** segregate_list)
+__vfree(void* ptr, struct cake_pile** segregate_list, size_t len)
 {
     size_t i = 0;
 {
     size_t i = 0;
-    for (; i < MAX_CLASS; i++) {
+    for (; i < len; i++) {
         if (cake_release(segregate_list[i], ptr)) {
             return;
         }
         if (cake_release(segregate_list[i], ptr)) {
             return;
         }
@@ -63,33 +73,56 @@ __vfree(void* ptr, struct cake_pile** segregate_list)
 void*
 valloc(unsigned int size)
 {
 void*
 valloc(unsigned int size)
 {
-    return __valloc(size, &piles);
+    return __valloc(size, piles, CLASS_LEN(piles_names), 3);
 }
 
 void*
 }
 
 void*
-vcalloc(unsigned int size)
+vzalloc(unsigned int size)
 {
 {
-    void* ptr = __valloc(size, &piles);
+    void* ptr = __valloc(size, piles, CLASS_LEN(piles_names), 3);
     memset(ptr, 0, size);
     return ptr;
 }
 
     memset(ptr, 0, size);
     return ptr;
 }
 
+void*
+vcalloc(unsigned int size, unsigned int count)
+{
+    unsigned int alloc_size;
+    if (umul_overflow(size, count, &alloc_size)) {
+        return 0;
+    }
+
+    void* ptr = __valloc(alloc_size, piles, CLASS_LEN(piles_names), 3);
+    memset(ptr, 0, alloc_size);
+    return ptr;
+}
+
 void
 vfree(void* ptr)
 {
 void
 vfree(void* ptr)
 {
-    __vfree(ptr, &piles);
+    __vfree(ptr, piles, CLASS_LEN(piles_names));
+}
+
+void
+vfree_safe(void* ptr)
+{
+    if (!ptr) {
+        return;
+    }
+
+    __vfree(ptr, piles, CLASS_LEN(piles_names));
 }
 
 void*
 valloc_dma(unsigned int size)
 {
 }
 
 void*
 valloc_dma(unsigned int size)
 {
-    return __valloc(size, &piles_dma);
+    return __valloc(size, piles_dma, CLASS_LEN(piles_names_dma), 7);
 }
 
 void*
 }
 
 void*
-vcalloc_dma(unsigned int size)
+vzalloc_dma(unsigned int size)
 {
 {
-    void* ptr = __valloc(size, &piles_dma);
+    void* ptr = __valloc(size, piles_dma, CLASS_LEN(piles_names_dma), 7);
     memset(ptr, 0, size);
     return ptr;
 }
     memset(ptr, 0, size);
     return ptr;
 }
@@ -97,5 +130,10 @@ vcalloc_dma(unsigned int size)
 void
 vfree_dma(void* ptr)
 {
 void
 vfree_dma(void* ptr)
 {
-    __vfree(ptr, &piles_dma);
+    __vfree(ptr, piles_dma, CLASS_LEN(piles_names_dma));
+}
+
+inline void must_inline
+valloc_ensure_valid(void* ptr) {
+    cake_ensure_valid(ptr);
 }
\ No newline at end of file
 }
\ No newline at end of file