1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef STACK_ALLOCATOR_H
11 #define STACK_ALLOCATOR_H
16 template <class T, std::size_t N>
19 char buf_[sizeof(T)*N];
23 typedef value_type* pointer;
24 typedef const value_type* const_pointer;
25 typedef value_type& reference;
26 typedef const value_type& const_reference;
27 typedef std::size_t size_type;
28 typedef std::ptrdiff_t difference_type;
30 template <class U> struct rebind {typedef stack_allocator<U, N> other;};
32 stack_allocator() : ptr_(buf_) {}
35 stack_allocator(const stack_allocator&);// = delete;
36 stack_allocator& operator=(const stack_allocator&);// = delete;
39 pointer allocate(size_type n, const void* = 0)
41 if (n > N - (ptr_ - buf_) / sizeof(value_type)) {
42 #ifndef _LIBCPP_NO_EXCEPTIONS
43 throw std::bad_alloc();
49 ptr_ += n * sizeof(T);
52 void deallocate(pointer p, size_type n)
54 if ((char*)(p + n) == ptr_)
58 size_type max_size() const {return N;}
61 template <class T, std::size_t N>
64 swap(stack_allocator<T, N>& x, stack_allocator<T, N>& y) {}
66 #endif // STACK_ALLOCATOR_H