GENESISAPI void* _geRam_DebugAllocate(uint32 size, const char* pFile, int line) { char *p; do { p = (char*)_malloc_dbg (size + EXTRA_SIZE, _NORMAL_BLOCK, pFile, line); } while ((p == NULL) && geRam_DoCriticalCallback ()); if (p == NULL) { return NULL; } // setup size stamps and memory overwrite checks geRam_SetupBlock (p, size, INITIALIZE_MEMORY); // and update the allocations stuff geRam_NumberOfAllocations++; geRam_CurrentlyUsed += size; if (geRam_NumberOfAllocations > geRam_MaximumNumberOfAllocations) { geRam_MaximumNumberOfAllocations = geRam_NumberOfAllocations; } if (geRam_CurrentlyUsed > geRam_MaximumUsed) { geRam_MaximumUsed = geRam_CurrentlyUsed; } return p+HEADER_SIZE; }
void* __cdecl operator new(size_t nSize) { void* pResult; #ifdef _AFXDLL _PNH pfnNewHandler = _pfnUninitialized; #endif for (;;) { #if !defined(_AFX_NO_DEBUG_CRT) && defined(_DEBUG) pResult = _malloc_dbg(nSize, _NORMAL_BLOCK, NULL, 0); #else pResult = malloc(nSize); #endif if (pResult != NULL) return pResult; #ifdef _AFXDLL if (pfnNewHandler == _pfnUninitialized) { AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState(); pfnNewHandler = pState->m_pfnNewHandler; } if (pfnNewHandler == NULL || (*pfnNewHandler)(nSize) == 0) break; #else if (_afxNewHandler == NULL || (*_afxNewHandler)(nSize) == 0) break; #endif } return pResult; }
char* NXwcsdupc_dbg(const wchar_t* str, const char* file, int line) { size_t len = wcslen(str); char* r = (char*)_malloc_dbg(sizeof(char) * (len + 1), _NORMAL_BLOCK, file, line); wcstombs_s(&len, r, len + 1, str, len); // ERR return r; }
char * __cdecl _strdup ( const char * string ) #endif /* _DEBUG */ { char *memory; size_t size = 0; if (!string) return(NULL); size = strlen(string) + 1; #ifdef _DEBUG if (memory = _malloc_dbg(size, nBlockUse, szFileName, nLine)) #else /* _DEBUG */ if (memory = malloc(size)) #endif /* _DEBUG */ { _ERRCHECK(strcpy_s(memory, size, string)); return memory; } return(NULL); }
void * operator new(size_t _Size) { // replace operator new void * _Ptr = _malloc_dbg(_Size > 0 ? _Size : 1, _CRT_BLOCK, __FILE__, __LINE__); if (!_Ptr) _Xbad_alloc(); return (_Ptr); }
void *_vm_malloc( int size, int quiet ) #endif { void *ptr = NULL; ptr = _malloc_dbg(size, _NORMAL_BLOCK, __FILE__, __LINE__ ); if (ptr == NULL) { mprintf(( "Malloc failed!!!!!!!!!!!!!!!!!!!\n" )); if (quiet) { return NULL; } Error(LOCATION, "Malloc Failed!\n"); } #ifndef NDEBUG TotalRam += size; if(Cmdline_show_mem_usage) register_malloc(size, filename, line, ptr); #endif return ptr; }
/** Allocate a sized block of memory. @param size The size of the memory block in bytes. @param flags Failure action modifiers (bitmasks). @return A pointer to the allocated memory block, or NULL on failure. */ static void *my_raw_malloc(size_t size, myf my_flags) { void* point; DBUG_ENTER("my_raw_malloc"); DBUG_PRINT("my",("size: %lu my_flags: %d", (ulong) size, my_flags)); /* Safety */ if (!size) size=1; #if defined(MY_MSCRT_DEBUG) if (my_flags & MY_ZEROFILL) point= _calloc_dbg(size, 1, _CLIENT_BLOCK, __FILE__, __LINE__); else point= _malloc_dbg(size, _CLIENT_BLOCK, __FILE__, __LINE__); #else if (my_flags & MY_ZEROFILL) point= calloc(size, 1); else point= malloc(size); #endif DBUG_EXECUTE_IF("simulate_out_of_memory", { free(point); point= NULL; });
void *xrealloc (void *ptr, size_t bytes) #endif { void *cp; MALLOC_CHECK(); #ifdef _DEBUG if (!ptr) cp = _malloc_dbg(bytes?bytes:1,_NORMAL_BLOCK,file,line); else cp = _realloc_dbg(ptr, bytes,_NORMAL_BLOCK,file,line); #else if (!ptr) cp = malloc (bytes?bytes:1); else cp = realloc(ptr, bytes); #endif if (cp == NULL) { char buf[80]; sprintf (buf, "out of memory; can not reallocate %lu bytes", (unsigned long) bytes); error (1, 0, buf); } MALLOC_CHECK(); return (cp); }
void *xmalloc (size_t bytes) #endif { void *cp; /* Parts of CVS try to xmalloc zero bytes and then free it. Some systems have a malloc which returns NULL for zero byte allocations but a free which can't handle NULL, so compensate. */ if (bytes == 0) bytes = 1; MALLOC_CHECK(); #ifdef _DEBUG cp = _malloc_dbg(bytes,_NORMAL_BLOCK,file,line); #else cp = malloc (bytes); #endif if (cp == NULL) { char buf[80]; sprintf (buf, "out of memory; can not allocate %lu bytes", (unsigned long) bytes); error (1, 0, buf); } MALLOC_CHECK(); return (cp); }
wchar_t* NXstrdupw_dbg(const char* str, const char* file, int line) { size_t len = strlen(str); wchar_t* r = (wchar_t*)_malloc_dbg(sizeof(wchar_t) * (len + 1), _NORMAL_BLOCK, file, line); mbstowcs_s(&len, r, len + 1, str, len); // ERR return r; }
void * __cdecl operator new(unsigned int size,const char *file,int line) { void *p; p = _malloc_dbg(size,_NORMAL_BLOCK,file,line); MemoryUsedCount+=_msize(p); return p; }
PTR MemAllocReal( UINT32 uiSize, const STR8 pcFile, INT32 iLine ) { PTR ptr; if( !uiSize ) { return NULL; } if ( !fMemManagerInit ) DbgMessage( TOPIC_MEMORY_MANAGER, DBG_LEVEL_0, String("MemAlloc: Warning -- Memory manager not initialized -- Line %d in %s", iLine, pcFile) ); ptr = _malloc_dbg( uiSize, _NORMAL_BLOCK, pcFile, iLine ); if (ptr != NULL) { guiMemTotal += uiSize; guiMemAlloced += uiSize; MemDebugCounter++; } else { DbgMessage( TOPIC_MEMORY_MANAGER, DBG_LEVEL_0, String("MemAlloc failed: %d bytes (line %d file %s)", uiSize, iLine, pcFile) ); } #ifdef DEBUG_MEM_LEAKS DbgMessage( TOPIC_MEMORY_MANAGER, DBG_LEVEL_1, String("MemAlloc %p: %d bytes (line %d file %s)", ptr, uiSize, iLine, pcFile) ); #endif return( ptr ); }
void* AllocMemory(size_t size) { #if (defined(_WIN32) || defined(_WIN64)) && !defined(NDEBUG) return _malloc_dbg(size, _NORMAL_BLOCK, __FILE__, __LINE__); #else return malloc(size); #endif }
void* __cdecl operator new(size_t nSize, int nType, LPCSTR lpszFileName, int nLine) { #ifdef _AFX_NO_DEBUG_CRT UNUSED_ALWAYS(nType); UNUSED_ALWAYS(lpszFileName); UNUSED_ALWAYS(nLine); return ::operator new(nSize); #else void* pResult; #ifdef _AFXDLL _PNH pfnNewHandler = _pfnUninitialized; #endif for (;;) { pResult = _malloc_dbg(nSize, nType, lpszFileName, nLine); if (pResult != NULL) return pResult; #ifdef _AFXDLL if (pfnNewHandler == _pfnUninitialized) { AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState(); pfnNewHandler = pState->m_pfnNewHandler; } if (pfnNewHandler == NULL || (*pfnNewHandler)(nSize) == 0) break; #else if (_afxNewHandler == NULL || (*_afxNewHandler)(nSize) == 0) break; #endif } return pResult; #endif }
void* allocMemoryCheck(size_t memSize, const char *fileName, unsigned int line) { SingleLocker locker(getMemoryCheckSyncObject()); void* p = _malloc_dbg(memSize, SCAN_MEMORY_BLOCK, fileName, line); assert(p != NULL); return p; }
/* * Standard memory functions */ void* BioAPI _BioAPI_malloc(uint32 size, void* ref, const char * szFilename, uint32 u32LineNumber) { #ifdef _DEBUG return _malloc_dbg( size, _NORMAL_BLOCK, szFilename, u32LineNumber ); #else return malloc( size ); #endif }
void* DefaultAllocator::AllocDebug(size_t size, const char* file, unsigned line) { OVR_UNUSED2(file, line); // should be here for debugopt config #if defined(OVR_CC_MSVC) && defined(_CRTDBG_MAP_ALLOC) return _malloc_dbg(size, _NORMAL_BLOCK, file, line); #else return malloc(size); #endif }
void* DefaultAllocator::AllocDebug(UPInt size, const char* file, unsigned line) { #if defined(OVR_CC_MSVC) && defined(_CRTDBG_MAP_ALLOC) return _malloc_dbg(size, _NORMAL_BLOCK, file, line); #else OVR_UNUSED2(file, line); return malloc(size); #endif }
void *M_Malloc_Dbg(size_t size, const char *file, int lineno) { void *block = _malloc_dbg(size, _NORMAL_BLOCK, file, lineno); if (block == NULL) I_FatalError("Could not malloc %zu bytes", size); GC::AllocBytes += _msize(block); return block; }
void* VMemoryManager_CRT::Alloc(size_t iSize) { #if defined(VBASE_USE_CRT_DEBUG) return _malloc_dbg(iSize, _NORMAL_BLOCK, 0, 0); #elif defined(_VISION_WIIU) return MEMAllocFromDefaultHeap(iSize); #else return malloc(iSize); #endif }
void allocNew(bool bFree) { int* leaked_memory = (int*)malloc(78); int* leaked_memory_dbg = (int*)_malloc_dbg(80, _NORMAL_BLOCK, __FILE__, __LINE__); if (bFree) { free(leaked_memory); _free_dbg(leaked_memory_dbg, _NORMAL_BLOCK); } }
void *_vm_malloc(size_t size, const memory::quiet_alloc_t &, const char *filename, int line) { auto ptr = _malloc_dbg(size, _NORMAL_BLOCK, filename, line); if (ptr == nullptr) { mprintf(("Malloc failed!!!!!!!!!!!!!!!!!!!\n")); } return ptr; }
void CMemLeakDetector::TrackAllocInfo(uintx p_AllocID, uintx p_Size) { AllocInfo& ainfo = m_AllocInfoMap[p_AllocID]; #if defined(PLATFORM_UNIX) ainfo.m_Size = p_Size; #endif ainfo.m_CallstackSize = CDebugHelp::DoStackWalk(ainfo.m_Callstack, AllocInfo::MaxCallstackDepth); #if defined(PLATFORM_WINDOWS) // determine the names of all modules referenced in our callstack to load the symbol information // later on when reporting a leak for(uintx i = 0; i < ainfo.m_CallstackSize; ++i) { ainfo.m_ModuleReference[i] = -1; if(m_CacheModuleInfo) { // determine the module handle for the current callstack address MEMORY_BASIC_INFORMATION mbi; VirtualQuery(reinterpret_cast<LPCVOID>(static_cast<size_t>(ainfo.m_Callstack[i])), &mbi, sizeof(mbi)); HMODULE hModule = static_cast<HMODULE>(mbi.AllocationBase); // check if we already have that module in our vector for(uintx curModInfo = 0; curModInfo < m_ModuleInfoVector.size(); ++curModInfo) { if(m_ModuleInfoVector[curModInfo].m_hModule == hModule) ainfo.m_ModuleReference[i] = curModInfo; } // if we haven't got that module - determine it's name if(ainfo.m_ModuleReference[i] == -1) { TCHAR moduleName[MAX_PATH]; DWORD moduleNameSize = MAX_PATH; if((moduleNameSize = GetModuleFileName(hModule, moduleName, moduleNameSize)) != 0) { ModuleInfo modInfo; modInfo.m_hModule = hModule; modInfo.m_ModuleName = static_cast<tchar*>(_malloc_dbg(++moduleNameSize * sizeof(tchar), _CRT_BLOCK, __FILE__, __LINE__)); _tcscpy_s(modInfo.m_ModuleName, moduleNameSize, moduleName); m_ModuleInfoVector.push_back(modInfo); ainfo.m_ModuleReference[i] = static_cast<intx>(m_ModuleInfoVector.size() - 1); } } } } #else // set all module refs to -1 for(uintx i = 0; i < ainfo.m_CallstackSize; ++i) ainfo.m_ModuleReference[i] = -1; #endif }
void* utAlloc(size_t size, const char* file, int line) { my_allocCount++; if (my_allocHandler) return my_allocHandler(size, file, line); #if defined(CRTDBG_MAP_ALLOC) return _malloc_dbg(size, _NORMAL_BLOCK, file, line); #else return malloc(size); #endif }
static void *MallocCHK(size_t size) #endif { unsigned long *p; unsigned char *tmp; #if defined BLAK_PLATFORM_WINDOWS && !defined NDEBUG tmp = (unsigned char*)_malloc_dbg( size + (sizeof(unsigned long)*3), _NORMAL_BLOCK,filename,linenumber ); #else tmp = (unsigned char *) malloc(size); #endif if( tmp == NULL) { return NULL ; } p = (unsigned long *) tmp; #ifdef MDEBUG printf("Size Req: %d Size Got: %d\n",size,(size + (sizeof(unsigned long)*3) )); printf("Block at: 0x%lx\n",p); #endif /* store actual size of allocated block */ p[0] = size ; /* store our check token */ p[1] = MCHK_START ; p = (unsigned long*) (tmp + ( size + (sizeof(unsigned long)*2) )); p[0] = MCHK_END ; tmp += 8; #ifdef MDEBUG printf("End: 0x%lx \n",p); printf("Actual: 0x%lx \n",tmp); #endif /* Charlie: Boundschecker 6.604 will complain about memory being leaked from this scope, its half right, if theres two messages about it, then it really is being leaked */ return tmp ; }
//----------------------------------------------------------------------------- // Debug versions of the main allocation methods //----------------------------------------------------------------------------- void *CDbgMemAlloc::Alloc( size_t nSize, const char *pFileName, int nLine ) { // GetActualDbgInfo( pFileName, nLine ); // m_Timer.Start(); void *pMem = _malloc_dbg( nSize, _NORMAL_BLOCK, pFileName, nLine ); // m_Timer.End(); // unsigned long nTime = m_Timer.GetDuration().GetMicroseconds(); // RegisterAllocation( m_GlobalInfo, nSize, nTime ); // RegisterAllocation( FindOrCreateEntry( pFileName, nLine ), nSize, nTime ); return pMem; }
void * debug_emalloc( u_int size, char *filename, int line ) { char *mem; if ((mem = (char *)_malloc_dbg(size, _NORMAL_BLOCK, filename, line)) == 0) { msyslog(LOG_ERR, "Exiting: No more memory!"); exit(1); } return mem; }
void *operator new( size_t nSize, const char *lpszFileName, int nLine ) { // Allocate the data. void *pData = _malloc_dbg( nSize, _CLIENT_BLOCK, lpszFileName, nLine ); // Failed? if ( pData == NULL ) { // Call the new handler which, in the case // of this library, will throw an exception. _PNH nh = _set_new_handler( 0 ); ( *nh )( nSize ); _set_new_handler( nh ); } // Return pointer or NULL. return pData; }
//============================================================================= void * MemAllocHelper (size_t bytes, const char file[], int line) { #ifdef USE_MALLOC if (void * result = _malloc_dbg(bytes, _NORMAL_BLOCK, file, line)) return result; #else REF(file); REF(line); if (!s_heap) s_heap = GetProcessHeap(); if (void * result = HeapAlloc(s_heap, 0, bytes)) return result; #endif OutOfMemory(); return NULL; }
void* operator new[]( size_t size, int memType, const char* filename, int lineNum ) { // We have to do this old-school since we're not allowed to dynamically allocate memory here. char buffer[2048]; int index = 0; index += strlen( ultoa( size, buffer, 10 ) ); strcpy( buffer + index, " -> " ); index += 4; strcpy( buffer + index, filename ); index += strlen( filename ); buffer[index] = ':'; ++index; index += strlen( itoa( lineNum, buffer + index, 10 ) ); buffer[index] = '\n'; ++index; buffer[index] = '\0'; ++index; OutputDebugStringA( buffer ); return _malloc_dbg( size, 1, filename, lineNum ); }