Browse Source

Get rid of MemAlloc.

Darryl Pogue 13 years ago committed by Adam Johnson
parent
commit
eb946f76a4
  1. 61
      Sources/Plasma/CoreLib/hsMalloc.h
  2. 138
      Sources/Plasma/CoreLibExe/hsExeMalloc.cpp
  3. 6
      Sources/Plasma/NucleusLib/pnUtils/Private/pnUtList.h
  4. 7
      Sources/Plasma/NucleusLib/pnUtils/Private/pnUtSpareList.cpp
  5. 2
      Sources/Plasma/PubUtilLib/plPhysX/plSimulationMgr.cpp

61
Sources/Plasma/CoreLib/hsMalloc.h

@ -62,16 +62,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
extern "C" { extern "C" {
#endif #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 * 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); unsigned MemSize (void * ptr);
@ -106,55 +97,6 @@ void MemSetColor (unsigned short color);
#endif #endif
/****************************************************************************
*
* C++ Operators
*
***/
#ifdef __cplusplus
#include <new>
#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 * Macros
@ -163,7 +105,8 @@ inline void CDECL operator delete (void *, void *) {}
#ifdef __cplusplus #ifdef __cplusplus
#define NEWZERO(t) new(MemAlloc(sizeof(t), kMemZero, __FILE__, __LINE__)) t #include <new>
#define NEWZERO(t) new(calloc(sizeof(t), 1)) t
#endif // __cplusplus #endif // __cplusplus

138
Sources/Plasma/CoreLibExe/hsExeMalloc.cpp

@ -452,143 +452,7 @@ void MemSetColor (unsigned short color) {
#endif // MEM_DEBUG #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) { unsigned MemSize (void * ptr) {
ASSERT(ptr); ASSERT(ptr);
unsigned result = 0; 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 * 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); MemCopy(dst, ptr, bytes);
return dst; return dst;
} }

6
Sources/Plasma/NucleusLib/pnUtils/Private/pnUtList.h

@ -557,11 +557,7 @@ inline T * TList<T>::New (ELinkType linkType, T * existingNode, const char file[
//=========================================================================== //===========================================================================
template<class T> template<class T>
inline T * TList<T>::NewFlags (unsigned flags, ELinkType linkType, T * existingNode, const char file[], int line) { inline T * TList<T>::NewFlags (unsigned flags, ELinkType linkType, T * existingNode, const char file[], int line) {
if (!file) { T * node = new T();
file = __FILE__;
line = __LINE__;
}
T * node = new(MemAlloc(sizeof(T), flags, file, line)) T;
if (linkType != kListUnlinked) if (linkType != kListUnlinked)
Link(node, linkType, existingNode); Link(node, linkType, existingNode);
return node; return node;

7
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 // allocate a block of memory to hold a bunch
// of T-objects, but allocate them as "raw" memory // of T-objects, but allocate them as "raw" memory
AllocNode * allocNode = (AllocNode *) MemAlloc( AllocNode * allocNode = (AllocNode *) malloc(
sizeof(AllocNode) + objectSize * m_chunkSize, sizeof(AllocNode) + objectSize * m_chunkSize
0, // flags
typeName, // file
0 // line
); );
// link allocation onto head of allocation list // link allocation onto head of allocation list

2
Sources/Plasma/PubUtilLib/plPhysX/plSimulationMgr.cpp

@ -300,7 +300,7 @@ public:
return malloc(size); return malloc(size);
} }
void * mallocDEBUG (NxU32 size, const char * fileName, int line) { void * mallocDEBUG (NxU32 size, const char * fileName, int line) {
return MemAlloc(size, 0, fileName, line); return malloc(size);
} }
void * realloc (void * memory, NxU32 size) { void * realloc (void * memory, NxU32 size) {
return realloc(memory, size); return realloc(memory, size);

Loading…
Cancel
Save