ABC: A System for Sequential Synthesis and Verification
 
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
21#ifndef Minisat_Alloc_h
22#define Minisat_Alloc_h
23
24#include "sat/bsat2/XAlloc.h"
25#include "sat/bsat2/Vec.h"
26
28
29namespace Minisat {
30
31//=================================================================================================
32// Simple Region-based memory allocator:
33
34template<class T>
36{
37 T* memory;
38 uint32_t sz;
39 uint32_t cap;
40 uint32_t wasted_;
41
42 void capacity(uint32_t min_cap);
43
44 public:
45 // TODO: make this a class for better type-checking?
46 typedef uint32_t Ref;
48 enum { Unit_Size = sizeof(uint32_t) };
49
50 explicit RegionAllocator(uint32_t start_cap = 1024*1024) : memory(NULL), sz(0), cap(0), wasted_(0){ capacity(start_cap); }
52 {
53 if (memory != NULL)
54 ::free(memory);
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) { assert(r >= 0 && r < sz); return memory[r]; }
66 const T& operator[](Ref r) const { assert(r >= 0 && r < sz); return memory[r]; }
67
68 T* lea (Ref r) { assert(r >= 0 && r < sz); return &memory[r]; }
69 const T* lea (Ref r) const { assert(r >= 0 && r < sz); return &memory[r]; }
70 Ref ael (const T* t) { assert((void*)t >= (void*)&memory[0] && (void*)t < (void*)&memory[sz-1]);
71 return (Ref)(t - &memory[0]); }
72
74 if (to.memory != NULL) ::free(to.memory);
75 to.memory = memory;
76 to.sz = sz;
77 to.cap = cap;
78 to.wasted_ = wasted_;
79
80 memory = NULL;
81 sz = cap = wasted_ = 0;
82 }
83
84
85};
86
87template<class T>
88void RegionAllocator<T>::capacity(uint32_t min_cap)
89{
90 if (cap >= min_cap) return;
91
92 uint32_t prev_cap = cap;
93 while (cap < min_cap){
94 // NOTE: Multiply by a factor (13/8) without causing overflow, then add 2 and make the
95 // result even by clearing the least significant bit. The resulting sequence of capacities
96 // is carefully chosen to hit a maximum capacity that is close to the '2^32-1' limit when
97 // using 'uint32_t' as indices so that as much as possible of this space can be used.
98 uint32_t delta = ((cap >> 1) + (cap >> 3) + 2) & ~1;
99 cap += delta;
100
101 if (cap <= prev_cap)
102#ifdef __wasm
103 abort();
104#else
105 throw OutOfMemoryException();
106#endif
107 }
108 // printf(" .. (%p) cap = %u\n", this, cap);
109
110 assert(cap > 0);
111 memory = (T*)xrealloc(memory, sizeof(T)*cap);
112}
113
114
115template<class T>
118{
119 // printf("ALLOC called (this = %p, size = %d)\n", this, size); fflush(stdout);
120 assert(size > 0);
121 capacity(sz + size);
122
123 uint32_t prev_sz = sz;
124 sz += size;
125
126 // Handle overflow:
127 if (sz < prev_sz)
128#ifdef __wasm
129 abort();
130#else
131 throw OutOfMemoryException();
132#endif
133
134 return prev_sz;
135}
136
137
138//=================================================================================================
139}
140
142
143#endif
unsigned int uint32_t
Definition Fxch.h:32
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
#define UINT32_MAX
Definition pstdint.h:388
@ Ref_Undef
Definition Alloc.h:47
void _free(int size)
Definition Alloc.h:62
void moveTo(RegionAllocator &to)
Definition Alloc.h:73
const T & operator[](Ref r) const
Definition Alloc.h:66
uint32_t size() const
Definition Alloc.h:58
Ref ael(const T *t)
Definition Alloc.h:70
uint32_t wasted() const
Definition Alloc.h:59
RegionAllocator(uint32_t start_cap=1024 *1024)
Definition Alloc.h:50
Ref alloc(int size)
Definition Alloc.h:117
const T * lea(Ref r) const
Definition Alloc.h:69
T & operator[](Ref r)
Definition Alloc.h:65
@ Unit_Size
Definition Alloc.h:48
Definition Alg.h:28
#define assert(ex)
Definition util_old.h:213
VOID_HACK abort()
VOID_HACK free()