diff options
Diffstat (limited to 'Build/source/libs/xpdf/xpdf-src/goo/gmem.cc')
-rw-r--r-- | Build/source/libs/xpdf/xpdf-src/goo/gmem.cc | 67 |
1 files changed, 58 insertions, 9 deletions
diff --git a/Build/source/libs/xpdf/xpdf-src/goo/gmem.cc b/Build/source/libs/xpdf/xpdf-src/goo/gmem.cc index 6fd51364071..c28d3e07fd6 100644 --- a/Build/source/libs/xpdf/xpdf-src/goo/gmem.cc +++ b/Build/source/libs/xpdf/xpdf-src/goo/gmem.cc @@ -12,6 +12,9 @@ #include <stddef.h> #include <string.h> #include <limits.h> +#if MULTITHREADED && defined(_WIN32) +# include <windows.h> +#endif #include "gmem.h" #ifdef DEBUG_MEM @@ -28,7 +31,7 @@ typedef struct _GMemHdr { #define gMemMagic 0xabcd9999 -#if gmemTrlSize==8 +#if ULONG_MAX > 0xffffffffL #define gMemDeadVal 0xdeadbeefdeadbeefUL #else #define gMemDeadVal 0xdeadbeefUL @@ -46,16 +49,43 @@ static int gMemAlloc = 0; static int gMemInUse = 0; static int gMaxMemInUse = 0; +#if MULTITHREADED +# ifdef _WIN32 + static CRITICAL_SECTION gMemMutex; + static INIT_ONCE gMemMutexInitStruct = INIT_ONCE_STATIC_INIT; + static BOOL CALLBACK gMemMutexInitFunc(PINIT_ONCE initOnce, PVOID param, + PVOID *context) { + InitializeCriticalSection(&gMemMutex); + return TRUE; + } +# define gMemInitMutex InitOnceExecuteOnce(&gMemMutexInitStruct, \ + &gMemMutexInitFunc, NULL, NULL) +# define gMemLock EnterCriticalSection(&gMemMutex); +# define gMemUnlock LeaveCriticalSection(&gMemMutex); +# else +# include <pthread.h> + static pthread_mutex_t gMemMutex = PTHREAD_MUTEX_INITIALIZER; +# define gMemInitMutex +# define gMemLock pthread_mutex_lock(&gMemMutex) +# define gMemUnlock pthread_mutex_unlock(&gMemMutex) +# endif +#else +# define gMemInitMutex +# define gMemLock +# define gMemUnlock +#endif + #endif /* DEBUG_MEM */ -void *gmalloc(int size) GMEM_EXCEP { #ifdef DEBUG_MEM +void *gmalloc(int size, int ignore) GMEM_EXCEP { int size1; char *mem; GMemHdr *hdr; void *data; unsigned long *trl, *p; + gMemInitMutex; if (size < 0) { gMemError("Invalid memory allocation size"); } @@ -71,7 +101,12 @@ void *gmalloc(int size) GMEM_EXCEP { trl = (unsigned long *)(mem + gMemHdrSize + size1); hdr->magic = gMemMagic; hdr->size = size; - hdr->index = gMemIndex++; + if (ignore) { + hdr->index = -1; + } else { + hdr->index = gMemIndex++; + } + gMemLock; if (gMemTail) { gMemTail->next = hdr; hdr->prev = gMemTail; @@ -86,11 +121,14 @@ void *gmalloc(int size) GMEM_EXCEP { if (gMemInUse > gMaxMemInUse) { gMaxMemInUse = gMemInUse; } + gMemUnlock; for (p = (unsigned long *)data; p <= trl; ++p) { *p = gMemDeadVal; } return data; +} #else +void *gmalloc(int size) GMEM_EXCEP { void *p; if (size < 0) { @@ -103,8 +141,8 @@ void *gmalloc(int size) GMEM_EXCEP { gMemError("Out of memory"); } return p; -#endif } +#endif void *grealloc(void *p, int size) GMEM_EXCEP { #ifdef DEBUG_MEM @@ -192,6 +230,7 @@ void gfree(void *p) { if (p) { hdr = (GMemHdr *)((char *)p - gMemHdrSize); + gMemLock; if (hdr->magic == gMemMagic && ((hdr->prev == NULL) == (hdr == gMemHead)) && ((hdr->next == NULL) == (hdr == gMemTail))) { @@ -207,6 +246,7 @@ void gfree(void *p) { } --gMemAlloc; gMemInUse -= hdr->size; + gMemUnlock; size = gMemDataSize(hdr->size); trl = (unsigned long *)((char *)hdr + gMemHdrSize + size); if (*trl != gMemDeadVal) { @@ -218,6 +258,7 @@ void gfree(void *p) { } free(hdr); } else { + gMemUnlock; fprintf(stderr, "Attempted to free bad address %p\n", p); } } @@ -240,17 +281,25 @@ void gMemError(const char *msg) GMEM_EXCEP { #ifdef DEBUG_MEM void gMemReport(FILE *f) { GMemHdr *p; + int left; fprintf(f, "%d memory allocations in all\n", gMemIndex); fprintf(f, "maximum memory in use: %d bytes\n", gMaxMemInUse); + left = 0; if (gMemAlloc > 0) { - fprintf(f, "%d memory blocks left allocated:\n", gMemAlloc); - fprintf(f, " index size\n"); - fprintf(f, "-------- --------\n"); for (p = gMemHead; p; p = p->next) { - fprintf(f, "%8d %8d\n", p->index, p->size); + if (p->index >= 0) { + if (!left) { + fprintf(f, "%d memory blocks left allocated:\n", gMemAlloc); + fprintf(f, " index size\n"); + fprintf(f, "-------- --------\n"); + left = 1; + } + fprintf(f, "%8d %8d\n", p->index, p->size); + } } - } else { + } + if (!left) { fprintf(f, "No memory blocks left allocated\n"); } } |