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
26namespace Minisat {
27namespace Internal {
28
29//=================================================================================================
30// Simple Region-based memory allocator:
31
32template<class T>
34{
39
41
42 public:
43 using Ref = uint32_t;
44
45 enum { Ref_Undef = UINT_MAX };
46 enum { Unit_Size = sizeof(uint32_t) };
47
48 explicit RegionAllocator(uint32_t start_cap = 1024 * 1024) : memory(nullptr), sz(0), cap(0), wasted_(0){ capacity(start_cap); }
49
51 {
52 if (memory != nullptr)
54 }
55
56
57 uint32_t size () const { return sz; }
58 uint32_t wasted () const { return wasted_; }
59
60 Ref alloc (int size);
61 void free (int size) { wasted_ += size; }
62
63 // Deref, Load Effective Address (LEA), Inverse of LEA (AEL):
64 T& operator[](Ref r) { OGDF_ASSERT(r < sz); return memory[r]; }
65 const T& operator[](Ref r) const { OGDF_ASSERT(r < sz); return memory[r]; }
66
67 T* lea (Ref r) { OGDF_ASSERT(r < sz); return &memory[r]; }
68 const T* lea (Ref r) const { OGDF_ASSERT(r < sz); return &memory[r]; }
69
70 Ref ael(const T* t) {
71 OGDF_ASSERT((const void*)t >= (const void*)&memory[0]);
72 OGDF_ASSERT((const void*)t < (const void*)&memory[sz-1]);
73 return (Ref)(t - &memory[0]);
74 }
75
77 if (to.memory != nullptr) ::free(to.memory);
78 to.memory = memory;
79 to.sz = sz;
80 to.cap = cap;
81 to.wasted_ = wasted_;
82
83 memory = nullptr;
84 sz = cap = wasted_ = 0;
85 }
86};
87
88
89template<class T>
91{
92 if (cap >= min_cap) return;
93
94 uint32_t prev_cap = cap;
95 while (cap < min_cap) {
96 // NOTE: Multiply by a factor (13/8) without causing overflow, then add 2 and make the
97 // result even by clearing the least significant bit. The resulting sequence of capacities
98 // is carefully chosen to hit a maximum capacity that is close to the '2^32-1' limit when
99 // using 'uint32_t' as indices so that as much as possible of this space can be used.
100 uint32_t delta = ((cap >> 1) + (cap >> 3) + 2) & ~1;
101 cap += delta;
102
103 if (cap <= prev_cap)
104 throw OutOfMemoryException();
105 }
106#if 0
107 printf(" .. (%p) cap = %u\n", this, cap);
108#endif
109
110 OGDF_ASSERT(cap > 0);
111 memory = static_cast<T*>(xrealloc(memory, sizeof(T)*cap));
112}
113
114
115template<class T>
118{
119#if 0
120 printf("ALLOC called (this = %p, size = %d)\n", this, size); fflush(stdout);
121#endif
122 OGDF_ASSERT(size > 0);
123 capacity(sz + size);
124
125 uint32_t prev_sz = sz;
126 sz += size;
127
128 // Handle overflow:
129 if (sz < prev_sz)
130 throw OutOfMemoryException();
131
132 return prev_sz;
133}
134
135
136//=================================================================================================
137} // namespace Internal
138} // namespace Minisat
Basic declarations, included by all source files.
const T * lea(Ref r) const
Definition Alloc.h:68
const T & operator[](Ref r) const
Definition Alloc.h:65
void capacity(uint32_t min_cap)
Definition Alloc.h:90
void moveTo(RegionAllocator &to)
Definition Alloc.h:76
RegionAllocator(uint32_t start_cap=1024 *1024)
Definition Alloc.h:48
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:41
int r[]
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
static void * xrealloc(void *ptr, size_t size)
Definition XAlloc.h:32