mem_block_cache.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * LOCATION: see http://www.boost.org for most recent version.
  12. * FILE mem_block_cache.hpp
  13. * VERSION see <boost/version.hpp>
  14. * DESCRIPTION: memory block cache used by the non-recursive matcher.
  15. */
  16. #ifndef BOOST_REGEX_V4_MEM_BLOCK_CACHE_HPP
  17. #define BOOST_REGEX_V4_MEM_BLOCK_CACHE_HPP
  18. #include <new>
  19. #ifdef BOOST_HAS_THREADS
  20. #include <boost/regex/pending/static_mutex.hpp>
  21. #endif
  22. #ifdef BOOST_HAS_ABI_HEADERS
  23. # include BOOST_ABI_PREFIX
  24. #endif
  25. #ifndef BOOST_NO_CXX11_HDR_ATOMIC
  26. #include <atomic>
  27. #if ATOMIC_POINTER_LOCK_FREE == 2
  28. #define BOOST_REGEX_MEM_BLOCK_CACHE_LOCK_FREE
  29. #define BOOST_REGEX_ATOMIC_POINTER std::atomic
  30. #endif
  31. #endif
  32. namespace boost{
  33. namespace BOOST_REGEX_DETAIL_NS{
  34. #ifdef BOOST_REGEX_MEM_BLOCK_CACHE_LOCK_FREE /* lock free implementation */
  35. struct mem_block_cache
  36. {
  37. std::atomic<void*> cache[BOOST_REGEX_MAX_CACHE_BLOCKS];
  38. ~mem_block_cache()
  39. {
  40. for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
  41. if (cache[i].load()) ::operator delete(cache[i].load());
  42. }
  43. }
  44. void* get()
  45. {
  46. for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
  47. void* p = cache[i].load();
  48. if (p != NULL) {
  49. if (cache[i].compare_exchange_strong(p, NULL)) return p;
  50. }
  51. }
  52. return ::operator new(BOOST_REGEX_BLOCKSIZE);
  53. }
  54. void put(void* ptr)
  55. {
  56. for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
  57. void* p = cache[i].load();
  58. if (p == NULL) {
  59. if (cache[i].compare_exchange_strong(p, ptr)) return;
  60. }
  61. }
  62. ::operator delete(ptr);
  63. }
  64. static mem_block_cache& instance()
  65. {
  66. static mem_block_cache block_cache = { { {nullptr} } };
  67. return block_cache;
  68. }
  69. };
  70. #else /* lock-based implementation */
  71. struct mem_block_node
  72. {
  73. mem_block_node* next;
  74. };
  75. struct mem_block_cache
  76. {
  77. // this member has to be statically initialsed:
  78. mem_block_node* next;
  79. unsigned cached_blocks;
  80. #ifdef BOOST_HAS_THREADS
  81. boost::static_mutex mut;
  82. #endif
  83. ~mem_block_cache()
  84. {
  85. while(next)
  86. {
  87. mem_block_node* old = next;
  88. next = next->next;
  89. ::operator delete(old);
  90. }
  91. }
  92. void* get()
  93. {
  94. #ifdef BOOST_HAS_THREADS
  95. boost::static_mutex::scoped_lock g(mut);
  96. #endif
  97. if(next)
  98. {
  99. mem_block_node* result = next;
  100. next = next->next;
  101. --cached_blocks;
  102. return result;
  103. }
  104. return ::operator new(BOOST_REGEX_BLOCKSIZE);
  105. }
  106. void put(void* p)
  107. {
  108. #ifdef BOOST_HAS_THREADS
  109. boost::static_mutex::scoped_lock g(mut);
  110. #endif
  111. if(cached_blocks >= BOOST_REGEX_MAX_CACHE_BLOCKS)
  112. {
  113. ::operator delete(p);
  114. }
  115. else
  116. {
  117. mem_block_node* old = static_cast<mem_block_node*>(p);
  118. old->next = next;
  119. next = old;
  120. ++cached_blocks;
  121. }
  122. }
  123. static mem_block_cache& instance()
  124. {
  125. #ifdef BOOST_HAS_THREADS
  126. static mem_block_cache block_cache = { 0, 0, BOOST_STATIC_MUTEX_INIT, };
  127. #else
  128. static mem_block_cache block_cache = { 0, 0, };
  129. #endif
  130. return block_cache;
  131. }
  132. };
  133. #endif
  134. #if BOOST_REGEX_MAX_CACHE_BLOCKS == 0
  135. inline void* BOOST_REGEX_CALL get_mem_block()
  136. {
  137. return ::operator new(BOOST_REGEX_BLOCKSIZE);
  138. }
  139. inline void BOOST_REGEX_CALL put_mem_block(void* p)
  140. {
  141. ::operator delete(p);
  142. }
  143. #else
  144. inline void* BOOST_REGEX_CALL get_mem_block()
  145. {
  146. return mem_block_cache::instance().get();
  147. }
  148. inline void BOOST_REGEX_CALL put_mem_block(void* p)
  149. {
  150. mem_block_cache::instance().put(p);
  151. }
  152. #endif
  153. }
  154. } // namespace boost
  155. #ifdef BOOST_HAS_ABI_HEADERS
  156. # include BOOST_ABI_SUFFIX
  157. #endif
  158. #endif