From eb946f76a4ee8e485f7e0ecb558184de8cdf1a8b Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Sat, 21 Jan 2012 16:02:20 -0800 Subject: [PATCH] Get rid of MemAlloc. --- Sources/Plasma/CoreLib/hsMalloc.h | 61 +------- Sources/Plasma/CoreLibExe/hsExeMalloc.cpp | 138 +----------------- .../NucleusLib/pnUtils/Private/pnUtList.h | 6 +- .../pnUtils/Private/pnUtSpareList.cpp | 7 +- .../PubUtilLib/plPhysX/plSimulationMgr.cpp | 2 +- 5 files changed, 7 insertions(+), 207 deletions(-) diff --git a/Sources/Plasma/CoreLib/hsMalloc.h b/Sources/Plasma/CoreLib/hsMalloc.h index b907c303..8f4436b5 100644 --- a/Sources/Plasma/CoreLib/hsMalloc.h +++ b/Sources/Plasma/CoreLib/hsMalloc.h @@ -62,16 +62,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com extern "C" { #endif -// MemAlloc flags -extern const unsigned kMemReallocInPlaceOnly; // use _expand when realloc'ing -extern const unsigned kMemZero; // fill allocated memory with zeros -extern const unsigned kMemIgnoreBlock; // don't track this allocation - - -void * MemAlloc (unsigned bytes, unsigned flags, const char file[], int line); void * MemDup (const void * ptr, unsigned bytes, unsigned flags, const char file[], int line); -void MemFree (void * ptr, unsigned flags); -void * MemRealloc (void * ptr, unsigned bytes, unsigned flags, const char file[], int line); unsigned MemSize (void * ptr); @@ -106,55 +97,6 @@ void MemSetColor (unsigned short color); #endif -/**************************************************************************** -* -* C++ Operators -* -***/ - -#ifdef __cplusplus - -#include - -#ifndef _MSC_VER -#define THROW(x) throw(x) -#define THROW_EMPTY throw() -#else -#define THROW(x) -#define THROW_EMPTY -#endif - -// standard new and delete -inline void* CDECL operator new (size_t bytes) THROW(std::bad_alloc) - { return MemAlloc((unsigned)bytes, 0, __FILE__, __LINE__); } -inline void* CDECL operator new [](size_t bytes) THROW(std::bad_alloc) - { return MemAlloc((unsigned)bytes, 0, __FILE__, __LINE__); } -inline void CDECL operator delete (void * ptr) THROW_EMPTY - { MemFree(ptr, 0); } -inline void CDECL operator delete [](void * ptr) THROW_EMPTY - { MemFree(ptr, 0); } - -// memcheck-friendly new -inline void* CDECL operator new (size_t bytes, const char file[], unsigned line) - { return MemAlloc((unsigned)bytes, 0, file, line); } -inline void* CDECL operator new [](size_t bytes, const char file[], unsigned line) - { return MemAlloc((unsigned)bytes, 0, file, line); } -inline void CDECL operator delete (void * ptr, const char [], unsigned) - { return MemFree(ptr, 0); } -inline void CDECL operator delete [](void * ptr, const char [], unsigned) - { return MemFree(ptr, 0); } - - -// placement new -#if defined(_MSC_VER) && !defined(__PLACEMENT_NEW_INLINE) -#define __PLACEMENT_NEW_INLINE -inline void* CDECL operator new (size_t, void * ptr) { return ptr; } -inline void CDECL operator delete (void *, void *) {} -#endif // ifndef __PLACEMENT_NEW_INLINE - -#endif // ifdef __cplusplus - - /**************************************************************************** * * Macros @@ -163,7 +105,8 @@ inline void CDECL operator delete (void *, void *) {} #ifdef __cplusplus -#define NEWZERO(t) new(MemAlloc(sizeof(t), kMemZero, __FILE__, __LINE__)) t +#include +#define NEWZERO(t) new(calloc(sizeof(t), 1)) t #endif // __cplusplus diff --git a/Sources/Plasma/CoreLibExe/hsExeMalloc.cpp b/Sources/Plasma/CoreLibExe/hsExeMalloc.cpp index 3a3483a3..c1aefbe3 100644 --- a/Sources/Plasma/CoreLibExe/hsExeMalloc.cpp +++ b/Sources/Plasma/CoreLibExe/hsExeMalloc.cpp @@ -452,143 +452,7 @@ void MemSetColor (unsigned short color) { #endif // MEM_DEBUG } -//=========================================================================== -void * MemAlloc (unsigned bytes, unsigned flags, const char file[], int line) { - -#ifdef MEM_DEBUG - unsigned block; - if (flags & kMemIgnoreBlock || s_memCheckOff) - block = _IGNORE_BLOCK; - else - block = _CLIENT_BLOCK | (s_memColor << 16); -#endif // MEM_DEBUG - -#ifdef MEM_DEBUG - if (s_critsect) - s_critsect->Enter(); - if (block == _IGNORE_BLOCK) - CLEAR_CRT_DEBUG_FIELD(_CRTDBG_ALLOC_MEM_DF); -#endif - - void * ptr = (flags & kMemZero) - ? _calloc_dbg(bytes, 1, block, file, line) - : _malloc_dbg(bytes, block, file, line); - -#ifdef MEM_DEBUG - if (block == _IGNORE_BLOCK) - SET_CRT_DEBUG_FIELD(_CRTDBG_ALLOC_MEM_DF); - if (s_critsect) - s_critsect->Leave(); -#endif - - if (!ptr) - ErrorFatal(__LINE__, __FILE__, "Out of memory"); - - // In debug mode ensure that memory is initialized to some freaky value - #ifdef HS_DEBUGGING - if (! (flags & kMemZero)) - memset(ptr, (uint8_t) ((uintptr_t)ptr >> 4), bytes); - #endif - -#ifdef _MSC_VER - // Compiler specific: - // Adding this line causes MSVC to stop assuming that memory allocation - // can fail thus producing more efficient assembler code. - __assume(ptr); -#endif - - // return the allocated buffer - return ptr; -} - //============================================================================ -void MemFree (void * ptr, unsigned flags) { - if (!ptr) - return; - -#ifdef MEM_DEBUG - const _CrtMemBlockHeader * pHead = pHdr(ptr); - unsigned block = pHead->nBlockUse; -#endif // MEM_DEBUG - - _free_dbg(ptr, block); -} - -//=========================================================================== -void * MemRealloc (void * ptr, unsigned bytes, unsigned flags, const char file[], int line) { - #ifdef HS_DEBUGGING - unsigned oldBytes = ptr ? MemSize(ptr) : 0; - #endif - -#ifdef MEM_DEBUG - unsigned block; - if (flags & kMemIgnoreBlock || s_memCheckOff) - block = _IGNORE_BLOCK; - else - block = _CLIENT_BLOCK | (s_memColor << 16); -#endif - - void * newPtr = nil; - -#ifdef MEM_DEBUG - if (s_critsect) - s_critsect->Enter(); - if (block == _IGNORE_BLOCK) - CLEAR_CRT_DEBUG_FIELD(_CRTDBG_ALLOC_MEM_DF); -#endif - - for (;;) { - if (flags & kMemReallocInPlaceOnly) { -#ifndef MEM_DEBUG - break; -#else - newPtr = _expand_dbg(ptr, bytes, block, file, line); - - // expand can succeed without making the block big enough -- check for this case! - if (!newPtr || _msize_dbg(newPtr, block) < bytes) - break; -#endif // MEM_DEBUG - } - else if (!bytes) { - newPtr = _malloc_dbg(0, block, file, line); - _free_dbg(ptr, block); - } - else { - newPtr = _realloc_dbg(ptr, bytes, block, file, line); - } - - if (!newPtr) - ErrorFatal(__LINE__, __FILE__, "Out of memory"); - - break; - } - -#ifdef MEM_DEBUG - if (block == _IGNORE_BLOCK) - SET_CRT_DEBUG_FIELD(_CRTDBG_ALLOC_MEM_DF); - if (s_critsect) - s_critsect->Leave(); -#endif - - /* This code doesn't work because the memory manager may have "rounded" the size - * of a previous allocation upward to keep it aligned. Therefore, the tail of - * the memory block may be initialized with garbage instead of zeroes, and the - * realloc call actually copied that memory. - if ((bytes > oldBytes) && (flags & kMemZero)) - MemZero((uint8_t *)newPtr + oldBytes, bytes - oldBytes); - */ - ASSERT(!(flags & kMemZero)); - - // In debug mode ensure that memory is initialized to some freaky value - #ifdef HS_DEBUGGING - if ((bytes > oldBytes) && !(flags & kMemZero)) - MemSet((uint8_t *)newPtr + oldBytes, (uint8_t) ((uintptr_t) newPtr >> 4), bytes - oldBytes); - #endif - - return newPtr; -} - -//=========================================================================== unsigned MemSize (void * ptr) { ASSERT(ptr); unsigned result = 0; @@ -622,7 +486,7 @@ void MemMove (void * dest, const void * source, unsigned bytes) { //=========================================================================== void * MemDup (const void * ptr, unsigned bytes, unsigned flags, const char file[], int line) { - void * dst = MemAlloc(bytes, flags, file, line); + void * dst = malloc(bytes); MemCopy(dst, ptr, bytes); return dst; } diff --git a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtList.h b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtList.h index 337585a8..2abbbf8d 100644 --- a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtList.h +++ b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtList.h @@ -557,11 +557,7 @@ inline T * TList::New (ELinkType linkType, T * existingNode, const char file[ //=========================================================================== template inline T * TList::NewFlags (unsigned flags, ELinkType linkType, T * existingNode, const char file[], int line) { - if (!file) { - file = __FILE__; - line = __LINE__; - } - T * node = new(MemAlloc(sizeof(T), flags, file, line)) T; + T * node = new T(); if (linkType != kListUnlinked) Link(node, linkType, existingNode); return node; diff --git a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtSpareList.cpp b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtSpareList.cpp index 61cd933d..48908f6b 100644 --- a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtSpareList.cpp +++ b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtSpareList.cpp @@ -116,11 +116,8 @@ void CBaseSpareList::GrowSpareList (unsigned objectSize, const char typeName[]) // allocate a block of memory to hold a bunch // of T-objects, but allocate them as "raw" memory - AllocNode * allocNode = (AllocNode *) MemAlloc( - sizeof(AllocNode) + objectSize * m_chunkSize, - 0, // flags - typeName, // file - 0 // line + AllocNode * allocNode = (AllocNode *) malloc( + sizeof(AllocNode) + objectSize * m_chunkSize ); // link allocation onto head of allocation list diff --git a/Sources/Plasma/PubUtilLib/plPhysX/plSimulationMgr.cpp b/Sources/Plasma/PubUtilLib/plPhysX/plSimulationMgr.cpp index 7f1970b3..88d1b163 100644 --- a/Sources/Plasma/PubUtilLib/plPhysX/plSimulationMgr.cpp +++ b/Sources/Plasma/PubUtilLib/plPhysX/plSimulationMgr.cpp @@ -300,7 +300,7 @@ public: return malloc(size); } void * mallocDEBUG (NxU32 size, const char * fileName, int line) { - return MemAlloc(size, 0, fileName, line); + return malloc(size); } void * realloc (void * memory, NxU32 size) { return realloc(memory, size);