diff options
author | Jonathan Kew <jfkthame@googlemail.com> | 2008-05-21 13:19:17 +0000 |
---|---|---|
committer | Jonathan Kew <jfkthame@googlemail.com> | 2008-05-21 13:19:17 +0000 |
commit | ebdcfba2fc6c9bf4db2008a00058f3422a9d1da8 (patch) | |
tree | 52772b6cbe81d9ca8b67d3990ea033a27d432916 /Build/source/texk/xdvipdfmx/src/mem.c | |
parent | 246e3a9180a28cb419402687d2005bdfb5313774 (diff) |
update to xdvipdfmx 0.7, from xetex svn r.688
git-svn-id: svn://tug.org/texlive/trunk@8272 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build/source/texk/xdvipdfmx/src/mem.c')
-rw-r--r-- | Build/source/texk/xdvipdfmx/src/mem.c | 171 |
1 files changed, 127 insertions, 44 deletions
diff --git a/Build/source/texk/xdvipdfmx/src/mem.c b/Build/source/texk/xdvipdfmx/src/mem.c index 382f0dd8e81..e474f407e6d 100644 --- a/Build/source/texk/xdvipdfmx/src/mem.c +++ b/Build/source/texk/xdvipdfmx/src/mem.c @@ -1,4 +1,4 @@ -/* $Header: /home/cvsroot/dvipdfmx/src/mem.c,v 1.3 2002/10/30 02:27:11 chofchof Exp $ +/* $Header: /home/cvsroot/dvipdfmx/src/mem.c,v 1.5 2007/11/17 18:08:58 matthias Exp $ This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. @@ -26,69 +26,152 @@ #include <stdlib.h> #include "mem.h" +#include "error.h" #ifdef MEM_DEBUG -FILE *debugfile = NULL; -static long int event = 0; -#endif /* MEM_DEBUG */ -#ifdef MEM_DEBUG +#include "dpxutil.h" + +#define MEM_LINE 128 + +static struct ht_table *mem_ht; +static long int mem_event; +static int mem_internal; +char *mem_str; + void mem_debug_init(void) { - if (debugfile == NULL) { - debugfile = fopen ("malloc.log", "w"); - fprintf (stderr, "*** Memory Debugging Log started ***\n"); + mem_event = 0; + mem_internal = 0; + mem_ht = malloc(sizeof(struct ht_table)); + ht_init_table(mem_ht); + fprintf(stderr, "*** Memory debugging started ***\n"); +} + +void mem_debug_check(void) +{ + struct ht_iter iter; + + fprintf(stderr, "*** Memory still in use ***\n"); + + if (ht_set_iter(mem_ht, &iter) == 0) { + do { + int pl; + void *p = *((void **) ht_iter_getkey(&iter, &pl)); + char *s = ht_iter_getval(&iter); + + fprintf(stderr, "%p %s\n", p, s); + } while (!ht_iter_next(&iter)); } + ht_clear_iter(&iter); + + fprintf(stderr, "*** End of used memory ***\n"); } -#endif /* MEM_DEBUG */ -void *new (size_t size, char *function, int line) +static void +mem_noop (void *vp) { - void *result; - if ((result = malloc (size)) == NULL) { - fprintf (stderr, "Out of memory in %s:%d\n", function, line); - fprintf (stderr, "Asked for %lu\n", (unsigned long) size); - exit (1); +} + +static void +mem_error (void *vp) +{ + ERROR("Internal memory debugging error\n1: %s\n2: %s\n", + vp, (char *) vp, mem_str); +} + +void *mem_add(void *ptr, const char *file, const char *function, int line) { + if (ptr && !mem_internal) { + mem_internal = 1; + mem_str = malloc(MEM_LINE); + char **p = malloc(sizeof(ptr)); + *p = ptr; + snprintf(mem_str, MEM_LINE, "(0x%08lx) %s (%s, %d)" +#ifdef __GNUC__ + ", %p" //" %p" +#endif + , ++mem_event, function, file, line +#ifdef __GNUC__ + , __builtin_return_address(1) //, __builtin_return_address(2) +#endif + ); + ht_insert_table(mem_ht, p, sizeof(ptr), mem_str, mem_error); + mem_internal = 0; } -#ifdef MEM_DEBUG - mem_debug_init(); - event += 1; - fprintf (debugfile, "%p %07ld [new] %s:%d\n", result, event, function, line); -#endif /* MEM_DEBUG */ + return ptr; +} - return result; +void *mem_remove(void *ptr, const char *file, const char *function, int line) { + if (ptr && !mem_internal) { + mem_internal = 1; + if (!(mem_ht && ht_remove_table(mem_ht, &ptr, sizeof(ptr), mem_noop))) { + WARN("Trying to free non-allocated memory\n" + "%p %s (%s, %d)" +#ifdef __GNUC__ + ", called from %p" +#endif + "\n", ptr, function, file, line +#ifdef __GNUC__ + , __builtin_return_address(1) +#endif + ); + } + mem_internal = 0; + } + + return ptr; +} + + +#else /* ! MEM_DEBUG */ + +static long int mem_count; + +void mem_debug_init(void) +{ + mem_count = 0; } -void *renew (void *mem, size_t size, char *function, int line) +void mem_debug_check(void) { - void *result; -#ifdef MEM_DEBUG - mem_debug_init(); - event += 1; - if (mem) - fprintf (debugfile, "%p %07ld [fre] %s:%d\n", mem, - event, function, line); + if (mem_count) + WARN("%ld memory objects still allocated\n", mem_count); +} + +void *mem_add(void *ptr) { + if (ptr) + mem_count++; + + return ptr; +} + +void *mem_remove(void *ptr) { + if (ptr) + mem_count--; + + return ptr; +} + #endif /* MEM_DEBUG */ - if ((result = realloc (mem, size)) == NULL) { - fprintf (stderr, "Out of memory!\n"); - exit (1); + + +void *new (size_t size) +{ + void *result = malloc (size); + if (!result) { + ERROR("Out of memory - asked for %lu bytes\n", (unsigned long) size); } -#ifdef MEM_DEBUG - if (result) - fprintf (debugfile, "%p %07ld [new] %s:%d\n", result, event, function, line); -#endif /* MEM_DEBUG */ + return result; } -void release (void *mem, char *function, int line) +void *renew (void *mem, size_t size) { + void *result = realloc (mem, size); + if (!result) { + ERROR("Out of memory - asked for %lu bytes\n", (unsigned long) size); + } -#ifdef MEM_DEBUG - mem_debug_init(); - event += 1; - fprintf (debugfile, "%p %07ld [fre] %s:%d\n", mem, event, function, line); -#endif /* MEM_DEBUG */ - - free (mem); + return result; } |