1*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc // The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*0a6a1f1dSLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc
104684ddb6SLionel Sambuc #ifndef STACK_ALLOCATOR_H
114684ddb6SLionel Sambuc #define STACK_ALLOCATOR_H
124684ddb6SLionel Sambuc
134684ddb6SLionel Sambuc #include <cstddef>
144684ddb6SLionel Sambuc #include <new>
154684ddb6SLionel Sambuc
164684ddb6SLionel Sambuc template <class T, std::size_t N>
174684ddb6SLionel Sambuc class stack_allocator
184684ddb6SLionel Sambuc {
194684ddb6SLionel Sambuc char buf_[sizeof(T)*N];
204684ddb6SLionel Sambuc char* ptr_;
214684ddb6SLionel Sambuc public:
224684ddb6SLionel Sambuc typedef T value_type;
234684ddb6SLionel Sambuc typedef value_type* pointer;
244684ddb6SLionel Sambuc typedef const value_type* const_pointer;
254684ddb6SLionel Sambuc typedef value_type& reference;
264684ddb6SLionel Sambuc typedef const value_type& const_reference;
274684ddb6SLionel Sambuc typedef std::size_t size_type;
284684ddb6SLionel Sambuc typedef std::ptrdiff_t difference_type;
294684ddb6SLionel Sambuc
304684ddb6SLionel Sambuc template <class U> struct rebind {typedef stack_allocator<U, N> other;};
314684ddb6SLionel Sambuc
stack_allocator()324684ddb6SLionel Sambuc stack_allocator() : ptr_(buf_) {}
334684ddb6SLionel Sambuc
344684ddb6SLionel Sambuc private:
354684ddb6SLionel Sambuc stack_allocator(const stack_allocator&);// = delete;
364684ddb6SLionel Sambuc stack_allocator& operator=(const stack_allocator&);// = delete;
374684ddb6SLionel Sambuc
384684ddb6SLionel Sambuc public:
394684ddb6SLionel Sambuc pointer allocate(size_type n, const void* = 0)
404684ddb6SLionel Sambuc {
414684ddb6SLionel Sambuc if (n > N - (ptr_ - buf_) / sizeof(value_type)) {
424684ddb6SLionel Sambuc #ifndef _LIBCPP_NO_EXCEPTIONS
434684ddb6SLionel Sambuc throw std::bad_alloc();
444684ddb6SLionel Sambuc #else
454684ddb6SLionel Sambuc std::terminate();
464684ddb6SLionel Sambuc #endif
474684ddb6SLionel Sambuc }
484684ddb6SLionel Sambuc pointer r = (T*)ptr_;
494684ddb6SLionel Sambuc ptr_ += n * sizeof(T);
504684ddb6SLionel Sambuc return r;
514684ddb6SLionel Sambuc }
deallocate(pointer p,size_type n)524684ddb6SLionel Sambuc void deallocate(pointer p, size_type n)
534684ddb6SLionel Sambuc {
544684ddb6SLionel Sambuc if ((char*)(p + n) == ptr_)
554684ddb6SLionel Sambuc ptr_ = (char*)p;
564684ddb6SLionel Sambuc }
574684ddb6SLionel Sambuc
max_size()584684ddb6SLionel Sambuc size_type max_size() const {return N;}
594684ddb6SLionel Sambuc };
604684ddb6SLionel Sambuc
614684ddb6SLionel Sambuc template <class T, std::size_t N>
624684ddb6SLionel Sambuc inline
634684ddb6SLionel Sambuc void
swap(stack_allocator<T,N> & x,stack_allocator<T,N> & y)644684ddb6SLionel Sambuc swap(stack_allocator<T, N>& x, stack_allocator<T, N>& y) {}
654684ddb6SLionel Sambuc
664684ddb6SLionel Sambuc #endif // STACK_ALLOCATOR_H
67