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)
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 
71 namespace 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  {
109  _TheManager* manager_;
110 
111  public:
112  void
113  operator() (XOX* victim)
114  {
115  REQUIRE (manager_);
117  manager_->destroyElement (victim);
118  }
119 
120  protected:
121  Killer(_TheManager* m)
122  : manager_(m)
123  { }
124  };
125 
133  template<class XOX>
134  struct Slot
135  : private Killer<XOX>
136  {
139  void* const storage_;
140 
145  build (XOX* toTrack)
146  {
147  return shared_ptr<XOX> (toTrack, getDeleter());
148  }
149 
150  Killer<XOX> const&
151  getDeleter()
152  {
153  return *this;
154  }
155 
156  protected:
157  Slot(_TheManager* don, void* mem)
158  : Killer<XOX>(don)
159  , storage_(mem)
160  { }
161 
162  friend class TypedAllocationManager;
163  };
164 
165 
166 
167 
168  /* ==== build objects with managed allocation ==== */
169 
170  template< class XX, typename...ARGS>
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>
194  allocateSlot ()
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:
239  lib::TypedCounter allocCnt_;
240  };
241 
242 
243 
244  template<class XX>
245  size_t
246  TypedAllocationManager::numSlots() const
247  {
248  return allocCnt_.get<XX>();
249  }
250 
251 } // namespace lib
252 #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:115
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).