RegionAllocator.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_REGION_ALLOCATOR_HPP
  26. #define CAT_REGION_ALLOCATOR_HPP
  27. #include <cat/Singleton.hpp>
  28. #include <memory>
  29. #include <xstring>
  30. #include <sstream>
  31. namespace cat {
  32. // A region-based allocator that is lock-free, supporting
  33. // a range of allocation block sizes that are pre-allocated
  34. // in a pre-determined way, tuned to the application.
  35. class RegionAllocator : public Singleton<RegionAllocator>
  36. {
  37. CAT_SINGLETON(RegionAllocator);
  38. protected:
  39. struct RegionInfoHead
  40. {
  41. u32 next_bitmap_entry;
  42. };
  43. struct RegionInfo : public RegionInfoHead
  44. {
  45. volatile u32 bitmap[1];
  46. };
  47. // 64, 128, 256, 512, 1024, 2048 only
  48. static const u32 REGION_COUNT = 6;
  49. static const u32 BLOCK_SIZE[REGION_COUNT];
  50. u32 bytes_overall;
  51. u32 blocks_per_region[REGION_COUNT];
  52. u32 bitmap_dwords[REGION_COUNT];
  53. u8 *regions[REGION_COUNT];
  54. RegionInfo *region_info[REGION_COUNT];
  55. //u32 errors;
  56. public:
  57. bool Valid();
  58. void Shutdown();
  59. public:
  60. void *Acquire(u32 bytes);
  61. void *Resize(void *ptr, u32 bytes);
  62. void Release(void *ptr);
  63. template<class T>
  64. CAT_INLINE void Delete(T *ptr)
  65. {
  66. ptr->~T();
  67. Release(ptr);
  68. }
  69. // Acquires a buffer from the allocator that is the size of the type.
  70. // It further allocates a number of extra bytes beyond the end of the buffer.
  71. // Release the buffer with:
  72. // RegionAllocator::ii->Release(ptr);
  73. template<class T> T *AcquireBuffer(u32 extra_bytes = 0)
  74. {
  75. return reinterpret_cast<T*>( Acquire(sizeof(T) + extra_bytes) );
  76. }
  77. };
  78. // Use STLRegionAllocator in place of the standard STL allocator
  79. // to make use of the RegionAllocator in STL types. Some common
  80. // usage typedefs follow this class definition below.
  81. template<typename T>
  82. class STLRegionAllocator
  83. {
  84. public:
  85. typedef size_t size_type;
  86. typedef size_t difference_type;
  87. typedef T *pointer;
  88. typedef const T *const_pointer;
  89. typedef T &reference;
  90. typedef const T &const_reference;
  91. typedef T value_type;
  92. template<typename S>
  93. struct rebind
  94. {
  95. typedef STLRegionAllocator<S> other;
  96. };
  97. pointer address(reference X) const
  98. {
  99. return &X;
  100. }
  101. const_pointer address(const_reference X) const
  102. {
  103. return &X;
  104. }
  105. STLRegionAllocator() throw ()
  106. {
  107. }
  108. template<typename S>
  109. STLRegionAllocator(const STLRegionAllocator<S> &cp) throw ()
  110. {
  111. }
  112. template<typename S>
  113. STLRegionAllocator<T> &operator=(const STLRegionAllocator<S> &cp) throw ()
  114. {
  115. return *this;
  116. }
  117. pointer allocate(size_type Count, const void *Hint = 0)
  118. {
  119. return (pointer)RegionAllocator::ii->Acquire((u32)Count * sizeof(T));
  120. }
  121. void deallocate(pointer Ptr, size_type Count)
  122. {
  123. RegionAllocator::ii->Release(Ptr);
  124. }
  125. void construct(pointer Ptr, const T &Val)
  126. {
  127. std::_Construct(Ptr, Val);
  128. }
  129. void destroy(pointer Ptr)
  130. {
  131. std::_Destroy(Ptr);
  132. }
  133. size_type max_size() const
  134. {
  135. return 0x00FFFFFF;
  136. }
  137. template<typename S>
  138. bool operator==(STLRegionAllocator <S> const &) const throw()
  139. {
  140. return true;
  141. }
  142. template<typename S>
  143. bool operator!=(STLRegionAllocator <S> const &) const throw()
  144. {
  145. return false;
  146. }
  147. };
  148. // Common usage typedefs for using RegionAllocator as the STL allocator
  149. typedef std::basic_ostringstream<char, std::char_traits<char>, STLRegionAllocator<char> > region_ostringstream;
  150. typedef std::basic_string<char, std::char_traits<char>, STLRegionAllocator<char> > region_string;
  151. } // namespace cat
  152. // Provide placement new constructor and delete pair to allow for
  153. // an easy syntax to create objects from the RegionAllocator:
  154. // T *a = new (RegionAllocator::ii) T();
  155. // The object can be freed with:
  156. // RegionAllocator::ii->Delete(a);
  157. // Which insures that the destructor is called before freeing memory
  158. inline void *operator new(size_t bytes, cat::RegionAllocator *allocator)
  159. {
  160. return allocator->Acquire((cat::u32)bytes);
  161. }
  162. inline void operator delete(void *ptr, cat::RegionAllocator *allocator)
  163. {
  164. allocator->Release(ptr);
  165. }
  166. #endif // CAT_REGION_ALLOCATOR_HPP
粤ICP备19079148号