pool_common_alloc.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-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_DETAIL_POOL_COMMON_ALLOC_HPP
  11. #define BOOST_CONTAINER_DETAIL_POOL_COMMON_ALLOC_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/throw_exception.hpp>
  21. #include <boost/intrusive/slist.hpp>
  22. #include <boost/container/detail/pool_common.hpp>
  23. #include <boost/container/detail/dlmalloc.hpp>
  24. #include <cstddef>
  25. namespace boost{
  26. namespace container{
  27. namespace dtl{
  28. struct node_slist_helper
  29. : public boost::container::dtl::node_slist<void*>
  30. {};
  31. struct fake_segment_manager
  32. {
  33. typedef void * void_pointer;
  34. static const std::size_t PayloadPerAllocation = BOOST_CONTAINER_ALLOCATION_PAYLOAD;
  35. typedef boost::container::dtl::
  36. basic_multiallocation_chain<void*> multiallocation_chain;
  37. static void deallocate(void_pointer p)
  38. { dlmalloc_free(p); }
  39. static void deallocate_many(multiallocation_chain &chain)
  40. {
  41. std::size_t size = chain.size();
  42. std::pair<void*, void*> ptrs = chain.extract_data();
  43. dlmalloc_memchain dlchain;
  44. BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&dlchain, ptrs.first, ptrs.second, size);
  45. dlmalloc_multidealloc(&dlchain);
  46. }
  47. typedef std::ptrdiff_t difference_type;
  48. typedef std::size_t size_type;
  49. static void *allocate_aligned(std::size_t nbytes, std::size_t alignment)
  50. {
  51. void *ret = dlmalloc_memalign(nbytes, alignment);
  52. if(!ret)
  53. boost::container::throw_bad_alloc();
  54. return ret;
  55. }
  56. static void *allocate(std::size_t nbytes)
  57. {
  58. void *ret = dlmalloc_malloc(nbytes);
  59. if(!ret)
  60. boost::container::throw_bad_alloc();
  61. return ret;
  62. }
  63. };
  64. } //namespace boost{
  65. } //namespace container{
  66. } //namespace dtl{
  67. namespace boost {
  68. namespace container {
  69. namespace dtl {
  70. template<class T>
  71. struct is_stateless_segment_manager;
  72. template<>
  73. struct is_stateless_segment_manager
  74. <boost::container::dtl::fake_segment_manager>
  75. {
  76. static const bool value = true;
  77. };
  78. } //namespace dtl {
  79. } //namespace container {
  80. } //namespace boost {
  81. #include <boost/container/detail/config_end.hpp>
  82. #endif //BOOST_CONTAINER_DETAIL_POOL_COMMON_ALLOC_HPP