comparison 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
comparison
equal deleted inserted replaced
322:aa89b73d83e4 323:5030e909fbf3
9 9
10 uw_unit uw_unit_v = {}; 10 uw_unit uw_unit_v = {};
11 11
12 #define ERROR_BUF_LEN 1024 12 #define ERROR_BUF_LEN 1024
13 13
14 typedef struct regions {
15 struct regions *next;
16 } regions;
17
14 struct uw_context { 18 struct uw_context {
15 char *page, *page_front, *page_back; 19 char *page, *page_front, *page_back;
16 char *heap, *heap_front, *heap_back; 20 char *heap, *heap_front, *heap_back;
17 char **inputs; 21 char **inputs;
18 22
19 void *db; 23 void *db;
20 24
21 jmp_buf jmp_buf; 25 jmp_buf jmp_buf;
22 26
27 regions *regions;
28
23 char error_message[ERROR_BUF_LEN]; 29 char error_message[ERROR_BUF_LEN];
24 }; 30 };
25 31
26 extern int uw_inputs_len; 32 extern int uw_inputs_len;
27 33
35 ctx->heap_back = ctx->heap_front + heap_len; 41 ctx->heap_back = ctx->heap_front + heap_len;
36 42
37 ctx->inputs = calloc(uw_inputs_len, sizeof(char *)); 43 ctx->inputs = calloc(uw_inputs_len, sizeof(char *));
38 44
39 ctx->db = NULL; 45 ctx->db = NULL;
46
47 ctx->regions = NULL;
40 48
41 ctx->error_message[0] = 0; 49 ctx->error_message[0] = 0;
42 50
43 return ctx; 51 return ctx;
44 } 52 }
59 } 67 }
60 68
61 void uw_reset_keep_request(uw_context ctx) { 69 void uw_reset_keep_request(uw_context ctx) {
62 ctx->page_front = ctx->page; 70 ctx->page_front = ctx->page;
63 ctx->heap_front = ctx->heap; 71 ctx->heap_front = ctx->heap;
72 ctx->regions = NULL;
64 73
65 ctx->error_message[0] = 0; 74 ctx->error_message[0] = 0;
66 } 75 }
67 76
68 void uw_reset_keep_error_message(uw_context ctx) { 77 void uw_reset_keep_error_message(uw_context ctx) {
69 ctx->page_front = ctx->page; 78 ctx->page_front = ctx->page;
70 ctx->heap_front = ctx->heap; 79 ctx->heap_front = ctx->heap;
80 ctx->regions = NULL;
71 } 81 }
72 82
73 void uw_reset(uw_context ctx) { 83 void uw_reset(uw_context ctx) {
74 uw_reset_keep_request(ctx); 84 uw_reset_keep_request(ctx);
75 memset(ctx->inputs, 0, uw_inputs_len * sizeof(char *)); 85 memset(ctx->inputs, 0, uw_inputs_len * sizeof(char *));
172 uw_check_heap(ctx, len); 182 uw_check_heap(ctx, len);
173 183
174 result = ctx->heap_front; 184 result = ctx->heap_front;
175 ctx->heap_front += len; 185 ctx->heap_front += len;
176 return result; 186 return result;
187 }
188
189 void uw_begin_region(uw_context ctx) {
190 regions *r = (regions *) ctx->heap_front;
191
192 uw_check_heap(ctx, sizeof(regions));
193
194 ctx->heap_front += sizeof(regions);
195
196 r->next = ctx->regions;
197 ctx->regions = r;
198 }
199
200 void uw_end_region(uw_context ctx) {
201 regions *r = ctx->regions;
202
203 if (r == NULL)
204 uw_error(ctx, FATAL, "Region stack underflow");
205
206 ctx->heap_front = (char *) r;
207 ctx->regions = r->next;
177 } 208 }
178 209
179 int uw_really_send(int sock, const void *buf, ssize_t len) { 210 int uw_really_send(int sock, const void *buf, ssize_t len) {
180 while (len > 0) { 211 while (len > 0) {
181 ssize_t n = send(sock, buf, len, 0); 212 ssize_t n = send(sock, buf, len, 0);