1  /*
2  * Copyright (c) 2002
3  * John Maddock
4  *
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)
8  *
9  */
10 
11  /*
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.
16   */
17 
18 #ifndef BOOST_REGEX_V5_MEM_BLOCK_CACHE_HPP
19 #define BOOST_REGEX_V5_MEM_BLOCK_CACHE_HPP
20 
21 #include <new>
22 #ifdef BOOST_HAS_THREADS
23 #include <mutex>
24 #endif
25 
26 #ifndef BOOST_NO_CXX11_HDR_ATOMIC
27   #include <atomic>
28   #if ATOMIC_POINTER_LOCK_FREE == 2
29     #define BOOST_REGEX_MEM_BLOCK_CACHE_LOCK_FREE
30     #define BOOST_REGEX_ATOMIC_POINTER std::atomic
31   #endif
32 #endif
33 
34 namespace boost{
35 namespace BOOST_REGEX_DETAIL_NS{
36 
37 #ifdef BOOST_REGEX_MEM_BLOCK_CACHE_LOCK_FREE /* lock free implementation */
38 struct mem_block_cache
39 {
40   std::atomic<void*> cache[BOOST_REGEX_MAX_CACHE_BLOCKS];
41 
~mem_block_cacheboost::BOOST_REGEX_DETAIL_NS::mem_block_cache42    ~mem_block_cache()
43    {
44      for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
45        if (cache[i].load()) ::operator delete(cache[i].load());
46      }
47    }
getboost::BOOST_REGEX_DETAIL_NS::mem_block_cache48    void* get()
49    {
50      for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
51        void* p = cache[i].load();
52        if (p != NULL) {
53          if (cache[i].compare_exchange_strong(p, NULL)) return p;
54        }
55      }
56      return ::operator new(BOOST_REGEX_BLOCKSIZE);
57    }
putboost::BOOST_REGEX_DETAIL_NS::mem_block_cache58    void put(void* ptr)
59    {
60      for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
61        void* p = cache[i].load();
62        if (p == NULL) {
63          if (cache[i].compare_exchange_strong(p, ptr)) return;
64        }
65      }
66      ::operator delete(ptr);
67    }
68 
instanceboost::BOOST_REGEX_DETAIL_NS::mem_block_cache69    static mem_block_cache& instance()
70    {
71       static mem_block_cache block_cache = { { {nullptr} } };
72       return block_cache;
73    }
74 };
75 
76 
77 #else /* lock-based implementation */
78 
79 
80 struct mem_block_node
81 {
82    mem_block_node* next;
83 };
84 
85 struct mem_block_cache
86 {
87    // this member has to be statically initialsed:
88    mem_block_node* next { nullptr };
89    unsigned cached_blocks { 0 };
90 #ifdef BOOST_HAS_THREADS
91    std::mutex mut;
92 #endif
93 
94    ~mem_block_cache()
95    {
96       while(next)
97       {
98          mem_block_node* old = next;
99          next = next->next;
100          ::operator delete(old);
101       }
102    }
103    void* get()
104    {
105 #ifdef BOOST_HAS_THREADS
106       std::lock_guard<std::mutex> g(mut);
107 #endif
108      if(next)
109       {
110          mem_block_node* result = next;
111          next = next->next;
112          --cached_blocks;
113          return result;
114       }
115       return ::operator new(BOOST_REGEX_BLOCKSIZE);
116    }
117    void put(void* p)
118    {
119 #ifdef BOOST_HAS_THREADS
120       std::lock_guard<std::mutex> g(mut);
121 #endif
122       if(cached_blocks >= BOOST_REGEX_MAX_CACHE_BLOCKS)
123       {
124          ::operator delete(p);
125       }
126       else
127       {
128          mem_block_node* old = static_cast<mem_block_node*>(p);
129          old->next = next;
130          next = old;
131          ++cached_blocks;
132       }
133    }
134    static mem_block_cache& instance()
135    {
136       static mem_block_cache block_cache;
137       return block_cache;
138    }
139 };
140 #endif
141 
142 #if BOOST_REGEX_MAX_CACHE_BLOCKS == 0
143 
get_mem_block()144 inline void*  get_mem_block()
145 {
146    return ::operator new(BOOST_REGEX_BLOCKSIZE);
147 }
148 
put_mem_block(void * p)149 inline void  put_mem_block(void* p)
150 {
151    ::operator delete(p);
152 }
153 
154 #else
155 
get_mem_block()156 inline void*  get_mem_block()
157 {
158    return mem_block_cache::instance().get();
159 }
160 
put_mem_block(void * p)161 inline void  put_mem_block(void* p)
162 {
163    mem_block_cache::instance().put(p);
164 }
165 
166 #endif
167 }
168 } // namespace boost
169 
170 #endif
171 
172