Zero  0.1.0
allocator.h
Go to the documentation of this file.
1 #ifndef __ALLOCATOR_H
2 #define __ALLOCATOR_H
3 
4 #include "w_defines.h"
5 #include <stdexcept>
6 #include "tls.h"
7 #include "block_alloc.h"
8 
9 class xct_t;
10 class plog_xct_t;
11 class logrec_t;
12 
14 public:
15  void* allocate(size_t n) {
16  return malloc(n);
17  }
18 
19  void release(void* p, size_t) {
20  free(p);
21  }
22 };
23 
25 private:
26 public:
27 
28  template<typename T>
29  T* allocate(size_t n);
30 
31  template<typename T>
32  void release(T* p, size_t n = 0);
33 };
34 
35 /*
36  * Macros to define class-specific new and delete operators that
37  * make use of smlevel_0::allocator
38  */
39 
40 #define DEFINE_SM_ALLOC(type) \
41  void* type::operator new(size_t s) \
42  {\
43  return smlevel_0::allocator.allocate<type>(s);\
44  }\
45 \
46  void type::operator delete(void* p, size_t s)\
47  {\
48  smlevel_0::allocator.release<type>((type*) p, s);\
49  }\
50 
51 
58 template<typename T, size_t Alignment = sizeof(T)>
60  typedef T value_type;
61 
62  typedef T* pointer;
63 
64  typedef const T* const_pointer;
65 
66  typedef T& reference;
67 
68  typedef const T& const_reference;
69 
70  typedef std::size_t size_type;
71 
72  typedef std::ptrdiff_t difference_type;
73 
74  static_assert(Alignment > 0 && (Alignment & (Alignment - 1)) == 0,
75  "Alignment argument of memalign_allocator must be power of 2");
76 
77  template<class U>
78  struct rebind {
80  };
81 
82  inline memalign_allocator() throw() {}
83 
84  inline memalign_allocator(const memalign_allocator&) throw() {}
85 
86  template<class U>
88 
89  inline ~memalign_allocator() throw() {}
90 
91  inline pointer address(reference r) {
92  return &r;
93  }
94 
95  inline const_pointer address(const_reference r) const {
96  return &r;
97  }
98 
99  inline void construct(pointer p, const_reference value) {
100  new(p) value_type(value);
101  }
102 
103  inline void destroy(pointer p) {
104  p->~value_type();
105  }
106 
107  inline size_type max_size() const throw() {
108  return size_type(-1) / sizeof(T);
109  }
110 
111  inline bool operator==(const memalign_allocator&) {
112  return true;
113  }
114 
115  inline bool operator!=(const memalign_allocator& rhs) {
116  return !operator==(rhs);
117  }
118 
119  pointer allocate(size_type count, const_pointer = 0) {
120  pointer p = nullptr;
121  int res = posix_memalign((void**)&p, Alignment, sizeof(value_type) * count);
122  if (res != 0) {
123  throw std::bad_alloc();
124  }
125 
126  return p;
127  }
128 
129  void deallocate(pointer p, size_t = 1) {
130  free(p);
131  }
132 };
133 
134 #endif // __ALLOCATOR_H
void * allocate(size_t n)
Definition: allocator.h:15
T & reference
Definition: allocator.h:66
size_type max_size() const
Definition: allocator.h:107
memalign_allocator(const memalign_allocator &)
Definition: allocator.h:84
A transaction. Internal to the storage manager.This class may be used in a limited way for the handli...
Definition: xct.h:185
bool operator==(PooledAllocator< T > const &left, PooledAllocator< U > const &right)
Returns true if objects allocated from one pool can be deallocated from the other.
Definition: stl_pooled_alloc.h:172
const T & const_reference
Definition: allocator.h:68
Definition: allocator.h:59
T * pointer
Definition: allocator.h:62
~memalign_allocator()
Definition: allocator.h:89
Represents a transactional log record.
Definition: logrec.h:143
pointer address(reference r)
Definition: allocator.h:91
void release(void *p, size_t)
Definition: allocator.h:19
bool operator!=(const memalign_allocator &rhs)
Definition: allocator.h:115
memalign_allocator(const memalign_allocator< U, Alignment > &)
Definition: allocator.h:87
memalign_allocator()
Definition: allocator.h:82
Definition: allocator.h:24
T value_type
Definition: allocator.h:60
Definition: allocator.h:13
const_pointer address(const_reference r) const
Definition: allocator.h:95
const T * const_pointer
Definition: allocator.h:64
pointer allocate(size_type count, const_pointer=0)
Definition: allocator.h:119
std::ptrdiff_t difference_type
Definition: allocator.h:72
std::size_t size_type
Definition: allocator.h:70
void construct(pointer p, const_reference value)
Definition: allocator.h:99
memalign_allocator< U, Alignment > other
Definition: allocator.h:79
void destroy(pointer p)
Definition: allocator.h:103
bool operator==(const memalign_allocator &)
Definition: allocator.h:111
#define T
Definition: w_okvl_inl.h:45
void deallocate(pointer p, size_t=1)
Definition: allocator.h:129
Definition: allocator.h:78