diff options
Diffstat (limited to 'fitz/base_memory.c')
-rw-r--r-- | fitz/base_memory.c | 143 |
1 files changed, 106 insertions, 37 deletions
diff --git a/fitz/base_memory.c b/fitz/base_memory.c index f1b668eb..9847aa76 100644 --- a/fitz/base_memory.c +++ b/fitz/base_memory.c @@ -1,77 +1,146 @@ #include "fitz.h" +#include "except.h" void * -fz_malloc(int size) +fz_malloc(fz_context *ctx, size_t size) { - void *p = malloc(size); + void *p; + fz_alloc_context *alloc; + + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + p = alloc->malloc(alloc->opaque, size); if (!p) { - fprintf(stderr, "fatal error: out of memory\n"); - abort(); + fz_throw(ctx, "malloc failed (%d bytes)", size); } return p; } -void * -fz_calloc(int count, int size) +void *fz_calloc(fz_context *ctx, size_t count, size_t size) { void *p; + fz_alloc_context *alloc; - if (count == 0 || size == 0) - return 0; - - if (count < 0 || size < 0 || count > INT_MAX / size) + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + p = alloc->calloc(alloc->opaque, count, size); + if (!p) { - fprintf(stderr, "fatal error: out of memory (integer overflow)\n"); - abort(); + fz_throw(ctx, "calloc failed (%d x %d bytes)", count, size); } + return p; +} + +void * +fz_realloc(fz_context *ctx, void *p, size_t size) +{ + fz_alloc_context *alloc; - p = malloc(count * size); + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + p = alloc->realloc(alloc->opaque, p, size); if (!p) { - fprintf(stderr, "fatal error: out of memory\n"); - abort(); + fz_throw(ctx, "realloc failed (%d bytes)", size); } return p; } +void +fz_free(fz_context *ctx, void *p) +{ + fz_alloc_context *alloc; + + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + alloc->free(alloc->opaque, p); +} + void * -fz_realloc(void *p, int count, int size) +fz_malloc_nothrow(fz_context *ctx, size_t size) { - void *np; + fz_alloc_context *alloc; - if (count == 0 || size == 0) - { - fz_free(p); - return 0; - } + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + return alloc->malloc(alloc->opaque, size); +} - if (count < 0 || size < 0 || count > INT_MAX / size) - { - fprintf(stderr, "fatal error: out of memory (integer overflow)\n"); - abort(); - } +void *fz_calloc_nothrow(fz_context *ctx, size_t count, size_t size) +{ + fz_alloc_context *alloc; - np = realloc(p, count * size); - if (np == NULL) - { - fprintf(stderr, "fatal error: out of memory\n"); - abort(); - } - return np; + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + return alloc->calloc(alloc->opaque, count, size); +} + +void * +fz_realloc_nothrow(fz_context *ctx, void *p, size_t size) +{ + fz_alloc_context *alloc; + + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + return alloc->realloc(alloc->opaque, p, size); +} + +void * +fz_malloc_default(void *opaque, size_t size) +{ + return malloc(size); +} + +void * +fz_calloc_default(void *opaque, size_t count, size_t size) +{ + return calloc(count, size); +} + +void * +fz_realloc_default(void *opaque, void *p, size_t size) +{ + return realloc(p, size); } void -fz_free(void *p) +fz_free_default(void *opaque, void *p) { free(p); } +fz_alloc_context fz_alloc_default = +{ + (void *)-1, + fz_malloc_default, + fz_realloc_default, + fz_free_default, + fz_calloc_default +}; + char * -fz_strdup(char *s) +fz_strdup(fz_context *ctx, char *s) { int len = strlen(s) + 1; - char *ns = fz_malloc(len); + char *ns = fz_malloc(ctx, len); memcpy(ns, s, len); return ns; } + +char * +fz_strdup_nothrow(fz_context *ctx, char *s) +{ + int len = strlen(s) + 1; + char *ns = fz_malloc_nothrow(ctx, len); + if (ns != NULL) + memcpy(ns, s, len); + return ns; +} |