fix issue 31 (#32)
[lunaix-os.git] / lunaix-os / includes / lunaix / mm / cake.h
index 0312c45d381ac7854a802cb248eb792ac496cbf6..cf1c16e63c49bdf89fcf6db10c8c6b6cf3d6c1e4 100644 (file)
@@ -2,10 +2,15 @@
 #define __LUNAIX_CAKE_H
 
 #include <lunaix/ds/llist.h>
+#include <lunaix/spike.h>
 
 #define PILE_NAME_MAXLEN 20
 
-#define PILE_CACHELINE 1
+#define PILE_ALIGN_CACHE 1
+
+struct cake_pile;
+
+typedef void (*pile_cb)(struct cake_pile*, void*);
 
 struct cake_pile
 {
@@ -13,18 +18,20 @@ struct cake_pile
     struct llist_header full;
     struct llist_header partial;
     struct llist_header free;
-    unsigned int offset;
-    unsigned int piece_size;
-    unsigned int cakes_count;
-    unsigned int alloced_pieces;
-    unsigned int pieces_per_cake;
-    unsigned int pg_per_cake;
-    char pile_name[PILE_NAME_MAXLEN];
+    u32_t offset;
+    u32_t piece_size;
+    u32_t cakes_count;
+    u32_t alloced_pieces;
+    u32_t pieces_per_cake;
+    u32_t pg_per_cake;
+    char pile_name[PILE_NAME_MAXLEN+1];
+
+    pile_cb ctor;
 };
 
 typedef unsigned int piece_index_t;
 
-#define EO_FREE_PIECE (-1)
+#define EO_FREE_PIECE ((u32_t)-1)
 
 struct cake_s
 {
@@ -49,6 +56,9 @@ cake_new_pile(char* name,
               unsigned int pg_per_cake,
               int options);
 
+void
+cake_set_constructor(struct cake_pile* pile, pile_cb ctor);
+
 /**
  * @brief 拿一块儿蛋糕
  *
@@ -70,11 +80,21 @@ cake_release(struct cake_pile* pile, void* area);
 void
 cake_init();
 
-/**
- * @brief 统计蛋糕数量 - 问问Pinkie :D
- *
- */
 void
-cake_stats();
+cake_export();
+
+/********** some handy constructor ***********/
+
+void
+cake_ctor_zeroing(struct cake_pile* pile, void* piece);
+
+#define DEADCAKE_MARK 0xdeadcafeUL
+
+static inline void
+cake_ensure_valid(void* area) {
+    if (unlikely(*(unsigned int*)area == DEADCAKE_MARK)) {
+        fail("access to freed cake piece");
+    }
+}
 
 #endif /* __LUNAIX_VALLOC_H */