.\" .de Id .. .de Sp .if n .sp .if t .sp 0.4 .. .TH stack_allocator 7rheolef "rheolef-7.0" "rheolef-7.0" "rheolef-7.0" .\" label: /*Class:man .SH NAME stack_allocator - stack-based allocator .SH DESCRIPTION Stack-based allocator, conform to the STL specification of allocators. Designed to use stack-based data passed as a parameter to the allocator constructor. Does not "free" the memory. Assumes that if the allocator is copied, stack memory is cleared and new allocations begin at the bottom of the stack again. .PP Also works with any memory buffer, including heap memory. If the caller passes in heap memory, the caller is responsible for freeing the memory. .PP This allocator handles a limited area of memory: if this limit is reached, a "std::bad_alloc" exception is emmited. For a non-limited memory handler in the same spirit, see "heap_allocator"(9). .SH EXAMPLE .\" begin_example .Sp .nf const size_t stack_size = 1024; vector stack (stack_size); stack_allocator stack_alloc (stack.begin().operator->(), stack.size()); typedef map , stack_allocator > > map_type; map_type a (less(), stack_alloc); a.insert (make_pair (0, 3.14)); a.insert (make_pair (1, 1.17)); for (map_type::iterator iter = a.begin(), last = a.end(); iter != last; iter++) { cout << (*iter).first << " " << (*iter).second << endl; } .Sp .fi .\" end_example .\" skip start:SEE ALSO: .\" skip start:AUTHORS: .\" skip start:DATE: .\" END .SH IMPLEMENTATION .\" begin_example .Sp .nf template class stack_allocator { protected: struct handler_type; // forward declaration: public: // typedefs: typedef size_t size_type; typedef std::ptrdiff_t difference_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; // constructors: stack_allocator() throw() : handler (new handler_type) { } stack_allocator (unsigned char* stack, size_t stack_size) throw() : handler (new handler_type (stack, stack_size)) { trace_macro ("stack_allocator cstor"); } stack_allocator (const stack_allocator& sa) throw() : handler (sa.handler) { ++handler->reference_count; } template stack_allocator (const stack_allocator& sa) throw() : handler ((typename stack_allocator::handler_type*)(sa.handler)) { ++handler->reference_count; } ~stack_allocator() throw() { trace_macro ("stack_allocator dstor"); check_macro (handler != NULL, "unexpected null mem_info"); if (--handler->reference_count == 0) delete handler; } // Rebind to allocators of other types template struct rebind { typedef stack_allocator other; }; // assignment: stack_allocator& operator= (const stack_allocator& sa) { handler = sa.handler; ++handler->reference_count; return *this; } // utility functions: pointer address (reference r) const { return &r; } const_pointer address (const_reference c) const { return &c; } size_type max_size() const { return std::numeric_limits::max() / sizeof(T); } // in-place construction/destruction void construct (pointer p, const_reference c) { // placement new operator: new( reinterpret_cast(p) ) T(c); } // C++ 2011: default construct a value of type T at the location referenced by p void construct (pointer p) { new ( reinterpret_cast(p) ) T(); } void destroy (pointer p) { // call destructor directly: (p)->~T(); } // allocate raw memory pointer allocate (size_type n, const void* = NULL) { trace_macro ("allocate "<stack != NULL, "unexpected null stack"); void* p = handler->stack + handler->allocated_size; handler->allocated_size += n*sizeof(T); if (handler->allocated_size + 1 > handler->max_size) { trace_macro ("stack is full: throwing..."); throw std::bad_alloc(); } return pointer (p); } void deallocate (pointer p, size_type n) { trace_macro ("deallocate "< GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.