mem_block_cache.hpp 3.6 KB

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