/usr/include/ucommon/reuse.h is in libucommon-dev 7.0.0-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
// Copyright (C) 2015 Cherokees of Idaho.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
/**
* Basic array and reusable object factory heap support.
* This offers ucommon support for forming reusable object pools. Reusable
* object pools can be tied to local heaps and offer a means to create type
* factories that do not require global locking through malloc.
* @file ucommon/reuse.h
*/
#ifndef _UCOMMON_REUSE_H_
#define _UCOMMON_REUSE_H_
#ifndef _UCOMMON_THREAD_H_
#include <ucommon/thread.h>
#endif
namespace ucommon {
typedef unsigned short vectorsize_t;
/**
* An array of reusable objects. This class is used to support the
* array_use template. A pool of objects are created which can be
* allocated as needed. Deallocated objects are returned to the pool
* so they can be reallocated later. This is a private fixed size heap.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT ArrayReuse : public ReusableAllocator
{
private:
size_t objsize;
unsigned count, limit, used;
caddr_t mem;
__DELETE_DEFAULTS(ArrayReuse);
protected:
ArrayReuse(size_t objsize, unsigned c);
ArrayReuse(size_t objsize, unsigned c, void *memory);
public:
/**
* Destroy reusable private heap array.
*/
~ArrayReuse();
protected:
bool avail(void) const;
ReusableObject *get(timeout_t timeout);
ReusableObject *get(void);
ReusableObject *request(void);
};
/**
* A mempager source of reusable objects. This is used by the reuse_pager
* template to allocate new objects either from a memory pager used as
* a private heap, or from previously allocated objects that have been
* returned for reuse.
* @author David Sugar <dyfet@gnutelephony.org>
*/
class __EXPORT PagerReuse : protected __PROTOCOL MemoryRedirect, protected ReusableAllocator
{
private:
unsigned limit, count;
size_t osize;
__DELETE_DEFAULTS(PagerReuse);
protected:
PagerReuse(mempager *pager, size_t objsize, unsigned count);
~PagerReuse();
bool avail(void) const;
ReusableObject *get(void);
ReusableObject *get(timeout_t timeout);
ReusableObject *request(void);
};
/**
* An array of reusable types. A pool of typed objects is created which can
* be allocated as needed. Deallocated typed objects are returned to the pool
* so they can be reallocated later. This is a private fixed size heap.
* @author David Sugar <dyfet@gnutelephony.org>
*/
template<class T>
class array_reuse : protected ArrayReuse
{
private:
__DELETE_DEFAULTS(array_reuse);
public:
/**
* Create private heap of reusable objects of specified type.
* @param count of objects of specified type to allocate.
*/
inline array_reuse(unsigned count) :
ArrayReuse(sizeof(T), count) {}
/**
* Create reusable objects of specific type in preallocated memory.
* @param count of objects of specified type in memory.
* @param memory to use.
*/
inline array_reuse(unsigned count, void *memory) :
ArrayReuse(sizeof(T), count, memory) {}
/**
* Test if typed objects available in heap or re-use list.
* @return true if objects still are available.
*/
inline operator bool() const {
return avail();
}
/**
* Test if the entire heap has been allocated.
* @return true if no objects are available.
*/
inline bool operator!() const {
return !avail();
}
/**
* Request immediately next available typed object from the heap.
* @return typed object pointer or NULL if heap is empty.
*/
inline T* request(void) {
return static_cast<T*>(ArrayReuse::request());
}
/**
* Get a typed object from the heap. This function blocks when the
* heap is empty until an object is returned to the heap.
* @return typed object pointer from heap.
*/
inline T* get(void) {
return static_cast<T*>(ArrayReuse::get());
}
/**
* Create a typed object from the heap. This function blocks when the
* heap is empty until an object is returned to the heap.
* @return typed object pointer from heap.
*/
inline T* create(void) {
return init<T>(static_cast<T*>(ArrayReuse::get()));
}
/**
* Get a typed object from the heap. This function blocks until the
* the heap has an object to return or the timer has expired.
* @param timeout to wait for heap in milliseconds.
* @return typed object pointer from heap or NULL if timeout.
*/
inline T* get(timeout_t timeout) {
return static_cast<T*>(ArrayReuse::get(timeout));
}
/**
* Create a typed object from the heap. This function blocks until the
* the heap has an object to return or the timer has expired.
* @param timeout to wait for heap in milliseconds.
* @return typed object pointer from heap or NULL if timeout.
*/
inline T* create(timeout_t timeout) {
return init<T>(static_cast<T*>(ArrayReuse::get(timeout)));
}
/**
* Release (return) a typed object back to the heap for re-use.
* @param object to return.
*/
inline void release(T *object) {
ArrayReuse::release(object);
}
/**
* Get a typed object from the heap by type casting reference. This
* function blocks while the heap is empty.
* @return typed object pointer from heap.
*/
inline operator T*() {
return array_reuse::get();
}
/**
* Get a typed object from the heap by pointer reference. This
* function blocks while the heap is empty.
* @return typed object pointer from heap.
*/
inline T *operator*() {
return array_reuse::get();
}
};
/**
* A reusable private pool of reusable types. A pool of typed objects is
* created which can be allocated from a memory pager. Deallocated typed
* objects are also returned to this pool so they can be reallocated later.
* @author David Sugar <dyfet@gnutelephony.org>
*/
template <class T>
class paged_reuse : protected PagerReuse
{
private:
__DELETE_DEFAULTS(paged_reuse);
public:
/**
* Create a managed reusable typed object pool. This manages a heap of
* typed objects that can either be reused from released objects or
* allocate from an existing memory pager pool.
* @param pager pool to allocate from.
* @param count of objects of specified type to allocate.
*/
inline paged_reuse(mempager *pager, unsigned count) :
PagerReuse(pager, sizeof(T), count) {}
/**
* Test if typed objects available from the pager or re-use list.
* @return true if objects still are available.
*/
inline operator bool() const {
return PagerReuse::avail();
}
/**
* Test if no objects are available for reuse or the pager.
* @return true if no objects are available.
*/
inline bool operator!() const {
return !PagerReuse::avail();
}
/**
* Get a typed object from the pager heap. This function blocks when the
* heap is empty until an object is returned to the heap.
* @return typed object pointer from heap.
*/
inline T *get(void) {
return static_cast<T*>(PagerReuse::get());
}
/**
* Get a typed object from the pager heap. This function blocks when the
* heap is empty until an object is returned to the heap. The objects
* default constructor is used.
* @return typed object pointer from heap.
*/
inline T *create(void) {
return init<T>(static_cast<T*>(PagerReuse::get()));
}
/**
* Get a typed object from the heap. This function blocks until the
* the heap has an object to return or the timer has expired.
* @param timeout to wait for heap in milliseconds.
* @return typed object pointer from heap or NULL if timeout.
*/
inline T *get(timeout_t timeout) {
return static_cast<T*>(PagerReuse::get(timeout));
}
/**
* Create a typed object from the heap. This function blocks until the
* the heap has an object to return or the timer has expired. The
* objects default constructor is used.
* @param timeout to wait for heap in milliseconds.
* @return typed object pointer from heap or NULL if timeout.
*/
inline T *create(timeout_t timeout) {
return init<T>(static_cast<T*>(PagerReuse::get(timeout)));
}
/**
* Request immediately next available typed object from the pager heap.
* @return typed object pointer or NULL if heap is empty.
*/
inline T *request(void) {
return static_cast<T*>(PagerReuse::request());
}
/**
* Release (return) a typed object back to the pager heap for re-use.
* @param object to return.
*/
inline void release(T *object) {
PagerReuse::release(object);
}
/**
* Get a typed object from the pager heap by type casting reference. This
* function blocks while the heap is empty.
* @return typed object pointer from heap.
*/
inline T *operator*() {
return paged_reuse::get();
}
/**
* Get a typed object from the pager heap by pointer reference. This
* function blocks while the heap is empty.
* @return typed object pointer from heap.
*/
inline operator T*() {
return paged_reuse::get();
}
};
} // namespace ucommon
#endif
|