blob: 4d3426b612a53b5b459dcc010ee8fa2990df2304 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#ifndef STACK_ALLOCATOR_H
#define STACK_ALLOCATOR_H
#include <cstddef>
#include <new>
template <class T, std::size_t N>
class stack_allocator
{
char buf_[sizeof(T)*N];
char* ptr_;
public:
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
template <class U> struct rebind {typedef stack_allocator<U, N> other;};
stack_allocator() : ptr_(buf_) {}
private:
stack_allocator(const stack_allocator&);// = delete;
stack_allocator& operator=(const stack_allocator&);// = delete;
public:
pointer allocate(size_type n, const void* = 0)
{
if (n > N - (ptr_ - buf_) / sizeof(value_type))
throw std::bad_alloc();
pointer r = (T*)ptr_;
ptr_ += n * sizeof(T);
return r;
}
void deallocate(pointer p, size_type n)
{
if ((char*)(p + n) == ptr_)
ptr_ = (char*)p;
}
size_type max_size() const {return N;}
};
template <class T, std::size_t N>
inline
void
swap(stack_allocator<T, N>& x, stack_allocator<T, N>& y) {}
#endif
|