main page
modules
namespaces
classes
files
Gecode home
Generated on Thu Feb 14 2013 20:59:47 for Gecode by
doxygen
1.8.3.1
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: 2010-07-29 00:13:53 +1000 (Thu, 29 Jul 2010) $ by $Author: schulte $
11
* $Revision: 11292 $
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