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