mem_block_cache.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. };
  65. #else /* lock-based implementation */
  66. struct mem_block_node
  67. {
  68. mem_block_node* next;
  69. };
  70. struct mem_block_cache
  71. {
  72. // this member has to be statically initialsed:
  73. mem_block_node* next;
  74. unsigned cached_blocks;
  75. #ifdef BOOST_HAS_THREADS
  76. boost::static_mutex mut;
  77. #endif
  78. ~mem_block_cache()
  79. {
  80. while(next)
  81. {
  82. mem_block_node* old = next;
  83. next = next->next;
  84. ::operator delete(old);
  85. }
  86. }
  87. void* get()
  88. {
  89. #ifdef BOOST_HAS_THREADS
  90. boost::static_mutex::scoped_lock g(mut);
  91. #endif
  92. if(next)
  93. {
  94. mem_block_node* result = next;
  95. next = next->next;
  96. --cached_blocks;
  97. return result;
  98. }
  99. return ::operator new(BOOST_REGEX_BLOCKSIZE);
  100. }
  101. void put(void* p)
  102. {
  103. #ifdef BOOST_HAS_THREADS
  104. boost::static_mutex::scoped_lock g(mut);
  105. #endif
  106. if(cached_blocks >= BOOST_REGEX_MAX_CACHE_BLOCKS)
  107. {
  108. ::operator delete(p);
  109. }
  110. else
  111. {
  112. mem_block_node* old = static_cast<mem_block_node*>(p);
  113. old->next = next;
  114. next = old;
  115. ++cached_blocks;
  116. }
  117. }
  118. };
  119. #endif
  120. extern mem_block_cache block_cache;
  121. }
  122. } // namespace boost
  123. #ifdef BOOST_HAS_ABI_HEADERS
  124. # include BOOST_ABI_SUFFIX
  125. #endif
  126. #endif