QCAD
Open Source 2D CAD
Loading...
Searching...
No Matches
PointerPool.h
Go to the documentation of this file.
1/******************************************************************************
2 * Project: libspatialindex - A C++ library for spatial indexing
3 * Author: Marios Hadjieleftheriou, [email protected]
4 ******************************************************************************
5 * Copyright (c) 2004, Marios Hadjieleftheriou
6 *
7 * All rights reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26******************************************************************************/
27
28#pragma once
29
30#include "PoolPointer.h"
31
32namespace Tools
33{
34 template <class X> class PointerPool
35 {
36 public:
37 explicit PointerPool(uint32_t capacity) : m_capacity(capacity)
38 {
39 #ifndef NDEBUG
40 m_hits = 0;
41 m_misses = 0;
43 #endif
44 }
45
47 {
48 assert(m_pool.size() <= m_capacity);
49
50 while (! m_pool.empty())
51 {
52 X* x = m_pool.top(); m_pool.pop();
53 #ifndef NDEBUG
55 #endif
56 delete x;
57 }
58
59 #ifndef NDEBUG
60 std::cerr << "Lost pointers: " << m_pointerCount << std::endl;
61 #endif
62 }
63
65 {
66 X* p = 0;
67
68 if (! m_pool.empty())
69 {
70 p = m_pool.top(); m_pool.pop();
71 #ifndef NDEBUG
72 m_hits++;
73 #endif
74 }
75 else
76 {
77 p = new X();
78 #ifndef NDEBUG
80 m_misses++;
81 #endif
82 }
83
84 return PoolPointer<X>(p, this);
85 }
86
87 void release(X* p)
88 {
89 if (m_pool.size() < m_capacity)
90 {
91 m_pool.push(p);
92 }
93 else
94 {
95 #ifndef NDEBUG
97 #endif
98 delete p;
99 }
100
101 assert(m_pool.size() <= m_capacity);
102 }
103
104 uint32_t getCapacity() const { return m_capacity; }
105 void setCapacity(uint32_t c)
106 {
107 assert (c >= 0);
108 m_capacity = c;
109 }
110
111 private:
112 uint32_t m_capacity;
113 std::stack<X*> m_pool;
114
115 #ifndef NDEBUG
116 public:
117 uint64_t m_hits;
118 uint64_t m_misses;
120 #endif
121 };
122}
123
Definition PointerPool.h:35
uint32_t m_capacity
Definition PointerPool.h:112
void release(X *p)
Definition PointerPool.h:87
uint32_t getCapacity() const
Definition PointerPool.h:104
PointerPool(uint32_t capacity)
Definition PointerPool.h:37
~PointerPool()
Definition PointerPool.h:46
uint64_t m_hits
Definition PointerPool.h:117
uint64_t m_pointerCount
Definition PointerPool.h:119
PoolPointer< X > acquire()
Definition PointerPool.h:64
std::stack< X * > m_pool
Definition PointerPool.h:113
void setCapacity(uint32_t c)
Definition PointerPool.h:105
uint64_t m_misses
Definition PointerPool.h:118
Definition PoolPointer.h:37
Definition PointerPool.h:33