Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
region.cpp
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 #include <gecode/kernel.hh>
39 
40 namespace Gecode {
41 
42  Region::Pool::Pool(void)
43  : c(new Chunk), n_c(2U) {
44  c->next = new Chunk; c->next->next = nullptr;
45  }
46  Region::Chunk*
47  Region::Pool::chunk(void) {
48  Chunk* n;
49  m.acquire();
50  if (c != nullptr) {
51  assert(n_c > 0U);
52  n = c; c = c->next; n_c--;
53  } else {
54  n = new Region::Chunk;
55  }
56  n->reset();
57  m.release();
58  return n;
59  }
60  void
61  Region::Pool::chunk(Chunk* u) {
62  m.acquire();
64  delete u;
65  } else {
66  u->next = c; c = u;
67  n_c++;
68  }
69  m.release();
70  }
71  Region::Pool::~Pool(void) {
72  m.acquire();
73  // If that were the case there is a memory leak!
74  assert(c != nullptr);
75  do {
76  Chunk* n=c->next;
77  delete c;
78  c=n;
79  } while (c != nullptr);
80  m.release();
81  }
82 
83  Region::Pool& Region::pool(void) {
84  static Region::Pool _p;
85  return _p;
86  }
87 
88  void*
89  Region::heap_alloc(size_t s) {
90  void* p = heap.ralloc(s);
91  if (hi == nullptr) {
92  hi = p;
93  assert(!Support::marked(hi));
94  } else if (!Support::marked(hi)) {
95  HeapInfo* h = static_cast<HeapInfo*>
96  (heap.ralloc(sizeof(HeapInfo)+(4-1)*sizeof(void*)));
97  h->n=2; h->size=4;
98  h->blocks[0]=hi; h->blocks[1]=p;
99  hi = Support::mark(h);
100  } else {
101  HeapInfo* h = static_cast<HeapInfo*>(Support::unmark(hi));
102  if (h->n == h->size) {
103  HeapInfo* n = static_cast<HeapInfo*>
104  (heap.ralloc(sizeof(HeapInfo)+(2*h->n-1)*sizeof(void*)));
105  n->size = 2*h->n;
106  n->n = h->n;
107  memcpy(&n->blocks[0], &h->blocks[0], h->n*sizeof(void*));
108  hi = Support::mark(n);
109  heap.rfree(h);
110  h = n;
111  }
112  h->blocks[h->n++] = p;
113  }
114  return p;
115  }
116 
117  void
118  Region::heap_free(void) {
119  assert(hi != nullptr);
120  if (Support::marked(hi)) {
121  HeapInfo* h = static_cast<HeapInfo*>(Support::unmark(hi));
122  for (unsigned int i=h->n; i--; )
123  heap.rfree(h->blocks[i]);
124  heap.rfree(h);
125  } else {
126  heap.rfree(hi);
127  }
128  }
129 
130 }
131 
132 // STATISTICS: kernel-memory
bool marked(void *p)
Check whether p is marked.
void rfree(void *p)
Free memory block starting at p.
Definition: heap.hpp:375
void * ralloc(size_t s)
Allocate s bytes from heap.
Definition: heap.hpp:361
void * mark(void *p)
Return marked pointer for unmarked pointer p.
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
union Gecode::@585::NNF::@62 u
Union depending on nodetype t.
void * unmark(void *p)
Return unmarked pointer for a marked pointer p.
Heap heap
The single global heap.
Definition: heap.cpp:48
int n
Number of elements.
Definition: array.hpp:515
const unsigned int n_hc_cache
How many heap chunks should be cached at most.
Definition: config.hpp:51
Gecode toplevel namespace