withMulklib working the first time
[lazyeval.git] / withMulklib.c
index cf4bf4f..1da0e37 100644 (file)
 #define __USE_GNU
 #include <ucontext.h>
 
 #define __USE_GNU
 #include <ucontext.h>
 
+#include "lists.h"
+
 /* This code is only for x86_64 gcc linux. It is NOT threadsafe. */
 
 /* 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;
 /* w00t, I am a three-star-programmer! */
 void ***dereferencedPointer;
 void ***lastDereferencedPointer;
@@ -78,8 +96,7 @@ void*** lazy_alloc (void* (*calculate) (void*), void* opt) {
   lockedPointer++;
   dereferencedPointer++;
 
   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;
   en->function = calculate;
   en->argument = opt;
   en->dereferencedPointer = ret;
@@ -90,6 +107,8 @@ void*** lazy_alloc (void* (*calculate) (void*), void* opt) {
   return ret;
 }
 
   return ret;
 }
 
+void initializeSignalHandler();
+
 void handle_segv(int segv, siginfo_t* siginfo, void* ucontext) {
   ucontext_t* uc = (ucontext_t*) ucontext;
 
 void handle_segv(int segv, siginfo_t* siginfo, void* ucontext) {
   ucontext_t* uc = (ucontext_t*) ucontext;
 
@@ -198,7 +217,7 @@ void handle_segv(int segv, siginfo_t* siginfo, void* ucontext) {
   intptr_t address = uc->uc_mcontext.gregs[setreg];
 
   if (!bpt_has_key(pointer_tree, address)) {
   intptr_t address = uc->uc_mcontext.gregs[setreg];
 
   if (!bpt_has_key(pointer_tree, address)) {
-    printf("Address not found in Patricia tree.\n");
+    printf("Address not found in Patricia tree: %d.\n", address);
     exit(-1);
   }
 
     exit(-1);
   }
 
@@ -214,20 +233,46 @@ inline void initializeSignalHandler () {
   struct sigaction q;
   bzero(&q, sizeof(q));
   q.sa_sigaction = handle_segv;
   struct sigaction q;
   bzero(&q, sizeof(q));
   q.sa_sigaction = handle_segv;
-  q.sa_flags = SA_SIGINFO;
+  q.sa_flags = SA_SIGINFO | SA_NODEFER;
   sigaction(11, &q, NULL);
 }
 
   sigaction(11, &q, NULL);
 }
 
-void *calculateLazy24 (void* bla) {
-  int* ret = (int*) malloc(sizeof(int));
-  *ret = 42;
+typedef struct {
+  int i;
+  int (*fun) (int, list);
+  list initial;
+} listComprehensionRecursorR;
+
+void * listComprehensionRecursor (void* x) {
+  listComprehensionRecursorR* lcr = (listComprehensionRecursorR*) x;
+  int value = (lcr->fun)(lcr->i, lcr->initial);
+  lcr->i++;
+
+  list cdr = (list) lazy_alloc(listComprehensionRecursor, (void*)lcr);
+  cons * ret = (cons*) slice_alloc(sizeof(cons));
+  doCons(value, cdr, ret);
   return (void*) ret;
 }
 
   return (void*) ret;
 }
 
+list listComprehension (int (*fun) (int, list)) {
+  listComprehensionRecursorR* lcr =
+    (listComprehensionRecursorR*) slice_alloc
+    (sizeof(listComprehensionRecursorR));
+  lcr->i = 0;
+  lcr->fun=fun;
+  lcr->initial = (list) lazy_alloc(listComprehensionRecursor, (void*)lcr);
+  return lcr->initial;
+}
+
+int fib (int x, list l) {
+  return x == 0 ? 0 : (x == 1 ? 1 : elt(l, x-1) + elt(l, x-2));
+}
+
 int main (void) {
   initializePointerTree ();
   initializeSignalHandler ();
 int main (void) {
   initializePointerTree ();
   initializeSignalHandler ();
-  void *** lazy42 = lazy_alloc(calculateLazy24, NULL);
-  printf("%d\n", **((int**)lazy42));
-
+  list lst = listComprehension(fib);
+  int i;
+  for (i = 0; i < 10; i++)
+    printf("%d\n", elt(lst, i));
 }
 }