diff src/c/urweb.c @ 323:5030e909fbf3

Region memory allocation for query parameters
author Adam Chlipala <adamc@hcoop.net>
date Thu, 11 Sep 2008 12:40:40 -0400
parents cd4cabaf3e52
children b91480c9a729
line wrap: on
line diff
--- 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);