Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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>
25 
26 namespace Minisat {
27 namespace Internal {
28 
29 //=================================================================================================
30 // Simple Region-based memory allocator:
31 
32 template<class T>
34 {
35  T* memory;
36  uint32_t sz;
37  uint32_t cap;
38  uint32_t wasted_;
39 
40  void capacity(uint32_t min_cap);
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)
53  ::free(memory);
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 
76  void moveTo(RegionAllocator& to) {
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 
89 template<class T>
90 void RegionAllocator<T>::capacity(uint32_t min_cap)
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 
115 template<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
Vec.h
Minisat::Internal::RegionAllocator::memory
T * memory
Definition: Alloc.h:35
Minisat::Internal::RegionAllocator::Ref_Undef
@ Ref_Undef
Definition: Alloc.h:45
Minisat::Internal::RegionAllocator< uint32_t >::Ref
uint32_t Ref
Definition: Alloc.h:43
Minisat::Internal::RegionAllocator::capacity
void capacity(uint32_t min_cap)
Definition: Alloc.h:90
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
Minisat::Internal::RegionAllocator::operator[]
const T & operator[](Ref r) const
Definition: Alloc.h:65
Minisat::Internal::RegionAllocator::size
uint32_t size() const
Definition: Alloc.h:57
Minisat::Internal::OutOfMemoryException
Definition: XAlloc.h:31
Minisat::Internal::RegionAllocator::alloc
Ref alloc(int size)
Definition: Alloc.h:117
XAlloc.h
Minisat::Internal::xrealloc
static void * xrealloc(void *ptr, size_t size)
Definition: XAlloc.h:32
Minisat::Internal::RegionAllocator::~RegionAllocator
~RegionAllocator()
Definition: Alloc.h:50
r
int r[]
Definition: hierarchical-ranking.cpp:8
Minisat::Internal::RegionAllocator::free
void free(int size)
Definition: Alloc.h:61
Minisat::Internal::RegionAllocator::Unit_Size
@ Unit_Size
Definition: Alloc.h:46
Minisat::Internal::RegionAllocator::lea
T * lea(Ref r)
Definition: Alloc.h:67
Minisat
Definition: Minisat.h:48
Minisat::Internal::RegionAllocator::cap
uint32_t cap
Definition: Alloc.h:37
Minisat::Internal::RegionAllocator::operator[]
T & operator[](Ref r)
Definition: Alloc.h:64
Minisat::Internal::RegionAllocator::ael
Ref ael(const T *t)
Definition: Alloc.h:70
basic.h
Basic declarations, included by all source files.
Minisat::Internal::RegionAllocator
Definition: Alloc.h:33
Minisat::Internal::RegionAllocator::lea
const T * lea(Ref r) const
Definition: Alloc.h:68
Minisat::Internal::RegionAllocator::moveTo
void moveTo(RegionAllocator &to)
Definition: Alloc.h:76
Minisat::Internal::RegionAllocator::RegionAllocator
RegionAllocator(uint32_t start_cap=1024 *1024)
Definition: Alloc.h:48
Minisat::Internal::RegionAllocator::wasted
uint32_t wasted() const
Definition: Alloc.h:58
Minisat::Internal::RegionAllocator::sz
uint32_t sz
Definition: Alloc.h:36
Minisat::Internal::RegionAllocator::wasted_
uint32_t wasted_
Definition: Alloc.h:38