Generated on Thu Apr 5 2018 19:44:19 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  * Last modified:
10  * $Date$ by $Author$
11  * $Revision$
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode {
39 
56  class Region {
58  private:
60  class Chunk : public HeapAllocated {
61  public:
63  size_t free;
65  double area[Kernel::MemoryConfig::region_area_size / sizeof(double)];
67  Chunk* next;
69  bool alloc(size_t s, void*& p);
71  void reset(void);
72  };
74  Chunk* chunk;
76  class GECODE_KERNEL_EXPORT Pool {
77  protected:
79  Chunk* c;
81  unsigned int n_c;
83  Support::Mutex m;
84  public:
86  Pool(void);
88  Chunk* chunk(void);
90  void chunk(Chunk* u);
92  ~Pool(void);
93  };
95  GECODE_KERNEL_EXPORT static Pool& pool();
97  class HeapInfo {
98  public:
100  unsigned int n;
102  unsigned int size;
104  void* blocks[1];
105  };
113  void* hi;
115  GECODE_KERNEL_EXPORT void* heap_alloc(size_t s);
117  GECODE_KERNEL_EXPORT void heap_free(void);
118  public:
120  Region(void);
127  void free(void);
129 
130 
136  template<class T>
137  T* alloc(long unsigned int n);
144  template<class T>
145  T* alloc(long int n);
152  template<class T>
153  T* alloc(unsigned int n);
160  template<class T>
161  T* alloc(int n);
171  template<class T>
172  void free(T* b, long unsigned int n);
182  template<class T>
183  void free(T* b, long int n);
193  template<class T>
194  void free(T* b, unsigned int n);
204  template<class T>
205  void free(T* b, int n);
217  template<class T>
218  T* realloc(T* b, long unsigned int n, long unsigned int m);
230  template<class T>
231  T* realloc(T* b, long int n, long int m);
243  template<class T>
244  T* realloc(T* b, unsigned int n, unsigned int m);
256  template<class T>
257  T* realloc(T* b, int n, int m);
259 
261  void* ralloc(size_t s);
269  void rfree(void* p, size_t s);
271 
273 
276  template<class T>
277  T& construct(void);
283  template<class T, typename A1>
284  T& construct(A1 const& a1);
290  template<class T, typename A1, typename A2>
291  T& construct(A1 const& a1, A2 const& a2);
297  template<class T, typename A1, typename A2, typename A3>
298  T& construct(A1 const& a1, A2 const& a2, A3 const& a3);
304  template<class T, typename A1, typename A2, typename A3, typename A4>
305  T& construct(A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4);
311  template<class T, typename A1, typename A2, typename A3, typename A4, typename A5>
312  T& construct(A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4, A5 const& a5);
314  ~Region(void);
316  private:
318  static void* operator new(size_t s) throw() { (void) s; return NULL; }
320  static void operator delete(void* p) { (void) p; };
322  Region(const Region&) {}
324  const Region& operator =(const Region&) { return *this; }
325  };
327 
328 
329  /*
330  * Implementation
331  *
332  */
333  forceinline bool
334  Region::Chunk::alloc(size_t s, void*& p) {
336  if (s > free)
337  return false;
338  free -= s;
339  p = ptr_cast<char*>(&area[0]) + free;
340  return true;
341  }
342 
343  forceinline void
344  Region::Chunk::reset(void) {
346  }
347 
348 
351  : chunk(pool().chunk()), hi(0) {}
352 
353  forceinline void
354  Region::free(void) {
355  chunk->reset();
356  }
357 
358  forceinline void*
359  Region::ralloc(size_t s) {
360  void* p;
361  if (chunk->alloc(s,p))
362  return p;
363  else
364  return heap_alloc(s);
365  }
366 
367  forceinline void
368  Region::rfree(void*, size_t) {}
369 
372  pool().chunk(chunk);
373  if (hi != NULL)
374  heap_free();
375  }
376 
377 
378  /*
379  * Typed allocation routines
380  *
381  */
382  template<class T>
383  forceinline T*
384  Region::alloc(long unsigned int n) {
385  T* p = static_cast<T*>(ralloc(sizeof(T)*n));
386  for (long unsigned int i=n; i--; )
387  (void) new (p+i) T();
388  return p;
389  }
390  template<class T>
391  forceinline T*
392  Region::alloc(long int n) {
393  assert(n >= 0);
394  return alloc<T>(static_cast<long unsigned int>(n));
395  }
396  template<class T>
397  forceinline T*
398  Region::alloc(unsigned int n) {
399  return alloc<T>(static_cast<long unsigned int>(n));
400  }
401  template<class T>
402  forceinline T*
404  assert(n >= 0);
405  return alloc<T>(static_cast<long unsigned int>(n));
406  }
407 
408  template<class T>
409  forceinline void
410  Region::free(T* b, long unsigned int n) {
411  for (long unsigned int i=n; i--; )
412  b[i].~T();
413  rfree(b,n*sizeof(T));
414  }
415  template<class T>
416  forceinline void
417  Region::free(T* b, long int n) {
418  assert(n >= 0);
419  free<T>(b,static_cast<long unsigned int>(n));
420  }
421  template<class T>
422  forceinline void
423  Region::free(T* b, unsigned int n) {
424  free<T>(b,static_cast<long unsigned int>(n));
425  }
426  template<class T>
427  forceinline void
428  Region::free(T* b, int n) {
429  assert(n >= 0);
430  free<T>(b,static_cast<long unsigned int>(n));
431  }
432 
433  template<class T>
434  forceinline T*
435  Region::realloc(T* b, long unsigned int n, long unsigned int m) {
436  if (n < m) {
437  T* p = static_cast<T*>(ralloc(sizeof(T)*m));
438  for (long unsigned int i=n; i--; )
439  (void) new (p+i) T(b[i]);
440  for (long unsigned int i=n; i<m; i++)
441  (void) new (p+i) T();
442  free<T>(b,n);
443  return p;
444  } else {
445  free<T>(b+m,m-n);
446  return b;
447  }
448  }
449  template<class T>
450  forceinline T*
451  Region::realloc(T* b, long int n, long int m) {
452  assert((n >= 0) && (m >= 0));
453  return realloc<T>(b,static_cast<long unsigned int>(n),
454  static_cast<long unsigned int>(m));
455  }
456  template<class T>
457  forceinline T*
458  Region::realloc(T* b, unsigned int n, unsigned int m) {
459  return realloc<T>(b,static_cast<long unsigned int>(n),
460  static_cast<long unsigned int>(m));
461  }
462  template<class T>
463  forceinline T*
464  Region::realloc(T* b, int n, int m) {
465  assert((n >= 0) && (m >= 0));
466  return realloc<T>(b,static_cast<long unsigned int>(n),
467  static_cast<long unsigned int>(m));
468  }
469 
470  /*
471  * Region construction support
472  *
473  */
474  template<class T>
475  forceinline T&
477  return alloc<T>(1);
478  }
479  template<class T, typename A1>
480  forceinline T&
481  Region::construct(A1 const& a1) {
482  T& t = *static_cast<T*>(ralloc(sizeof(T)));
483  new (&t) T(a1);
484  return t;
485  }
486  template<class T, typename A1, typename A2>
487  forceinline T&
488  Region::construct(A1 const& a1, A2 const& a2) {
489  T& t = *static_cast<T*>(ralloc(sizeof(T)));
490  new (&t) T(a1,a2);
491  return t;
492  }
493  template<class T, typename A1, typename A2, typename A3>
494  forceinline T&
495  Region::construct(A1 const& a1, A2 const& a2, A3 const& a3) {
496  T& t = *static_cast<T*>(ralloc(sizeof(T)));
497  new (&t) T(a1,a2,a3);
498  return t;
499  }
500  template<class T, typename A1, typename A2, typename A3, typename A4>
501  forceinline T&
502  Region::construct(A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4) {
503  T& t = *static_cast<T*>(ralloc(sizeof(T)));
504  new (&t) T(a1,a2,a3,a4);
505  return t;
506  }
507  template<class T, typename A1, typename A2, typename A3, typename A4, typename A5>
508  forceinline T&
509  Region::construct(A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4, A5 const& a5) {
510  T& t = *static_cast<T*>(ralloc(sizeof(T)));
511  new (&t) T(a1,a2,a3,a4,a5);
512  return t;
513  }
514 
515 }
516 
517 // STATISTICS: kernel-memory
void rfree(void *p, size_t s)
Free memory previously allocated.
Definition: region.hpp:368
void free(void)
Free allocate memory.
Definition: region.hpp:354
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:476
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition: region.hpp:384
void align(size_t &s)
Align size s to the required alignment.
Definition: config.hpp:148
Region(void)
Initialize region.
Definition: region.hpp:350
~Region(void)
Return memory.
Definition: region.hpp:371
#define forceinline
Definition: config.hpp:182
Gecode::FloatVal c(-8, 8)
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
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:138
union Gecode::@585::NNF::@62 u
Union depending on nodetype t.
#define GECODE_KERNEL_EXPORT
Definition: kernel.hh:74
T ptr_cast(void *p)
Cast p into pointer of type T.
Definition: cast.hpp:46
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:359
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:435