added a slice allocator
[lazyeval.git] / withMulklib.c
index cf4bf4f..f8c4b34 100644 (file)
 
 /* This code is only for x86_64 gcc linux. It is NOT threadsafe. */
 
+void* slice_alloc (size_t length) {
+  static intptr_t *nextslice = NULL + 1;
+  static intptr_t *lastslice = NULL;
+
+  int num = length / sizeof(intptr_t) + 1;
+
+  if (lastslice < nextslice + num) {
+    nextslice = (intptr_t *)malloc(1024*sizeof(intptr_t));
+    lastslice = nextslice + 1023;
+  }
+
+  void* ret = (void*) nextslice;
+  nextslice += num;
+  return ret;
+}
+
 /* w00t, I am a three-star-programmer! */
 void ***dereferencedPointer;
 void ***lastDereferencedPointer;
@@ -78,8 +94,7 @@ void*** lazy_alloc (void* (*calculate) (void*), void* opt) {
   lockedPointer++;
   dereferencedPointer++;
 
-  /* we should use a slice allocator here */
-  tree_entry *en = (tree_entry*) malloc (sizeof(tree_entry));
+  tree_entry *en = (tree_entry*) slice_alloc (sizeof(tree_entry));
   en->function = calculate;
   en->argument = opt;
   en->dereferencedPointer = ret;
@@ -219,7 +234,7 @@ inline void initializeSignalHandler () {
 }
 
 void *calculateLazy24 (void* bla) {
-  int* ret = (int*) malloc(sizeof(int));
+  int* ret = (int*) slice_alloc (sizeof(int));
   *ret = 42;
   return (void*) ret;
 }