Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
typed-allocation-manager.hpp
Go to the documentation of this file.
1/*
2 TYPED-ALLOCATION-MANAGER.hpp - abstract backbone to build custom memory managers
3
4 Copyright (C)
5 2009, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
14
58#ifndef LIB_TYPED_ALLOCATION_MANAGER_H
59#define LIB_TYPED_ALLOCATION_MANAGER_H
60
61#include "lib/error.hpp"
62#include "lib/meta/util.hpp"
63#include "lib/typed-counter.hpp"
64#include "include/logging.h"
65
66
67#include <utility>
68#include <memory>
69
70
71namespace lib {
72
73 using std::shared_ptr;
74
75
76
77
94 {
96
97 public:
98 template<class XX>
99 size_t numSlots() const;
100
101
102 /* ======= managing the created objects ============= */
103
106 template<class XOX>
107 class Killer
108 {
110
111 public:
112 void
113 operator() (XOX* victim)
114 {
115 REQUIRE (manager_);
117 manager_->destroyElement (victim);
118 }
119
120 protected:
122 : manager_(m)
123 { }
124 };
125
133 template<class XOX>
134 struct Slot
135 : private Killer<XOX>
136 {
139 void* const storage_;
140
144 shared_ptr<XOX>
145 build (XOX* toTrack)
146 {
147 return shared_ptr<XOX> (toTrack, getDeleter());
148 }
149
150 Killer<XOX> const&
152 {
153 return *this;
154 }
155
156 protected:
157 Slot(_TheManager* don, void* mem)
158 : Killer<XOX>(don)
159 , storage_(mem)
160 { }
161
163 };
164
165
166
167
168 /* ==== build objects with managed allocation ==== */
169
170 template< class XX, typename...ARGS>
171 shared_ptr<XX>
172 create (ARGS&& ...args)
173 {
174 Slot<XX> slot = allocateSlot<XX>();
175 try {
176 return slot.build (new(slot.storage_) XX (std::forward<ARGS> (args)...) );
177 }
178 catch(...)
179 {
180 releaseSlot<XX>(slot.storage_);
181 throw;
182 }
183 }
184
185
186
187
188
189
190 protected: /* ======= Managed Allocation Implementation ========== */
191
192 template<class XX>
193 Slot<XX>
195 {
197 TRACE (memory, "allocate «%s»", util::typeStr<XX>().c_str());
198 void* space = new char[sizeof(XX)];
199 allocCnt_.inc<XX>();
200 return Slot<XX> (this, space);
201 }
202
203 template<class XX>
204 void
205 releaseSlot (void* entry)
206 {
208 TRACE (memory, "release «%s»", util::typeStr<XX>().c_str());
209 typedef char Storage[sizeof(XX)];
210 delete[] reinterpret_cast<Storage*> (entry);
211 allocCnt_.dec<XX>();
212 }
213
214
215 template<class XX>
216 void
217 destroyElement (XX* entry)
218 {
219 if (!entry) return;
221 try
222 {
223 entry->~XX();
224 }
225 catch(...)
226 {
227 lumiera_err errorID = lumiera_error();
228 WARN (command_dbg, "dtor of «%s» failed: %s", util::typeStr(entry).c_str()
229 , errorID );
230 }
231 releaseSlot<XX> (entry);
232 }
233
234 template<class>
235 friend class Killer;
236
237
238 private:
240 };
241
242
243
244 template<class XX>
245 size_t
247 {
248 return allocCnt_.get<XX>();
249 }
250
251} // namespace lib
252#endif
opaque link to the manager, to be used by handles and smart-ptrs to trigger preconfigured destruction...
Foundation for a custom allocation manager, tracking the created objects by smart-ptrs.
shared_ptr< XX > create(ARGS &&...args)
Utility providing a set of counters, each tied to a specific type.
int64_t get() const
lumiera_err lumiera_error(void)
Get and clear current error state.
const char * lumiera_err
Definition error.h:48
Lumiera error handling (C++ interface).
This header is for including and configuring NoBug.
Simple and lightweight helpers for metaprogramming and type detection.
Implementation namespace for support and library code.
a token representing a newly opened slot capable for holding an object of type XOX .
void *const storage_
pointer to the allocated storage with sizeof(XOX) bytes
shared_ptr< XOX > build(XOX *toTrack)
build a refcounting smart-ptr, complete with back-link to the manager for de-allocation
Creating series of type-based contexts.