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 
34 namespace ucommon {
35 
36 typedef unsigned short vectorsize_t;
37 
45 class __EXPORT ArrayReuse : public ReusableAllocator
46 {
47 private:
48  size_t objsize;
49  unsigned count, limit, used;
50  caddr_t mem;
51 
52  __DELETE_DEFAULTS(ArrayReuse);
53 
54 protected:
55  ArrayReuse(size_t objsize, unsigned c);
56  ArrayReuse(size_t objsize, unsigned c, void *memory);
57 
58 public:
62  ~ArrayReuse();
63 
64 protected:
65  bool avail(void) const;
66 
67  ReusableObject *get(timeout_t timeout);
68  ReusableObject *get(void);
69  ReusableObject *request(void);
70 };
71 
79 class __EXPORT PagerReuse : protected __PROTOCOL MemoryRedirect, protected ReusableAllocator
80 {
81 private:
82  unsigned limit, count;
83  size_t osize;
84 
85  __DELETE_DEFAULTS(PagerReuse);
86 
87 protected:
88  PagerReuse(mempager *pager, size_t objsize, unsigned count);
89  ~PagerReuse();
90 
91  bool avail(void) const;
92  ReusableObject *get(void);
93  ReusableObject *get(timeout_t timeout);
94  ReusableObject *request(void);
95 };
96 
103 template<class T>
104 class array_reuse : protected ArrayReuse
105 {
106 private:
107  __DELETE_DEFAULTS(array_reuse);
108 
109 public:
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 
220 template <class T>
221 class paged_reuse : protected PagerReuse
222 {
223 private:
224  __DELETE_DEFAULTS(paged_reuse);
225 
226 public:
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
A mempager source of reusable objects.
Definition: reuse.h:79
Class for resource bound memory pools between threads.
Definition: thread.h:416
array_reuse(unsigned count)
Create private heap of reusable objects of specified type.
Definition: reuse.h:114
T * create(void)
Get a typed object from the pager heap.
Definition: reuse.h:268
Mempager managed type factory for pager pool objects.
Definition: memory.h:853
A reusable private pool of reusable types.
Definition: reuse.h:221
bool operator!() const
Test if the entire heap has been allocated.
Definition: reuse.h:137
T * create(timeout_t timeout)
Create a typed object from the heap.
Definition: reuse.h:289
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
void release(T *object)
Release (return) a typed object back to the pager heap for re-use.
Definition: reuse.h:305
void release(T *object)
Release (return) a typed object back to the heap for re-use.
Definition: reuse.h:191
T &() limit(T &value, T &low, T &high)
Convenience macro to range restrict values.
Definition: generics.h:468
T * request(void)
Request immediately next available typed object from the pager heap.
Definition: reuse.h:297
array_reuse(unsigned count, void *memory)
Create reusable objects of specific type in preallocated memory.
Definition: reuse.h:122
Thread classes and sychronization objects.
T * operator *()
Get a typed object from the pager heap by type casting reference.
Definition: reuse.h:314
void release(ReusableObject *object)
Release resuable object.
A managed private heap for small allocations.
Definition: memory.h:184
Reusable objects for forming private heaps.
Definition: linked.h:152
An array of reusable objects.
Definition: reuse.h:45
T * get(void)
Get a typed object from the pager heap.
Definition: reuse.h:258
T * create(timeout_t timeout)
Create a typed object from the heap.
Definition: reuse.h:183
Common namespace for all ucommon objects.
Definition: access.h:47
T * get(timeout_t timeout)
Get a typed object from the heap.
Definition: reuse.h:173
T * request(void)
Request immediately next available typed object from the heap.
Definition: reuse.h:145
T * create(void)
Create a typed object from the heap.
Definition: reuse.h:163
An array of reusable types.
Definition: reuse.h:104
paged_reuse(mempager *pager, unsigned count)
Create a managed reusable typed object pool.
Definition: reuse.h:234
A redirection base class for the memory protocol.
Definition: protocols.h:100
T * operator *()
Get a typed object from the heap by pointer reference.
Definition: reuse.h:209
T * get(void)
Get a typed object from the heap.
Definition: reuse.h:154