Lumiera  0.pre.03
»edit your freedom«
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) Lumiera.org
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
67 #ifndef LIB_TYPED_ALLOCATION_MANAGER_H
68 #define LIB_TYPED_ALLOCATION_MANAGER_H
69 
70 #include "lib/error.hpp"
71 #include "lib/meta/util.hpp"
72 #include "lib/typed-counter.hpp"
73 #include "include/logging.h"
74 
75 
76 #include <utility>
77 #include <memory>
78 
79 
80 namespace lib {
81 
82  using std::shared_ptr;
83 
84 
85 
86 
103  {
105 
106  public:
107  template<class XX>
108  size_t numSlots() const;
109 
110 
111  /* ======= managing the created objects ============= */
112 
115  template<class XOX>
116  class Killer
117  {
118  _TheManager* manager_;
119 
120  public:
121  void
122  operator() (XOX* victim)
123  {
124  REQUIRE (manager_);
126  manager_->destroyElement (victim);
127  }
128 
129  protected:
130  Killer(_TheManager* m)
131  : manager_(m)
132  { }
133  };
134 
142  template<class XOX>
143  struct Slot
144  : private Killer<XOX>
145  {
148  void* const storage_;
149 
154  build (XOX* toTrack)
155  {
156  return shared_ptr<XOX> (toTrack, getDeleter());
157  }
158 
159  Killer<XOX> const&
160  getDeleter()
161  {
162  return *this;
163  }
164 
165  protected:
166  Slot(_TheManager* don, void* mem)
167  : Killer<XOX>(don)
168  , storage_(mem)
169  { }
170 
171  friend class TypedAllocationManager;
172  };
173 
174 
175 
176 
177  /* ==== build objects with managed allocation ==== */
178 
179  template< class XX, typename...ARGS>
181  create (ARGS&& ...args)
182  {
183  Slot<XX> slot = allocateSlot<XX>();
184  try {
185  return slot.build (new(slot.storage_) XX (std::forward<ARGS> (args)...) );
186  }
187  catch(...)
188  {
189  releaseSlot<XX>(slot.storage_);
190  throw;
191  }
192  }
193 
194 
195 
196 
197 
198 
199  protected: /* ======= Managed Allocation Implementation ========== */
200 
201  template<class XX>
202  Slot<XX>
203  allocateSlot ()
204  {
206  TRACE (memory, "allocate «%s»", util::typeStr<XX>().c_str());
207  void* space = new char[sizeof(XX)];
208  allocCnt_.inc<XX>();
209  return Slot<XX> (this, space);
210  }
211 
212  template<class XX>
213  void
214  releaseSlot (void* entry)
215  {
217  TRACE (memory, "release «%s»", util::typeStr<XX>().c_str());
218  typedef char Storage[sizeof(XX)];
219  delete[] reinterpret_cast<Storage*> (entry);
220  allocCnt_.dec<XX>();
221  }
222 
223 
224  template<class XX>
225  void
226  destroyElement (XX* entry)
227  {
228  if (!entry) return;
230  try
231  {
232  entry->~XX();
233  }
234  catch(...)
235  {
236  lumiera_err errorID = lumiera_error();
237  WARN (command_dbg, "dtor of «%s» failed: %s", util::typeStr(entry).c_str()
238  , errorID );
239  }
240  releaseSlot<XX> (entry);
241  }
242 
243  template<class>
244  friend class Killer;
245 
246 
247  private:
248  lib::TypedCounter allocCnt_;
249  };
250 
251 
252 
253  template<class XX>
254  size_t
255  TypedAllocationManager::numSlots() const
256  {
257  return allocCnt_.get<XX>();
258  }
259 
260 } // namespace lib
261 #endif
Creating series of type-based contexts.
Utility providing a set of counters, each tied to a specific type.
void *const storage_
pointer to the allocated storage with sizeof(XOX) bytes
AnyPair entry(Query< TY > const &query, typename WrapReturn< TY >::Wrapper &obj)
helper to simplify creating mock table entries, wrapped correctly
Simple and lightweight helpers for metaprogramming and type detection.
shared_ptr< XOX > build(XOX *toTrack)
build a refcounting smart-ptr, complete with back-link to the manager for de-allocation ...
This header is for including and configuring NoBug.
Implementation namespace for support and library code.
a token representing a newly opened slot capable for holding an object of type XOX ...
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:124
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.
Lumiera error handling (C++ interface).