mem_block_cache.hpp 3.6 KB

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