Debian Codegraph @

btas/varray/allocators.h

raw
1 /*
2 * allocators.h
3 *
4 * Created on: Jan 4, 2014
5 * Author: evaleev
6 */
7
8 #ifndef BTAS_VARRAY_ALLOCATORS_H_
9 #define BTAS_VARRAY_ALLOCATORS_H_
10
11 #include <cstddef>
12 #include <algorithm>
13 #include <memory>
14 #include <vector>
15 #include <stdexcept>
16 #include <iostream>
17 #include <iomanip>
18
19 namespace btas {
20
21 struct stack_arena {
22 size_t size; //!< in bytes
23 char* const buffer; //!< buffer begin
24 char* current; //!< ptr to the free space in buffer
25
26 template<typename T>
27 stack_arena(T* b, size_t s) :
28 size(s),
29 buffer(reinterpret_cast<char*>(b)),
30 current(reinterpret_cast<char*>(b))
31 {
32 }
33
34 void increment(const size_t n) { current += n; }
35 void decrement(const size_t n) { current -= n; }
36 };
37
38
39 /// This is a very simple allocator implementation that uses an externally managed memory stack.
40 /// It's mostly for demonstration purposes.
41
42 /// stack_allocator is a first-in-last-out allocator,
43 /// i.e. deallocation of the memory must happen in the opposite order of allocation.
44 template<typename T>
45 class stack_allocator {
46 private:
47 std::shared_ptr<stack_arena> arena_;
48
49 size_t size() const {
50 return arena_->size / sizeof(T);
51 }
52 T* buffer() const {
53 return reinterpret_cast<T*>(arena_->buffer);
54 }
55 T* current() const {
56 return reinterpret_cast<T*>(arena_->current);
57 }
58
59 void increment(const size_t n) {
60 arena_->increment(n * sizeof(T));
61 }
62 void decrement(const size_t n) {
63 arena_->decrement(n * sizeof(T));
64 }
65
66 public:
67 // do not allocate here
68 stack_allocator(std::shared_ptr<stack_arena> a) :
69 arena_(a) {
70 }
71
72 stack_allocator(const stack_allocator<T>& o) :
73 arena_(o.arena_) {
74 }
75 stack_allocator& operator=(const stack_allocator<T>& o) {
76 arena_ = o.arena_;
77 return *this;
78 }
79 struct rebind {
80 typedef stack_allocator<T> other;
81 };
82
83 typedef T* pointer;
84 typedef T& reference;
85 typedef const T* const_pointer;
86 typedef const T& const_reference;
87 typedef T value_type;
88 typedef size_t size_type;
89 typedef ptrdiff_t difference_type;
90
91 pointer allocate(size_type n, const void* = 0) {
92 //std::cout << "Allocating " << std::setw(6) << sizeof(T)*n << " bytes. "
93 // << "Available memory: "
94 // << std::setw(6) << sizeof(T) * (size() - std::distance(buffer(), current())) << " bytes."<< std::endl;
95 pointer out = current();
96 increment(n);
97 if (std::distance(buffer(), current()) > size())
98 throw std::runtime_error("preallocated memory exhausted");
99 return out;
100 }
101
102 pointer address(reference x) const {
103 return &x;
104 }
105 const_pointer address(const_reference x) const {
106 return &x;
107 }
108
109 void deallocate(pointer p, size_type n) {
110 assert(p == current() - n);
111 decrement(n);
112 }
113
114 void construct(pointer p, const T& val) {
115 *p = val;
116 }
117 void destroy(pointer p) {
118 }
119
120 size_type max_size() const {
121 return size();
122 }
123 };
124
125 }
126
127
128 #endif /* BTAS_VARRAY_ALLOCATORS_H_ */