Generated on Sat Feb 7 2015 02:01:28 for Gecode by doxygen 1.8.9.1
region.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Contributing authors:
7  * Filip Konvicka <filip.konvicka@logis.cz>
8  *
9  * Copyright:
10  * Christian Schulte, 2008
11  * LOGIS, s.r.o., 2009
12  *
13  * Last modified:
14  * $Date: 2013-02-20 18:27:38 +0100 (Wed, 20 Feb 2013) $ by $Author: schulte $
15  * $Revision: 13347 $
16  *
17  * This file is part of Gecode, the generic constraint
18  * development environment:
19  * http://www.gecode.org
20  *
21  * Permission is hereby granted, free of charge, to any person obtaining
22  * a copy of this software and associated documentation files (the
23  * "Software"), to deal in the Software without restriction, including
24  * without limitation the rights to use, copy, modify, merge, publish,
25  * distribute, sublicense, and/or sell copies of the Software, and to
26  * permit persons to whom the Software is furnished to do so, subject to
27  * the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be
30  * included in all copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39  *
40  */
41 
42 namespace Gecode {
43 
60  class Region {
62  private:
64  Space& home;
66  size_t free_reset;
68  class HeapInfo {
69  public:
71  unsigned int n;
73  unsigned int size;
75  void* blocks[1];
76  };
84  void* hi;
86  GECODE_KERNEL_EXPORT void* heap_alloc(size_t s);
88  GECODE_KERNEL_EXPORT void heap_free(void);
89  public:
91  Region(const Space& home);
93 
94 
100  template<class T>
101  T* alloc(long unsigned int n);
108  template<class T>
109  T* alloc(long int n);
116  template<class T>
117  T* alloc(unsigned int n);
124  template<class T>
125  T* alloc(int n);
135  template<class T>
136  void free(T* b, long unsigned int n);
146  template<class T>
147  void free(T* b, long int n);
157  template<class T>
158  void free(T* b, unsigned int n);
168  template<class T>
169  void free(T* b, int n);
181  template<class T>
182  T* realloc(T* b, long unsigned int n, long unsigned int m);
194  template<class T>
195  T* realloc(T* b, long int n, long int m);
207  template<class T>
208  T* realloc(T* b, unsigned int n, unsigned int m);
220  template<class T>
221  T* realloc(T* b, int n, int m);
223 
225  void* ralloc(size_t s);
233  void rfree(void* p, size_t s);
235 
237 
240  template<class T>
241  T& construct(void);
247  template<class T, typename A1>
248  T& construct(A1 const& a1);
254  template<class T, typename A1, typename A2>
255  T& construct(A1 const& a1, A2 const& a2);
261  template<class T, typename A1, typename A2, typename A3>
262  T& construct(A1 const& a1, A2 const& a2, A3 const& a3);
268  template<class T, typename A1, typename A2, typename A3, typename A4>
269  T& construct(A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4);
275  template<class T, typename A1, typename A2, typename A3, typename A4, typename A5>
276  T& construct(A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4, A5 const& a5);
278  ~Region(void);
280  private:
282  static void* operator new(size_t s) throw() { (void) s; return NULL; }
284  static void operator delete(void* p) { (void) p; };
286  Region(const Region& r) : home(r.home) {}
288  const Region& operator =(const Region&) { return *this; }
289  };
291 
292 
293  /*
294  * Implementation
295  *
296  */
299  : home(const_cast<Space&>(h)), free_reset(home.sm->region.free), hi(0) {}
300 
301  forceinline void*
302  Region::ralloc(size_t s) {
303  void* p;
304  if (home.sm->region_alloc(s,p))
305  return p;
306  return heap_alloc(s);
307  }
308 
309  forceinline void
310  Region::rfree(void*, size_t) {}
311 
314  home.sm->region.free = free_reset;
315  if (hi != NULL)
316  heap_free();
317  }
318 
319 
320  /*
321  * Typed allocation routines
322  *
323  */
324  template<class T>
325  forceinline T*
326  Region::alloc(long unsigned int n) {
327  T* p = static_cast<T*>(ralloc(sizeof(T)*n));
328  for (long unsigned int i=n; i--; )
329  (void) new (p+i) T();
330  return p;
331  }
332  template<class T>
333  forceinline T*
334  Region::alloc(long int n) {
335  assert(n >= 0);
336  return alloc<T>(static_cast<long unsigned int>(n));
337  }
338  template<class T>
339  forceinline T*
340  Region::alloc(unsigned int n) {
341  return alloc<T>(static_cast<long unsigned int>(n));
342  }
343  template<class T>
344  forceinline T*
346  assert(n >= 0);
347  return alloc<T>(static_cast<long unsigned int>(n));
348  }
349 
350  template<class T>
351  forceinline void
352  Region::free(T* b, long unsigned int n) {
353  for (long unsigned int i=n; i--; )
354  b[i].~T();
355  rfree(b,n*sizeof(T));
356  }
357  template<class T>
358  forceinline void
359  Region::free(T* b, long int n) {
360  assert(n >= 0);
361  free<T>(b,static_cast<long unsigned int>(n));
362  }
363  template<class T>
364  forceinline void
365  Region::free(T* b, unsigned int n) {
366  free<T>(b,static_cast<long unsigned int>(n));
367  }
368  template<class T>
369  forceinline void
370  Region::free(T* b, int n) {
371  assert(n >= 0);
372  free<T>(b,static_cast<long unsigned int>(n));
373  }
374 
375  template<class T>
376  forceinline T*
377  Region::realloc(T* b, long unsigned int n, long unsigned int m) {
378  if (n < m) {
379  T* p = static_cast<T*>(ralloc(sizeof(T)*m));
380  for (long unsigned int i=n; i--; )
381  (void) new (p+i) T(b[i]);
382  for (long unsigned int i=n; i<m; i++)
383  (void) new (p+i) T();
384  free<T>(b,n);
385  return p;
386  } else {
387  free<T>(b+m,m-n);
388  return b;
389  }
390  }
391  template<class T>
392  forceinline T*
393  Region::realloc(T* b, long int n, long int m) {
394  assert((n >= 0) && (m >= 0));
395  return realloc<T>(b,static_cast<long unsigned int>(n),
396  static_cast<long unsigned int>(m));
397  }
398  template<class T>
399  forceinline T*
400  Region::realloc(T* b, unsigned int n, unsigned int m) {
401  return realloc<T>(b,static_cast<long unsigned int>(n),
402  static_cast<long unsigned int>(m));
403  }
404  template<class T>
405  forceinline T*
406  Region::realloc(T* b, int n, int m) {
407  assert((n >= 0) && (m >= 0));
408  return realloc<T>(b,static_cast<long unsigned int>(n),
409  static_cast<long unsigned int>(m));
410  }
411 
412  /*
413  * Region construction support
414  *
415  */
416  template<class T>
417  forceinline T&
419  return alloc<T>(1);
420  }
421  template<class T, typename A1>
422  forceinline T&
423  Region::construct(A1 const& a1) {
424  T& t = *static_cast<T*>(ralloc(sizeof(T)));
425  new (&t) T(a1);
426  return t;
427  }
428  template<class T, typename A1, typename A2>
429  forceinline T&
430  Region::construct(A1 const& a1, A2 const& a2) {
431  T& t = *static_cast<T*>(ralloc(sizeof(T)));
432  new (&t) T(a1,a2);
433  return t;
434  }
435  template<class T, typename A1, typename A2, typename A3>
436  forceinline T&
437  Region::construct(A1 const& a1, A2 const& a2, A3 const& a3) {
438  T& t = *static_cast<T*>(ralloc(sizeof(T)));
439  new (&t) T(a1,a2,a3);
440  return t;
441  }
442  template<class T, typename A1, typename A2, typename A3, typename A4>
443  forceinline T&
444  Region::construct(A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4) {
445  T& t = *static_cast<T*>(ralloc(sizeof(T)));
446  new (&t) T(a1,a2,a3,a4);
447  return t;
448  }
449  template<class T, typename A1, typename A2, typename A3, typename A4, typename A5>
450  forceinline T&
451  Region::construct(A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4, A5 const& a5) {
452  T& t = *static_cast<T*>(ralloc(sizeof(T)));
453  new (&t) T(a1,a2,a3,a4,a5);
454  return t;
455  }
456 
457 }
458 
459 // STATISTICS: kernel-memory
void rfree(void *p, size_t s)
Free memory previously allocated.
Definition: region.hpp:310
NodeType t
Type of node.
Definition: bool-expr.cpp:234
T & construct(void)
Constructs a single object of type T from region using the default constructor.
Definition: region.hpp:418
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition: region.hpp:326
~Region(void)
Return memory.
Definition: region.hpp:313
bool region_alloc(size_t s, void *&p)
Return memory chunk if available.
Computation spaces.
Definition: core.hpp:1362
Region(const Space &home)
Initialize region from space.
Definition: region.hpp:298
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
unsigned int size(I &i)
Size of all ranges of range iterator i.
#define GECODE_KERNEL_EXPORT
Definition: kernel.hh:70
#define forceinline
Definition: config.hpp:132
void free(T *b, long unsigned int n)
Delete n objects allocated from the region starting at b.
Definition: region.hpp:352
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
size_t free
Amount of free memory.
void * ralloc(size_t s)
Allocate memory from region.
Definition: region.hpp:302
T * realloc(T *b, long unsigned int n, long unsigned int m)
Reallocate block of n objects starting at b to m objects of type T from the region.
Definition: region.hpp:377