From f4ff1430f0d6ae7dd5a6be0bd665678b30a63aca Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Fri, 3 Dec 2010 22:16:16 -0700 Subject: first iteration of memory pool code --- indra/llcommon/llmemory.h | 174 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 172 insertions(+), 2 deletions(-) (limited to 'indra/llcommon/llmemory.h') diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h index 9bf4248bb7..d9e93d0e96 100644 --- a/indra/llcommon/llmemory.h +++ b/indra/llcommon/llmemory.h @@ -26,8 +26,6 @@ #ifndef LLMEMORY_H #define LLMEMORY_H - - extern S32 gTotalDAlloc; extern S32 gTotalDAUse; extern S32 gDACount; @@ -44,8 +42,180 @@ public: // Return the resident set size of the current process, in bytes. // Return value is zero if not known. static U64 getCurrentRSS(); + + static void* tryToAlloc(void* address, U32 size); + static void initMaxHeapSizeGB(F32 max_heap_size_gb, BOOL prevent_heap_failure); + static void updateMemoryInfo() ; + static void logMemoryInfo(BOOL update = FALSE); + static S32 isMemoryPoolLow(); + + static U32 getAvailableMemKB() ; + static U32 getMaxMemKB() ; + static U32 getAllocatedMemKB() ; private: static char* reserveMem; + static U32 sAvailPhysicalMemInKB ; + static U32 sMaxPhysicalMemInKB ; + static U32 sAllocatedMemInKB; + static U32 sAllocatedPageSizeInKB ; + + static U32 sMaxHeapSizeInKB; + static BOOL sEnableMemoryFailurePrevention; +}; + +class LL_COMMON_API LLPrivateMemoryPool +{ +public: + class LL_COMMON_API LLMemoryBlock //each block is devided into slots uniformly + { + public: + LLMemoryBlock() ; + ~LLMemoryBlock() ; + + void init(char* buffer, U32 buffer_size, U32 slot_size) ; + void setBuffer(char* buffer, U32 buffer_size) ; + + char* allocate() ; + void free(void* addr) ; + + bool empty() {return !mAllocatedSlots;} + bool isFull() {return mAllocatedSlots == mTotalSlots;} + bool isFree() {return !mTotalSlots;} + + U32 getSlotSize()const {return mSlotSize;} + U32 getTotalSlots()const {return mTotalSlots;} + U32 getBufferSize()const {return mBufferSize;} + char* getBuffer() const {return mBuffer;} + + private: + char* mBuffer; + U32 mSlotSize ; //when the block is not initialized, it is the buffer size. + U32 mBufferSize ; + U32 mUsageBits ; + U8 mTotalSlots ; + U8 mAllocatedSlots ; + U8 mDummySize ; //size of extra U32 reserved for mUsageBits. + + public: + LLMemoryBlock* mPrev ; + LLMemoryBlock* mNext ; + LLMemoryBlock* mSelf ; + }; + + class LL_COMMON_API LLMemoryChunk //is divided into memory blocks. + { + public: + LLMemoryChunk() ; + ~LLMemoryChunk() ; + + void init(char* buffer, U32 buffer_size, U32 min_slot_size, U32 max_slot_size, U32 min_block_size, U32 max_block_size) ; + void setBuffer(char* buffer, U32 buffer_size) ; + + bool empty() ; + + char* allocate(U32 size) ; + void free(void* addr) ; + + const char* getBuffer() const {return mBuffer;} + U32 getBufferSize() const {return mBufferSize;} + + static U32 getMaxOverhead(U32 data_buffer_size, U32 min_page_size) ; + + private: + LLMemoryBlock* addBlock(U32 blk_idx) ; + void popAvailBlockList(U32 blk_idx) ; + void addToFreeSpace(LLMemoryBlock* blk) ; + void removeFromFreeSpace(LLMemoryBlock* blk) ; + void removeBlock(LLMemoryBlock* blk) ; + void addToAvailBlockList(LLMemoryBlock* blk) ; + LLMemoryBlock* createNewBlock(LLMemoryBlock** cur_idxp, U32 buffer_size, U32 slot_size, U32 blk_idx) ; + + private: + LLMemoryBlock** mAvailBlockList ;//256 by mMinSlotSize + LLMemoryBlock** mFreeSpaceList; + LLMemoryBlock* mBlocks ; //index of blocks by address. + + char* mBuffer ; + U32 mBufferSize ; + char* mDataBuffer ; + char* mMetaBuffer ; + U32 mMinBlockSize ; + U32 mMaxBlockSize; + U32 mMinSlotSize ; + U16 mBlockLevels; + U16 mPartitionLevels; + + public: + //form a linked list + LLMemoryChunk* mNext ; + LLMemoryChunk* mPrev ; + + U32 mKey ; //= mBuffer + } ; + +public: + LLPrivateMemoryPool(U32 max_size, bool threaded) ; + ~LLPrivateMemoryPool() ; + + char *allocate(U32 size) ; + void free(void* addr) ; + void dump() ; + +private: + void lock() ; + void unlock() ; + S32 getChunkIndex(U32 size) ; + LLMemoryChunk* addChunk(S32 chunk_index) ; + void removeChunk(LLMemoryChunk* chunk) ; + U16 findChunk(const char* addr) ; + void destroyPool() ; + +private: + LLMutex* mMutexp ; + U32 mMaxPoolSize; + U32 mReservedPoolSize ; + + enum + { + SMALL_ALLOCATION = 0, //from 8 bytes to 2KB(exclusive), page size 2KB, max chunk size is 4MB. + MEDIUM_ALLOCATION, //from 2KB to 512KB(exclusive), page size 32KB, max chunk size 4MB + LARGE_ALLOCATION, //from 512KB to 4MB(inclusive), page size 64KB, max chunk size 16MB + SUPER_ALLOCATION //allocation larger than 4MB. + }; + + LLMemoryChunk* mChunkList[SUPER_ALLOCATION] ; //all memory chunks reserved by this pool, sorted by address + std::vector mChunks ; + U16 mNumOfChunks ; + U16 mChunkVectorCapacity ; +}; + +// +//the below singleton is used to test the private memory pool. +// +class LLPrivateMemoryPoolTester +{ +private: + LLPrivateMemoryPoolTester() ; + ~LLPrivateMemoryPoolTester() ; + +public: + static LLPrivateMemoryPoolTester* getInstance() ; + static void destroy() ; + + void run() ; + +private: + void correctnessTest() ; + void reliabilityTest() ; + void performanceTest() ; + void fragmentationtest() ; + + void* operator new(size_t); + void operator delete(void*); + +private: + static LLPrivateMemoryPoolTester* sInstance; + static LLPrivateMemoryPool* sPool ; }; // LLRefCount moved to llrefcount.h -- cgit v1.2.3 From 43f4429363e63484f35663c10ca993d0d812e855 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Wed, 8 Dec 2010 20:50:39 -0700 Subject: test code and some code change --- indra/llcommon/llmemory.h | 62 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 5 deletions(-) (limited to 'indra/llcommon/llmemory.h') diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h index d9e93d0e96..128e7aefe6 100644 --- a/indra/llcommon/llmemory.h +++ b/indra/llcommon/llmemory.h @@ -63,6 +63,14 @@ private: static BOOL sEnableMemoryFailurePrevention; }; +// +//class LLPrivateMemoryPool defines a private memory pool for an application to use, so the application does not +//need to access the heap directly fro each memory allocation. Throught this, the allocation speed is faster, +//and reduces virtaul address space gragmentation problem. +//Note: this class is thread-safe by passing true to the constructor function. However, you do not need to do this unless +//you are sure the memory allocation and de-allocation will happen in different threads. To make the pool thread safe +//increases allocation and deallocation cost. +// class LL_COMMON_API LLPrivateMemoryPool { public: @@ -122,6 +130,8 @@ public: static U32 getMaxOverhead(U32 data_buffer_size, U32 min_page_size) ; private: + U32 getBlockLevel(U32 size) ; + U32 getPageLevel(U32 size) ; LLMemoryBlock* addBlock(U32 blk_idx) ; void popAvailBlockList(U32 blk_idx) ; void addToFreeSpace(LLMemoryBlock* blk) ; @@ -142,6 +152,7 @@ public: U32 mMinBlockSize ; U32 mMaxBlockSize; U32 mMinSlotSize ; + U32 mAlloatedSize ; U16 mBlockLevels; U16 mPartitionLevels; @@ -192,7 +203,7 @@ private: // //the below singleton is used to test the private memory pool. // -class LLPrivateMemoryPoolTester +class LL_COMMON_API LLPrivateMemoryPoolTester { private: LLPrivateMemoryPoolTester() ; @@ -202,22 +213,63 @@ public: static LLPrivateMemoryPoolTester* getInstance() ; static void destroy() ; - void run() ; + void run(bool threaded) ; private: void correctnessTest() ; - void reliabilityTest() ; void performanceTest() ; void fragmentationtest() ; - void* operator new(size_t); - void operator delete(void*); + void test(U32 min_size, U32 max_size, U32 stride, U32 times, bool random_deletion, bool output_statistics) ; + +public: + void* operator new(size_t size) + { + return (void*)sPool->allocate(size) ; + } + void operator delete(void* addr) + { + sPool->free(addr) ; + } + void* operator new[](size_t size) + { + return (void*)sPool->allocate(size) ; + } + void operator delete[](void* addr) + { + sPool->free(addr) ; + } private: static LLPrivateMemoryPoolTester* sInstance; static LLPrivateMemoryPool* sPool ; + static LLPrivateMemoryPool* sThreadedPool ; }; +#if 0 +//static +void* LLPrivateMemoryPoolTester::operator new(size_t size) +{ + return (void*)sPool->allocate(size) ; +} + +//static +void LLPrivateMemoryPoolTester::operator delete(void* addr) +{ + sPool->free(addr) ; +} +//static +void* LLPrivateMemoryPoolTester::operator new[](size_t size) +{ + return (void*)sPool->allocate(size) ; +} + +//static +void LLPrivateMemoryPoolTester::operator delete[](void* addr) +{ + sPool->free(addr) ; +} +#endif // LLRefCount moved to llrefcount.h // LLPointer moved to llpointer.h -- cgit v1.2.3 From 5654abd50d834c3a7d0efb5dde393ff34f09be17 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Tue, 4 Jan 2011 13:14:36 -0700 Subject: a wroking version with a lot of debugging code (to be removed). --- indra/llcommon/llmemory.h | 50 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 13 deletions(-) (limited to 'indra/llcommon/llmemory.h') diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h index 128e7aefe6..f0e26d6b2f 100644 --- a/indra/llcommon/llmemory.h +++ b/indra/llcommon/llmemory.h @@ -95,6 +95,8 @@ public: U32 getBufferSize()const {return mBufferSize;} char* getBuffer() const {return mBuffer;} + //debug use + void resetBitMap() ; private: char* mBuffer; U32 mSlotSize ; //when the block is not initialized, it is the buffer size. @@ -108,6 +110,14 @@ public: LLMemoryBlock* mPrev ; LLMemoryBlock* mNext ; LLMemoryBlock* mSelf ; + + struct CompareAddress + { + bool operator()(const LLMemoryBlock* const& lhs, const LLMemoryBlock* const& rhs) + { + return (U32)lhs->getBuffer() < (U32)rhs->getBuffer(); + } + }; }; class LL_COMMON_API LLMemoryChunk //is divided into memory blocks. @@ -126,19 +136,26 @@ public: const char* getBuffer() const {return mBuffer;} U32 getBufferSize() const {return mBufferSize;} + U32 getAllocatedSize() const {return mAlloatedSize;} + + bool containsAddress(const char* addr) const; static U32 getMaxOverhead(U32 data_buffer_size, U32 min_page_size) ; + void dump() ; + private: + U32 getPageIndex(U32 addr) ; U32 getBlockLevel(U32 size) ; - U32 getPageLevel(U32 size) ; + U16 getPageLevel(U32 size) ; LLMemoryBlock* addBlock(U32 blk_idx) ; void popAvailBlockList(U32 blk_idx) ; void addToFreeSpace(LLMemoryBlock* blk) ; void removeFromFreeSpace(LLMemoryBlock* blk) ; void removeBlock(LLMemoryBlock* blk) ; void addToAvailBlockList(LLMemoryBlock* blk) ; - LLMemoryBlock* createNewBlock(LLMemoryBlock** cur_idxp, U32 buffer_size, U32 slot_size, U32 blk_idx) ; + U32 calcBlockSize(U32 slot_size); + LLMemoryBlock* createNewBlock(LLMemoryBlock* blk, U32 buffer_size, U32 slot_size, U32 blk_idx) ; private: LLMemoryBlock** mAvailBlockList ;//256 by mMinSlotSize @@ -150,18 +167,21 @@ public: char* mDataBuffer ; char* mMetaBuffer ; U32 mMinBlockSize ; - U32 mMaxBlockSize; U32 mMinSlotSize ; + U32 mMaxSlotSize ; U32 mAlloatedSize ; U16 mBlockLevels; U16 mPartitionLevels; + //debug use + std::set mActiveBlockList ; + public: //form a linked list LLMemoryChunk* mNext ; LLMemoryChunk* mPrev ; - U32 mKey ; //= mBuffer + LLMemoryChunk* mHashNext ; } ; public: @@ -170,22 +190,22 @@ public: char *allocate(U32 size) ; void free(void* addr) ; + void dump() ; + U32 getTotalAllocatedSize() ; private: void lock() ; void unlock() ; S32 getChunkIndex(U32 size) ; LLMemoryChunk* addChunk(S32 chunk_index) ; - void removeChunk(LLMemoryChunk* chunk) ; - U16 findChunk(const char* addr) ; + void checkSize(U32 asked_size) ; + void removeChunk(LLMemoryChunk* chunk, U16 key) ; + U16 findHashKey(const char* addr); + LLMemoryChunk* findChunk(const char* addr, U16& key) ; void destroyPool() ; -private: - LLMutex* mMutexp ; - U32 mMaxPoolSize; - U32 mReservedPoolSize ; - +public: enum { SMALL_ALLOCATION = 0, //from 8 bytes to 2KB(exclusive), page size 2KB, max chunk size is 4MB. @@ -194,10 +214,14 @@ private: SUPER_ALLOCATION //allocation larger than 4MB. }; +private: + LLMutex* mMutexp ; + U32 mMaxPoolSize; + U32 mReservedPoolSize ; + LLMemoryChunk* mChunkList[SUPER_ALLOCATION] ; //all memory chunks reserved by this pool, sorted by address - std::vector mChunks ; + std::vector mChunkHashList ; U16 mNumOfChunks ; - U16 mChunkVectorCapacity ; }; // -- cgit v1.2.3 From f4a8027feb2bbeafe7b0cfb3b05fd27f3cf243d3 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Thu, 6 Jan 2011 12:36:44 -0700 Subject: removed some debug code, redesigned the hash function, fixed bugs --- indra/llcommon/llmemory.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'indra/llcommon/llmemory.h') diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h index f0e26d6b2f..f7ca33a279 100644 --- a/indra/llcommon/llmemory.h +++ b/indra/llcommon/llmemory.h @@ -173,9 +173,6 @@ public: U16 mBlockLevels; U16 mPartitionLevels; - //debug use - std::set mActiveBlockList ; - public: //form a linked list LLMemoryChunk* mNext ; @@ -200,9 +197,13 @@ private: S32 getChunkIndex(U32 size) ; LLMemoryChunk* addChunk(S32 chunk_index) ; void checkSize(U32 asked_size) ; - void removeChunk(LLMemoryChunk* chunk, U16 key) ; + void removeChunk(LLMemoryChunk* chunk) ; U16 findHashKey(const char* addr); - LLMemoryChunk* findChunk(const char* addr, U16& key) ; + void addToHashTable(LLMemoryChunk* chunk) ; + void removeFromHashTable(LLMemoryChunk* chunk) ; + void rehash() ; + LLMemoryChunk* findChunk(const char* addr) ; + void destroyPool() ; public: @@ -222,6 +223,7 @@ private: LLMemoryChunk* mChunkList[SUPER_ALLOCATION] ; //all memory chunks reserved by this pool, sorted by address std::vector mChunkHashList ; U16 mNumOfChunks ; + U16 mHashFactor ; }; // @@ -245,6 +247,7 @@ private: void fragmentationtest() ; void test(U32 min_size, U32 max_size, U32 stride, U32 times, bool random_deletion, bool output_statistics) ; + void testAndTime(U32 size, U32 times) ; public: void* operator new(size_t size) -- cgit v1.2.3 From a3759a7815f7ba55b825bc76f30a1e333e01f295 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Thu, 6 Jan 2011 16:17:38 -0700 Subject: add the class LLPrivateMemoryPoolManager --- indra/llcommon/llmemory.h | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'indra/llcommon/llmemory.h') diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h index f7ca33a279..e42dc174b5 100644 --- a/indra/llcommon/llmemory.h +++ b/indra/llcommon/llmemory.h @@ -73,6 +73,8 @@ private: // class LL_COMMON_API LLPrivateMemoryPool { + friend class LLPrivateMemoryPoolManager ; + public: class LL_COMMON_API LLMemoryBlock //each block is devided into slots uniformly { @@ -181,15 +183,17 @@ public: LLMemoryChunk* mHashNext ; } ; -public: +private: LLPrivateMemoryPool(U32 max_size, bool threaded) ; ~LLPrivateMemoryPool() ; +public: char *allocate(U32 size) ; void free(void* addr) ; void dump() ; U32 getTotalAllocatedSize() ; + U32 getTotalReservedSize() {return mReservedPoolSize;} private: void lock() ; @@ -202,6 +206,7 @@ private: void addToHashTable(LLMemoryChunk* chunk) ; void removeFromHashTable(LLMemoryChunk* chunk) ; void rehash() ; + bool fillHashTable(U16 start, U16 end, LLMemoryChunk* chunk) ; LLMemoryChunk* findChunk(const char* addr) ; void destroyPool() ; @@ -226,6 +231,31 @@ private: U16 mHashFactor ; }; +class LL_COMMON_API LLPrivateMemoryPoolManager +{ +private: + LLPrivateMemoryPoolManager() ; + ~LLPrivateMemoryPoolManager() ; + +public: + static LLPrivateMemoryPoolManager* getInstance() ; + static void destroyClass() ; + + LLPrivateMemoryPool* newPool(U32 max_size, bool threaded) ; + void deletePool(LLPrivateMemoryPool* pool) ; + +private: + static LLPrivateMemoryPoolManager* sInstance ; + std::set mPoolList ; + +public: + //debug and statistics info. + void updateStatistics() ; + + U32 mTotalReservedSize ; + U32 mTotalAllocatedSize ; +}; + // //the below singleton is used to test the private memory pool. // -- cgit v1.2.3 From 7daa3d1ca10199468946feef0ce8eb67489deee0 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Fri, 7 Jan 2011 14:57:35 -0700 Subject: fixed a hash bug, enlarged the overhead for large allocations, and add new chunk to the tail of the linked list so new allocations go to oldest chunks first. --- indra/llcommon/llmemory.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'indra/llcommon/llmemory.h') diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h index e42dc174b5..5a2889958b 100644 --- a/indra/llcommon/llmemory.h +++ b/indra/llcommon/llmemory.h @@ -142,7 +142,8 @@ public: bool containsAddress(const char* addr) const; - static U32 getMaxOverhead(U32 data_buffer_size, U32 min_page_size) ; + static U32 getMaxOverhead(U32 data_buffer_size, U32 min_slot_size, + U32 max_slot_size, U32 min_block_size, U32 max_block_size) ; void dump() ; -- cgit v1.2.3 From 108980f68c184341e83454bbd5e72a5803b33092 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Wed, 23 Feb 2011 17:53:08 -0700 Subject: add types to LLPrivateMemoryPool --- indra/llcommon/llmemory.h | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'indra/llcommon/llmemory.h') diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h index 4474df6f86..a5dbabec5a 100644 --- a/indra/llcommon/llmemory.h +++ b/indra/llcommon/llmemory.h @@ -231,7 +231,7 @@ public: } ; private: - LLPrivateMemoryPool(U32 max_size, bool threaded) ; + LLPrivateMemoryPool(S32 type) ; ~LLPrivateMemoryPool() ; public: @@ -241,7 +241,9 @@ public: void dump() ; U32 getTotalAllocatedSize() ; U32 getTotalReservedSize() {return mReservedPoolSize;} - + S32 getType() const {return mType; } + bool isEmpty() const {return !mNumOfChunks; } + private: void lock() ; void unlock() ; @@ -267,6 +269,15 @@ public: SUPER_ALLOCATION //allocation larger than 4MB. }; + enum + { + STATIC = 0 , //static pool(each alllocation stays for a long time) without threading support + VOLATILE, //Volatile pool(each allocation stays for a very short time) without threading support + STATIC_THREADED, //static pool with threading support + VOLATILE_THREADED, //volatile pool with threading support + MAX_TYPES + }; //pool types + private: LLMutex* mMutexp ; U32 mMaxPoolSize; @@ -276,6 +287,8 @@ private: std::vector mChunkHashList ; U16 mNumOfChunks ; U16 mHashFactor ; + + S32 mType ; }; class LL_COMMON_API LLPrivateMemoryPoolManager @@ -288,12 +301,12 @@ public: static LLPrivateMemoryPoolManager* getInstance() ; static void destroyClass() ; - LLPrivateMemoryPool* newPool(U32 max_size, bool threaded) ; + LLPrivateMemoryPool* newPool(S32 type) ; void deletePool(LLPrivateMemoryPool* pool) ; private: static LLPrivateMemoryPoolManager* sInstance ; - std::set mPoolList ; + std::vector mPoolList ; public: //debug and statistics info. @@ -316,7 +329,7 @@ public: static LLPrivateMemoryPoolTester* getInstance() ; static void destroy() ; - void run(bool threaded) ; + void run(S32 type) ; private: void correctnessTest() ; -- cgit v1.2.3 From fc106df53085f549acdbb2f8149ca75e400532fa Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Thu, 24 Feb 2011 19:47:55 -0700 Subject: fix the compiling error: "free" is defined and in use globally. --- indra/llcommon/llmemory.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'indra/llcommon/llmemory.h') diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h index a5dbabec5a..001ff9c123 100644 --- a/indra/llcommon/llmemory.h +++ b/indra/llcommon/llmemory.h @@ -132,7 +132,7 @@ public: void setBuffer(char* buffer, U32 buffer_size) ; char* allocate() ; - void free(void* addr) ; + void freeMem(void* addr) ; bool empty() {return !mAllocatedSlots;} bool isFull() {return mAllocatedSlots == mTotalSlots;} @@ -180,7 +180,7 @@ public: bool empty() ; char* allocate(U32 size) ; - void free(void* addr) ; + void freeMem(void* addr) ; const char* getBuffer() const {return mBuffer;} U32 getBufferSize() const {return mBufferSize;} @@ -236,7 +236,7 @@ private: public: char *allocate(U32 size) ; - void free(void* addr) ; + void freeMem(void* addr) ; void dump() ; U32 getTotalAllocatedSize() ; @@ -339,6 +339,7 @@ private: void test(U32 min_size, U32 max_size, U32 stride, U32 times, bool random_deletion, bool output_statistics) ; void testAndTime(U32 size, U32 times) ; +#if 0 public: void* operator new(size_t size) { @@ -346,7 +347,7 @@ public: } void operator delete(void* addr) { - sPool->free(addr) ; + sPool->freeMem(addr) ; } void* operator new[](size_t size) { @@ -354,8 +355,9 @@ public: } void operator delete[](void* addr) { - sPool->free(addr) ; + sPool->freeMem(addr) ; } +#endif private: static LLPrivateMemoryPoolTester* sInstance; -- cgit v1.2.3 From b594d3b04d3095f15750436910debdd5a602a872 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Tue, 10 May 2011 21:02:20 -0600 Subject: add debug mode to track the memory allocation/deallocation. --- indra/llcommon/llmemory.h | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'indra/llcommon/llmemory.h') diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h index 001ff9c123..d50ae99823 100644 --- a/indra/llcommon/llmemory.h +++ b/indra/llcommon/llmemory.h @@ -34,6 +34,10 @@ extern S32 gDACount; extern void* ll_allocate (size_t size); extern void ll_release (void *p); +#ifndef __DEBUG_PRIVATE_MEM__ +#define __DEBUG_PRIVATE_MEM__ 0 +#endif + class LL_COMMON_API LLMemory { public: @@ -234,7 +238,6 @@ private: LLPrivateMemoryPool(S32 type) ; ~LLPrivateMemoryPool() ; -public: char *allocate(U32 size) ; void freeMem(void* addr) ; @@ -314,8 +317,27 @@ public: U32 mTotalReservedSize ; U32 mTotalAllocatedSize ; + +public: +#if __DEBUG_PRIVATE_MEM__ + static char* allocate(LLPrivateMemoryPool* poolp, U32 size, const char* function, const int line) ; + + typedef std::map mem_allocation_info_t ; + static mem_allocation_info_t sMemAllocationTracker; +#else + static char* allocate(LLPrivateMemoryPool* poolp, U32 size) ; +#endif + static void freeMem(LLPrivateMemoryPool* poolp, void* addr) ; }; +//------------------------------------------------------------------------------------- +#if __DEBUG_PRIVATE_MEM__ +#define ALLOCATE_MEM(poolp, size) LLPrivateMemoryPoolManager::allocate((poolp), (size), __FUNCTION__, __LINE__) +#else +#define ALLOCATE_MEM(poolp, size) LLPrivateMemoryPoolManager::allocate((poolp), (size)) +#endif +#define FREE_MEM(poolp, addr) LLPrivateMemoryPoolManager::freeMem((poolp), (addr)) +//------------------------------------------------------------------------------------- // //the below singleton is used to test the private memory pool. // -- cgit v1.2.3 From 76eca5d0bce3e303f6d77b0d16f114320830ac6a Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Tue, 19 Jul 2011 23:17:55 -0600 Subject: fix for memory alignment to 16 bytes. --- indra/llcommon/llmemory.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'indra/llcommon/llmemory.h') diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h index d3b824c6e9..26488423a3 100644 --- a/indra/llcommon/llmemory.h +++ b/indra/llcommon/llmemory.h @@ -226,7 +226,7 @@ public: U32 mUsageBits ; U8 mTotalSlots ; U8 mAllocatedSlots ; - U8 mDummySize ; //size of extra U32 reserved for mUsageBits. + U8 mDummySize ; //size of extra bytes reserved for mUsageBits. public: LLMemoryBlock* mPrev ; @@ -256,7 +256,7 @@ public: char* allocate(U32 size) ; void freeMem(void* addr) ; - const char* getBuffer() const {return mBuffer;} + char* getBuffer() const {return mBuffer;} U32 getBufferSize() const {return mBufferSize;} U32 getAllocatedSize() const {return mAlloatedSize;} @@ -408,9 +408,11 @@ public: #endif #define FREE_MEM(poolp, addr) LLPrivateMemoryPoolManager::freeMem((poolp), (addr)) //------------------------------------------------------------------------------------- + // //the below singleton is used to test the private memory pool. // +#if 0 class LL_COMMON_API LLPrivateMemoryPoolTester { private: @@ -481,6 +483,7 @@ void LLPrivateMemoryPoolTester::operator delete[](void* addr) sPool->free(addr) ; } #endif +#endif // LLRefCount moved to llrefcount.h // LLPointer moved to llpointer.h -- cgit v1.2.3 From 48d949150cd445ce1e801a7a8ee67597a965f14b Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Wed, 20 Jul 2011 16:05:19 -0600 Subject: add a debug setting "MemoryPrivatePoolEnabled" to turn on/off private memory pool. --- indra/llcommon/llmemory.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'indra/llcommon/llmemory.h') diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h index 26488423a3..f9099da612 100644 --- a/indra/llcommon/llmemory.h +++ b/indra/llcommon/llmemory.h @@ -367,11 +367,12 @@ private: class LL_COMMON_API LLPrivateMemoryPoolManager { private: - LLPrivateMemoryPoolManager() ; + LLPrivateMemoryPoolManager(BOOL enabled) ; ~LLPrivateMemoryPoolManager() ; -public: +public: static LLPrivateMemoryPoolManager* getInstance() ; + static void initClass(BOOL enabled) ; static void destroyClass() ; LLPrivateMemoryPool* newPool(S32 type) ; @@ -380,6 +381,7 @@ public: private: static LLPrivateMemoryPoolManager* sInstance ; std::vector mPoolList ; + BOOL mPrivatePoolEnabled; public: //debug and statistics info. -- cgit v1.2.3 From ba2ae6bc9583420a07245b4a7af2a779fb02b6c9 Mon Sep 17 00:00:00 2001 From: Xiaohong Bao Date: Fri, 2 Sep 2011 11:17:03 -0600 Subject: re-write the hash table code to eliminate potential flaws and simplify the implementation. --- indra/llcommon/llmemory.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'indra/llcommon/llmemory.h') diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h index f9099da612..db753f0d8b 100644 --- a/indra/llcommon/llmemory.h +++ b/indra/llcommon/llmemory.h @@ -300,8 +300,6 @@ public: //form a linked list LLMemoryChunk* mNext ; LLMemoryChunk* mPrev ; - - LLMemoryChunk* mHashNext ; } ; private: @@ -356,12 +354,30 @@ private: U32 mMaxPoolSize; U32 mReservedPoolSize ; - LLMemoryChunk* mChunkList[SUPER_ALLOCATION] ; //all memory chunks reserved by this pool, sorted by address - std::vector mChunkHashList ; + LLMemoryChunk* mChunkList[SUPER_ALLOCATION] ; //all memory chunks reserved by this pool, sorted by address U16 mNumOfChunks ; U16 mHashFactor ; S32 mType ; + + class LLChunkHashElement + { + public: + LLChunkHashElement() {mFirst = NULL ; mSecond = NULL ;} + + bool add(LLMemoryChunk* chunk) ; + void remove(LLMemoryChunk* chunk) ; + LLMemoryChunk* findChunk(const char* addr) ; + + bool empty() {return !mFirst && !mSecond; } + bool full() {return mFirst && mSecond; } + bool hasElement(LLMemoryChunk* chunk) {return mFirst == chunk || mSecond == chunk;} + + private: + LLMemoryChunk* mFirst ; + LLMemoryChunk* mSecond ; + }; + std::vector mChunkHashList ; }; class LL_COMMON_API LLPrivateMemoryPoolManager -- cgit v1.2.3