yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
21 #ifndef Minisat_Alloc_h
22 #define Minisat_Alloc_h
23 
24 #include "XAlloc.h"
25 #include "Vec.h"
26 
27 namespace Minisat {
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  // TODO: make this a class for better type-checking?
44  typedef uint32_t Ref;
45  enum { Ref_Undef = UINT32_MAX };
46  enum { Unit_Size = sizeof(T) };
47 
48  explicit RegionAllocator(uint32_t start_cap = 1024*1024) : memory(NULL), sz(0), cap(0), wasted_(0){ capacity(start_cap); }
50  {
51  if (memory != NULL)
52  ::free(memory);
53  }
54 
55 
56  uint32_t size () const { return sz; }
57  uint32_t wasted () const { return wasted_; }
58 
59  Ref alloc (int size);
60  void free (int size) { wasted_ += size; }
61 
62  // Deref, Load Effective Address (LEA), Inverse of LEA (AEL):
63  T& operator[](Ref r) { assert(r < sz); return memory[r]; }
64  const T& operator[](Ref r) const { assert(r < sz); return memory[r]; }
65 
66  T* lea (Ref r) { assert(r < sz); return &memory[r]; }
67  const T* lea (Ref r) const { assert(r < sz); return &memory[r]; }
68  Ref ael (const T* t) { assert((void*)t >= (void*)&memory[0] && (void*)t < (void*)&memory[sz-1]);
69  return (Ref)(t - &memory[0]); }
70 
71  void moveTo(RegionAllocator& to) {
72  if (to.memory != NULL) ::free(to.memory);
73  to.memory = memory;
74  to.sz = sz;
75  to.cap = cap;
76  to.wasted_ = wasted_;
77 
78  memory = NULL;
79  sz = cap = wasted_ = 0;
80  }
81 
82 
83 };
84 
85 template<class T>
86 void RegionAllocator<T>::capacity(uint32_t min_cap)
87 {
88  if (cap >= min_cap) return;
89 
90  uint32_t prev_cap = cap;
91  while (cap < min_cap){
92  // NOTE: Multiply by a factor (13/8) without causing overflow, then add 2 and make the
93  // result even by clearing the least significant bit. The resulting sequence of capacities
94  // is carefully chosen to hit a maximum capacity that is close to the '2^32-1' limit when
95  // using 'uint32_t' as indices so that as much as possible of this space can be used.
96  uint32_t delta = ((cap >> 1) + (cap >> 3) + 2) & ~1;
97  cap += delta;
98 
99  if (cap <= prev_cap)
100  throw OutOfMemoryException();
101  }
102  // printf(" .. (%p) cap = %u\n", this, cap);
103 
104  assert(cap > 0);
105  memory = (T*)xrealloc(memory, sizeof(T)*cap);
106 }
107 
108 
109 template<class T>
112 {
113  // printf("ALLOC called (this = %p, size = %d)\n", this, size); fflush(stdout);
114  assert(size > 0);
115  capacity(sz + size);
116 
117  uint32_t prev_sz = sz;
118  sz += size;
119 
120  // Handle overflow:
121  if (sz < prev_sz)
122  throw OutOfMemoryException();
123 
124  return prev_sz;
125 }
126 
127 
128 //=================================================================================================
129 }
130 
131 #endif
uint32_t size() const
Definition: Alloc.h:56
uint32_t wasted() const
Definition: Alloc.h:57
Ref alloc(int size)
Definition: Alloc.h:111
T * lea(Ref r)
Definition: Alloc.h:66
Ref ael(const T *t)
Definition: Alloc.h:68
RegionAllocator(uint32_t start_cap=1024 *1024)
Definition: Alloc.h:48
const T * lea(Ref r) const
Definition: Alloc.h:67
const T & operator[](Ref r) const
Definition: Alloc.h:64
void moveTo(RegionAllocator &to)
Definition: Alloc.h:71
#define NULL
void free(int size)
Definition: Alloc.h:60
static void * xrealloc(void *ptr, size_t size)
Definition: XAlloc.h:33
T & operator[](Ref r)
Definition: Alloc.h:63
void capacity(uint32_t min_cap)
Definition: Alloc.h:86
#define UINT32_MAX
Definition: ilang_lexer.cc:103