void* UserAllocator::malloc(size_t size, NxMemoryType type) { #ifdef _DEBUG return mallocDEBUG(size, NULL, 0, "Undefined", type); #endif if(size == 0) { printf("Warning: trying to allocate 0 bytes\n"); return NULL; } LockAlloc(); void* ptr = (void*)::malloc(size+2*sizeof(size_t)); mTotalNbAllocs++; mNbAllocs++; size_t* blockStart = (size_t*)ptr; blockStart[0] = g_pointerMarker; blockStart[1] = size; mNbAllocatedBytes+=size; if(mNbAllocatedBytes>mHighWaterMark) mHighWaterMark = mNbAllocatedBytes; UnlockAlloc(); return ((size_t*)ptr)+2; }
void __wrap_free( void *ptr ) { LockAlloc(); __real_free( ptr ); UnlockAlloc(); }
void UserAllocator::reset() { LockAlloc(); mNbAllocatedBytes = 0; mHighWaterMark = 0; mNbAllocs = 0; UnlockAlloc(); }
void UserAllocator::free(void* memory) { if(memory == NULL) { printf("Warning: trying to free null pointer\n"); return; } LockAlloc(); #ifdef _DEBUG size_t* ptr = ((size_t*)memory)-6; if(ptr[0] != g_pointerMarker) { printf("Error: free unknown memory!!\n"); } mNbAllocatedBytes -= ptr[1]; mNbAllocs--; const char* File = (const char*)ptr[2]; size_t Line = ptr[3]; size_t MemBlockFirstFree = ptr[4]; // Remove the block from the Memory block list if(mMemBlockList) { mMemBlockList[MemBlockFirstFree] = 0; mMemBlockUsed--; } #else size_t* ptr = ((size_t*)memory)-2; if(ptr[0] != g_pointerMarker) { printf("Error: free unknown memory!!\n"); } mNbAllocatedBytes -= ptr[1]; if(mNbAllocatedBytes < 0) { printf("Oops (%d)\n", ptr[1]); } mNbAllocs--; #endif ptr[0]=g_pointerMarker; ptr[1]=0; ::free(ptr); UnlockAlloc(); }
size_t GetMemoryFreeSize( bool fast ) { MemoryList *memTop = NULL, *memPrev = NULL; size_t freeSize = 0, bufSize = 20*1024*1024; size_t minSize = (fast ? 1024 : 2); LockAlloc(); while( 1 ) { MemoryList *memCurr = (MemoryList*)malloc( sizeof(MemoryList) ); if( memCurr == NULL ) break; memCurr->buf = malloc( bufSize ); if( memCurr->buf == NULL ) { free( memCurr ); freeSize += sizeof(MemoryList); if( bufSize <= minSize ) break; bufSize /= 2; continue; } freeSize += bufSize; if( memPrev == NULL ) memTop = memCurr; else memPrev->next = memCurr; memCurr->next = NULL; memPrev = memCurr; } MemoryList *memCurr = memTop; while( memCurr != NULL ) { MemoryList *memNext = memCurr->next; free( memCurr->buf ); free( memCurr ); memCurr = memNext; } UnlockAlloc(); return freeSize; }
void* __wrap_realloc( void *ptr, size_t size ) { LockAlloc(); void *new_ptr = __real_realloc( ptr, size ); if( new_ptr == NULL && size != 0 && IsSingleLocked() && g_bMemoryAssert ) { TempLog( "realloc failed: %p %ubytes\r\n", ptr, size ); MemoryError(); } UnlockAlloc(); return new_ptr; }
void* __wrap_malloc( size_t size ) { LockAlloc(); void *ptr = __real_malloc( size ); if( ptr == NULL && size != 0 && IsSingleLocked() && g_bMemoryAssert ) { TempLog( "malloc failed: %ubytes\r\n", size ); MemoryError(); } UnlockAlloc(); return ptr; }
void* __wrap_memalign( size_t align, size_t size ) { LockAlloc(); void *ptr = __real_memalign( align, size ); if( ptr == NULL && size > 0 && IsSingleLocked() && g_bMemoryAssert ) { TempLog( "memalign failed: %ubytes(aligned %u)\r\n", size, align ); MemoryError(); } UnlockAlloc(); return ptr; }
void* UserAllocator::realloc(void* memory, size_t size) { // return ::realloc(memory, size); if(memory == NULL) { printf("Warning: trying to realloc null pointer\n"); return NULL; } if(size == 0) { printf("Warning: trying to realloc 0 bytes\n"); } #ifdef _DEBUG LockAlloc(); size_t* ptr = ((size_t*)memory)-6; if(ptr[0]!=g_pointerMarker) { printf("Error: realloc unknown memory!!\n"); } mNbAllocatedBytes -= ptr[1]; mNbAllocatedBytes += size; if(mNbAllocatedBytes>mHighWaterMark) mHighWaterMark = mNbAllocatedBytes; //need to store this for removal later size_t oldMemBlockIndex = ptr[4]; void* ptr2 = ::realloc(ptr, size+6*sizeof(size_t)); mNbReallocs++; *(((size_t*)ptr2)+1) = size; if(ptr==ptr2) { UnlockAlloc(); return memory; } *(((size_t*)ptr2)) = g_pointerMarker; *(((size_t*)ptr2)+1) = size; *(((size_t*)ptr2)+2) = 0; // File *(((size_t*)ptr2)+3) = 0; // Line NxU32* blockStart = (NxU32*)ptr2; // Remove the old allocated block from the debug memory block list if(mMemBlockList) { mMemBlockList[oldMemBlockIndex] = 0; mMemBlockUsed--; } // Insert the allocated block in the debug memory block list if(mMemBlockList) { mMemBlockList[mMemBlockFirstFree] = (size_t)ptr; blockStart[4] = mMemBlockFirstFree; mMemBlockUsed++; if(mMemBlockUsed==mMemBlockListSize) { size_t* tps = (size_t*)::malloc((mMemBlockListSize+MEMBLOCKSTART)*sizeof(size_t)); memcpy(tps, mMemBlockList, mMemBlockListSize*sizeof(size_t)); memset((tps+mMemBlockListSize), 0, MEMBLOCKSTART*sizeof(size_t)); ::free(mMemBlockList); mMemBlockList = tps; mMemBlockFirstFree = mMemBlockListSize-1; mMemBlockListSize += MEMBLOCKSTART; } while(mMemBlockList[++mMemBlockFirstFree] && (mMemBlockFirstFree<mMemBlockListSize)); if(mMemBlockFirstFree==mMemBlockListSize) { mMemBlockFirstFree = (size_t)-1; while(mMemBlockList[++mMemBlockFirstFree] && (mMemBlockFirstFree<mMemBlockListSize)); } } else { blockStart[4] = 0; } blockStart[5] = 0; // Classname UnlockAlloc(); return ((size_t*)ptr2)+6; #else LockAlloc(); size_t* ptr = ((size_t*)memory)-2; if(ptr[0]!=g_pointerMarker) { printf("Error: realloc unknown memory!!\n"); } mNbAllocatedBytes -= ptr[1]; mNbAllocatedBytes += size; if(mNbAllocatedBytes>mHighWaterMark) mHighWaterMark = mNbAllocatedBytes; void* ptr2 = ::realloc(ptr, size+2*sizeof(size_t)); mNbReallocs++; *(((size_t*)ptr2)+1) = size; if(ptr == ptr2) { UnlockAlloc(); return memory; } *(((size_t*)ptr2)) = g_pointerMarker; UnlockAlloc(); return ((size_t*)ptr2)+2; #endif }
void* UserAllocator::mallocDEBUG(size_t size, const char* file, int line, const char* className, NxMemoryType type) { #ifndef _DEBUG return malloc(size, type); #endif if(size == 0) { printf("Warning: trying to allocate 0 bytes\n"); return NULL; } LockAlloc(); void* ptr = (void*)::malloc(size+6*sizeof(size_t)); mTotalNbAllocs++; mNbAllocs++; size_t* blockStart = (size_t*)ptr; blockStart[0] = g_pointerMarker; blockStart[1] = size; blockStart[2] = (size_t)file; blockStart[3] = line; mNbAllocatedBytes+=size; if(mNbAllocatedBytes>mHighWaterMark) mHighWaterMark = mNbAllocatedBytes; // Insert the allocated block in the debug memory block list if(mMemBlockList) { mMemBlockList[mMemBlockFirstFree] = (size_t)ptr; blockStart[4] = mMemBlockFirstFree; mMemBlockUsed++; if(mMemBlockUsed==mMemBlockListSize) { size_t* tps = (size_t*)::malloc((mMemBlockListSize+MEMBLOCKSTART)*sizeof(size_t)); memcpy(tps, mMemBlockList, mMemBlockListSize*sizeof(size_t)); memset((tps+mMemBlockListSize), 0, MEMBLOCKSTART*sizeof(size_t)); ::free(mMemBlockList); mMemBlockList = tps; mMemBlockFirstFree = mMemBlockListSize-1; mMemBlockListSize += MEMBLOCKSTART; } while(mMemBlockList[++mMemBlockFirstFree] && (mMemBlockFirstFree<mMemBlockListSize)); if(mMemBlockFirstFree==mMemBlockListSize) { mMemBlockFirstFree = (size_t)-1; while(mMemBlockList[++mMemBlockFirstFree] && (mMemBlockFirstFree<mMemBlockListSize)); } } else { blockStart[4] = 0; } blockStart[5] = (size_t)className; UnlockAlloc(); return ((size_t*)ptr)+6; }
void SetMemoryAssert( bool b ) { LockAlloc(); g_bMemoryAssert = b; UnlockAlloc(); }