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 Glucose_Alloc_h
22#define Glucose_Alloc_h
23
24#include "sat/glucose2/XAlloc.h"
25#include "sat/glucose2/Vec.h"
26
28
29namespace Gluco2 {
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 void clear () { sz = 0; wasted_=0; }
64
65 // Deref, Load Effective Address (LEA), Inverse of LEA (AEL):
66 T& operator[](Ref r) { assert(r >= 0 && r < sz); return memory[r]; }
67 const T& operator[](Ref r) const { assert(r >= 0 && r < sz); return memory[r]; }
68
69 T* lea (Ref r) { assert(r >= 0 && r < sz); return &memory[r]; }
70 const T* lea (Ref r) const { assert(r >= 0 && r < sz); return &memory[r]; }
71 Ref ael (const T* t) { assert((void*)t >= (void*)&memory[0] && (void*)t < (void*)&memory[sz-1]);
72 return (Ref)(t - &memory[0]); }
73
75 if (to.memory != NULL) ::free(to.memory);
76 to.memory = memory;
77 to.sz = sz;
78 to.cap = cap;
79 to.wasted_ = wasted_;
80
81 memory = NULL;
82 sz = cap = wasted_ = 0;
83 }
84
85
86};
87
88template<class T>
89void RegionAllocator<T>::capacity(uint32_t min_cap)
90{
91 if (cap >= min_cap) return;
92
93 uint32_t prev_cap = cap;
94 while (cap < min_cap){
95 // NOTE: Multiply by a factor (13/8) without causing overflow, then add 2 and make the
96 // result even by clearing the least significant bit. The resulting sequence of capacities
97 // is carefully chosen to hit a maximum capacity that is close to the '2^32-1' limit when
98 // using 'uint32_t' as indices so that as much as possible of this space can be used.
99 uint32_t delta = ((cap >> 1) + (cap >> 3) + 2) & ~1;
100 cap += delta;
101
102 if (cap <= prev_cap)
103 fatal_out_of_memory();
104 }
105 //printf(" .. (%p) cap = %u\n", this, cap);
106
107 assert(cap > 0);
108 memory = (T*)xrealloc(memory, sizeof(T)*cap);
109}
110
111
112template<class T>
115{
116 //printf("ALLOC called (this = %p, size = %d)\n", this, size); fflush(stdout);
117 assert(size > 0);
118 capacity(sz + size);
119
120 uint32_t prev_sz = sz;
121 sz += size;
122
123 // Handle overflow:
124 if (sz < prev_sz)
125 fatal_out_of_memory();
126
127 return prev_sz;
128}
129
130
131//=================================================================================================
132}
133
135
136#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 ael(const T *t)
Definition Alloc.h:71
const T * lea(Ref r) const
Definition Alloc.h:70
uint32_t wasted() const
Definition Alloc.h:59
uint32_t size() const
Definition Alloc.h:58
RegionAllocator(uint32_t start_cap=1024 *1024)
Definition Alloc.h:50
void moveTo(RegionAllocator &to)
Definition Alloc.h:74
const T & operator[](Ref r) const
Definition Alloc.h:67
@ Ref_Undef
Definition Alloc.h:47
@ Unit_Size
Definition Alloc.h:48
T & operator[](Ref r)
Definition Alloc.h:66
Ref alloc(int size)
Definition Alloc.h:114
void free_(int size)
Definition Alloc.h:62
Definition Alg.h:28
#define assert(ex)
Definition util_old.h:213
VOID_HACK free()