UCommon
reuse.h
Go to the documentation of this file.
1// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2// Copyright (C) 2015 Cherokees of Idaho.
3//
4// This file is part of GNU uCommon C++.
5//
6// GNU uCommon C++ is free software: you can redistribute it and/or modify
7// it under the terms of the GNU Lesser General Public License as published
8// by the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// GNU uCommon C++ is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU Lesser General Public License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18
27#ifndef _UCOMMON_REUSE_H_
28#define _UCOMMON_REUSE_H_
29
30#ifndef _UCOMMON_THREAD_H_
31#include <ucommon/thread.h>
32#endif
33
34namespace ucommon {
35
36typedef unsigned short vectorsize_t;
37
45class __EXPORT ArrayReuse : public ReusableAllocator
46{
47private:
48 size_t objsize;
49 unsigned count, limit, used;
50 caddr_t mem;
51
52 __DELETE_DEFAULTS(ArrayReuse);
53
54protected:
55 ArrayReuse(size_t objsize, unsigned c);
56 ArrayReuse(size_t objsize, unsigned c, void *memory);
57
58public:
63
64protected:
65 bool avail(void) const;
66
67 ReusableObject *get(timeout_t timeout);
68 ReusableObject *get(void);
69 ReusableObject *request(void);
70};
71
79class __EXPORT PagerReuse : protected __PROTOCOL MemoryRedirect, protected ReusableAllocator
80{
81private:
82 unsigned limit, count;
83 size_t osize;
84
85 __DELETE_DEFAULTS(PagerReuse);
86
87protected:
88 PagerReuse(mempager *pager, size_t objsize, unsigned count);
90
91 bool avail(void) const;
92 ReusableObject *get(void);
93 ReusableObject *get(timeout_t timeout);
94 ReusableObject *request(void);
95};
96
103template<class T>
104class array_reuse : protected ArrayReuse
105{
106private:
107 __DELETE_DEFAULTS(array_reuse);
108
109public:
114 inline array_reuse(unsigned count) :
115 ArrayReuse(sizeof(T), count) {}
116
122 inline array_reuse(unsigned count, void *memory) :
123 ArrayReuse(sizeof(T), count, memory) {}
124
129 inline operator bool() const {
130 return avail();
131 }
132
137 inline bool operator!() const {
138 return !avail();
139 }
140
145 inline T* request(void) {
146 return static_cast<T*>(ArrayReuse::request());
147 }
148
154 inline T* get(void) {
155 return static_cast<T*>(ArrayReuse::get());
156 }
157
163 inline T* create(void) {
164 return init<T>(static_cast<T*>(ArrayReuse::get()));
165 }
166
173 inline T* get(timeout_t timeout) {
174 return static_cast<T*>(ArrayReuse::get(timeout));
175 }
176
183 inline T* create(timeout_t timeout) {
184 return init<T>(static_cast<T*>(ArrayReuse::get(timeout)));
185 }
186
191 inline void release(T *object) {
192 ArrayReuse::release(object);
193 }
194
200 inline operator T*() {
201 return array_reuse::get();
202 }
203
209 inline T *operator*() {
210 return array_reuse::get();
211 }
212};
213
220template <class T>
221class paged_reuse : protected PagerReuse
222{
223private:
224 __DELETE_DEFAULTS(paged_reuse);
225
226public:
234 inline paged_reuse(mempager *pager, unsigned count) :
235 PagerReuse(pager, sizeof(T), count) {}
236
241 inline operator bool() const {
242 return PagerReuse::avail();
243 }
244
249 inline bool operator!() const {
250 return !PagerReuse::avail();
251 }
252
258 inline T *get(void) {
259 return static_cast<T*>(PagerReuse::get());
260 }
261
268 inline T *create(void) {
269 return init<T>(static_cast<T*>(PagerReuse::get()));
270 }
271
278 inline T *get(timeout_t timeout) {
279 return static_cast<T*>(PagerReuse::get(timeout));
280 }
281
289 inline T *create(timeout_t timeout) {
290 return init<T>(static_cast<T*>(PagerReuse::get(timeout)));
291 }
292
297 inline T *request(void) {
298 return static_cast<T*>(PagerReuse::request());
299 }
300
305 inline void release(T *object) {
306 PagerReuse::release(object);
307 }
308
314 inline T *operator*() {
315 return paged_reuse::get();
316 }
317
323 inline operator T*() {
324 return paged_reuse::get();
325 }
326};
327
328} // namespace ucommon
329
330#endif
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
Definition platform.h:551
Common namespace for all ucommon objects.
Definition access.h:47
T & limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
Definition generics.h:468
Reusable objects for forming private heaps.
Definition linked.h:153
A managed private heap for small allocations.
Definition memory.h:185
Mempager managed type factory for pager pool objects.
Definition memory.h:854
A redirection base class for the memory protocol.
Definition protocols.h:101
An array of reusable objects.
Definition reuse.h:46
~ArrayReuse()
Destroy reusable private heap array.
A mempager source of reusable objects.
Definition reuse.h:80
An array of reusable types.
Definition reuse.h:105
T * request(void)
Request immediately next available typed object from the heap.
Definition reuse.h:145
array_reuse(unsigned count, void *memory)
Create reusable objects of specific type in preallocated memory.
Definition reuse.h:122
T * create(timeout_t timeout)
Create a typed object from the heap.
Definition reuse.h:183
T * create(void)
Create a typed object from the heap.
Definition reuse.h:163
T * get(void)
Get a typed object from the heap.
Definition reuse.h:154
array_reuse(unsigned count)
Create private heap of reusable objects of specified type.
Definition reuse.h:114
void release(T *object)
Release (return) a typed object back to the heap for re-use.
Definition reuse.h:191
bool operator!() const
Test if the entire heap has been allocated.
Definition reuse.h:137
T * operator*()
Get a typed object from the heap by pointer reference.
Definition reuse.h:209
T * get(timeout_t timeout)
Get a typed object from the heap.
Definition reuse.h:173
A reusable private pool of reusable types.
Definition reuse.h:222
T * create(void)
Get a typed object from the pager heap.
Definition reuse.h:268
void release(T *object)
Release (return) a typed object back to the pager heap for re-use.
Definition reuse.h:305
T * create(timeout_t timeout)
Create a typed object from the heap.
Definition reuse.h:289
T * get(void)
Get a typed object from the pager heap.
Definition reuse.h:258
T * operator*()
Get a typed object from the pager heap by type casting reference.
Definition reuse.h:314
bool operator!() const
Test if no objects are available for reuse or the pager.
Definition reuse.h:249
T * get(timeout_t timeout)
Get a typed object from the heap.
Definition reuse.h:278
paged_reuse(mempager *pager, unsigned count)
Create a managed reusable typed object pool.
Definition reuse.h:234
T * request(void)
Request immediately next available typed object from the pager heap.
Definition reuse.h:297
Class for resource bound memory pools between threads.
Definition thread.h:417
void release(ReusableObject *object)
Release resuable object.
Thread classes and sychronization objects.