diff options
| author | Tim Foley <tfoley@nvidia.com> | 2017-06-15 13:24:25 -0700 |
|---|---|---|
| committer | Tim Foley <tfoley@nvidia.com> | 2017-06-15 13:24:25 -0700 |
| commit | 205187b561c3b31fa931e73e8f7263f0c4b1de41 (patch) | |
| tree | 7bd2cd5ae3c14416b71ef8319ff02ace429d1132 /source/core/memory-pool.h | |
| parent | 517513645afb8eaf4841e7b7035f1ba3a9c7cd57 (diff) | |
Rename `CoreLib::*` to `Slang`
Getting rid of more namespace complexity and stripping things down to the basics.
This also gets rid of some dead code in the "core" library.
Diffstat (limited to 'source/core/memory-pool.h')
| -rw-r--r-- | source/core/memory-pool.h | 194 |
1 files changed, 95 insertions, 99 deletions
diff --git a/source/core/memory-pool.h b/source/core/memory-pool.h index 6bfded060..e735fb11c 100644 --- a/source/core/memory-pool.h +++ b/source/core/memory-pool.h @@ -4,123 +4,119 @@ #include "basic.h" #include "int-set.h" -namespace CoreLib +namespace Slang { - namespace Basic + struct MemoryBlockFields { - struct MemoryBlockFields - { - unsigned int Occupied : 1; - unsigned int Order : 31; - }; - struct FreeListNode - { - FreeListNode * PrevPtr = nullptr, *NextPtr = nullptr; - }; - class MemoryPool - { - private: - static const int MaxLevels = 32; - int blockSize = 0, log2BlockSize = 0; - int numLevels = 0; - int bytesAllocated = 0; - int bytesWasted = 0; - unsigned char * buffer = nullptr; - FreeListNode * freeList[MaxLevels]; - IntSet used; - int AllocBlock(int level); - void FreeBlock(unsigned char * ptr, int level); - public: - MemoryPool(unsigned char * buffer, int log2BlockSize, int numBlocks); - MemoryPool() = default; - void Init(unsigned char * buffer, int log2BlockSize, int numBlocks); - unsigned char * Alloc(int size); - void Free(unsigned char * ptr, int size); - }; + unsigned int Occupied : 1; + unsigned int Order : 31; + }; + struct FreeListNode + { + FreeListNode * PrevPtr = nullptr, *NextPtr = nullptr; + }; + class MemoryPool + { + private: + static const int MaxLevels = 32; + int blockSize = 0, log2BlockSize = 0; + int numLevels = 0; + int bytesAllocated = 0; + int bytesWasted = 0; + unsigned char * buffer = nullptr; + FreeListNode * freeList[MaxLevels]; + IntSet used; + int AllocBlock(int level); + void FreeBlock(unsigned char * ptr, int level); + public: + MemoryPool(unsigned char * buffer, int log2BlockSize, int numBlocks); + MemoryPool() = default; + void Init(unsigned char * buffer, int log2BlockSize, int numBlocks); + unsigned char * Alloc(int size); + void Free(unsigned char * ptr, int size); + }; - class OutofPoolMemoryException : public Exception - {}; + class OutofPoolMemoryException : public Exception + {}; - template<typename T, int PoolSize> - class ObjectPool + template<typename T, int PoolSize> + class ObjectPool + { + static const int ObjectSize = sizeof(T) < 8 ? 8 : sizeof(T); + private: + struct FreeList { - static const int ObjectSize = sizeof(T) < 8 ? 8 : sizeof(T); - private: - struct FreeList - { - FreeList* Next; - }; - FreeList * freeList = nullptr; - int allocPtr = 0; - int poolSize = 0; - void * buffer = 0; - T * GetFreeObject() - { - if (freeList) - { - auto rs = (T*)freeList; - freeList = freeList->Next; - return rs; - } - return nullptr; - } - public: - ObjectPool() + FreeList* Next; + }; + FreeList * freeList = nullptr; + int allocPtr = 0; + int poolSize = 0; + void * buffer = 0; + T * GetFreeObject() + { + if (freeList) { - freeList = nullptr; - allocPtr = 0; - buffer = malloc(PoolSize * ObjectSize); + auto rs = (T*)freeList; + freeList = freeList->Next; + return rs; } + return nullptr; + } + public: + ObjectPool() + { + freeList = nullptr; + allocPtr = 0; + buffer = malloc(PoolSize * ObjectSize); + } - void Close() - { - free(buffer); - } + void Close() + { + free(buffer); + } - void Free(T * obj) - { - auto newList = (FreeList*)obj; - newList->Next = freeList; - freeList = newList; - } + void Free(T * obj) + { + auto newList = (FreeList*)obj; + newList->Next = freeList; + freeList = newList; + } - void * Buffer() - { - return buffer; - } + void * Buffer() + { + return buffer; + } - T * Alloc() + T * Alloc() + { + auto rs = GetFreeObject(); + if (!rs) { - auto rs = GetFreeObject(); - if (!rs) - { - if (allocPtr < PoolSize) - { - rs = (T*)buffer + allocPtr; - allocPtr++; - } - } - if (!rs) + if (allocPtr < PoolSize) { - throw OutofPoolMemoryException(); + rs = (T*)buffer + allocPtr; + allocPtr++; } - return rs; } - }; + if (!rs) + { + throw OutofPoolMemoryException(); + } + return rs; + } }; +}; #define USE_POOL_ALLOCATOR(T, PoolSize) \ - private:\ - static CoreLib::ObjectPool<T, PoolSize> _pool;\ - public:\ - void * operator new(std::size_t) { return _pool.Alloc(); } \ - void operator delete(void * ptr) {_pool.Free((T*)ptr); }\ - int GetObjectId() { return (int)(this - (T*)_pool.Buffer()); }\ - static void ClosePool(); +private:\ + static Slang::ObjectPool<T, PoolSize> _pool;\ +public:\ + void * operator new(std::size_t) { return _pool.Alloc(); } \ + void operator delete(void * ptr) {_pool.Free((T*)ptr); }\ + int GetObjectId() { return (int)(this - (T*)_pool.Buffer()); }\ + static void ClosePool(); #define IMPL_POOL_ALLOCATOR(T, PoolSize) \ - CoreLib::ObjectPool<T, PoolSize> T::_pool;\ - void T::ClosePool() { _pool.Close(); } - -} +Slang::ObjectPool<T, PoolSize> T::_pool;\ +void T::ClosePool() { _pool.Close(); } #endif
\ No newline at end of file |
