Lumiera  0.pre.03
»edityourfreedom«
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 <vector>
57 
58 
59 
60 namespace lib {
61 
62 
92  {
93 
94  public:
96  ~AllocationCluster () noexcept;
97 
98 
99  template<class TY>
100  TY&
102  {
103  TY* obj = new(allocation<TY>()) TY();
104  return commit(obj);
105  }
106 
107  template<class TY, typename P0>
108  TY&
109  create (P0& p0)
110  {
111  TY* obj = new(allocation<TY>()) TY (p0);
112  return commit(obj);
113  }
114 
115  template<class TY, typename P0, typename P1>
116  TY&
117  create (P0& p0, P1& p1)
118  {
119  TY* obj = new(allocation<TY>()) TY (p0,p1);
120  return commit(obj);
121  }
122 
123  template<class TY, typename P0, typename P1, typename P2>
124  TY&
125  create (P0& p0, P1& p1, P2& p2)
126  {
127  TY* obj = new(allocation<TY>()) TY (p0,p1,p2);
128  return commit(obj);
129  }
130 
131  template<class TY, typename P0, typename P1, typename P2, typename P3>
132  TY&
133  create (P0& p0, P1& p1, P2& p2, P3& p3)
134  {
135  TY* obj = new(allocation<TY>()) TY (p0,p1,p2,p3);
136  return commit(obj);
137  }
138 
139 
140  /* === diagnostics === */
141 
142  size_t size() const;
143 
144  template<class TY>
145  size_t count() const;
146 
147 
148  private:
150  template<class TY>
151  void*
152  allocation();
153 
155  template<class TY>
156  TY&
157  commit (TY* obj);
158 
159 
164  struct TypeInfo;
165 
170  class MemoryManager;
171 
175  template<class TY>
176  struct TypeSlot;
177 
178  static size_t maxTypeIDs;
179 
180 
183  typedef std::vector<HMemManager,Allo> ManagerTable;
184 
185  ManagerTable typeHandlers_;
186 
187 
188 
189  HMemManager&
190  handler (size_t slot)
191  {
192  ASSERT (0<slot && slot<=typeHandlers_.size());
193  return typeHandlers_[slot-1];
194  }
195 
196  HMemManager const&
197  handler (size_t slot) const
198  {
199  ASSERT (0<slot && slot<=typeHandlers_.size());
200  return typeHandlers_[slot-1];
201  }
202 
205  void* initiateAlloc (size_t& slot);
206  void* initiateAlloc (TypeInfo type, size_t& slot);
207 
209  void finishAlloc (size_t& slot, void*);
210 
213  size_t countActiveInstances (size_t& slot) const;
214  };
215 
216 
217 
218 
219 
220  //-----implementation-details------------------------
221 
223  {
224  size_t allocSize;
225  void (*killIt)(void*);
226 
227  template<class TY>
228  TypeInfo(TY*)
229  : allocSize(sizeof(TY)),
230  killIt(&TypeSlot<TY>::kill)
231  { }
232 
234  : allocSize(0),
235  killIt(0)
236  { }
237  };
238 
239 
240 
241  template<class TY>
243  {
244  static size_t id_;
245 
246  static size_t &
247  get()
248  {
249  return id_;
250  }
251 
252  static TypeInfo
254  {
256  if (!id_)
257  id_= ++maxTypeIDs;
258 
259  return TypeInfo ((TY*) 0 );
260  }
261 
262  static void
263  kill (void* obj)
264  {
265  TY* p = static_cast<TY*>(obj);
266  ASSERT (p);
267  ASSERT (INSTANCEOF (TY,p));
268  p->~TY();
269  }
270 
271  };
272 
273 
275  template<class TY>
277 
278 
279 
280  template<class TY>
281  inline void*
283  {
284  void *mem = initiateAlloc (TypeSlot<TY>::get());
285  if (!mem)
287  ENSURE (mem);
288  return mem;
289  }
290 
291  template<class TY>
292  inline TY&
294  {
295  REQUIRE (obj);
297  return *obj;
298  }
299 
300 
305  template<class TY>
306  size_t
308  {
310  }
311 
312 
313 
314 
315 
316 } // namespace lib
317 #endif
HMemManager const & handler(size_t slot) const
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
TY & create(P0 &p0, P1 &p1, P2 &p2)
TY & create(P0 &p0, P1 &p1)
#define INSTANCEOF(CLASS, EXPR)
shortcut for subclass test, intended for assertions only.
Definition: util.hpp:421
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.
std::vector< HMemManager, Allo > ManagerTable
TY & create(P0 &p0, P1 &p1, P2 &p2, P3 &p3)
size_t countActiveInstances(size_t &slot) const
HMemManager & handler(size_t slot)
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...
Allocator_TransferNoncopyable< HMemManager > Allo
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
"Low-level" Memory manager for allocating small objects of a fixed size.
ScopedPtrHolder< MemoryManager > HMemManager
Extension to std::unique_ptr, allowing copy operations on empty pointers (i.e.
ENSURE(r==&pq)
static size_t id_
table pos+1 of the memory manager in charge for type TY
HashVal slot(Query< asset::Pipe > const &query)
yield a suitable table slot for this query
~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...