From 6ddde92a3a45e970b05770633dc6a337d5d013c5 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 27 Sep 2004 02:15:04 +0200 Subject: Initial import --- base/memory.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 base/memory.c (limited to 'base/memory.c') diff --git a/base/memory.c b/base/memory.c new file mode 100644 index 00000000..88102d00 --- /dev/null +++ b/base/memory.c @@ -0,0 +1,81 @@ +#include + +/* Make this thread local storage if you wish. */ + +static void *stdmalloc(fz_memorycontext *mem, int n) +{ +#if 0 + void *p = malloc(n); + if (!p) + fprintf(stderr, "failed to malloc %d bytes\n", n); + return p; +#else + return malloc(n); +#endif +} + +static void *stdrealloc(fz_memorycontext *mem, void *p, int n) +{ +#if 0 + void *np = realloc(p, n); + if (np == nil) + fprintf(stderr, "realloc failed %d nytes", n); + else if (np == p) + fprintf(stderr, "realloc kept %d\n", n); + else + fprintf(stderr, "realloc moved %d\n", n); + return np; +#else + return realloc(p, n); +#endif +} + +static void stdfree(fz_memorycontext *mem, void *p) +{ + free(p); +} + +static fz_memorycontext defmem = { stdmalloc, stdrealloc, stdfree }; +static fz_memorycontext *curmem = &defmem; + +fz_error fz_koutofmem = { + .msg = {"out of memory"}, + .func = {""}, + .file = {"memory.c"}, + .line = 0, + .frozen = 1 +}; + +fz_memorycontext * +fz_currentmemorycontext() +{ + return curmem; +} + +void +fz_setmemorycontext(fz_memorycontext *mem) +{ + curmem = mem; +} + +void * +fz_malloc(int n) +{ + fz_memorycontext *mem = fz_currentmemorycontext(); + return mem->malloc(mem, n); +} + +void * +fz_realloc(void *p, int n) +{ + fz_memorycontext *mem = fz_currentmemorycontext(); + return mem->realloc(mem, p, n); +} + +void +fz_free(void *p) +{ + fz_memorycontext *mem = fz_currentmemorycontext(); + mem->free(mem, p); +} + -- cgit v1.2.3