diff src/c/urweb.c @ 1782:61c7eb1d3867

Support fancy expressions in module-level 'val' declarations
author Adam Chlipala <adam@chlipala.net>
date Wed, 18 Jul 2012 17:29:13 -0400
parents 22858505bb2d
children 69daa6d70299
line wrap: on
line diff
--- a/src/c/urweb.c	Fri Jul 13 09:01:01 2012 -0400
+++ b/src/c/urweb.c	Wed Jul 18 17:29:13 2012 -0400
@@ -465,6 +465,8 @@
 
   unsigned nextId;
 
+  int amInitializing;
+
   char error_message[ERROR_BUF_LEN];
 };
 
@@ -536,6 +538,8 @@
 
   ctx->nextId = 0;
 
+  ctx->amInitializing = 0;
+
   return ctx;
 }
 
@@ -613,6 +617,7 @@
   ctx->script_header = "";
   ctx->queryString = NULL;
   ctx->nextId = 0;
+  ctx->amInitializing = 0;
 }
 
 void uw_reset_keep_request(uw_context ctx) {
@@ -1204,14 +1209,31 @@
   ctx->heap.front = fr;
 }
 
+void uw_begin_initializing(uw_context ctx) {
+  ctx->amInitializing = 1;
+}
+
+void uw_end_initializing(uw_context ctx) {
+  ctx->amInitializing = 0;
+}
+
 void *uw_malloc(uw_context ctx, size_t len) {
   void *result;
 
-  uw_check_heap(ctx, len);
-
-  result = ctx->heap.front;
-  ctx->heap.front += len;
-  return result;
+  if (ctx->amInitializing) {
+    result = malloc(len);
+
+    if (result)
+      return result;
+    else
+      uw_error(ctx, FATAL, "uw_malloc: malloc() returns 0");
+  } else {
+    uw_check_heap(ctx, len);
+
+    result = ctx->heap.front;
+    ctx->heap.front += len;
+    return result;
+  }
 }
 
 void uw_begin_region(uw_context ctx) {