quick_allocator.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // detail/quick_allocator.hpp
  9. //
  10. // Copyright (c) 2003 David Abrahams
  11. // Copyright (c) 2003 Peter Dimov
  12. //
  13. // Distributed under the Boost Software License, Version 1.0. (See
  14. // accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. #include <boost/config.hpp>
  18. #include <boost/smart_ptr/detail/lightweight_mutex.hpp>
  19. #include <boost/type_traits/type_with_alignment.hpp>
  20. #include <boost/type_traits/alignment_of.hpp>
  21. #include <new> // ::operator new, ::operator delete
  22. #include <cstddef> // std::size_t
  23. namespace boost
  24. {
  25. namespace detail
  26. {
  27. template<unsigned size, unsigned align_> union freeblock
  28. {
  29. typedef typename boost::type_with_alignment<align_>::type aligner_type;
  30. aligner_type aligner;
  31. char bytes[size];
  32. freeblock * next;
  33. };
  34. template<unsigned size, unsigned align_> struct allocator_impl
  35. {
  36. typedef freeblock<size, align_> block;
  37. // It may seem odd to use such small pages.
  38. //
  39. // However, on a typical Windows implementation that uses
  40. // the OS allocator, "normal size" pages interact with the
  41. // "ordinary" operator new, slowing it down dramatically.
  42. //
  43. // 512 byte pages are handled by the small object allocator,
  44. // and don't interfere with ::new.
  45. //
  46. // The other alternative is to use much bigger pages (1M.)
  47. //
  48. // It is surprisingly easy to hit pathological behavior by
  49. // varying the page size. g++ 2.96 on Red Hat Linux 7.2,
  50. // for example, passionately dislikes 496. 512 seems OK.
  51. #if defined(BOOST_QA_PAGE_SIZE)
  52. enum { items_per_page = BOOST_QA_PAGE_SIZE / size };
  53. #else
  54. enum { items_per_page = 512 / size }; // 1048560 / size
  55. #endif
  56. #ifdef BOOST_HAS_THREADS
  57. static lightweight_mutex & mutex()
  58. {
  59. static lightweight_mutex m;
  60. return m;
  61. }
  62. static lightweight_mutex * mutex_init;
  63. #endif
  64. static block * free;
  65. static block * page;
  66. static unsigned last;
  67. static inline void * alloc()
  68. {
  69. #ifdef BOOST_HAS_THREADS
  70. lightweight_mutex::scoped_lock lock( mutex() );
  71. #endif
  72. if(block * x = free)
  73. {
  74. free = x->next;
  75. return x;
  76. }
  77. else
  78. {
  79. if(last == items_per_page)
  80. {
  81. // "Listen to me carefully: there is no memory leak"
  82. // -- Scott Meyers, Eff C++ 2nd Ed Item 10
  83. page = ::new block[items_per_page];
  84. last = 0;
  85. }
  86. return &page[last++];
  87. }
  88. }
  89. static inline void * alloc(std::size_t n)
  90. {
  91. if(n != size) // class-specific new called for a derived object
  92. {
  93. return ::operator new(n);
  94. }
  95. else
  96. {
  97. #ifdef BOOST_HAS_THREADS
  98. lightweight_mutex::scoped_lock lock( mutex() );
  99. #endif
  100. if(block * x = free)
  101. {
  102. free = x->next;
  103. return x;
  104. }
  105. else
  106. {
  107. if(last == items_per_page)
  108. {
  109. page = ::new block[items_per_page];
  110. last = 0;
  111. }
  112. return &page[last++];
  113. }
  114. }
  115. }
  116. static inline void dealloc(void * pv)
  117. {
  118. if(pv != 0) // 18.4.1.1/13
  119. {
  120. #ifdef BOOST_HAS_THREADS
  121. lightweight_mutex::scoped_lock lock( mutex() );
  122. #endif
  123. block * pb = static_cast<block *>(pv);
  124. pb->next = free;
  125. free = pb;
  126. }
  127. }
  128. static inline void dealloc(void * pv, std::size_t n)
  129. {
  130. if(n != size) // class-specific delete called for a derived object
  131. {
  132. ::operator delete(pv);
  133. }
  134. else if(pv != 0) // 18.4.1.1/13
  135. {
  136. #ifdef BOOST_HAS_THREADS
  137. lightweight_mutex::scoped_lock lock( mutex() );
  138. #endif
  139. block * pb = static_cast<block *>(pv);
  140. pb->next = free;
  141. free = pb;
  142. }
  143. }
  144. };
  145. #ifdef BOOST_HAS_THREADS
  146. template<unsigned size, unsigned align_>
  147. lightweight_mutex * allocator_impl<size, align_>::mutex_init = &allocator_impl<size, align_>::mutex();
  148. #endif
  149. template<unsigned size, unsigned align_>
  150. freeblock<size, align_> * allocator_impl<size, align_>::free = 0;
  151. template<unsigned size, unsigned align_>
  152. freeblock<size, align_> * allocator_impl<size, align_>::page = 0;
  153. template<unsigned size, unsigned align_>
  154. unsigned allocator_impl<size, align_>::last = allocator_impl<size, align_>::items_per_page;
  155. template<class T>
  156. struct quick_allocator: public allocator_impl< sizeof(T), boost::alignment_of<T>::value >
  157. {
  158. };
  159. } // namespace detail
  160. } // namespace boost
  161. #endif // #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED