/src/resiprocate/rutil/DinkyPool.hxx
Line | Count | Source |
1 | | #ifndef DinkyPool_Include_Guard |
2 | | #define DinkyPool_Include_Guard |
3 | | |
4 | | #include <limits> |
5 | | #include <memory> |
6 | | #include <stddef.h> |
7 | | |
8 | | #include "rutil/PoolBase.hxx" |
9 | | |
10 | | namespace resip |
11 | | { |
12 | | /** |
13 | | A dirt-simple lightweight pool allocator meant for use in short-lifetime |
14 | | objects. This will pool-allocate at most S bytes, after which no further pool |
15 | | allocation will be performed, and fallback to the system new/delete will be |
16 | | used (deallocating a pool allocated object will _not_ free up room in the |
17 | | pool; the memory will be freed when the DinkyPool goes away). |
18 | | */ |
19 | | template<unsigned int S> |
20 | | class DinkyPool : public PoolBase |
21 | | { |
22 | | public: |
23 | 9.51k | DinkyPool() : count(0), heapBytes(0) {} |
24 | | ~DinkyPool(){} |
25 | | |
26 | | void* allocate(size_t size) |
27 | 98.8k | { |
28 | 98.8k | if((8*count)+size <= S) |
29 | 41.5k | { |
30 | 41.5k | void* result=mBuf[count]; |
31 | 41.5k | count+=(size+7)/8; |
32 | 41.5k | return result; |
33 | 41.5k | } |
34 | 57.2k | heapBytes += size; |
35 | 57.2k | return ::operator new(size); |
36 | 98.8k | } |
37 | | |
38 | | void deallocate(void* ptr) |
39 | 98.8k | { |
40 | 98.8k | if(ptr >= (void*)mBuf[0] && ptr < (void*)mBuf[(S+7)/8]) |
41 | 41.5k | { |
42 | 41.5k | return; |
43 | 41.5k | } |
44 | 57.2k | ::operator delete(ptr); |
45 | 57.2k | } |
46 | | |
47 | | size_t max_size() const |
48 | 31.9k | { |
49 | 31.9k | return std::numeric_limits<size_t>::max(); |
50 | 31.9k | } |
51 | | |
52 | | size_t getHeapBytes() const { return heapBytes; } |
53 | | size_t getPoolBytes() const { return count*8; } |
54 | | size_t getPoolSizeBytes() const { return sizeof(mBuf); } |
55 | | |
56 | | private: |
57 | | // disabled |
58 | | DinkyPool& operator=(const DinkyPool& rhs); |
59 | | DinkyPool(const DinkyPool& other); |
60 | | |
61 | | size_t count; // 8-byte chunks alloced so far |
62 | | char mBuf[(S+7)/8][8]; // 8-byte chunks for alignment |
63 | | size_t heapBytes; |
64 | | }; |
65 | | |
66 | | } |
67 | | #endif |
68 | | |
69 | | |
70 | | /* ==================================================================== |
71 | | * The Vovida Software License, Version 1.0 |
72 | | * |
73 | | * Redistribution and use in source and binary forms, with or without |
74 | | * modification, are permitted provided that the following conditions |
75 | | * are met: |
76 | | * |
77 | | * 1. Redistributions of source code must retain the above copyright |
78 | | * notice, this list of conditions and the following disclaimer. |
79 | | * |
80 | | * 2. Redistributions in binary form must reproduce the above copyright |
81 | | * notice, this list of conditions and the following disclaimer in |
82 | | * the documentation and/or other materials provided with the |
83 | | * distribution. |
84 | | * |
85 | | * 3. The names "VOCAL", "Vovida Open Communication Application Library", |
86 | | * and "Vovida Open Communication Application Library (VOCAL)" must |
87 | | * not be used to endorse or promote products derived from this |
88 | | * software without prior written permission. For written |
89 | | * permission, please contact vocal@vovida.org. |
90 | | * |
91 | | * 4. Products derived from this software may not be called "VOCAL", nor |
92 | | * may "VOCAL" appear in their name, without prior written |
93 | | * permission of Vovida Networks, Inc. |
94 | | * |
95 | | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
96 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
97 | | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND |
98 | | * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA |
99 | | * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES |
100 | | * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, |
101 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
102 | | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
103 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
104 | | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
105 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
106 | | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
107 | | * DAMAGE. |
108 | | * |
109 | | * ==================================================================== |
110 | | * |
111 | | * This software consists of voluntary contributions made by Vovida |
112 | | * Networks, Inc. and many individuals on behalf of Vovida Networks, |
113 | | * Inc. For more information on Vovida Networks, Inc., please see |
114 | | * <http://www.vovida.org/>. |
115 | | * |
116 | | */ |