allocator.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. 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. friend bool operator==(const allocator &, const allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  175. { return true; }
  176. //!An allocator always compares to false, as memory allocated with one
  177. //!instance can be deallocated by another instance
  178. BOOST_CONTAINER_FORCEINLINE friend bool operator!=(const allocator &, const allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  179. { return false; }
  180. //!An advanced function that offers in-place expansion shrink to fit and new allocation
  181. //!capabilities. Memory allocated with this function can only be deallocated with deallocate()
  182. //!or deallocate_many().
  183. //!This function is available only with Version == 2
  184. pointer allocation_command(allocation_type command,
  185. size_type limit_size,
  186. size_type &prefer_in_recvd_out_size,
  187. pointer &reuse)
  188. {
  189. BOOST_STATIC_ASSERT(( Version > 1 ));
  190. const allocation_type mask(AllocationDisableMask);
  191. command &= ~mask;
  192. pointer ret = this->priv_allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse);
  193. if(!ret && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION))
  194. boost::container::throw_bad_alloc();
  195. return ret;
  196. }
  197. //!Returns maximum the number of objects the previously allocated memory
  198. //!pointed by p can hold.
  199. //!Memory must not have been allocated with
  200. //!allocate_one or allocate_individual.
  201. //!This function is available only with Version == 2
  202. size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW
  203. {
  204. BOOST_STATIC_ASSERT(( Version > 1 ));
  205. return dlmalloc_size(p);
  206. }
  207. //!Allocates just one object. Memory allocated with this function
  208. //!must be deallocated only with deallocate_one().
  209. //!Throws bad_alloc if there is no enough memory
  210. //!This function is available only with Version == 2
  211. BOOST_CONTAINER_FORCEINLINE pointer allocate_one()
  212. {
  213. BOOST_STATIC_ASSERT(( Version > 1 ));
  214. return this->allocate(1);
  215. }
  216. //!Allocates many elements of size == 1.
  217. //!Elements must be individually deallocated with deallocate_one()
  218. //!This function is available only with Version == 2
  219. BOOST_CONTAINER_FORCEINLINE void allocate_individual(std::size_t num_elements, multiallocation_chain &chain)
  220. {
  221. BOOST_STATIC_ASSERT(( Version > 1 ));
  222. this->allocate_many(1, num_elements, chain);
  223. }
  224. //!Deallocates memory previously allocated with allocate_one().
  225. //!You should never use deallocate_one to deallocate memory allocated
  226. //!with other functions different from allocate_one() or allocate_individual.
  227. //Never throws
  228. void deallocate_one(pointer p) BOOST_NOEXCEPT_OR_NOTHROW
  229. {
  230. BOOST_STATIC_ASSERT(( Version > 1 ));
  231. return this->deallocate(p, 1);
  232. }
  233. //!Deallocates memory allocated with allocate_one() or allocate_individual().
  234. //!This function is available only with Version == 2
  235. BOOST_CONTAINER_FORCEINLINE void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
  236. {
  237. BOOST_STATIC_ASSERT(( Version > 1 ));
  238. return this->deallocate_many(chain);
  239. }
  240. //!Allocates many elements of size elem_size.
  241. //!Elements must be individually deallocated with deallocate()
  242. //!This function is available only with Version == 2
  243. void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain)
  244. {
  245. BOOST_STATIC_ASSERT(( Version > 1 ));
  246. dlmalloc_memchain ch;
  247. BOOST_CONTAINER_MEMCHAIN_INIT(&ch);
  248. if(!dlmalloc_multialloc_nodes(n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch)){
  249. boost::container::throw_bad_alloc();
  250. }
  251. chain.incorporate_after(chain.before_begin()
  252. ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch)
  253. ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
  254. ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );
  255. /*
  256. if(!dlmalloc_multialloc_nodes(n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain))){
  257. boost::container::throw_bad_alloc();
  258. }*/
  259. }
  260. //!Allocates n_elements elements, each one of size elem_sizes[i]
  261. //!Elements must be individually deallocated with deallocate()
  262. //!This function is available only with Version == 2
  263. void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
  264. {
  265. BOOST_STATIC_ASSERT(( Version > 1 ));
  266. dlmalloc_memchain ch;
  267. BOOST_CONTAINER_MEMCHAIN_INIT(&ch);
  268. if(!dlmalloc_multialloc_arrays(n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch)){
  269. boost::container::throw_bad_alloc();
  270. }
  271. chain.incorporate_after(chain.before_begin()
  272. ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch)
  273. ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
  274. ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );
  275. /*
  276. if(!dlmalloc_multialloc_arrays(n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain))){
  277. boost::container::throw_bad_alloc();
  278. }*/
  279. }
  280. //!Deallocates several elements allocated by
  281. //!allocate_many(), allocate(), or allocation_command().
  282. //!This function is available only with Version == 2
  283. void deallocate_many(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
  284. {
  285. BOOST_STATIC_ASSERT(( Version > 1 ));
  286. dlmalloc_memchain ch;
  287. void *beg(&*chain.begin()), *last(&*chain.last());
  288. size_t size(chain.size());
  289. BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&ch, beg, last, size);
  290. dlmalloc_multidealloc(&ch);
  291. //dlmalloc_multidealloc(reinterpret_cast<dlmalloc_memchain *>(&chain));
  292. }
  293. private:
  294. pointer priv_allocation_command
  295. (allocation_type command, std::size_t limit_size
  296. ,size_type &prefer_in_recvd_out_size
  297. ,pointer &reuse_ptr)
  298. {
  299. std::size_t const preferred_size = prefer_in_recvd_out_size;
  300. dlmalloc_command_ret_t ret = {0 , 0};
  301. if((limit_size > this->max_size()) | (preferred_size > this->max_size())){
  302. return pointer();
  303. }
  304. std::size_t l_size = limit_size*sizeof(T);
  305. std::size_t p_size = preferred_size*sizeof(T);
  306. std::size_t r_size;
  307. {
  308. void* reuse_ptr_void = reuse_ptr;
  309. ret = dlmalloc_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr_void);
  310. reuse_ptr = ret.second ? static_cast<T*>(reuse_ptr_void) : 0;
  311. }
  312. prefer_in_recvd_out_size = r_size/sizeof(T);
  313. return (pointer)ret.first;
  314. }
  315. };
  316. } //namespace container {
  317. } //namespace boost {
  318. #include <boost/container/detail/config_end.hpp>
  319. #endif //BOOST_CONTAINER_ALLOCATOR_HPP