Open
Graph Drawing
Framework
v. 2023.09 (Elderberry)
Overview
Class Hierarchy
Class Index
Class List
Members
Namespaces
Source Files
Alloc.h
Go to the documentation of this file.
1
/*****************************************************************************************[Alloc.h]
2
Copyright (c) 2008-2010, Niklas Sorensson
3
4
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5
associated documentation files (the "Software"), to deal in the Software without restriction,
6
including without limitation the rights to use, copy, modify, merge, publish, distribute,
7
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8
furnished to do so, subject to the following conditions:
9
10
The above copyright notice and this permission notice shall be included in all copies or
11
substantial portions of the Software.
12
13
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18
**************************************************************************************************/
19
20
#pragma once
21
22
#include <
ogdf/basic/basic.h
>
23
#include <
ogdf/lib/minisat/mtl/XAlloc.h
>
24
#include <
ogdf/lib/minisat/mtl/Vec.h
>
25
26
#pragma GCC visibility push(default)
27
namespace
Minisat
{
28
namespace
Internal {
29
30
//=================================================================================================
31
// Simple Region-based memory allocator:
32
33
template
<
class
T>
34
class
RegionAllocator
35
{
36
T*
memory
;
37
uint32_t
sz
;
38
uint32_t
cap
;
39
uint32_t
wasted_
;
40
41
void
capacity
(uint32_t min_cap);
42
43
public
:
44
using
Ref
= uint32_t;
45
46
enum
{
Ref_Undef
= UINT_MAX };
47
enum
{
Unit_Size
=
sizeof
(uint32_t) };
48
49
explicit
RegionAllocator
(uint32_t start_cap = 1024 * 1024) :
memory
(nullptr),
sz
(0),
cap
(0),
wasted_
(0){
capacity
(start_cap); }
50
51
~RegionAllocator
()
52
{
53
if
(
memory
!=
nullptr
)
54
::free
(
memory
);
55
}
56
57
58
uint32_t
size
()
const
{
return
sz
; }
59
uint32_t
wasted
()
const
{
return
wasted_
; }
60
61
Ref
alloc
(
int
size
);
62
void
free
(
int
size
) {
wasted_
+=
size
; }
63
64
// Deref, Load Effective Address (LEA), Inverse of LEA (AEL):
65
T&
operator[]
(
Ref
r
) {
OGDF_ASSERT
(
r
<
sz
);
return
memory
[
r
]; }
66
const
T&
operator[]
(
Ref
r
)
const
{
OGDF_ASSERT
(
r
<
sz
);
return
memory
[
r
]; }
67
68
T*
lea
(
Ref
r
) {
OGDF_ASSERT
(
r
<
sz
);
return
&
memory
[
r
]; }
69
const
T*
lea
(
Ref
r
)
const
{
OGDF_ASSERT
(
r
<
sz
);
return
&
memory
[
r
]; }
70
71
Ref
ael
(
const
T* t) {
72
OGDF_ASSERT
((
const
void
*)t >= (
const
void
*)&
memory
[0]);
73
OGDF_ASSERT
((
const
void
*)t < (
const
void
*)&
memory
[
sz
-1]);
74
return
(
Ref
)(t - &
memory
[0]);
75
}
76
77
void
moveTo
(
RegionAllocator
& to) {
78
if
(to.
memory
!=
nullptr
)
::free
(to.
memory
);
79
to.
memory
=
memory
;
80
to.
sz
=
sz
;
81
to.
cap
=
cap
;
82
to.
wasted_
=
wasted_
;
83
84
memory
=
nullptr
;
85
sz
=
cap
=
wasted_
= 0;
86
}
87
};
88
89
90
template
<
class
T>
91
void
RegionAllocator<T>::capacity
(uint32_t min_cap)
92
{
93
if
(cap >= min_cap)
return
;
94
95
uint32_t prev_cap = cap;
96
while
(cap < min_cap) {
97
// NOTE: Multiply by a factor (13/8) without causing overflow, then add 2 and make the
98
// result even by clearing the least significant bit. The resulting sequence of capacities
99
// is carefully chosen to hit a maximum capacity that is close to the '2^32-1' limit when
100
// using 'uint32_t' as indices so that as much as possible of this space can be used.
101
uint32_t delta = ((cap >> 1) + (cap >> 3) + 2) & ~1;
102
cap += delta;
103
104
if
(cap <= prev_cap)
105
throw
OutOfMemoryException
();
106
}
107
#if 0
108
printf(
" .. (%p) cap = %u\n"
,
this
, cap);
109
#endif
110
111
OGDF_ASSERT
(cap > 0);
112
memory =
static_cast<
T*
>
(
xrealloc
(memory,
sizeof
(T)*cap));
113
}
114
115
116
template
<
class
T>
117
typename
RegionAllocator<T>::Ref
118
RegionAllocator<T>::alloc
(
int
size)
119
{
120
#if 0
121
printf(
"ALLOC called (this = %p, size = %d)\n"
,
this
, size); fflush(stdout);
122
#endif
123
OGDF_ASSERT
(size > 0);
124
capacity(sz + size);
125
126
uint32_t prev_sz = sz;
127
sz += size;
128
129
// Handle overflow:
130
if
(sz < prev_sz)
131
throw
OutOfMemoryException
();
132
133
return
prev_sz;
134
}
135
136
137
//=================================================================================================
138
}
// namespace Internal
139
}
// namespace Minisat
140
#pragma GCC visibility pop
Vec.h
Minisat::Internal::RegionAllocator::memory
T * memory
Definition:
Alloc.h:36
Minisat::Internal::RegionAllocator::Ref_Undef
@ Ref_Undef
Definition:
Alloc.h:46
Minisat::Internal::RegionAllocator< uint32_t >::Ref
uint32_t Ref
Definition:
Alloc.h:44
Minisat::Internal::RegionAllocator::capacity
void capacity(uint32_t min_cap)
Definition:
Alloc.h:91
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition:
basic.h:66
Minisat::Internal::RegionAllocator::operator[]
const T & operator[](Ref r) const
Definition:
Alloc.h:66
Minisat::Internal::RegionAllocator::size
uint32_t size() const
Definition:
Alloc.h:58
Minisat::Internal::OutOfMemoryException
Definition:
XAlloc.h:32
Minisat::Internal::RegionAllocator::alloc
Ref alloc(int size)
Definition:
Alloc.h:118
XAlloc.h
Minisat::Internal::xrealloc
static void * xrealloc(void *ptr, size_t size)
Definition:
XAlloc.h:33
Minisat::Internal::RegionAllocator::~RegionAllocator
~RegionAllocator()
Definition:
Alloc.h:51
r
int r[]
Definition:
hierarchical-ranking.cpp:13
Minisat::Internal::RegionAllocator::free
void free(int size)
Definition:
Alloc.h:62
Minisat::Internal::RegionAllocator::Unit_Size
@ Unit_Size
Definition:
Alloc.h:47
Minisat::Internal::RegionAllocator::lea
T * lea(Ref r)
Definition:
Alloc.h:68
Minisat
Definition:
Minisat.h:55
Minisat::Internal::RegionAllocator::cap
uint32_t cap
Definition:
Alloc.h:38
Minisat::Internal::RegionAllocator::operator[]
T & operator[](Ref r)
Definition:
Alloc.h:65
Minisat::Internal::RegionAllocator::ael
Ref ael(const T *t)
Definition:
Alloc.h:71
basic.h
Basic declarations, included by all source files.
Minisat::Internal::RegionAllocator
Definition:
Alloc.h:34
Minisat::Internal::RegionAllocator::lea
const T * lea(Ref r) const
Definition:
Alloc.h:69
Minisat::Internal::RegionAllocator::moveTo
void moveTo(RegionAllocator &to)
Definition:
Alloc.h:77
Minisat::Internal::RegionAllocator::RegionAllocator
RegionAllocator(uint32_t start_cap=1024 *1024)
Definition:
Alloc.h:49
Minisat::Internal::RegionAllocator::wasted
uint32_t wasted() const
Definition:
Alloc.h:59
Minisat::Internal::RegionAllocator::sz
uint32_t sz
Definition:
Alloc.h:37
Minisat::Internal::RegionAllocator::wasted_
uint32_t wasted_
Definition:
Alloc.h:39
include
ogdf
lib
minisat
mtl
Alloc.h
This site is powered by Netlify.
© 1999–2024
The OGDF Team