Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
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
36namespace 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 {
46 Support::Lock l(m);
47 if (c != nullptr) {
48 assert(n_c > 0U);
49 n = c; c = c->next; n_c--;
50 } else {
51 n = new Region::Chunk;
52 }
53 n->reset();
54 }
55 return n;
56 }
57 void
58 Region::Pool::chunk(Chunk* u) {
59 Support::Lock l(m);
60 if (n_c == Kernel::MemoryConfig::n_hc_cache) {
61 delete u;
62 } else {
63 u->next = c; c = u;
64 n_c++;
65 }
66 }
67 Region::Pool::~Pool(void) {
68 Support::Lock l(m);
69 // If that were the case there would be 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 }
77
78 Region::Pool& Region::pool(void) {
79 static Region::Pool _p;
80 return _p;
81 }
82
83 void*
84 Region::heap_alloc(size_t s) {
85 void* p = heap.ralloc(s);
86 if (hi == nullptr) {
87 hi = p;
88 assert(!Support::marked(hi));
89 } else if (!Support::marked(hi)) {
90 HeapInfo* h = static_cast<HeapInfo*>
91 (heap.ralloc(sizeof(HeapInfo)+(4-1)*sizeof(void*)));
92 h->n=2; h->size=4;
93 h->blocks[0]=hi; h->blocks[1]=p;
94 hi = Support::mark(h);
95 } else {
96 HeapInfo* h = static_cast<HeapInfo*>(Support::unmark(hi));
97 if (h->n == h->size) {
98 HeapInfo* n = static_cast<HeapInfo*>
99 (heap.ralloc(sizeof(HeapInfo)+(2*h->n-1)*sizeof(void*)));
100 n->size = 2*h->n;
101 n->n = h->n;
102 memcpy(&n->blocks[0], &h->blocks[0], h->n*sizeof(void*));
103 hi = Support::mark(n);
104 heap.rfree(h);
105 h = n;
106 }
107 h->blocks[h->n++] = p;
108 }
109 return p;
110 }
111
112 void
113 Region::heap_free(void) {
114 assert(hi != nullptr);
115 if (Support::marked(hi)) {
116 HeapInfo* h = static_cast<HeapInfo*>(Support::unmark(hi));
117 for (unsigned int i=0U; i<h->n; i++)
118 heap.rfree(h->blocks[i]);
119 heap.rfree(h);
120 } else {
121 heap.rfree(hi);
122 }
123 }
124
125}
126
127// STATISTICS: kernel-memory
NNF * l
Left subtree.
union Gecode::@603::NNF::@65 u
Union depending on nodetype t.
int p
Number of positive literals for node type.
int n
Number of negative literals for node type.
int n
Number of elements.
Definition array.hpp:540
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
Heap heap
The single global heap.
Definition heap.cpp:44
Gecode toplevel namespace
Gecode::FloatVal c(-8, 8)
Gecode::IntArgs i({1, 2, 3, 4})