diff options
Diffstat (limited to 'Build/source/texk/dvipdfmx/src/mem.c')
-rw-r--r-- | Build/source/texk/dvipdfmx/src/mem.c | 201 |
1 files changed, 88 insertions, 113 deletions
diff --git a/Build/source/texk/dvipdfmx/src/mem.c b/Build/source/texk/dvipdfmx/src/mem.c index ecc42c6321c..e474f407e6d 100644 --- a/Build/source/texk/dvipdfmx/src/mem.c +++ b/Build/source/texk/dvipdfmx/src/mem.c @@ -1,4 +1,4 @@ -/* $Header: /home/cvsroot/dvipdfmx/src/mem.c,v 1.4 2007/11/14 03:36:01 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. @@ -25,35 +25,31 @@ #include <stdio.h> #include <stdlib.h> -#include "dpxutil.h" #include "mem.h" - -static long int mem_count = 0; -static int mem_internal = 0; -static struct ht_table *mem_ht = NULL; +#include "error.h" #ifdef MEM_DEBUG +#include "dpxutil.h" + #define MEM_LINE 128 -long int mem_event = 0; +static struct ht_table *mem_ht; +static long int mem_event; +static int mem_internal; +char *mem_str; -static void -mem_noop (void *vp) -{ -} - -static void -mem_error (void *vp) +void mem_debug_init(void) { - fprintf(stderr, "pointer %p released, but not allocated\n", vp); + mem_event = 0; + mem_internal = 0; + mem_ht = malloc(sizeof(struct ht_table)); + ht_init_table(mem_ht); + fprintf(stderr, "*** Memory debugging started ***\n"); } -#endif /* MEM_DEBUG */ - void mem_debug_check(void) { -#ifdef MEM_DEBUG struct ht_iter iter; fprintf(stderr, "*** Memory still in use ***\n"); @@ -61,7 +57,7 @@ void mem_debug_check(void) if (ht_set_iter(mem_ht, &iter) == 0) { do { int pl; - void *p = ht_iter_getkey(&iter, &pl); + void *p = *((void **) ht_iter_getkey(&iter, &pl)); char *s = ht_iter_getval(&iter); fprintf(stderr, "%p %s\n", p, s); @@ -70,133 +66,112 @@ void mem_debug_check(void) ht_clear_iter(&iter); fprintf(stderr, "*** End of used memory ***\n"); -#endif /* MEM_DEBUG */ - - if (mem_count) - fprintf(stderr, "*** WARNING: %ld memory objects still allocated\n", mem_count); } -void mem_debug_init(void) +static void +mem_noop (void *vp) { -#ifdef MEM_DEBUG - mem_ht = malloc(sizeof(struct ht_table)); - ht_init_table(mem_ht); - fprintf(stderr, "*** Memory debugging started ***\n"); -#else - mem_ht = (struct ht_table *) &mem_count; /* just any non-null value */ -#endif /* MEM_DEBUG */ } -void *new (size_t size, const char *file, const char *function, int line) +static void +mem_error (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); - } + ERROR("Internal memory debugging error\n1: %s\n2: %s\n", + vp, (char *) vp, mem_str); +} - if (!mem_internal) { - mem_count++; -#ifdef MEM_DEBUG +void *mem_add(void *ptr, const char *file, const char *function, int line) { + if (ptr && !mem_internal) { mem_internal = 1; - char *s = malloc(MEM_LINE), **p = malloc(sizeof(result)); - *p = result; - snprintf(s, MEM_LINE, "(0x%08lx) %s (%s, %d)" + mem_str = malloc(MEM_LINE); + char **p = malloc(sizeof(ptr)); + *p = ptr; + snprintf(mem_str, MEM_LINE, "(0x%08lx) %s (%s, %d)" #ifdef __GNUC__ - ", called from %p" + ", %p" //" %p" #endif , ++mem_event, function, file, line #ifdef __GNUC__ - , __builtin_return_address(1) + , __builtin_return_address(1) //, __builtin_return_address(2) #endif ); - ht_insert_table(mem_ht, p, sizeof(result), s, mem_error); + ht_insert_table(mem_ht, p, sizeof(ptr), mem_str, mem_error); mem_internal = 0; -#endif /* MEM_DEBUG */ } - return result; + return ptr; } -void *renew (void *mem, size_t size, const char *file, const char *function, int line) -{ - void *result; - - if (!mem_internal) { - if (!mem) - mem_count++; -#ifdef MEM_DEBUG - else { - mem_internal = 1; - if (!(mem_ht && ht_remove_table(mem_ht, &mem, sizeof(mem), mem_noop))) { - fprintf(stderr, "*** ERROR: Trying to re-allocate non-allocated memory\n"); - fprintf(stderr, "%p %s (%s, %d)" +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" + ", called from %p" #endif - "\n", mem, function, file, line + "\n", ptr, function, file, line #ifdef __GNUC__ - , __builtin_return_address(1) + , __builtin_return_address(1) #endif - ); - exit(1); - } - mem_internal = 0; + ); } -#endif /* MEM_DEBUG */ + mem_internal = 0; } - if ((result = realloc (mem, size)) == NULL) { - fprintf (stderr, "Out of memory!\n"); - exit (1); - } + return ptr; +} -#ifdef MEM_DEBUG - if (!mem_internal) { - mem_internal = 1; - char *s = malloc(MEM_LINE), **p = malloc(sizeof(result)); - *p = result; - snprintf(s, MEM_LINE, "(0x%08lx) %s (%s, %d)" -#ifdef __GNUC__ - ", called from %p" -#endif - , ++mem_event, function, file, line -#ifdef __GNUC__ - , __builtin_return_address(1) -#endif - ); - ht_insert_table(mem_ht, p, sizeof(result), s, mem_error); - mem_internal = 0; - } -#endif /* MEM_DEBUG */ - return result; +#else /* ! MEM_DEBUG */ + +static long int mem_count; + +void mem_debug_init(void) +{ + mem_count = 0; } -void release (void *mem, const char *file, const char *function, int line) +void mem_debug_check(void) { + 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; +} - if (mem && !mem_internal) { - mem_count--; -#ifdef MEM_DEBUG - mem_internal = 1; - if (!(mem_ht && ht_remove_table(mem_ht, &mem, sizeof(mem), mem_noop))) { - fprintf(stderr, "*** ERROR: Trying to free non-allocated memory\n"); - fprintf(stderr, "%p %s (%s, %d)" -#ifdef __GNUC__ - ", called from %p" -#endif - "\n", mem, function, file, line -#ifdef __GNUC__ - , __builtin_return_address(1) -#endif - ); - exit(1); - } - mem_internal = 0; #endif /* MEM_DEBUG */ + + +void *new (size_t size) +{ + void *result = malloc (size); + if (!result) { + ERROR("Out of memory - asked for %lu bytes\n", (unsigned long) size); + } + + return result; +} + +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); } - free (mem); + return result; } |