Generated on Sat Jun 2 2018 07:17:44 for Gecode by doxygen 1.8.13
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  * Copyright:
7  * Christian Schulte, 2008
8  *
9  * This file is part of Gecode, the generic constraint
10  * development environment:
11  * http://www.gecode.org
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  */
33 
34 namespace Gecode {
35 
52  class Region {
54  private:
56  class Chunk : public HeapAllocated {
57  public:
59  size_t free;
61  double area[Kernel::MemoryConfig::region_area_size / sizeof(double)];
63  Chunk* next;
65  bool alloc(size_t s, void*& p);
67  void reset(void);
68  };
70  Chunk* chunk;
72  class GECODE_KERNEL_EXPORT Pool {
73  protected:
75  Chunk* c;
77  unsigned int n_c;
79  Support::Mutex m;
80  public:
82  Pool(void);
84  Chunk* chunk(void);
86  void chunk(Chunk* u);
88  ~Pool(void);
89  };
91  GECODE_KERNEL_EXPORT static Pool& pool();
93  class HeapInfo {
94  public:
96  unsigned int n;
98  unsigned int size;
100  void* blocks[1];
101  };
109  void* hi;
111  GECODE_KERNEL_EXPORT void* heap_alloc(size_t s);
113  GECODE_KERNEL_EXPORT void heap_free(void);
114  public:
116  Region(void);
123  void free(void);
125 
126 
132  template<class T>
133  T* alloc(long unsigned int n);
140  template<class T>
141  T* alloc(long int n);
148  template<class T>
149  T* alloc(unsigned int n);
156  template<class T>
157  T* alloc(int n);
167  template<class T>
168  void free(T* b, long unsigned int n);
178  template<class T>
179  void free(T* b, long int n);
189  template<class T>
190  void free(T* b, unsigned int n);
200  template<class T>
201  void free(T* b, int n);
213  template<class T>
214  T* realloc(T* b, long unsigned int n, long unsigned int m);
226  template<class T>
227  T* realloc(T* b, long int n, long int m);
239  template<class T>
240  T* realloc(T* b, unsigned int n, unsigned int m);
252  template<class T>
253  T* realloc(T* b, int n, int m);
255 
257  void* ralloc(size_t s);
265  void rfree(void* p, size_t s);
267 
269 
272  template<class T>
273  T& construct(void);
279  template<class T, typename A1>
280  T& construct(A1 const& a1);
286  template<class T, typename A1, typename A2>
287  T& construct(A1 const& a1, A2 const& a2);
293  template<class T, typename A1, typename A2, typename A3>
294  T& construct(A1 const& a1, A2 const& a2, A3 const& a3);
300  template<class T, typename A1, typename A2, typename A3, typename A4>
301  T& construct(A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4);
307  template<class T, typename A1, typename A2, typename A3, typename A4, typename A5>
308  T& construct(A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4, A5 const& a5);
310  ~Region(void);
312  private:
314  static void* operator new(size_t s) throw() { (void) s; return NULL; }
316  static void operator delete(void* p) { (void) p; };
318  Region(const Region&) {}
320  const Region& operator =(const Region&) { return *this; }
321  };
323 
324 
325  /*
326  * Implementation
327  *
328  */
329  forceinline bool
330  Region::Chunk::alloc(size_t s, void*& p) {
332  if (s > free)
333  return false;
334  free -= s;
335  p = ptr_cast<char*>(&area[0]) + free;
336  return true;
337  }
338 
339  forceinline void
340  Region::Chunk::reset(void) {
342  }
343 
344 
347  : chunk(pool().chunk()), hi(0) {}
348 
349  forceinline void
350  Region::free(void) {
351  chunk->reset();
352  }
353 
354  forceinline void*
355  Region::ralloc(size_t s) {
356  void* p;
357  if (chunk->alloc(s,p))
358  return p;
359  else
360  return heap_alloc(s);
361  }
362 
363  forceinline void
364  Region::rfree(void*, size_t) {}
365 
368  pool().chunk(chunk);
369  if (hi != NULL)
370  heap_free();
371  }
372 
373 
374  /*
375  * Typed allocation routines
376  *
377  */
378  template<class T>
379  forceinline T*
380  Region::alloc(long unsigned int n) {
381  T* p = static_cast<T*>(ralloc(sizeof(T)*n));
382  for (long unsigned int i=n; i--; )
383  (void) new (p+i) T();
384  return p;
385  }
386  template<class T>
387  forceinline T*
388  Region::alloc(long int n) {
389  assert(n >= 0);
390  return alloc<T>(static_cast<long unsigned int>(n));
391  }
392  template<class T>
393  forceinline T*
394  Region::alloc(unsigned int n) {
395  return alloc<T>(static_cast<long unsigned int>(n));
396  }
397  template<class T>
398  forceinline T*
400  assert(n >= 0);
401  return alloc<T>(static_cast<long unsigned int>(n));
402  }
403 
404  template<class T>
405  forceinline void
406  Region::free(T* b, long unsigned int n) {
407  for (long unsigned int i=n; i--; )
408  b[i].~T();
409  rfree(b,n*sizeof(T));
410  }
411  template<class T>
412  forceinline void
413  Region::free(T* b, long int n) {
414  assert(n >= 0);
415  free<T>(b,static_cast<long unsigned int>(n));
416  }
417  template<class T>
418  forceinline void
419  Region::free(T* b, unsigned int n) {
420  free<T>(b,static_cast<long unsigned int>(n));
421  }
422  template<class T>
423  forceinline void
424  Region::free(T* b, int n) {
425  assert(n >= 0);
426  free<T>(b,static_cast<long unsigned int>(n));
427  }
428 
429  template<class T>
430  forceinline T*
431  Region::realloc(T* b, long unsigned int n, long unsigned int m) {
432  if (n < m) {
433  T* p = static_cast<T*>(ralloc(sizeof(T)*m));
434  for (long unsigned int i=n; i--; )
435  (void) new (p+i) T(b[i]);
436  for (long unsigned int i=n; i<m; i++)
437  (void) new (p+i) T();
438  free<T>(b,n);
439  return p;
440  } else {
441  free<T>(b+m,m-n);
442  return b;
443  }
444  }
445  template<class T>
446  forceinline T*
447  Region::realloc(T* b, long int n, long int m) {
448  assert((n >= 0) && (m >= 0));
449  return realloc<T>(b,static_cast<long unsigned int>(n),
450  static_cast<long unsigned int>(m));
451  }
452  template<class T>
453  forceinline T*
454  Region::realloc(T* b, unsigned int n, unsigned int m) {
455  return realloc<T>(b,static_cast<long unsigned int>(n),
456  static_cast<long unsigned int>(m));
457  }
458  template<class T>
459  forceinline T*
460  Region::realloc(T* b, int n, int m) {
461  assert((n >= 0) && (m >= 0));
462  return realloc<T>(b,static_cast<long unsigned int>(n),
463  static_cast<long unsigned int>(m));
464  }
465 
466  /*
467  * Region construction support
468  *
469  */
470  template<class T>
471  forceinline T&
473  return alloc<T>(1);
474  }
475  template<class T, typename A1>
476  forceinline T&
477  Region::construct(A1 const& a1) {
478  T& t = *static_cast<T*>(ralloc(sizeof(T)));
479  new (&t) T(a1);
480  return t;
481  }
482  template<class T, typename A1, typename A2>
483  forceinline T&
484  Region::construct(A1 const& a1, A2 const& a2) {
485  T& t = *static_cast<T*>(ralloc(sizeof(T)));
486  new (&t) T(a1,a2);
487  return t;
488  }
489  template<class T, typename A1, typename A2, typename A3>
490  forceinline T&
491  Region::construct(A1 const& a1, A2 const& a2, A3 const& a3) {
492  T& t = *static_cast<T*>(ralloc(sizeof(T)));
493  new (&t) T(a1,a2,a3);
494  return t;
495  }
496  template<class T, typename A1, typename A2, typename A3, typename A4>
497  forceinline T&
498  Region::construct(A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4) {
499  T& t = *static_cast<T*>(ralloc(sizeof(T)));
500  new (&t) T(a1,a2,a3,a4);
501  return t;
502  }
503  template<class T, typename A1, typename A2, typename A3, typename A4, typename A5>
504  forceinline T&
505  Region::construct(A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4, A5 const& a5) {
506  T& t = *static_cast<T*>(ralloc(sizeof(T)));
507  new (&t) T(a1,a2,a3,a4,a5);
508  return t;
509  }
510 
511 }
512 
513 // STATISTICS: kernel-memory
void rfree(void *p, size_t s)
Free memory previously allocated.
Definition: region.hpp:364
void free(void)
Free allocate memory.
Definition: region.hpp:350
NodeType t
Type of node.
Definition: bool-expr.cpp:230
T & construct(void)
Constructs a single object of type T from region using the default constructor.
Definition: region.hpp:472
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition: region.hpp:380
void align(size_t &s)
Align size s to the required alignment.
Definition: config.hpp:144
Region(void)
Initialize region.
Definition: region.hpp:346
~Region(void)
Return memory.
Definition: region.hpp:367
#define forceinline
Definition: config.hpp:185
Gecode::FloatVal c(-8, 8)
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
unsigned int size(I &i)
Size of all ranges of range iterator i.
const size_t region_area_size
Size of region area.
Definition: config.hpp:134
union Gecode::@585::NNF::@62 u
Union depending on nodetype t.
#define GECODE_KERNEL_EXPORT
Definition: kernel.hh:70
T ptr_cast(void *p)
Cast p into pointer of type T.
Definition: cast.hpp:42
struct Gecode::@585::NNF::@62::@63 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
void * ralloc(size_t s)
Allocate memory from region.
Definition: region.hpp:355
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:431