SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
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 #ifndef Minisat_Alloc_h
21 #define Minisat_Alloc_h
22 
23 #include "XAlloc.h"
24 #include "Vec.h"
25 
26 namespace Minisat
27 {
28  //=================================================================================================
29  // Simple Region-based memory allocator:
30 
31  template<class T>
33  {
34  T* memory;
35  uint32_t sz;
36  uint32_t cap;
37  uint32_t wasted_;
38 
39  void capacity( uint32_t min_cap );
40 
41  public:
42  // TODO: make this a class for better type-checking?
43  typedef uint32_t Ref;
44 
45  enum { Ref_Undef = UINT32_MAX };
46 
47  enum { Unit_Size = sizeof(uint32_t) };
48 
49  explicit RegionAllocator( uint32_t start_cap = 1024 * 1024 ):
50  memory( NULL ),
51  sz( 0 ),
52  cap( 0 ),
53  wasted_( 0 )
54  {
55  capacity( start_cap );
56  }
58  {
59  if( memory != NULL )
60  ::free( memory );
61  }
62 
63  uint32_t size() const
64  {
65  return sz;
66  }
67 
68  uint32_t wasted() const
69  {
70  return wasted_;
71  }
72 
73  Ref alloc( int size );
74 
75  void free( int size )
76  {
77  wasted_ += (uint32_t)size;
78  }
79 
80  // Deref, Load Effective Address (LEA), Inverse of LEA (AEL):
81  T& operator []( Ref r )
82  {
83  //assert( r >= 0 );
84  assert( r < sz );
85  return memory[r];
86  }
87 
88  const T& operator []( Ref r ) const
89  {
90  assert( /*r >= 0 &&*/ r < sz );
91  return memory[r];
92  }
93 
94  T* lea( Ref r )
95  {
96  assert( /*r >= 0 &&*/ r < sz );
97  return &memory[r];
98  }
99 
100  const T* lea( Ref r ) const
101  {
102  assert( /*r >= 0 &&*/ r < sz );
103  return &memory[r];
104  }
105 
106  Ref ael( const T* t )
107  {
108  assert( (void*)t >= (void*)&memory[0] && (void*)t < (void*)&memory[sz - 1] );
109  return (Ref)(t - &memory[0]);
110  }
111 
113  {
114  if( to.memory != NULL )
115  ::free( to.memory );
116  to.memory = memory;
117  to.sz = sz;
118  to.cap = cap;
119  to.wasted_ = wasted_;
120 
121  memory = NULL;
122  sz = cap = wasted_ = 0;
123  }
124 
125  };
126 
127  template<class T>
128  void RegionAllocator<T>::capacity( uint32_t min_cap )
129  {
130  if( cap >= min_cap )
131  return;
132 
133  uint32_t prev_cap = cap;
134  while( cap < min_cap )
135  {
136  // NOTE: Multiply by a factor (13/8) without causing overflow, then add 2 and make the
137  // result even by clearing the least significant bit. The resulting sequence of capacities
138  // is carefully chosen to hit a maximum capacity that is close to the '2^32-1' limit when
139  // using 'uint32_t' as indices so that as much as possible of this space can be used.
140  uint32_t delta = ((cap >> 1) + (cap >> 3) + 2) & (uint32_t)~1;
141  cap += delta;
142 
143  if( cap <= prev_cap )
144  throw OutOfMemoryException();
145  }
146  // printf(" .. (%p) cap = %u\n", this, cap);
147 
148  assert( cap > 0 );
149  memory = (T*)xrealloc( memory, sizeof(T) * cap );
150  }
151 
152  template<class T>
154  {
155  // printf("ALLOC called (this = %p, size = %d)\n", this, size); fflush(stdout);
156  assert( size > 0 );
157  capacity( sz + (uint32_t)size );
158 
159  uint32_t prev_sz = sz;
160  sz += (uint32_t)size;
161 
162  // Handle overflow:
163  if( sz < prev_sz )
164  throw OutOfMemoryException();
165 
166  return prev_sz;
167  }
168 
169  //=================================================================================================
170 }
171 
172 #endif
void moveTo(RegionAllocator &to)
Definition: Alloc.h:112
const T * lea(Ref r) const
Definition: Alloc.h:100
T & operator[](Ref r)
Definition: Alloc.h:81
void capacity(uint32_t min_cap)
Definition: Alloc.h:128
T * lea(Ref r)
Definition: Alloc.h:94
uint32_t size() const
Definition: Alloc.h:63
Ref ael(const T *t)
Definition: Alloc.h:106
uint32_t wasted() const
Definition: Alloc.h:68
RegionAllocator(uint32_t start_cap=1024 *1024)
Definition: Alloc.h:49
Ref alloc(int size)
Definition: Alloc.h:153
void free(int size)
Definition: Alloc.h:75
Definition: Alg.h:27
static void * xrealloc(void *ptr, size_t size)
Definition: XAlloc.h:32