diff options
Diffstat (limited to 'fitz/base_memory.c')
-rw-r--r-- | fitz/base_memory.c | 128 |
1 files changed, 35 insertions, 93 deletions
diff --git a/fitz/base_memory.c b/fitz/base_memory.c index 643c85b6..27474cc3 100644 --- a/fitz/base_memory.c +++ b/fitz/base_memory.c @@ -1,130 +1,72 @@ #include "fitz.h" void * -fz_malloc(fz_context *ctx, size_t size) +fz_malloc(fz_context *ctx, unsigned int size) { - void *p; - fz_alloc_context *alloc; - - assert(ctx != NULL); - alloc = ctx->alloc; - assert(alloc != NULL); - p = alloc->malloc(alloc->opaque, size); + void *p = malloc(size); if (!p) { - fz_throw(ctx, "malloc failed (%d bytes)", size); + fprintf(stderr, "fatal error: out of memory\n"); + abort(); } return p; } -void *fz_calloc(fz_context *ctx, size_t count, size_t size) +void * +fz_malloc_array(fz_context *ctx, unsigned int count, unsigned int size) { void *p; - fz_alloc_context *alloc; - assert(ctx != NULL); - alloc = ctx->alloc; - assert(alloc != NULL); - p = alloc->calloc(alloc->opaque, count, size); - if (!p) + if (count == 0 || size == 0) + return 0; + + if (count > UINT_MAX / size) { - fz_throw(ctx, "calloc failed (%d x %d bytes)", count, size); + fprintf(stderr, "fatal error: out of memory (integer overflow)\n"); + abort(); } - return p; -} -void * -fz_realloc(fz_context *ctx, void *p, size_t size) -{ - fz_alloc_context *alloc; - - assert(ctx != NULL); - alloc = ctx->alloc; - assert(alloc != NULL); - p = alloc->realloc(alloc->opaque, p, size); + p = malloc(count * size); if (!p) { - fz_throw(ctx, "realloc failed (%d bytes)", size); + fprintf(stderr, "fatal error: out of memory\n"); + abort(); } 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_malloc_nothrow(fz_context *ctx, size_t size) -{ - fz_alloc_context *alloc; - - assert(ctx != NULL); - alloc = ctx->alloc; - assert(alloc != NULL); - return alloc->malloc(alloc->opaque, size); -} - -void *fz_calloc_nothrow(fz_context *ctx, size_t count, size_t size) -{ - fz_alloc_context *alloc; - - 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_resize_array(fz_context *ctx, void *p, unsigned int count, unsigned int size) { - fz_alloc_context *alloc; + void *np; - 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); -} + if (count == 0 || size == 0) + { + fz_free(ctx, p); + return 0; + } -void * -fz_calloc_default(void *opaque, size_t count, size_t size) -{ - return calloc(count, size); -} + if (count > UINT_MAX / size) + { + fprintf(stderr, "fatal error: out of memory (integer overflow)\n"); + abort(); + } -void * -fz_realloc_default(void *opaque, void *p, size_t size) -{ - return realloc(p, size); + np = realloc(p, count * size); + if (np == NULL) + { + fprintf(stderr, "fatal error: out of memory\n"); + abort(); + } + return np; } void -fz_free_default(void *opaque, void *p) +fz_free(fz_context *ctx, 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(fz_context *ctx, char *s) { |