mem_block_cache.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. namespace boost{
  26. namespace re_detail{
  27. struct mem_block_node
  28. {
  29. mem_block_node* next;
  30. };
  31. struct mem_block_cache
  32. {
  33. // this member has to be statically initialsed:
  34. mem_block_node* next;
  35. unsigned cached_blocks;
  36. #ifdef BOOST_HAS_THREADS
  37. boost::static_mutex mut;
  38. #endif
  39. ~mem_block_cache()
  40. {
  41. while(next)
  42. {
  43. mem_block_node* old = next;
  44. next = next->next;
  45. ::operator delete(old);
  46. }
  47. }
  48. void* get()
  49. {
  50. #ifdef BOOST_HAS_THREADS
  51. boost::static_mutex::scoped_lock g(mut);
  52. #endif
  53. if(next)
  54. {
  55. mem_block_node* result = next;
  56. next = next->next;
  57. --cached_blocks;
  58. return result;
  59. }
  60. return ::operator new(BOOST_REGEX_BLOCKSIZE);
  61. }
  62. void put(void* p)
  63. {
  64. #ifdef BOOST_HAS_THREADS
  65. boost::static_mutex::scoped_lock g(mut);
  66. #endif
  67. if(cached_blocks >= BOOST_REGEX_MAX_CACHE_BLOCKS)
  68. {
  69. ::operator delete(p);
  70. }
  71. else
  72. {
  73. mem_block_node* old = static_cast<mem_block_node*>(p);
  74. old->next = next;
  75. next = old;
  76. ++cached_blocks;
  77. }
  78. }
  79. };
  80. extern mem_block_cache block_cache;
  81. }
  82. } // namespace boost
  83. #ifdef BOOST_HAS_ABI_HEADERS
  84. # include BOOST_ABI_SUFFIX
  85. #endif
  86. #endif