5 * Use, modification and distribution are subject to the
6 * Boost Software License, Version 1.0. (See accompanying file
7 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
12 * LOCATION: see http://www.boost.org for most recent version.
13 * FILE mem_block_cache.hpp
14 * VERSION see <boost/version.hpp>
15 * DESCRIPTION: memory block cache used by the non-recursive matcher.
18 #ifndef BOOST_REGEX_V4_MEM_BLOCK_CACHE_HPP
19 #define BOOST_REGEX_V4_MEM_BLOCK_CACHE_HPP
22 #ifdef BOOST_HAS_THREADS
23 #include <boost/regex/pending/static_mutex.hpp>
26 #ifdef BOOST_HAS_ABI_HEADERS
27 # include BOOST_ABI_PREFIX
30 #ifndef BOOST_NO_CXX11_HDR_ATOMIC
32 #if ATOMIC_POINTER_LOCK_FREE == 2
33 #define BOOST_REGEX_MEM_BLOCK_CACHE_LOCK_FREE
34 #define BOOST_REGEX_ATOMIC_POINTER std::atomic
39 namespace BOOST_REGEX_DETAIL_NS{
41 #ifdef BOOST_REGEX_MEM_BLOCK_CACHE_LOCK_FREE /* lock free implementation */
42 struct mem_block_cache
44 std::atomic<void*> cache[BOOST_REGEX_MAX_CACHE_BLOCKS];
48 for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
49 if (cache[i].load()) ::operator delete(cache[i].load());
54 for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
55 void* p = cache[i].load();
57 if (cache[i].compare_exchange_strong(p, NULL)) return p;
60 return ::operator new(BOOST_REGEX_BLOCKSIZE);
64 for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
65 void* p = cache[i].load();
67 if (cache[i].compare_exchange_strong(p, ptr)) return;
70 ::operator delete(ptr);
75 #else /* lock-based implementation */
83 struct mem_block_cache
85 // this member has to be statically initialsed:
87 unsigned cached_blocks;
88 #ifdef BOOST_HAS_THREADS
89 boost::static_mutex mut;
96 mem_block_node* old = next;
98 ::operator delete(old);
103 #ifdef BOOST_HAS_THREADS
104 boost::static_mutex::scoped_lock g(mut);
108 mem_block_node* result = next;
113 return ::operator new(BOOST_REGEX_BLOCKSIZE);
117 #ifdef BOOST_HAS_THREADS
118 boost::static_mutex::scoped_lock g(mut);
120 if(cached_blocks >= BOOST_REGEX_MAX_CACHE_BLOCKS)
122 ::operator delete(p);
126 mem_block_node* old = static_cast<mem_block_node*>(p);
135 extern mem_block_cache block_cache;
140 #ifdef BOOST_HAS_ABI_HEADERS
141 # include BOOST_ABI_SUFFIX