1 #ifndef STACK_ALLOCATOR_H
2 #define STACK_ALLOCATOR_H
7 template <class T, std::size_t N>
10 char buf_[sizeof(T)*N];
14 typedef value_type* pointer;
15 typedef const value_type* const_pointer;
16 typedef value_type& reference;
17 typedef const value_type& const_reference;
18 typedef std::size_t size_type;
19 typedef std::ptrdiff_t difference_type;
21 template <class U> struct rebind {typedef stack_allocator<U, N> other;};
23 stack_allocator() : ptr_(buf_) {}
26 stack_allocator(const stack_allocator&);// = delete;
27 stack_allocator& operator=(const stack_allocator&);// = delete;
30 pointer allocate(size_type n, const void* = 0)
32 if (n > N - (ptr_ - buf_) / sizeof(value_type)) {
33 #ifndef _LIBCPP_NO_EXCEPTIONS
34 throw std::bad_alloc();
40 ptr_ += n * sizeof(T);
43 void deallocate(pointer p, size_type n)
45 if ((char*)(p + n) == ptr_)
49 size_type max_size() const {return N;}
52 template <class T, std::size_t N>
55 swap(stack_allocator<T, N>& x, stack_allocator<T, N>& y) {}
57 #endif // STACK_ALLOCATOR_H