mem_block_cache.hpp 3.5 KB

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