main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Mar 24 2020 14:04:04 for Gecode by
doxygen
1.8.17
gecode
kernel
memory
region.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, 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 <cstddef>
35
36
namespace
Gecode
{
37
54
class
Region {
56
private
:
58
class
Chunk :
public
HeapAllocated {
59
public
:
61
size_t
free
;
63
alignas
((
alignof
(std::max_align_t) >
GECODE_MEMORY_ALIGNMENT
) ?
64
alignof
(std::max_align_t) :
GECODE_MEMORY_ALIGNMENT
)
65
double
area[
Kernel::MemoryConfig::region_area_size
/
sizeof
(double)];
67
Chunk* next;
69
bool
alloc
(
size_t
s,
void
*&
p
);
71
void
reset(
void
);
72
};
74
Chunk* chunk;
76
class
GECODE_KERNEL_EXPORT
Pool {
77
protected
:
79
Chunk*
c
;
81
unsigned
int
n_c;
83
Support::Mutex m;
84
public
:
86
Pool(
void
);
88
Chunk* chunk(
void
);
90
void
chunk(Chunk*
u
);
92
~Pool(
void
);
93
};
95
GECODE_KERNEL_EXPORT
static
Pool& pool();
97
class
HeapInfo {
98
public
:
100
unsigned
int
n
;
102
unsigned
int
size
;
104
void
* blocks[1];
105
};
113
void
* hi;
115
GECODE_KERNEL_EXPORT
void
* heap_alloc(
size_t
s);
117
GECODE_KERNEL_EXPORT
void
heap_free(
void
);
118
public
:
120
Region
(
void
);
127
void
free
(
void
);
129
130
136
template
<
class
T>
137
T*
alloc
(
long
unsigned
int
n
);
144
template
<
class
T>
145
T*
alloc
(
long
int
n
);
152
template
<
class
T>
153
T*
alloc
(
unsigned
int
n
);
160
template
<
class
T>
161
T*
alloc
(
int
n
);
171
template
<
class
T>
172
void
free
(T*
b
,
long
unsigned
int
n
);
182
template
<
class
T>
183
void
free
(T*
b
,
long
int
n
);
193
template
<
class
T>
194
void
free
(T*
b
,
unsigned
int
n
);
204
template
<
class
T>
205
void
free
(T*
b
,
int
n
);
217
template
<
class
T>
218
T*
realloc
(T*
b
,
long
unsigned
int
n
,
long
unsigned
int
m);
230
template
<
class
T>
231
T*
realloc
(T*
b
,
long
int
n
,
long
int
m);
243
template
<
class
T>
244
T*
realloc
(T*
b
,
unsigned
int
n
,
unsigned
int
m);
256
template
<
class
T>
257
T*
realloc
(T*
b
,
int
n
,
int
m);
259
261
void
*
ralloc
(
size_t
s);
269
void
rfree
(
void
*
p
,
size_t
s);
271
273
276
template
<
class
T>
277
T&
construct
(
void
);
283
template
<
class
T,
typename
A1>
284
T&
construct
(A1
const
& a1);
290
template
<
class
T,
typename
A1,
typename
A2>
291
T&
construct
(A1
const
& a1, A2
const
& a2);
297
template
<
class
T,
typename
A1,
typename
A2,
typename
A3>
298
T&
construct
(A1
const
& a1, A2
const
& a2, A3
const
& a3);
304
template
<
class
T,
typename
A1,
typename
A2,
typename
A3,
typename
A4>
305
T&
construct
(A1
const
& a1, A2
const
& a2, A3
const
& a3, A4
const
& a4);
311
template
<
class
T,
typename
A1,
typename
A2,
typename
A3,
typename
A4,
typename
A5>
312
T&
construct
(A1
const
& a1, A2
const
& a2, A3
const
& a3, A4
const
& a4, A5
const
& a5);
314
~Region
(
void
);
316
private
:
318
static
void
*
operator
new
(
size_t
s)
throw
() { (void) s;
return
NULL; }
320
static
void
operator
delete
(
void
*
p
) { (void)
p
; };
322
Region
(
const
Region
&) {}
324
const
Region
& operator =(
const
Region
&) {
return
*
this
; }
325
};
327
328
329
/*
330
* Implementation
331
*
332
*/
333
forceinline
bool
334
Region::Chunk::alloc(
size_t
s,
void
*&
p
) {
335
Kernel::MemoryConfig::align
336
(s,((
alignof
(std::max_align_t) >
GECODE_MEMORY_ALIGNMENT
) ?
337
alignof
(std::max_align_t) :
GECODE_MEMORY_ALIGNMENT
));
338
if
(s > free)
339
return
false
;
340
free -= s;
341
p
= ptr_cast<char*>(&area[0]) + free;
342
return
true
;
343
}
344
345
forceinline
void
346
Region::Chunk::reset(
void
) {
347
free =
Kernel::MemoryConfig::region_area_size
;
348
}
349
350
351
forceinline
352
Region::Region
(
void
)
353
: chunk(pool().chunk()), hi(0) {}
354
355
forceinline
void
356
Region::free
(
void
) {
357
chunk->reset();
358
}
359
360
forceinline
void
*
361
Region::ralloc
(
size_t
s) {
362
void
*
p
;
363
if
(chunk->alloc(s,
p
))
364
return
p
;
365
else
366
return
heap_alloc(s);
367
}
368
369
forceinline
void
370
Region::rfree
(
void
*,
size_t
) {}
371
372
forceinline
373
Region::~Region
(
void
) {
374
pool().chunk(chunk);
375
if
(hi != NULL)
376
heap_free();
377
}
378
379
380
/*
381
* Typed allocation routines
382
*
383
*/
384
template
<
class
T>
385
forceinline
T*
386
Region::alloc
(
long
unsigned
int
n
) {
387
T*
p
=
static_cast<
T*
>
(
ralloc
(
sizeof
(T)*
n
));
388
for
(
long
unsigned
int
i
=0U;
i
<
n
;
i
++)
389
(
void
)
new
(
p
+
i
) T();
390
return
p
;
391
}
392
template
<
class
T>
393
forceinline
T*
394
Region::alloc
(
long
int
n
) {
395
assert(
n
>= 0);
396
return
alloc<T>(
static_cast<
long
unsigned
int
>
(
n
));
397
}
398
template
<
class
T>
399
forceinline
T*
400
Region::alloc
(
unsigned
int
n
) {
401
return
alloc<T>(
static_cast<
long
unsigned
int
>
(
n
));
402
}
403
template
<
class
T>
404
forceinline
T*
405
Region::alloc
(
int
n
) {
406
assert(
n
>= 0);
407
return
alloc<T>(
static_cast<
long
unsigned
int
>
(
n
));
408
}
409
410
template
<
class
T>
411
forceinline
void
412
Region::free
(T*
b
,
long
unsigned
int
n
) {
413
for
(
long
unsigned
int
i
=0U;
i
<
n
;
i
++)
414
b
[
i
].~T();
415
rfree
(
b
,
n
*
sizeof
(T));
416
}
417
template
<
class
T>
418
forceinline
void
419
Region::free
(T*
b
,
long
int
n
) {
420
assert(
n
>= 0);
421
free<T>(
b
,
static_cast<
long
unsigned
int
>
(
n
));
422
}
423
template
<
class
T>
424
forceinline
void
425
Region::free
(T*
b
,
unsigned
int
n
) {
426
free<T>(
b
,
static_cast<
long
unsigned
int
>
(
n
));
427
}
428
template
<
class
T>
429
forceinline
void
430
Region::free
(T*
b
,
int
n
) {
431
assert(
n
>= 0);
432
free<T>(
b
,
static_cast<
long
unsigned
int
>
(
n
));
433
}
434
435
template
<
class
T>
436
forceinline
T*
437
Region::realloc
(T*
b
,
long
unsigned
int
n
,
long
unsigned
int
m) {
438
if
(
n
< m) {
439
T*
p
=
static_cast<
T*
>
(
ralloc
(
sizeof
(T)*m));
440
for
(
long
unsigned
int
i
=0U;
i
<
n
;
i
++)
441
(
void
)
new
(
p
+
i
) T(
b
[
i
]);
442
for
(
long
unsigned
int
i
=
n
;
i
<m;
i
++)
443
(
void
)
new
(
p
+
i
) T();
444
free<T>(
b
,
n
);
445
return
p
;
446
}
else
{
447
free<T>(
b
+m,m-
n
);
448
return
b
;
449
}
450
}
451
template
<
class
T>
452
forceinline
T*
453
Region::realloc
(T*
b
,
long
int
n
,
long
int
m) {
454
assert((
n
>= 0) && (m >= 0));
455
return
realloc<T>(
b
,
static_cast<
long
unsigned
int
>
(
n
),
456
static_cast<
long
unsigned
int
>
(m));
457
}
458
template
<
class
T>
459
forceinline
T*
460
Region::realloc
(T*
b
,
unsigned
int
n
,
unsigned
int
m) {
461
return
realloc<T>(
b
,
static_cast<
long
unsigned
int
>
(
n
),
462
static_cast<
long
unsigned
int
>
(m));
463
}
464
template
<
class
T>
465
forceinline
T*
466
Region::realloc
(T*
b
,
int
n
,
int
m) {
467
assert((
n
>= 0) && (m >= 0));
468
return
realloc<T>(
b
,
static_cast<
long
unsigned
int
>
(
n
),
469
static_cast<
long
unsigned
int
>
(m));
470
}
471
472
/*
473
* Region construction support
474
*
475
*/
476
template
<
class
T>
477
forceinline
T&
478
Region::construct
(
void
) {
479
return
alloc<T>(1);
480
}
481
template
<
class
T,
typename
A1>
482
forceinline
T&
483
Region::construct
(A1
const
& a1) {
484
T&
t
= *
static_cast<
T*
>
(
ralloc
(
sizeof
(T)));
485
new
(&
t
) T(a1);
486
return
t
;
487
}
488
template
<
class
T,
typename
A1,
typename
A2>
489
forceinline
T&
490
Region::construct
(A1
const
& a1, A2
const
& a2) {
491
T&
t
= *
static_cast<
T*
>
(
ralloc
(
sizeof
(T)));
492
new
(&
t
) T(a1,a2);
493
return
t
;
494
}
495
template
<
class
T,
typename
A1,
typename
A2,
typename
A3>
496
forceinline
T&
497
Region::construct
(A1
const
& a1, A2
const
& a2, A3
const
& a3) {
498
T&
t
= *
static_cast<
T*
>
(
ralloc
(
sizeof
(T)));
499
new
(&
t
) T(a1,a2,a3);
500
return
t
;
501
}
502
template
<
class
T,
typename
A1,
typename
A2,
typename
A3,
typename
A4>
503
forceinline
T&
504
Region::construct
(A1
const
& a1, A2
const
& a2, A3
const
& a3, A4
const
& a4) {
505
T&
t
= *
static_cast<
T*
>
(
ralloc
(
sizeof
(T)));
506
new
(&
t
) T(a1,a2,a3,a4);
507
return
t
;
508
}
509
template
<
class
T,
typename
A1,
typename
A2,
typename
A3,
typename
A4,
typename
A5>
510
forceinline
T&
511
Region::construct
(A1
const
& a1, A2
const
& a2, A3
const
& a3, A4
const
& a4, A5
const
& a5) {
512
T&
t
= *
static_cast<
T*
>
(
ralloc
(
sizeof
(T)));
513
new
(&
t
) T(a1,a2,a3,a4,a5);
514
return
t
;
515
}
516
517
}
518
519
// STATISTICS: kernel-memory
Gecode::Region::realloc
T * realloc(T *b, long unsigned int n, long unsigned int m)
Reallocate block of n objects starting at b to m objects of type T from the region.
Definition:
region.hpp:437
Gecode::Kernel::MemoryConfig::region_area_size
const size_t region_area_size
Size of region area.
Definition:
config.hpp:134
Gecode::Iter::Ranges::size
unsigned int size(I &i)
Size of all ranges of range iterator i.
Definition:
ranges-operations.hpp:74
Gecode::Region::alloc
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition:
region.hpp:386
t
NodeType t
Type of node.
Definition:
bool-expr.cpp:230
Gecode::Region::rfree
void rfree(void *p, size_t s)
Free memory previously allocated.
Definition:
region.hpp:370
Gecode::Region::~Region
~Region(void)
Return memory.
Definition:
region.hpp:373
Gecode
Gecode toplevel namespace
u
union Gecode::@602::NNF::@65 u
Union depending on nodetype t.
GECODE_MEMORY_ALIGNMENT
#define GECODE_MEMORY_ALIGNMENT
Memory alignment.
Definition:
config.hpp:125
Gecode::Region::construct
T & construct(void)
Constructs a single object of type T from region using the default constructor.
Definition:
region.hpp:478
Gecode::Kernel::MemoryConfig::align
void align(size_t &s, size_t a=GECODE_MEMORY_ALIGNMENT)
Align size s to the required alignment a.
Definition:
config.hpp:144
b
struct Gecode::@602::NNF::@65::@66 b
For binary nodes (and, or, eqv)
Gecode::Region::free
void free(void)
Free allocate memory.
Definition:
region.hpp:356
GECODE_KERNEL_EXPORT
#define GECODE_KERNEL_EXPORT
Definition:
kernel.hh:70
forceinline
#define forceinline
Definition:
config.hpp:185
Gecode::Region::ralloc
void * ralloc(size_t s)
Allocate memory from region.
Definition:
region.hpp:361
Test::Float::Arithmetic::c
Gecode::FloatVal c(-8, 8)
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:234
Test::Int::Basic::i
Gecode::IntArgs i({1, 2, 3, 4})
p
int p
Number of positive literals for node type.
Definition:
bool-expr.cpp:232
Gecode::Region::Region
Region(void)
Initialize region.
Definition:
region.hpp:352