diff --git a/src/z_zone.c b/src/z_zone.c index 0130b3b38..c8ad29a7c 100644 --- a/src/z_zone.c +++ b/src/z_zone.c @@ -29,6 +29,7 @@ #include "doomstat.h" #include "r_patch.h" #include "i_system.h" // I_GetFreeMem +#include "i_threads.h" #include "i_video.h" // rendermode #include "z_zone.h" #include "m_misc.h" // M_Memcpy @@ -50,6 +51,16 @@ static boolean Z_calloc = false; //#define ZDEBUG2 #endif +#ifdef HAVE_THREADS +static I_mutex z_mutex; + +# define Lock_state() I_lock_mutex(&z_mutex) +# define Unlock_state() I_unlock_mutex(z_mutex) +#else/*HAVE_THREADS*/ +# define Lock_state() +# define Unlock_state() +#endif/*HAVE_THREADS*/ + typedef struct memblock_s { void **user; @@ -135,6 +146,8 @@ void Z_Free(void *ptr) CONS_Debug(DBG_MEMORY, "Z_Free %s:%d\n", file, line); #endif + Lock_state(); + block = MEMBLOCK(ptr); #ifdef PARANOIA if (block->id != ZONEID) @@ -167,6 +180,8 @@ void Z_Free(void *ptr) block->prev->next = block->next; block->next->prev = block->prev; free(block); + + Unlock_state(); } /** malloc() that doesn't accept failure. @@ -231,7 +246,9 @@ void *Z_MallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits) #ifdef HAVE_VALGRIND Z_calloc = false; -#endif +#endif + + Lock_state(); block->next = head.next; block->prev = &head; @@ -258,7 +275,9 @@ void *Z_MallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits) block->user = user; *(void **)user = ptr; } - + + Unlock_state(); + if (tag >= PU_PURGELEVEL) I_Error("Z_Malloc: attempted to allocate purgable block " "(size %s) with no user", sizeu1(size)); @@ -393,13 +412,18 @@ void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignb void Z_FreeTags(INT32 lowtag, INT32 hightag) { memblock_t *block, *next; + + Lock_state(); + Z_CheckHeap(420); for (block = head.next; block != &head; block = next) { next = block->next; // get link before freeing if (block->tag >= lowtag && block->tag <= hightag) Z_Free(MEMORY(block)); - } + } + + Unlock_state(); } /** Iterates through all memory for a given set of tags. @@ -627,6 +651,8 @@ size_t Z_TagsUsage(INT32 lowtag, INT32 hightag) { size_t cnt = 0; memblock_t *rover; + + Lock_state(); for (rover = head.next; rover != &head; rover = rover->next) { @@ -634,6 +660,8 @@ size_t Z_TagsUsage(INT32 lowtag, INT32 hightag) continue; cnt += rover->size + sizeof *rover; } + + Unlock_state(); return cnt; } @@ -648,6 +676,8 @@ size_t Z_TagsUsage(INT32 lowtag, INT32 hightag) static void Command_Memfree_f(void) { size_t freebytes, totalbytes; + + Lock_state(); Z_CheckHeap(-1); CONS_Printf("\x82%s", M_GetText("Memory Info\n")); @@ -678,6 +708,8 @@ static void Command_Memfree_f(void) freebytes = I_GetFreeMem(&totalbytes); CONS_Printf(M_GetText(" Total physical memory: %s KB\n"), sizeu1(totalbytes>>10)); CONS_Printf(M_GetText("Available physical memory: %s KB\n"), sizeu1(freebytes>>10)); + + Unlock_state(); } @@ -699,13 +731,17 @@ static void Command_Memdump_f(void) if ((i = COM_CheckParm("-max"))) maxtag = atoi(COM_Argv(i + 1)); + + Lock_state(); for (block = head.next; block != &head; block = block->next) - if (block->tag >= mintag && block->tag <= maxtag) - { - char *filename = strrchr(block->ownerfile, PATHSEP[0]); - CONS_Printf("[%3d] %s (%s) bytes @ %s:%d\n", block->tag, sizeu1(block->size), sizeu2(block->realsize), filename ? filename + 1 : block->ownerfile, block->ownerline); - } + if (block->tag >= mintag && block->tag <= maxtag) + { + char *filename = strrchr(block->ownerfile, PATHSEP[0]); + CONS_Printf("[%3d] %s (%s) bytes @ %s:%d\n", block->tag, sizeu1(block->size), sizeu2(block->realsize), filename ? filename + 1 : block->ownerfile, block->ownerline); + } + + Unlock_state(); } #endif