Lumiera  0.pre.03
»edit your freedom«
allocation-cluster.hpp
Go to the documentation of this file.
1 /*
2  ALLOCATION-CLUSTER.hpp - allocating and owning a pile of objects
3 
4  Copyright (C) Lumiera.org
5  2008, 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 
47 #ifndef LIB_ALLOCATION_CLUSTER_H
48 #define LIB_ALLOCATION_CLUSTER_H
49 
50 #include "lib/error.hpp"
51 #include "lib/nocopy.hpp"
52 #include "lib/sync-classlock.hpp"
53 #include "lib/scoped-holder.hpp"
55 
56 #include <utility>
57 #include <vector>
58 
59 
60 
61 namespace lib {
62 
63 
93  {
94 
95  public:
97  ~AllocationCluster () noexcept;
98 
99 
100  template<class TY, typename...ARGS>
101  TY&
102  create (ARGS&& ...args)
103  {
104  TY* obj = new(allocation<TY>()) TY (std::forward<ARGS> (args)...);
105  return commit(obj);
106  }
107 
108 
109  /* === diagnostics === */
110 
111  size_t size() const;
112 
113  template<class TY>
114  size_t count() const;
115 
116 
117  private:
119  template<class TY>
120  void*
121  allocation();
122 
124  template<class TY>
125  TY&
126  commit (TY* obj);
127 
128 
133  struct TypeInfo;
134 
139  class MemoryManager;
140 
144  template<class TY>
145  struct TypeSlot;
146 
147  static size_t maxTypeIDs;
148 
149 
152  using ManagerTable = std::vector<HMemManager,Allo>;
153 
154  ManagerTable typeHandlers_;
155 
156 
157 
158  HMemManager&
159  handler (size_t slot)
160  {
161  ASSERT (0<slot && slot<=typeHandlers_.size());
162  return typeHandlers_[slot-1];
163  }
164 
165  HMemManager const&
166  handler (size_t slot) const
167  {
168  ASSERT (0<slot && slot<=typeHandlers_.size());
169  return typeHandlers_[slot-1];
170  }
171 
174  void* initiateAlloc (size_t& slot);
175  void* initiateAlloc (TypeInfo type, size_t& slot);
176 
178  void finishAlloc (size_t& slot, void*);
179 
182  size_t countActiveInstances (size_t& slot) const;
183  };
184 
185 
186 
187 
188 
189  //-----implementation-details------------------------
190 
192  {
193  size_t allocSize;
194  void (*killIt)(void*);
195 
196  template<class TY>
197  TypeInfo(TY*)
198  : allocSize(sizeof(TY)),
199  killIt(&TypeSlot<TY>::kill)
200  { }
201 
202  TypeInfo()
203  : allocSize(0),
204  killIt(0)
205  { }
206  };
207 
208 
209 
210  template<class TY>
212  {
213  static size_t id_;
214 
215  static size_t &
216  get()
217  {
218  return id_;
219  }
220 
221  static TypeInfo
222  setup()
223  {
225  if (0 == id_)
226  id_= ++maxTypeIDs;
227 
228  return TypeInfo ((TY*) 0 );
229  }
230 
231  static void
232  kill (void* obj)
233  {
234  TY* p = static_cast<TY*>(obj);
235  ASSERT (p);
236  ASSERT (INSTANCEOF (TY,p));
237  p->~TY();
238  }
239 
240  };
241 
242 
244  template<class TY>
246 
247 
248 
249  template<class TY>
250  inline void*
252  {
253  void *mem = initiateAlloc (TypeSlot<TY>::get());
254  if (not mem)
256  ENSURE (mem);
257  return mem;
258  }
259 
260  template<class TY>
261  inline TY&
263  {
264  REQUIRE (obj);
266  return *obj;
267  }
268 
269 
274  template<class TY>
275  size_t
277  {
279  }
280 
281 
282 
283 } // namespace lib
284 #endif /*LIB_ALLOCATION_CLUSTER_H*/
static size_t maxTypeIDs
storage for static bookkeeping of type allocation slots
size_t count() const
helper for diagnostics
Some wrappers for coping with ownership problems.
void finishAlloc(size_t &slot, void *)
enrol the allocation after successful ctor call
#define INSTANCEOF(CLASS, EXPR)
shortcut for subclass test, intended for assertions only.
Definition: util.hpp:492
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
Implementation namespace for support and library code.
TY & commit(TY *obj)
finish the allocation after the ctor is successful
A mechanism to take ownership without allowing copy.
Addendum to scoped-holder.hpp for transferring the lifecycle management to another instance...
Mix-Ins to allow or prohibit various degrees of copying and cloning.
size_t countActiveInstances(size_t &slot) const
void * initiateAlloc(size_t &slot)
implementation of the actual memory allocation is pushed down to the MemoryManager impl...
ManagerTable typeHandlers_
table of active MemoryManager instances
A special implementation of lib::Sync, where the storage of the object monitor is associated directly...
Lumiera error handling (C++ interface).
A pile of objects sharing common allocation and lifecycle.
A synchronisation protection guard employing a lock scoped to the parameter type as a whole...
void * allocation()
initiate an allocation for the given type
auto setup(FUN &&workFun)
Helper: setup a Worker-Pool configuration for the test.
"Low-level" Memory manager for allocating small objects of a fixed size.
Extension to std::unique_ptr, allowing copy operations on empty pointers (i.e.
static size_t id_
table pos+1 of the memory manager in charge for type TY
~AllocationCluster() noexcept
On shutdown of the AllocationCluster we need to assure a certain destruction order is maintained by e...
organising the association Type -> table entry
AllocationCluster()
creating a new AllocationCluster prepares a table capable of holding the individual object families t...