comparison src/c/urweb.c @ 1089:f1647f16097d

Convenience libifying; allow more NULLs with globals
author Adam Chlipala <adamc@hcoop.net>
date Thu, 24 Dec 2009 15:49:52 -0500
parents e81434513720
children db52c32dbe42
comparison
equal deleted inserted replaced
1088:5f72a66402f7 1089:f1647f16097d
466 466
467 for (i = 0; i < ctx->n_deltas; ++i) 467 for (i = 0; i < ctx->n_deltas; ++i)
468 buf_free(&ctx->deltas[i].msgs); 468 buf_free(&ctx->deltas[i].msgs);
469 469
470 for (i = 0; i < ctx->n_globals; ++i) 470 for (i = 0; i < ctx->n_globals; ++i)
471 ctx->globals[i].free(ctx->globals[i].data); 471 if (ctx->globals[i].free)
472 ctx->globals[i].free(ctx->globals[i].data);
472 473
473 free(ctx); 474 free(ctx);
474 } 475 }
475 476
476 void uw_reset_keep_error_message(uw_context ctx) { 477 void uw_reset_keep_error_message(uw_context ctx) {
3175 } 3176 }
3176 3177
3177 void uw_set_global(uw_context ctx, char *name, void *data, void (*free)(void*)) { 3178 void uw_set_global(uw_context ctx, char *name, void *data, void (*free)(void*)) {
3178 int i; 3179 int i;
3179 3180
3180 if (data == NULL) uw_error(ctx, FATAL, "NULL data value for global '%s'", name);
3181
3182 for (i = 0; i < ctx->n_globals; ++i) 3181 for (i = 0; i < ctx->n_globals; ++i)
3183 if (!strcmp(name, ctx->globals[i].name)) { 3182 if (!strcmp(name, ctx->globals[i].name)) {
3184 if (ctx->globals[i].data) 3183 if (ctx->globals[i].free)
3185 ctx->globals[i].free(ctx->globals[i].data); 3184 ctx->globals[i].free(ctx->globals[i].data);
3186 ctx->globals[i].data = data; 3185 ctx->globals[i].data = data;
3187 ctx->globals[i].free = free; 3186 ctx->globals[i].free = free;
3188 return; 3187 return;
3189 } 3188 }
3190 3189
3191 ++ctx->n_globals; 3190 ++ctx->n_globals;
3192 ctx->globals = realloc(ctx->globals, ctx->n_globals * sizeof(global)); 3191 ctx->globals = realloc(ctx->globals, ctx->n_globals * sizeof(global));
3193 ctx->globals[ctx->n_globals-1].name = name; 3192 ctx->globals[ctx->n_globals-1].name = name;
3194 ctx->globals[ctx->n_globals-1].data = data; 3193 ctx->globals[ctx->n_globals-1].data = data;
3195 ctx->globals[ctx->n_globals-1].free = free; 3194 ctx->globals[ctx->n_globals-1].free = free;