Adding support for reading wrong assumptions
[satlib.git] / glucose-syrup / mtl / Alloc.h
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 Glucose_Alloc_h
22 #define Glucose_Alloc_h
23
24 #include "mtl/XAlloc.h"
25 #include "mtl/Vec.h"
26
27 namespace Glucose {
28
29 //=================================================================================================
30 // Simple Region-based memory allocator:
31
32 template<class T>
33 class RegionAllocator
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(uint32_t) };
47
48     explicit RegionAllocator(uint32_t start_cap = 1024*1024) : memory(NULL), sz(0), cap(0), wasted_(0){ capacity(start_cap); }
49     ~RegionAllocator()
50     {
51         if (memory != NULL)
52             ::free(memory);
53     }
54
55
56     uint32_t size      () const      { return sz; }
57     uint32_t getCap    () const      { return cap;}
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)       { assert(r >= 0 && r < sz); return memory[r]; }
65     const T& operator[](Ref r) const { assert(r >= 0 && r < sz); return memory[r]; }
66
67     T*       lea       (Ref r)       { assert(r >= 0 && r < sz); return &memory[r]; }
68     const T* lea       (Ref r) const { assert(r >= 0 && r < sz); return &memory[r]; }
69     Ref      ael       (const T* t)  { assert((void*)t >= (void*)&memory[0] && (void*)t < (void*)&memory[sz-1]);
70         return  (Ref)(t - &memory[0]); }
71
72     void     moveTo(RegionAllocator& to) {
73         if (to.memory != NULL) ::free(to.memory);
74         to.memory = memory;
75         to.sz = sz;
76         to.cap = cap;
77         to.wasted_ = wasted_;
78
79         memory = NULL;
80         sz = cap = wasted_ = 0;
81     }
82
83     void copyTo(RegionAllocator& to) const {
84      //   if (to.memory != NULL) ::free(to.memory);
85         to.memory = (T*)xrealloc(to.memory, sizeof(T)*cap);
86         memcpy(to.memory,memory,sizeof(T)*cap);        
87         to.sz = sz;
88         to.cap = cap;
89         to.wasted_ = wasted_;
90     }
91
92
93
94 };
95
96 template<class T>
97 void RegionAllocator<T>::capacity(uint32_t min_cap)
98 {
99     if (cap >= min_cap) return;
100
101     uint32_t prev_cap = cap;
102     while (cap < min_cap){
103         // NOTE: Multiply by a factor (13/8) without causing overflow, then add 2 and make the
104         // result even by clearing the least significant bit. The resulting sequence of capacities
105         // is carefully chosen to hit a maximum capacity that is close to the '2^32-1' limit when
106         // using 'uint32_t' as indices so that as much as possible of this space can be used.
107         uint32_t delta = ((cap >> 1) + (cap >> 3) + 2) & ~1;
108         cap += delta;
109
110         if (cap <= prev_cap)
111             throw OutOfMemoryException();
112     }
113     //printf(" .. (%p) cap = %u\n", this, cap);
114
115     assert(cap > 0);
116     memory = (T*)xrealloc(memory, sizeof(T)*cap);
117 }
118
119
120 template<class T>
121 typename RegionAllocator<T>::Ref
122 RegionAllocator<T>::alloc(int size)
123
124     //printf("ALLOC called (this = %p, size = %d)\n", this, size); fflush(stdout);
125     assert(size > 0);
126     capacity(sz + size);
127
128     uint32_t prev_sz = sz;
129     sz += size;
130     
131     // Handle overflow:
132     if (sz < prev_sz)
133         throw OutOfMemoryException();
134
135     return prev_sz;
136 }
137
138
139 //=================================================================================================
140 }
141
142 #endif