| 1 | // | 
|---|
| 2 | // MemoryPool.h | 
|---|
| 3 | // | 
|---|
| 4 | // Library: Foundation | 
|---|
| 5 | // Package: Core | 
|---|
| 6 | // Module:  MemoryPool | 
|---|
| 7 | // | 
|---|
| 8 | // Definition of the MemoryPool class. | 
|---|
| 9 | // | 
|---|
| 10 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. | 
|---|
| 11 | // and Contributors. | 
|---|
| 12 | // | 
|---|
| 13 | // SPDX-License-Identifier:	BSL-1.0 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 |  | 
|---|
| 17 | #ifndef Foundation_MemoryPool_INCLUDED | 
|---|
| 18 | #define Foundation_MemoryPool_INCLUDED | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | #include "Poco/Foundation.h" | 
|---|
| 22 | #include "Poco/Mutex.h" | 
|---|
| 23 | #include <vector> | 
|---|
| 24 | #include <cstddef> | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | namespace Poco { | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | class Foundation_API MemoryPool | 
|---|
| 31 | /// A simple pool for fixed-size memory blocks. | 
|---|
| 32 | /// | 
|---|
| 33 | /// The main purpose of this class is to speed-up | 
|---|
| 34 | /// memory allocations, as well as to reduce memory | 
|---|
| 35 | /// fragmentation in situations where the same blocks | 
|---|
| 36 | /// are allocated all over again, such as in server | 
|---|
| 37 | /// applications. | 
|---|
| 38 | /// | 
|---|
| 39 | /// All allocated blocks are retained for future use. | 
|---|
| 40 | /// A limit on the number of blocks can be specified. | 
|---|
| 41 | /// Blocks can be preallocated. | 
|---|
| 42 | { | 
|---|
| 43 | public: | 
|---|
| 44 | MemoryPool(std::size_t blockSize, int preAlloc = 0, int maxAlloc = 0); | 
|---|
| 45 | /// Creates a MemoryPool for blocks with the given blockSize. | 
|---|
| 46 | /// The number of blocks given in preAlloc are preallocated. | 
|---|
| 47 |  | 
|---|
| 48 | ~MemoryPool(); | 
|---|
| 49 |  | 
|---|
| 50 | void* get(); | 
|---|
| 51 | /// Returns a memory block. If there are no more blocks | 
|---|
| 52 | /// in the pool, a new block will be allocated. | 
|---|
| 53 | /// | 
|---|
| 54 | /// If maxAlloc blocks are already allocated, an | 
|---|
| 55 | /// OutOfMemoryException is thrown. | 
|---|
| 56 |  | 
|---|
| 57 | void release(void* ptr); | 
|---|
| 58 | /// Releases a memory block and returns it to the pool. | 
|---|
| 59 |  | 
|---|
| 60 | std::size_t blockSize() const; | 
|---|
| 61 | /// Returns the block size. | 
|---|
| 62 |  | 
|---|
| 63 | int allocated() const; | 
|---|
| 64 | /// Returns the number of allocated blocks. | 
|---|
| 65 |  | 
|---|
| 66 | int available() const; | 
|---|
| 67 | /// Returns the number of available blocks in the pool. | 
|---|
| 68 |  | 
|---|
| 69 | private: | 
|---|
| 70 | MemoryPool(); | 
|---|
| 71 | MemoryPool(const MemoryPool&); | 
|---|
| 72 | MemoryPool& operator = (const MemoryPool&); | 
|---|
| 73 |  | 
|---|
| 74 | void clear(); | 
|---|
| 75 |  | 
|---|
| 76 | enum | 
|---|
| 77 | { | 
|---|
| 78 | BLOCK_RESERVE = 128 | 
|---|
| 79 | }; | 
|---|
| 80 |  | 
|---|
| 81 | typedef std::vector<char*> BlockVec; | 
|---|
| 82 |  | 
|---|
| 83 | std::size_t _blockSize; | 
|---|
| 84 | int         _maxAlloc; | 
|---|
| 85 | int         _allocated; | 
|---|
| 86 | BlockVec    _blocks; | 
|---|
| 87 | FastMutex   _mutex; | 
|---|
| 88 | }; | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | // | 
|---|
| 92 | // inlines | 
|---|
| 93 | // | 
|---|
| 94 | inline std::size_t MemoryPool::blockSize() const | 
|---|
| 95 | { | 
|---|
| 96 | return _blockSize; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 |  | 
|---|
| 100 | inline int MemoryPool::allocated() const | 
|---|
| 101 | { | 
|---|
| 102 | return _allocated; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 |  | 
|---|
| 106 | inline int MemoryPool::available() const | 
|---|
| 107 | { | 
|---|
| 108 | return (int) _blocks.size(); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 |  | 
|---|
| 112 | } // namespace Poco | 
|---|
| 113 |  | 
|---|
| 114 |  | 
|---|
| 115 | #endif // Foundation_MemoryPool_INCLUDED | 
|---|
| 116 |  | 
|---|