3 * Copyright (c) 1998-2002
6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE regex_stack.hpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Implements customised internal regex stacks.
17 * Note this is an internal header file included
18 * by regex.hpp, do not include on its own.
21 #ifndef BOOST_REGEX_STACK_HPP
22 #define BOOST_REGEX_STACK_HPP
24 #ifndef BOOST_REGEX_CONFIG_HPP
25 #include <boost/regex/config.hpp>
27 #ifndef BOOST_REGEX_RAW_BUFFER_HPP
28 #include <boost/regex/v4/regex_raw_buffer.hpp>
34 #ifdef BOOST_HAS_ABI_HEADERS
35 # include BOOST_ABI_PREFIX
40 // simplified stack optimised for push/peek/pop
41 // operations, we could use std::stack<std::vector<T>> instead...
43 template <class T, class Allocator = BOOST_DEFAULT_ALLOCATOR(T) >
47 typedef typename boost::detail::rebind_allocator<unsigned char, Allocator>::type allocator_type;
49 typedef typename boost::detail::rebind_allocator<T, Allocator>::type T_alloc_type;
50 typedef typename T_alloc_type::size_type size_type;
55 T* start; // first item
57 T* last; // end of storage
61 // empty base member optimisation:
62 struct data : public allocator_type
64 padding buf[(sizeof(T) * 16 + sizeof(padding) - 1) / sizeof(padding)];
65 data(const Allocator& a) : allocator_type(a){}
69 mutable node* m_stack;
74 void BOOST_REGEX_CALL pop_aux()const;
75 void BOOST_REGEX_CALL push_aux();
78 jstack(size_type n = 64, const Allocator& a = Allocator());
82 node* BOOST_REGEX_CALL get_node()
84 node* new_stack = reinterpret_cast<node*>(alloc_inst.allocate(sizeof(node) + sizeof(T) * block_size));
85 BOOST_REGEX_NOEH_ASSERT(new_stack)
86 new_stack->last = reinterpret_cast<T*>(new_stack+1);
87 new_stack->start = new_stack->end = new_stack->last + block_size;
92 bool BOOST_REGEX_CALL empty()
94 return (m_stack->start == m_stack->end) && (m_stack->next == 0);
97 bool BOOST_REGEX_CALL good()
99 return (m_stack->start != m_stack->end) || (m_stack->next != 0);
102 T& BOOST_REGEX_CALL peek()
104 if(m_stack->start == m_stack->end)
106 return *m_stack->end;
109 const T& BOOST_REGEX_CALL peek()const
111 if(m_stack->start == m_stack->end)
113 return *m_stack->end;
116 void BOOST_REGEX_CALL pop()
118 if(m_stack->start == m_stack->end)
120 ::boost::re_detail::pointer_destroy(m_stack->end);
124 void BOOST_REGEX_CALL pop(T& t)
126 if(m_stack->start == m_stack->end)
129 ::boost::re_detail::pointer_destroy(m_stack->end);
133 void BOOST_REGEX_CALL push(const T& t)
135 if(m_stack->end == m_stack->last)
138 pointer_construct(m_stack->end, t);
143 template <class T, class Allocator>
144 jstack<T, Allocator>::jstack(size_type n, const Allocator& a)
150 base.last = reinterpret_cast<T*>(alloc_inst.buf);
151 base.end = base.start = base.last + 16;
155 template <class T, class Allocator>
156 void BOOST_REGEX_CALL jstack<T, Allocator>::push_aux()
158 // make sure we have spare space on TOS:
159 register node* new_node;
163 unused = new_node->next;
164 new_node->next = m_stack;
169 new_node = get_node();
170 new_node->next = m_stack;
175 template <class T, class Allocator>
176 void BOOST_REGEX_CALL jstack<T, Allocator>::pop_aux()const
178 // make sure that we have a valid item
180 BOOST_ASSERT(m_stack->next);
181 register node* p = m_stack;
187 template <class T, class Allocator>
188 jstack<T, Allocator>::~jstack()
196 unused = unused->next;
197 alloc_inst.deallocate(reinterpret_cast<unsigned char*>(condemned), sizeof(node) + sizeof(T) * block_size);
199 while(m_stack != &base)
202 m_stack = m_stack->next;
203 alloc_inst.deallocate(reinterpret_cast<unsigned char*>(condemned), sizeof(node) + sizeof(T) * block_size);
207 #ifdef BOOST_HAS_ABI_HEADERS
208 # include BOOST_ABI_SUFFIX
211 } // namespace re_detail