1 | // |
---|
2 | // MemoryPool.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/MemoryPool.h#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Core |
---|
8 | // Module: MemoryPool |
---|
9 | // |
---|
10 | // Definition of the MemoryPool class. |
---|
11 | // |
---|
12 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_MemoryPool_INCLUDED |
---|
40 | #define Foundation_MemoryPool_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/Mutex.h" |
---|
45 | #include <vector> |
---|
46 | #include <cstddef> |
---|
47 | |
---|
48 | |
---|
49 | namespace Poco { |
---|
50 | |
---|
51 | |
---|
52 | class Foundation_API MemoryPool |
---|
53 | /// A simple pool for fixed-size memory blocks. |
---|
54 | /// |
---|
55 | /// The main purpose of this class is to speed-up |
---|
56 | /// memory allocations, as well as to reduce memory |
---|
57 | /// fragmentation in situations where the same blocks |
---|
58 | /// are allocated all over again, such as in server |
---|
59 | /// applications. |
---|
60 | /// |
---|
61 | /// All allocated blocks are retained for future use. |
---|
62 | /// A limit on the number of blocks can be specified. |
---|
63 | /// Blocks can be preallocated. |
---|
64 | { |
---|
65 | public: |
---|
66 | MemoryPool(std::size_t blockSize, int preAlloc = 0, int maxAlloc = 0); |
---|
67 | /// Creates a MemoryPool for blocks with the given blockSize. |
---|
68 | /// The number of blocks given in preAlloc are preallocated. |
---|
69 | |
---|
70 | ~MemoryPool(); |
---|
71 | |
---|
72 | void* get(); |
---|
73 | /// Returns a memory block. If there are no more blocks |
---|
74 | /// in the pool, a new block will be allocated. |
---|
75 | /// |
---|
76 | /// If maxAlloc blocks are already allocated, an |
---|
77 | /// OutOfMemoryException is thrown. |
---|
78 | |
---|
79 | void release(void* ptr); |
---|
80 | /// Releases a memory block and returns it to the pool. |
---|
81 | |
---|
82 | std::size_t blockSize() const; |
---|
83 | /// Returns the block size. |
---|
84 | |
---|
85 | int allocated() const; |
---|
86 | /// Returns the number of allocated blocks. |
---|
87 | |
---|
88 | int available() const; |
---|
89 | /// Returns the number of available blocks in the pool. |
---|
90 | |
---|
91 | private: |
---|
92 | MemoryPool(); |
---|
93 | MemoryPool(const MemoryPool&); |
---|
94 | MemoryPool& operator = (const MemoryPool&); |
---|
95 | |
---|
96 | enum |
---|
97 | { |
---|
98 | BLOCK_RESERVE = 128 |
---|
99 | }; |
---|
100 | |
---|
101 | typedef std::vector<char*> BlockVec; |
---|
102 | |
---|
103 | std::size_t _blockSize; |
---|
104 | int _maxAlloc; |
---|
105 | int _allocated; |
---|
106 | BlockVec _blocks; |
---|
107 | FastMutex _mutex; |
---|
108 | }; |
---|
109 | |
---|
110 | |
---|
111 | // |
---|
112 | // inlines |
---|
113 | // |
---|
114 | inline std::size_t MemoryPool::blockSize() const |
---|
115 | { |
---|
116 | return _blockSize; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | inline int MemoryPool::allocated() const |
---|
121 | { |
---|
122 | return _allocated; |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | inline int MemoryPool::available() const |
---|
127 | { |
---|
128 | return (int) _blocks.size(); |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | } // namespace Poco |
---|
133 | |
---|
134 | |
---|
135 | #endif // Foundation_MemoryPool_INCLUDED |
---|