Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
Alloc.h
Go to the documentation of this file.
1/*****************************************************************************************[Alloc.h]
2Copyright (c) 2008-2010, Niklas Sorensson
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5associated documentation files (the "Software"), to deal in the Software without restriction,
6including without limitation the rights to use, copy, modify, merge, publish, distribute,
7sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8furnished to do so, subject to the following conditions:
9
10The above copyright notice and this permission notice shall be included in all copies or
11substantial portions of the Software.
12
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17OF 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>
25
26#pragma GCC visibility push(default)
27namespace Minisat {
28namespace Internal {
29
30//=================================================================================================
31// Simple Region-based memory allocator:
32
33template<class T>
35{
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
52 {
53 if (memory != nullptr)
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
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
90template<class T>
91void 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
116template<class T>
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
Basic declarations, included by all source files.
const T * lea(Ref r) const
Definition Alloc.h:69
const T & operator[](Ref r) const
Definition Alloc.h:66
void capacity(uint32_t min_cap)
Definition Alloc.h:91
void moveTo(RegionAllocator &to)
Definition Alloc.h:77
RegionAllocator(uint32_t start_cap=1024 *1024)
Definition Alloc.h:49
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
static void * xrealloc(void *ptr, size_t size)
Definition XAlloc.h:33