1 /*****************************************************************************************[Alloc.h]
2 Copyright (c) 2008-2010, Niklas Sorensson
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:
10 The above copyright notice and this permission notice shall be included in all copies or
11 substantial portions of the Software.
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 **************************************************************************************************/
21 #ifndef Minisat_Alloc_h
22 #define Minisat_Alloc_h
24 #include "libs/minisat/XAlloc.h"
25 #include "libs/minisat/Vec.h"
29 //=================================================================================================
30 // Simple Region-based memory allocator:
40 void capacity(uint32_t min_cap
);
43 // TODO: make this a class for better type-checking?
45 enum { Ref_Undef
= UINT32_MAX
};
46 enum { Unit_Size
= sizeof(T
) };
48 explicit RegionAllocator(uint32_t start_cap
= 1024*1024) : memory(NULL
), sz(0), cap(0), wasted_(0){ capacity(start_cap
); }
56 uint32_t size () const { return sz
; }
57 uint32_t wasted () const { return wasted_
; }
60 void free (int size
) { wasted_
+= size
; }
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
]; }
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]); }
71 void moveTo(RegionAllocator
& to
) {
72 if (to
.memory
!= NULL
) ::free(to
.memory
);
79 sz
= cap
= wasted_
= 0;
86 void RegionAllocator
<T
>::capacity(uint32_t min_cap
)
88 if (cap
>= min_cap
) return;
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;
100 throw OutOfMemoryException();
102 // printf(" .. (%p) cap = %u\n", this, cap);
105 memory
= (T
*)xrealloc(memory
, sizeof(T
)*cap
);
110 typename RegionAllocator
<T
>::Ref
111 RegionAllocator
<T
>::alloc(int size
)
113 // printf("ALLOC called (this = %p, size = %d)\n", this, size); fflush(stdout);
117 uint32_t prev_sz
= sz
;
122 throw OutOfMemoryException();
128 //=================================================================================================