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 Glucose_Alloc_h
22 #define Glucose_Alloc_h
24 #include "mtl/XAlloc.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(uint32_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 getCap () const { return cap;}
58 uint32_t wasted () const { return wasted_; }
61 void free (int size) { wasted_ += size; }
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]; }
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]); }
72 void moveTo(RegionAllocator& to) {
73 if (to.memory != NULL) ::free(to.memory);
80 sz = cap = wasted_ = 0;
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);
97 void RegionAllocator<T>::capacity(uint32_t min_cap)
99 if (cap >= min_cap) return;
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;
111 throw OutOfMemoryException();
113 //printf(" .. (%p) cap = %u\n", this, cap);
116 memory = (T*)xrealloc(memory, sizeof(T)*cap);
121 typename RegionAllocator<T>::Ref
122 RegionAllocator<T>::alloc(int size)
124 //printf("ALLOC called (this = %p, size = %d)\n", this, size); fflush(stdout);
128 uint32_t prev_sz = sz;
133 throw OutOfMemoryException();
139 //=================================================================================================