allocator.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_ALLOCATOR_HPP
  11. #define BOOST_CONTAINER_ALLOCATOR_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. #include <boost/container/container_fwd.hpp>
  21. #include <boost/container/detail/version_type.hpp>
  22. #include <boost/container/throw_exception.hpp>
  23. #include <boost/container/detail/dlmalloc.hpp>
  24. #include <boost/container/detail/multiallocation_chain.hpp>
  25. #include <boost/static_assert.hpp>
  26. #include <cstddef>
  27. #include <cassert>
  28. //!\file
  29. namespace boost {
  30. namespace container {
  31. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  32. template<unsigned Version, unsigned int AllocationDisableMask>
  33. class allocator<void, Version, AllocationDisableMask>
  34. {
  35. typedef allocator<void, Version, AllocationDisableMask> self_t;
  36. public:
  37. typedef void value_type;
  38. typedef void * pointer;
  39. typedef const void* const_pointer;
  40. typedef int & reference;
  41. typedef const int & const_reference;
  42. typedef std::size_t size_type;
  43. typedef std::ptrdiff_t difference_type;
  44. typedef boost::container::dtl::
  45. version_type<self_t, Version> version;
  46. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  47. typedef boost::container::dtl::
  48. basic_multiallocation_chain<void*> multiallocation_chain;
  49. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  50. //!Obtains an allocator that allocates
  51. //!objects of type T2
  52. template<class T2>
  53. struct rebind
  54. {
  55. typedef allocator< T2
  56. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  57. , Version, AllocationDisableMask
  58. #endif
  59. > other;
  60. };
  61. //!Default constructor
  62. //!Never throws
  63. allocator()
  64. {}
  65. //!Constructor from other allocator.
  66. //!Never throws
  67. allocator(const allocator &)
  68. {}
  69. //!Constructor from related allocator.
  70. //!Never throws
  71. template<class T2>
  72. allocator(const allocator<T2, Version, AllocationDisableMask> &)
  73. {}
  74. };
  75. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  76. //! This class is an extended STL-compatible that offers advanced allocation mechanism
  77. //!(in-place expansion, shrinking, burst-allocation...)
  78. //!
  79. //! This allocator is a wrapper around a modified DLmalloc.
  80. //! If Version is 1, the allocator is a STL conforming allocator. If Version is 2,
  81. //! the allocator offers advanced expand in place and burst allocation capabilities.
  82. //!
  83. //! AllocationDisableMask works only if Version is 2 and it can be an inclusive OR
  84. //! of allocation types the user wants to disable.
  85. template< class T
  86. , unsigned Version BOOST_CONTAINER_DOCONLY(=2)
  87. , unsigned int AllocationDisableMask BOOST_CONTAINER_DOCONLY(=0)>
  88. class allocator
  89. {
  90. typedef unsigned int allocation_type;
  91. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  92. private:
  93. //Self type
  94. typedef allocator<T, Version, AllocationDisableMask> self_t;
  95. //Not assignable from related allocator
  96. template<class T2, unsigned int Version2, unsigned int AllocationDisableMask2>
  97. allocator& operator=(const allocator<T2, Version2, AllocationDisableMask2>&);
  98. static const unsigned int ForbiddenMask =
  99. BOOST_CONTAINER_ALLOCATE_NEW | BOOST_CONTAINER_EXPAND_BWD | BOOST_CONTAINER_EXPAND_FWD ;
  100. //The mask can't disable all the allocation types
  101. BOOST_STATIC_ASSERT(( (AllocationDisableMask & ForbiddenMask) != ForbiddenMask ));
  102. //The mask is only valid for version 2 allocators
  103. BOOST_STATIC_ASSERT(( Version != 1 || (AllocationDisableMask == 0) ));
  104. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  105. public:
  106. typedef T value_type;
  107. typedef T * pointer;
  108. typedef const T * const_pointer;
  109. typedef T & reference;
  110. typedef const T & const_reference;
  111. typedef std::size_t size_type;
  112. typedef std::ptrdiff_t difference_type;
  113. typedef boost::container::dtl::
  114. version_type<self_t, Version> version;
  115. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  116. typedef boost::container::dtl::
  117. basic_multiallocation_chain<void*> void_multiallocation_chain;
  118. typedef boost::container::dtl::
  119. transform_multiallocation_chain
  120. <void_multiallocation_chain, T> multiallocation_chain;
  121. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  122. //!Obtains an allocator that allocates
  123. //!objects of type T2
  124. template<class T2>
  125. struct rebind
  126. {
  127. typedef allocator<T2, Version, AllocationDisableMask> other;
  128. };
  129. //!Default constructor
  130. //!Never throws
  131. allocator() BOOST_NOEXCEPT_OR_NOTHROW
  132. {}
  133. //!Constructor from other allocator.
  134. //!Never throws
  135. allocator(const allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  136. {}
  137. //!Constructor from related allocator.
  138. //!Never throws
  139. template<class T2>
  140. allocator(const allocator<T2
  141. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  142. , Version, AllocationDisableMask
  143. #endif
  144. > &) BOOST_NOEXCEPT_OR_NOTHROW
  145. {}
  146. //!Allocates memory for an array of count elements.
  147. //!Throws std::bad_alloc if there is no enough memory
  148. //!If Version is 2, this allocated memory can only be deallocated
  149. //!with deallocate() or (for Version == 2) deallocate_many()
  150. BOOST_CONTAINER_ATTRIBUTE_NODISCARD pointer allocate(size_type count, const void * hint= 0)
  151. {
  152. (void)hint;
  153. if(count > size_type(-1)/(2u*sizeof(T)))
  154. boost::container::throw_bad_alloc();
  155. void *ret = dlmalloc_malloc(count*sizeof(T));
  156. if(!ret)
  157. boost::container::throw_bad_alloc();
  158. return static_cast<pointer>(ret);
  159. }
  160. //!Deallocates previously allocated memory.
  161. //!Never throws
  162. BOOST_CONTAINER_FORCEINLINE void deallocate(pointer ptr, size_type) BOOST_NOEXCEPT_OR_NOTHROW
  163. { dlmalloc_free(ptr); }
  164. //!Returns the maximum number of elements that could be allocated.
  165. //!Never throws
  166. BOOST_CONTAINER_FORCEINLINE size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
  167. { return size_type(-1)/(2u*sizeof(T)); }
  168. //!Swaps two allocators, does nothing
  169. //!because this allocator is stateless
  170. BOOST_CONTAINER_FORCEINLINE friend void swap(self_t &, self_t &) BOOST_NOEXCEPT_OR_NOTHROW
  171. {}
  172. //!An allocator always compares to true, as memory allocated with one
  173. //!instance can be deallocated by another instance
  174. BOOST_CONTAINER_ATTRIBUTE_NODISCARD
  175. friend bool operator==(const allocator &, const allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  176. { return true; }
  177. //!An allocator always compares to false, as memory allocated with one
  178. //!instance can be deallocated by another instance
  179. BOOST_CONTAINER_ATTRIBUTE_NODISCARD BOOST_CONTAINER_FORCEINLINE
  180. friend bool operator!=(const allocator &, const allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  181. { return false; }
  182. //!An advanced function that offers in-place expansion shrink to fit and new allocation
  183. //!capabilities. Memory allocated with this function can only be deallocated with deallocate()
  184. //!or deallocate_many().
  185. //!This function is available only with Version == 2
  186. BOOST_CONTAINER_ATTRIBUTE_NODISCARD pointer allocation_command(allocation_type command,
  187. size_type limit_size,
  188. size_type &prefer_in_recvd_out_size,
  189. pointer &reuse)
  190. {
  191. BOOST_STATIC_ASSERT(( Version > 1 ));
  192. const allocation_type mask(AllocationDisableMask);
  193. command &= ~mask;
  194. pointer ret = this->priv_allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse);
  195. if(!ret && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION))
  196. boost::container::throw_bad_alloc();
  197. return ret;
  198. }
  199. //!Returns maximum the number of objects the previously allocated memory
  200. //!pointed by p can hold.
  201. //!Memory must not have been allocated with
  202. //!allocate_one or allocate_individual.
  203. //!This function is available only with Version == 2
  204. BOOST_CONTAINER_ATTRIBUTE_NODISCARD size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW
  205. {
  206. BOOST_STATIC_ASSERT(( Version > 1 ));
  207. return dlmalloc_size(p);
  208. }
  209. //!Allocates just one object. Memory allocated with this function
  210. //!must be deallocated only with deallocate_one().
  211. //!Throws bad_alloc if there is no enough memory
  212. //!This function is available only with Version == 2
  213. BOOST_CONTAINER_ATTRIBUTE_NODISCARD BOOST_CONTAINER_FORCEINLINE pointer allocate_one()
  214. {
  215. BOOST_STATIC_ASSERT(( Version > 1 ));
  216. return this->allocate(1);
  217. }
  218. //!Allocates many elements of size == 1.
  219. //!Elements must be individually deallocated with deallocate_one()
  220. //!This function is available only with Version == 2
  221. BOOST_CONTAINER_FORCEINLINE void allocate_individual(std::size_t num_elements, multiallocation_chain &chain)
  222. {
  223. BOOST_STATIC_ASSERT(( Version > 1 ));
  224. this->allocate_many(1, num_elements, chain);
  225. }
  226. //!Deallocates memory previously allocated with allocate_one().
  227. //!You should never use deallocate_one to deallocate memory allocated
  228. //!with other functions different from allocate_one() or allocate_individual.
  229. //Never throws
  230. void deallocate_one(pointer p) BOOST_NOEXCEPT_OR_NOTHROW
  231. {
  232. BOOST_STATIC_ASSERT(( Version > 1 ));
  233. return this->deallocate(p, 1);
  234. }
  235. //!Deallocates memory allocated with allocate_one() or allocate_individual().
  236. //!This function is available only with Version == 2
  237. BOOST_CONTAINER_FORCEINLINE
  238. void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
  239. {
  240. BOOST_STATIC_ASSERT(( Version > 1 ));
  241. return this->deallocate_many(chain);
  242. }
  243. //!Allocates many elements of size elem_size.
  244. //!Elements must be individually deallocated with deallocate()
  245. //!This function is available only with Version == 2
  246. void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain)
  247. {
  248. BOOST_STATIC_ASSERT(( Version > 1 ));
  249. dlmalloc_memchain ch;
  250. BOOST_CONTAINER_MEMCHAIN_INIT(&ch);
  251. if(!dlmalloc_multialloc_nodes(n_elements, elem_size*sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch)){
  252. boost::container::throw_bad_alloc();
  253. }
  254. chain.incorporate_after(chain.before_begin()
  255. ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch)
  256. ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
  257. ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );
  258. /*
  259. if(!dlmalloc_multialloc_nodes(n_elements, elem_size*sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain))){
  260. boost::container::throw_bad_alloc();
  261. }*/
  262. }
  263. //!Allocates n_elements elements, each one of size elem_sizes[i]
  264. //!Elements must be individually deallocated with deallocate()
  265. //!This function is available only with Version == 2
  266. void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
  267. {
  268. BOOST_STATIC_ASSERT(( Version > 1 ));
  269. dlmalloc_memchain ch;
  270. BOOST_CONTAINER_MEMCHAIN_INIT(&ch);
  271. if(!dlmalloc_multialloc_arrays(n_elements, elem_sizes, sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch)){
  272. boost::container::throw_bad_alloc();
  273. }
  274. chain.incorporate_after(chain.before_begin()
  275. ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch)
  276. ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
  277. ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );
  278. /*
  279. if(!dlmalloc_multialloc_arrays(n_elements, elem_sizes, sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain))){
  280. boost::container::throw_bad_alloc();
  281. }*/
  282. }
  283. //!Deallocates several elements allocated by
  284. //!allocate_many(), allocate(), or allocation_command().
  285. //!This function is available only with Version == 2
  286. void deallocate_many(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
  287. {
  288. BOOST_STATIC_ASSERT(( Version > 1 ));
  289. dlmalloc_memchain ch;
  290. void *beg(&*chain.begin()), *last(&*chain.last());
  291. size_t size(chain.size());
  292. BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&ch, beg, last, size);
  293. dlmalloc_multidealloc(&ch);
  294. //dlmalloc_multidealloc(reinterpret_cast<dlmalloc_memchain *>(&chain));
  295. }
  296. private:
  297. pointer priv_allocation_command
  298. (allocation_type command, std::size_t limit_size
  299. ,size_type &prefer_in_recvd_out_size
  300. ,pointer &reuse_ptr)
  301. {
  302. std::size_t const preferred_size = prefer_in_recvd_out_size;
  303. dlmalloc_command_ret_t ret = {0 , 0};
  304. if((limit_size > this->max_size()) | (preferred_size > this->max_size())){
  305. return pointer();
  306. }
  307. std::size_t l_size = limit_size*sizeof(T);
  308. std::size_t p_size = preferred_size*sizeof(T);
  309. std::size_t r_size;
  310. {
  311. void* reuse_ptr_void = reuse_ptr;
  312. ret = dlmalloc_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr_void);
  313. reuse_ptr = ret.second ? static_cast<T*>(reuse_ptr_void) : 0;
  314. }
  315. prefer_in_recvd_out_size = r_size/sizeof(T);
  316. return (pointer)ret.first;
  317. }
  318. };
  319. } //namespace container {
  320. } //namespace boost {
  321. #include <boost/container/detail/config_end.hpp>
  322. #endif //BOOST_CONTAINER_ALLOCATOR_HPP