main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Jan 28 2020 00:00:00 for Gecode by
doxygen
1.8.17
gecode
support
block-allocator.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, 2004
8
*
9
* Last modified:
10
* $Date: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
11
* $Revision: 14967 $
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
{
namespace
Support {
39
48
template
<
class
T,
class
A,
int
blocksize = 512>
49
class
BlockAllocator
{
50
private
:
52
A& a;
54
class
Block {
55
public
:
56
T
b
[blocksize];
57
Block* next;
58
};
60
Block* b;
62
T* n;
64
size_t
_size;
66
void
allocate(
void
);
67
public
:
69
BlockAllocator
(A&
a
);
71
~BlockAllocator
(
void
);
73
A&
allocator
(
void
);
75
T*
operator ()
(
void
);
77
size_t
size
(
void
)
const
;
78
};
79
87
template
<
class
T,
class
A,
int
blocksize = 512>
88
class
BlockClient
{
89
public
:
91
static
void
*
operator
new
(
size_t
s,
BlockAllocator<T,A,blocksize>
&
ba
);
93
static
void
operator
delete
(
void
*,
BlockAllocator<T,A,blocksize>
&
ba
);
95
static
void
operator
delete
(
void
*);
96
};
97
98
99
100
template
<
class
T,
class
A,
int
blocksize>
101
forceinline
102
BlockAllocator<T,A,blocksize>::BlockAllocator
(A& a0) :
a
(a0) {
103
b
=
static_cast<
Block*
>
(
a
.ralloc(
sizeof
(Block)));
104
b
->next = NULL;
105
n
= &
b
->b[blocksize];
106
_size =
sizeof
(Block);
107
}
108
109
template
<
class
T,
class
A,
int
blocksize>
110
forceinline
111
BlockAllocator<T,A,blocksize>::~BlockAllocator
(
void
) {
112
while
(
b
!= NULL) {
113
Block*
f
=
b
;
b
=
b
->next;
114
a
.rfree(
f
,
sizeof
(Block));
115
}
116
}
117
118
template
<
class
T,
class
A,
int
blocksize>
119
forceinline
A
&
120
BlockAllocator<T,A,blocksize>::allocator
(
void
) {
121
return
a
;
122
}
123
124
template
<
class
T,
class
A,
int
blocksize>
125
forceinline
T*
126
BlockAllocator<T,A,blocksize>::operator ()
(
void
) {
127
T*
t
= --
n
;
128
if
(
t
== &
b
->b[0])
129
allocate();
130
return
t
;
131
}
132
133
template
<
class
T,
class
A,
int
blocksize>
134
void
135
BlockAllocator<T,A,blocksize>::allocate
(
void
) {
136
// Allocate another block
137
Block* nb =
static_cast<
Block*
>
(
a
.ralloc(
sizeof
(Block)));
138
nb->next =
b
;
b
= nb;
139
n
= &nb->b[blocksize];
140
_size +=
sizeof
(Block);
141
}
142
143
template
<
class
T,
class
A,
int
blocksize>
144
forceinline
size_t
145
BlockAllocator<T,A,blocksize>::size
(
void
)
const
{
146
return
_size;
147
}
148
149
150
151
template
<
class
T,
class
A,
int
blocksize>
152
forceinline
void
153
BlockClient<T,A,blocksize>::operator
154
delete
(
void
*,
BlockAllocator<T,A,blocksize>
&) {
155
}
156
template
<
class
T,
class
A,
int
blocksize>
157
forceinline
void
158
BlockClient<T,A,blocksize>::operator
delete
(
void
*) {
159
}
160
template
<
class
T,
class
A,
int
blocksize>
161
forceinline
void
*
162
BlockClient<T,A,blocksize>::operator
new
(size_t,
163
BlockAllocator<T,A,blocksize>
&
ba
) {
164
return
ba
();
165
}
166
167
}}
168
169
// STATISTICS: support-any
Gecode::Support::BlockAllocator::allocator
A & allocator(void)
Return allocator used.
Definition:
block-allocator.hpp:120
forceinline
#define forceinline
Definition:
config.hpp:173
Gecode::Support::BlockAllocator
Manage memory organized into block lists (allocator)
Definition:
block-allocator.hpp:49
Gecode::Support::BlockAllocator::operator()
T * operator()(void)
Return memory of size required by T.
Definition:
block-allocator.hpp:126
Gecode::Support::allocator
Allocator allocator
The single global default memory allocator.
Definition:
allocator.cpp:42
Gecode::Support::BlockAllocator::~BlockAllocator
~BlockAllocator(void)
Free all allocated blocks.
Definition:
block-allocator.hpp:111
Gecode::Iter::Ranges::size
unsigned int size(I &i)
Size of all ranges of range iterator i.
Definition:
ranges-operations.hpp:78
Gecode::Support::BlockAllocator::BlockAllocator
BlockAllocator(A &a)
Initialize.
Definition:
block-allocator.hpp:102
t
NodeType t
Type of node.
Definition:
bool-expr.cpp:234
Gecode::Support::BlockClient
Client for block allocator of type T.
Definition:
block-allocator.hpp:88
Gecode
Gecode toplevel namespace
Gecode::ba
IntPropLevel ba(IntPropLevel ipl)
Extract basic or advanced from propagation level.
Definition:
ipl.hpp:53
a
struct Gecode::@579::NNF::@61::@63 a
For atomic nodes.
b
struct Gecode::@579::NNF::@61::@62 b
For binary nodes (and, or, eqv)
QueenArmies::A
IntSet * A
Position of a piece in a square board.
Definition:
queen-armies.cpp:50
Gecode::f
Post propagator for f(x \diamond_{\mathit{op}} y) \sim_r z \f$ void rel(Home home
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:238
Gecode::Support::BlockAllocator::size
size_t size(void) const
Return size of memory required by allocator.
Definition:
block-allocator.hpp:145