changeset 323:5030e909fbf3

Region memory allocation for query parameters
author Adam Chlipala <adamc@hcoop.net>
date Thu, 11 Sep 2008 12:40:40 -0400
parents aa89b73d83e4
children b91480c9a729
files include/urweb.h src/c/urweb.c src/cjr_print.sml
diffstat 3 files changed, 38 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Thu Sep 11 12:22:06 2008 -0400
+++ b/include/urweb.h	Thu Sep 11 12:40:40 2008 -0400
@@ -21,6 +21,9 @@
 char *uw_error_message(uw_context);
 
 void *uw_malloc(uw_context, size_t);
+void uw_begin_region(uw_context);
+void uw_end_region(uw_context);
+
 int uw_send(uw_context, int sock);
 
 void uw_set_input(uw_context, char *name, char *value);
--- a/src/c/urweb.c	Thu Sep 11 12:22:06 2008 -0400
+++ b/src/c/urweb.c	Thu Sep 11 12:40:40 2008 -0400
@@ -11,6 +11,10 @@
 
 #define ERROR_BUF_LEN 1024
 
+typedef struct regions {
+  struct regions *next;
+} regions;
+
 struct uw_context {
   char *page, *page_front, *page_back;
   char *heap, *heap_front, *heap_back;
@@ -20,6 +24,8 @@
 
   jmp_buf jmp_buf;
 
+  regions *regions;
+
   char error_message[ERROR_BUF_LEN];
 };
 
@@ -38,6 +44,8 @@
 
   ctx->db = NULL;
 
+  ctx->regions = NULL;
+
   ctx->error_message[0] = 0;
 
   return ctx;
@@ -61,6 +69,7 @@
 void uw_reset_keep_request(uw_context ctx) {
   ctx->page_front = ctx->page;
   ctx->heap_front = ctx->heap;
+  ctx->regions = NULL;
 
   ctx->error_message[0] = 0;
 }
@@ -68,6 +77,7 @@
 void uw_reset_keep_error_message(uw_context ctx) {
   ctx->page_front = ctx->page;
   ctx->heap_front = ctx->heap;
+  ctx->regions = NULL;
 }
 
 void uw_reset(uw_context ctx) {
@@ -176,6 +186,27 @@
   return result;
 }
 
+void uw_begin_region(uw_context ctx) {
+  regions *r = (regions *) ctx->heap_front;
+
+  uw_check_heap(ctx, sizeof(regions));
+
+  ctx->heap_front += sizeof(regions);
+
+  r->next = ctx->regions;
+  ctx->regions = r;
+}
+
+void uw_end_region(uw_context ctx) {
+  regions *r = ctx->regions;
+
+  if (r == NULL)
+    uw_error(ctx, FATAL, "Region stack underflow");
+
+  ctx->heap_front = (char *) r;
+  ctx->regions = r->next;
+}
+
 int uw_really_send(int sock, const void *buf, ssize_t len) {
   while (len > 0) {
     ssize_t n = send(sock, buf, len, 0);
--- a/src/cjr_print.sml	Thu Sep 11 12:22:06 2008 -0400
+++ b/src/cjr_print.sml	Thu Sep 11 12:40:40 2008 -0400
@@ -712,7 +712,7 @@
                                                                                               
             val outputs = exps @ tables
         in
-            box [string "({",
+            box [string "(uw_begin_region(ctx), ({",
                  newline,
                  string "PGconn *conn = uw_get_db(ctx);",
                  newline,
@@ -791,6 +791,8 @@
                  newline,
                  newline,
 
+                 string "uw_end_region(ctx);",
+                 newline,
                  string "n = PQntuples(res);",
                  newline,
                  string "for (i = 0; i < n; ++i) {",
@@ -851,7 +853,7 @@
                  newline,
                  string "acc;",
                  newline,
-                 string "})"]
+                 string "}))"]
         end
 
       | EDml {dml, prepared} =>