summaryrefslogtreecommitdiff
path: root/fitz/base_memory.c
diff options
context:
space:
mode:
Diffstat (limited to 'fitz/base_memory.c')
-rw-r--r--fitz/base_memory.c158
1 files changed, 131 insertions, 27 deletions
diff --git a/fitz/base_memory.c b/fitz/base_memory.c
index f1b668eb..43ec0dd9 100644
--- a/fitz/base_memory.c
+++ b/fitz/base_memory.c
@@ -1,77 +1,181 @@
#include "fitz.h"
void *
-fz_malloc(int size)
+fz_malloc(fz_context *ctx, unsigned int size)
{
- void *p = malloc(size);
- if (!p)
+ void *p = ctx->alloc->malloc(ctx->alloc->user, size);
+ if (p == NULL)
+ fz_throw(ctx, "malloc of %d bytes failed", size);
+ return p;
+}
+
+void *
+fz_malloc_no_throw(fz_context *ctx, unsigned int size)
+{
+ return ctx->alloc->malloc(ctx->alloc->user, size);
+}
+
+void *
+fz_malloc_array(fz_context *ctx, unsigned int count, unsigned int size)
+{
+ void *p;
+
+ if (count == 0 || size == 0)
+ return 0;
+
+ if (count > UINT_MAX / size)
+ fz_throw(ctx, "malloc of array (%d x %d bytes) failed (integer overflow)", count, size);
+
+ p = ctx->alloc->malloc(ctx->alloc->user, count * size);
+ if (p == NULL)
+ fz_throw(ctx, "malloc of array (%d x %d bytes) failed", count, size);
+ return p;
+}
+
+void *
+fz_malloc_array_no_throw(fz_context *ctx, unsigned int count, unsigned int size)
+{
+ if (count == 0 || size == 0)
+ return 0;
+
+ if (count > UINT_MAX / size)
{
- fprintf(stderr, "fatal error: out of memory\n");
- abort();
+ fprintf(stderr, "error: malloc of array (%d x %d bytes) failed (integer overflow)", count, size);
+ return NULL;
}
+
+ return ctx->alloc->malloc(ctx->alloc->user, count * size);
+}
+
+void *
+fz_calloc(fz_context *ctx, unsigned int count, unsigned int size)
+{
+ void *p;
+
+ if (count == 0 || size == 0)
+ return 0;
+
+ if (count > UINT_MAX / size)
+ {
+ fz_throw(ctx, "calloc (%d x %d bytes) failed (integer overflow)", count, size);
+ }
+
+ p = ctx->alloc->malloc(ctx->alloc->user, count * size);
+ if (p == NULL)
+ {
+ fz_throw(ctx, "calloc (%d x %d bytes) failed", count, size);
+ }
+ memset(p, 0, count*size);
return p;
}
void *
-fz_calloc(int count, int size)
+fz_calloc_no_throw(fz_context *ctx, unsigned int count, unsigned int size)
{
void *p;
if (count == 0 || size == 0)
return 0;
- if (count < 0 || size < 0 || count > INT_MAX / size)
+ if (count > UINT_MAX / size)
{
- fprintf(stderr, "fatal error: out of memory (integer overflow)\n");
- abort();
+ fprintf(stderr, "error: calloc (%d x %d bytes) failed (integer overflow)\n", count, size);
+ return NULL;
}
- p = malloc(count * size);
- if (!p)
+ p = ctx->alloc->malloc(ctx->alloc->user, count * size);
+ if (p != NULL)
{
- fprintf(stderr, "fatal error: out of memory\n");
- abort();
+ memset(p, 0, count*size);
}
return p;
}
void *
-fz_realloc(void *p, int count, int size)
+fz_resize_array(fz_context *ctx, void *p, unsigned int count, unsigned int size)
{
void *np;
if (count == 0 || size == 0)
{
- fz_free(p);
+ fz_free(ctx, p);
return 0;
}
- if (count < 0 || size < 0 || count > INT_MAX / size)
+ if (count > UINT_MAX / size)
+ fz_throw(ctx, "resize array (%d x %d bytes) failed (integer overflow)", count, size);
+
+ np = ctx->alloc->realloc(ctx->alloc->user, p, count * size);
+ if (np == NULL)
+ fz_throw(ctx, "resize array (%d x %d bytes) failed", count, size);
+ return np;
+}
+
+void *
+fz_resize_array_no_throw(fz_context *ctx, void *p, unsigned int count, unsigned int size)
+{
+ if (count == 0 || size == 0)
{
- fprintf(stderr, "fatal error: out of memory (integer overflow)\n");
- abort();
+ fz_free(ctx, p);
+ return 0;
}
- np = realloc(p, count * size);
- if (np == NULL)
+ if (count > UINT_MAX / size)
{
- fprintf(stderr, "fatal error: out of memory\n");
- abort();
+ fprintf(stderr, "error: resize array (%d x %d bytes) failed (integer overflow)\n", count, size);
+ return NULL;
}
- return np;
+
+ return ctx->alloc->realloc(ctx->alloc->user, p, count * size);
}
void
-fz_free(void *p)
+fz_free(fz_context *ctx, void *p)
{
- free(p);
+ ctx->alloc->free(ctx->alloc->user, p);
}
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_no_throw(fz_context *ctx, char *s)
+{
+ int len = strlen(s) + 1;
+ char *ns = fz_malloc_no_throw(ctx, len);
+ if (ns != NULL)
+ memcpy(ns, s, len);
+ return ns;
+}
+
+static void *
+fz_malloc_default(void *opaque, unsigned int size)
+{
+ return malloc(size);
+}
+
+static void *
+fz_realloc_default(void *opaque, void *old, unsigned int size)
+{
+ return realloc(old, size);
+}
+
+static void
+fz_free_default(void *opaque, void *ptr)
+{
+ free(ptr);
+}
+
+fz_alloc_context fz_alloc_default =
+{
+ NULL,
+ fz_malloc_default,
+ fz_realloc_default,
+ fz_free_default
+} ; \ No newline at end of file