3 * Copyright (c) 1998-2004
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.
15 * VERSION: see <boost/version.hpp>
16 * DESCRIPTION: Misc boost::regbase member funnctions.
20 #define BOOST_REGEX_SOURCE
22 #include <boost/config.hpp>
24 #include <boost/regex.hpp>
25 #include <boost/throw_exception.hpp>
27 #if defined(BOOST_REGEX_HAS_MS_STACK_GUARD) && defined(_MSC_VER) && (_MSC_VER >= 1300)
30 #ifdef BOOST_REGEX_HAS_MS_STACK_GUARD
31 #define WIN32_LEAN_AND_MEAN
40 #if defined(BOOST_REGEX_NON_RECURSIVE) && !defined(BOOST_REGEX_V3)
41 #if BOOST_REGEX_MAX_CACHE_BLOCKS == 0
44 #include <boost/regex/v4/mem_block_cache.hpp>
49 #pragma warning(disable:383)
55 // fix: these are declared out of line here to ensure
56 // that dll builds contain the Virtual table for these
57 // types - this ensures that exceptions can be thrown
58 // from the dll and caught in an exe.
59 regex_error::regex_error(const std::string& s, regex_constants::error_type err, std::ptrdiff_t pos)
60 : std::runtime_error(s)
66 regex_error::regex_error(regex_constants::error_type err)
67 : std::runtime_error(::boost::BOOST_REGEX_DETAIL_NS::get_default_error_string(err))
73 regex_error::~regex_error() throw()
77 void regex_error::raise()const
79 #ifndef BOOST_NO_EXCEPTIONS
80 ::boost::throw_exception(*this);
86 namespace BOOST_REGEX_DETAIL_NS{
88 BOOST_REGEX_DECL void BOOST_REGEX_CALL raise_runtime_error(const std::runtime_error& ex)
90 ::boost::throw_exception(ex);
93 // error checking API:
95 BOOST_REGEX_DECL void BOOST_REGEX_CALL verify_options(boost::regex::flag_type /*ef*/, match_flag_type mf)
97 #ifndef BOOST_REGEX_V3
99 // can't mix match_extra with POSIX matching rules:
101 if((mf & match_extra) && (mf & match_posix))
103 std::logic_error msg("Usage Error: Can't mix regular expression captures with POSIX matching rules");
104 throw_exception(msg);
109 #ifdef BOOST_REGEX_HAS_MS_STACK_GUARD
111 static void execute_eror()
113 // we only get here after a stack overflow,
114 // this has to be a separate proceedure because we
115 // can't mix __try{}__except block with local objects
116 // that have destructors:
117 reset_stack_guard_page();
118 std::runtime_error err("Out of stack space, while attempting to match a regular expression.");
119 raise_runtime_error(err);
122 bool BOOST_REGEX_CALL abstract_protected_call::execute()const
126 }__except(EXCEPTION_STACK_OVERFLOW == GetExceptionCode())
130 // We never really get here at all:
134 BOOST_REGEX_DECL void BOOST_REGEX_CALL reset_stack_guard_page()
136 #if defined(BOOST_REGEX_HAS_MS_STACK_GUARD) && defined(_MSC_VER) && (_MSC_VER >= 1300)
140 // We need to locate the current page being used by the stack,
141 // move to the page below it and then deallocate and protect
142 // that page. Note that ideally we would protect only the lowest
143 // stack page that has been allocated: in practice there
144 // seems to be no easy way to locate this page, in any case as
145 // long as the next page is protected, then Windows will figure
146 // the rest out for us...
150 MEMORY_BASIC_INFORMATION mi;
151 DWORD previous_protection_status;
153 // this is an address in our stack space:
155 LPBYTE page = (LPBYTE)&page;
157 // Get the current memory page in use:
159 VirtualQuery(page, &mi, sizeof(mi));
161 // Go to the page one below this:
163 page = (LPBYTE)(mi.BaseAddress)-si.dwPageSize;
165 // Free and protect everything from the start of the
166 // allocation range, to the end of the page below the
169 if (!VirtualFree(mi.AllocationBase, (LPBYTE)page - (LPBYTE)mi.AllocationBase, MEM_DECOMMIT)
170 || !VirtualProtect(page, si.dwPageSize, PAGE_GUARD | PAGE_READWRITE, &previous_protection_status))
172 throw std::bad_exception();
178 #if defined(BOOST_REGEX_NON_RECURSIVE) && !defined(BOOST_REGEX_V3)
180 #if BOOST_REGEX_MAX_CACHE_BLOCKS == 0
182 BOOST_REGEX_DECL void* BOOST_REGEX_CALL get_mem_block()
184 return ::operator new(BOOST_REGEX_BLOCKSIZE);
187 BOOST_REGEX_DECL void BOOST_REGEX_CALL put_mem_block(void* p)
189 ::operator delete(p);
194 #if defined(BOOST_REGEX_MEM_BLOCK_CACHE_LOCK_FREE)
195 mem_block_cache block_cache = { { {nullptr} } } ;
196 #elif defined(BOOST_HAS_THREADS)
197 mem_block_cache block_cache = { 0, 0, BOOST_STATIC_MUTEX_INIT, };
199 mem_block_cache block_cache = { 0, 0, };
202 BOOST_REGEX_DECL void* BOOST_REGEX_CALL get_mem_block()
204 return block_cache.get();
207 BOOST_REGEX_DECL void BOOST_REGEX_CALL put_mem_block(void* p)
216 } // namespace BOOST_REGEX_DETAIL_NS
222 #if defined(BOOST_RE_USE_VCL) && defined(BOOST_REGEX_DYN_LINK)
224 int WINAPI DllEntryPoint(HINSTANCE , unsigned long , void*)