diff options
author | Andrey Lihatskiy <andreylproductengine@lindenlab.com> | 2018-06-22 12:55:19 +0300 |
---|---|---|
committer | Andrey Lihatskiy <andreylproductengine@lindenlab.com> | 2018-06-22 12:55:19 +0300 |
commit | 5f432147e373c3223ac06797ecff147d447ed79f (patch) | |
tree | 942f163ad33ad3997aee3000cae321f5f693ad57 /indra/llcommon/llmemory.h | |
parent | 99d9befb629c97f7c778c8a9ddfb9d0060d54de9 (diff) | |
parent | dc07de2f4a4c49d1877bf743b6f0d209392f6eb6 (diff) |
Merged lindenlab/viewer-release into default
Diffstat (limited to 'indra/llcommon/llmemory.h')
-rw-r--r-- | indra/llcommon/llmemory.h | 321 |
1 files changed, 0 insertions, 321 deletions
diff --git a/indra/llcommon/llmemory.h b/indra/llcommon/llmemory.h index c37967e10e..5b17d9e3a4 100644 --- a/indra/llcommon/llmemory.h +++ b/indra/llcommon/llmemory.h @@ -356,327 +356,6 @@ 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 -{ - friend class LLPrivateMemoryPoolManager ; - -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 freeMem(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;} - - //debug use - void resetBitMap() ; - 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 bytes reserved for mUsageBits. - - public: - LLMemoryBlock* mPrev ; - LLMemoryBlock* mNext ; - LLMemoryBlock* mSelf ; - - struct CompareAddress - { - bool operator()(const LLMemoryBlock* const& lhs, const LLMemoryBlock* const& rhs) - { - return (uintptr_t)lhs->getBuffer() < (uintptr_t)rhs->getBuffer(); - } - }; - }; - - 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 freeMem(void* addr) ; - - 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_slot_size, - U32 max_slot_size, U32 min_block_size, U32 max_block_size) ; - - void dump() ; - - private: - U32 getPageIndex(uintptr_t addr) ; - U32 getBlockLevel(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) ; - U32 calcBlockSize(U32 slot_size); - LLMemoryBlock* createNewBlock(LLMemoryBlock* blk, 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 mMinSlotSize ; - U32 mMaxSlotSize ; - U32 mAlloatedSize ; - U16 mBlockLevels; - U16 mPartitionLevels; - - public: - //form a linked list - LLMemoryChunk* mNext ; - LLMemoryChunk* mPrev ; - } ; - -private: - LLPrivateMemoryPool(S32 type, U32 max_pool_size) ; - ~LLPrivateMemoryPool() ; - - char *allocate(U32 size) ; - void freeMem(void* addr) ; - - void dump() ; - U32 getTotalAllocatedSize() ; - U32 getTotalReservedSize() {return mReservedPoolSize;} - S32 getType() const {return mType; } - bool isEmpty() const {return !mNumOfChunks; } - -private: - void lock() ; - void unlock() ; - S32 getChunkIndex(U32 size) ; - LLMemoryChunk* addChunk(S32 chunk_index) ; - bool checkSize(U32 asked_size) ; - void removeChunk(LLMemoryChunk* chunk) ; - U16 findHashKey(const char* addr); - 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() ; - -public: - 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. - }; - - 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; - U32 mReservedPoolSize ; - - 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<LLChunkHashElement> mChunkHashList ; -}; - -class LL_COMMON_API LLPrivateMemoryPoolManager -{ -private: - LLPrivateMemoryPoolManager(BOOL enabled, U32 max_pool_size) ; - ~LLPrivateMemoryPoolManager() ; - -public: - static LLPrivateMemoryPoolManager* getInstance() ; - static void initClass(BOOL enabled, U32 pool_size) ; - static void destroyClass() ; - - LLPrivateMemoryPool* newPool(S32 type) ; - void deletePool(LLPrivateMemoryPool* pool) ; - -private: - std::vector<LLPrivateMemoryPool*> mPoolList ; - U32 mMaxPrivatePoolSize; - - static LLPrivateMemoryPoolManager* sInstance ; - static BOOL sPrivatePoolEnabled; - static std::vector<LLPrivateMemoryPool*> sDanglingPoolList ; -public: - //debug and statistics info. - void updateStatistics() ; - - 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<char*, std::string> 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. -// -#if 0 -class LL_COMMON_API LLPrivateMemoryPoolTester -{ -private: - LLPrivateMemoryPoolTester() ; - ~LLPrivateMemoryPoolTester() ; - -public: - static LLPrivateMemoryPoolTester* getInstance() ; - static void destroy() ; - - void run(S32 type) ; - -private: - void correctnessTest() ; - void performanceTest() ; - 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) ; - -#if 0 -public: - void* operator new(size_t size) - { - return (void*)sPool->allocate(size) ; - } - void operator delete(void* addr) - { - sPool->freeMem(addr) ; - } - void* operator new[](size_t size) - { - return (void*)sPool->allocate(size) ; - } - void operator delete[](void* addr) - { - sPool->freeMem(addr) ; - } -#endif - -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 -#endif // LLRefCount moved to llrefcount.h // LLPointer moved to llpointer.h |