default_allocator.qbk 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. [/
  2. Copyright 2019 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. ]
  7. [section:default_allocator default_allocator]
  8. [simplesect Authors]
  9. * Glen Fernandes
  10. [endsimplesect]
  11. [section Overview]
  12. The header <boost/core/default_allocator.hpp> provides the class template
  13. `boost::default_allocator` to serve as a minimal default allocator that:
  14. * Like C++2a's `std::allocator`, does not provide members such as `construct()`
  15. and `destroy()` to be eligible for optimizations by allocator-aware code that
  16. detects the absence of these members to provide more optimal construction.
  17. * Supports `BOOST_NO_EXCEPTIONS` in allocation.
  18. * Does not have `std` as an associated namespace.
  19. [endsect]
  20. [section Examples]
  21. The following snippet shows the use of this allocator as the default allocator
  22. for a container.
  23. ```
  24. template<class Key, class Compare = std::less<Key>,
  25. class Allocator = boost::default_allocator<Key> >
  26. class FlatSet;
  27. ```
  28. Facilities like `make_shared` can be implemented using `allocate_shared` with
  29. `default_allocator`.
  30. ```
  31. template<class T, class... Args>
  32. enable_if_t<!is_array_v<T>, shared_ptr<T> >
  33. make_shared(Args&&... args)
  34. {
  35. return allocate_shared<T>(boost::default_allocator<remove_cv_t<T> >(),
  36. std::forward<Args>(args)...);
  37. }
  38. ```
  39. [endsect]
  40. [section Reference]
  41. ```
  42. namespace boost {
  43. template<class T>
  44. struct default_allocator {
  45. typedef T value_type;
  46. typedef T* pointer;
  47. typedef const T* const_pointer;
  48. typedef std::add_lvalue_reference_t<T> reference;
  49. typedef std::add_lvalue_reference_t<const T> const_reference;
  50. typedef std::size_t size_type;
  51. typedef std::ptrdiff_t difference_type;
  52. typedef ``['true_type]`` propagate_on_container_move_assignment;
  53. typedef ``['true_type]`` is_always_equal;
  54. template<class U>
  55. struct rebind {
  56. typedef default_allocator<U> other;
  57. };
  58. constexpr default_allocator() = default;
  59. template<class U>
  60. constexpr default_allocator(const default_allocator<U>&) noexcept { }
  61. constexpr std::size_t max_size() const noexcept;
  62. T* allocate(std::size_t n);
  63. void deallocate(T* p, std::size_t);
  64. };
  65. template<class T, class U>
  66. constexpr bool operator==(const default_allocator<T>&,
  67. const default_allocator<U>&) noexcept;
  68. template<class T, class U>
  69. constexpr bool operator!=(const default_allocator<T>&,
  70. const default_allocator<U>&) noexcept;
  71. } /* boost */
  72. ```
  73. [section Members]
  74. [variablelist
  75. [[`constexpr std::size_t max_size() const noexcept;`]
  76. [[variablelist
  77. [[Returns][The largest value `N` for which the call `allocate(N)` might
  78. succeed.]]]]]
  79. [[`T* allocate(std::size_t n);`]
  80. [[variablelist
  81. [[Returns]
  82. [A pointer to the initial element of an array of storage of size
  83. `n * sizeof(T)`, aligned appropriately for objects of type `T`.]]
  84. [[Remarks][The storage is obtained by calling `::operator new`.]]
  85. [[Throws][`std::bad_alloc` if the storage cannot be obtained.]]]]]
  86. [[`void deallocate(T* p, std::size_t n);`]
  87. [[variablelist
  88. [[Requires]
  89. [`p` shall be a pointer value obtained from `allocate()`. `n` shall equal the
  90. value passed as the first argument to the invocation of `allocate` which
  91. returned `p`.]]
  92. [[Effects][Deallocates the storage referenced by `p`.]]
  93. [[Remarks][Uses `::operator delete`.]]]]]]
  94. [endsect]
  95. [section Operators]
  96. [variablelist
  97. [[`template<class T, class U> constexpr bool operator==(const
  98. default_allocator<T>&, const default_allocator<U>&) noexcept;`]
  99. [[variablelist
  100. [[Returns][`true`.]]]]]
  101. [[`template<class T, class U> constexpr bool operator!=(const
  102. default_allocator<T>&, const default_allocator<U>&) noexcept;`]
  103. [[variablelist
  104. [[Returns][`false`.]]]]]]
  105. [endsect]
  106. [endsect]
  107. [endsect]