abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Minisat::RegionAllocator< T > Class Template Reference

#include <Alloc.h>

Public Types

enum  { Ref_Undef = UINT32_MAX }
 
enum  { Unit_Size = sizeof(uint32_t) }
 
typedef uint32_t Ref
 

Public Member Functions

 RegionAllocator (uint32_t start_cap=1024 *1024)
 
 ~RegionAllocator ()
 
uint32_t size () const
 
uint32_t wasted () const
 
Ref alloc (int size)
 
void _free (int size)
 
T & operator[] (Ref r)
 
const T & operator[] (Ref r) const
 
T * lea (Ref r)
 
const T * lea (Ref r) const
 
Ref ael (const T *t)
 
void moveTo (RegionAllocator &to)
 

Private Member Functions

void capacity (uint32_t min_cap)
 

Private Attributes

T * memory
 
uint32_t sz
 
uint32_t cap
 
uint32_t wasted_
 

Detailed Description

template<class T>
class Minisat::RegionAllocator< T >

Definition at line 33 of file Alloc.h.

Member Typedef Documentation

template<class T>
typedef uint32_t Minisat::RegionAllocator< T >::Ref

Definition at line 44 of file Alloc.h.

Member Enumeration Documentation

template<class T>
anonymous enum
Enumerator
Ref_Undef 

Definition at line 45 of file Alloc.h.

45 { Ref_Undef = UINT32_MAX };
#define UINT32_MAX
Definition: pstdint.h:388
template<class T>
anonymous enum
Enumerator
Unit_Size 

Definition at line 46 of file Alloc.h.

46 { Unit_Size = sizeof(uint32_t) };

Constructor & Destructor Documentation

template<class T>
Minisat::RegionAllocator< T >::RegionAllocator ( uint32_t  start_cap = 1024*1024)
inlineexplicit

Definition at line 48 of file Alloc.h.

48 : memory(NULL), sz(0), cap(0), wasted_(0){ capacity(start_cap); }
void capacity(uint32_t min_cap)
Definition: Alloc.h:86
template<class T>
Minisat::RegionAllocator< T >::~RegionAllocator ( )
inline

Definition at line 49 of file Alloc.h.

50  {
51  if (memory != NULL)
52  ::free(memory);
53  }
VOID_HACK free()

Member Function Documentation

template<class T>
void Minisat::RegionAllocator< T >::_free ( int  size)
inline

Definition at line 60 of file Alloc.h.

60 { wasted_ += size; }
uint32_t size() const
Definition: Alloc.h:56
template<class T>
Ref Minisat::RegionAllocator< T >::ael ( const T *  t)
inline

Definition at line 68 of file Alloc.h.

68  { assert((void*)t >= (void*)&memory[0] && (void*)t < (void*)&memory[sz-1]);
69  return (Ref)(t - &memory[0]); }
#define assert(ex)
Definition: util_old.h:213
template<class T >
RegionAllocator< T >::Ref Minisat::RegionAllocator< T >::alloc ( int  size)

Definition at line 111 of file Alloc.h.

112 {
113  // printf("ALLOC called (this = %p, size = %d)\n", this, size); fflush(stdout);
114  assert(size > 0);
115  capacity(sz + size);
116 
117  uint32_t prev_sz = sz;
118  sz += size;
119 
120  // Handle overflow:
121  if (sz < prev_sz)
122  throw OutOfMemoryException();
123 
124  return prev_sz;
125 }
uint32_t size() const
Definition: Alloc.h:56
void capacity(uint32_t min_cap)
Definition: Alloc.h:86
#define assert(ex)
Definition: util_old.h:213
template<class T >
void Minisat::RegionAllocator< T >::capacity ( uint32_t  min_cap)
private

Definition at line 86 of file Alloc.h.

87 {
88  if (cap >= min_cap) return;
89 
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;
97  cap += delta;
98 
99  if (cap <= prev_cap)
100  throw OutOfMemoryException();
101  }
102  // printf(" .. (%p) cap = %u\n", this, cap);
103 
104  assert(cap > 0);
105  memory = (T*)xrealloc(memory, sizeof(T)*cap);
106 }
static void * xrealloc(void *ptr, size_t size)
Definition: XAlloc.h:33
#define assert(ex)
Definition: util_old.h:213
template<class T>
T* Minisat::RegionAllocator< T >::lea ( Ref  r)
inline

Definition at line 66 of file Alloc.h.

66 { assert(r >= 0 && r < sz); return &memory[r]; }
#define assert(ex)
Definition: util_old.h:213
template<class T>
const T* Minisat::RegionAllocator< T >::lea ( Ref  r) const
inline

Definition at line 67 of file Alloc.h.

67 { assert(r >= 0 && r < sz); return &memory[r]; }
#define assert(ex)
Definition: util_old.h:213
template<class T>
void Minisat::RegionAllocator< T >::moveTo ( RegionAllocator< T > &  to)
inline

Definition at line 71 of file Alloc.h.

71  {
72  if (to.memory != NULL) ::free(to.memory);
73  to.memory = memory;
74  to.sz = sz;
75  to.cap = cap;
76  to.wasted_ = wasted_;
77 
78  memory = NULL;
79  sz = cap = wasted_ = 0;
80  }
VOID_HACK free()
template<class T>
T& Minisat::RegionAllocator< T >::operator[] ( Ref  r)
inline

Definition at line 63 of file Alloc.h.

63 { assert(r >= 0 && r < sz); return memory[r]; }
#define assert(ex)
Definition: util_old.h:213
template<class T>
const T& Minisat::RegionAllocator< T >::operator[] ( Ref  r) const
inline

Definition at line 64 of file Alloc.h.

64 { assert(r >= 0 && r < sz); return memory[r]; }
#define assert(ex)
Definition: util_old.h:213
template<class T>
uint32_t Minisat::RegionAllocator< T >::size ( ) const
inline

Definition at line 56 of file Alloc.h.

56 { return sz; }
template<class T>
uint32_t Minisat::RegionAllocator< T >::wasted ( ) const
inline

Definition at line 57 of file Alloc.h.

57 { return wasted_; }

Field Documentation

template<class T>
uint32_t Minisat::RegionAllocator< T >::cap
private

Definition at line 37 of file Alloc.h.

template<class T>
T* Minisat::RegionAllocator< T >::memory
private

Definition at line 35 of file Alloc.h.

template<class T>
uint32_t Minisat::RegionAllocator< T >::sz
private

Definition at line 36 of file Alloc.h.

template<class T>
uint32_t Minisat::RegionAllocator< T >::wasted_
private

Definition at line 38 of file Alloc.h.


The documentation for this class was generated from the following file: