/* Memory utilities for OctaSTD. * * This file is part of OctaSTD. See COPYING.md for futher information. */ #ifndef OCTA_MEMORY_H #define OCTA_MEMORY_H #include #include "octa/new.h" #include "octa/utility.h" #include "octa/type_traits.h" namespace octa { /* address of */ template constexpr T *address_of(T &v) { return reinterpret_cast(&const_cast (reinterpret_cast(v))); } /* pointer traits */ namespace detail { template struct HasElement { template static int test(...); template static char test(typename U::Element * = 0); static constexpr bool value = (sizeof(test(0)) == 1); }; template::value> struct PointerElementBase; template struct PointerElementBase { typedef typename T::Element Type; }; template class T, typename U, typename ...A> struct PointerElementBase, true> { typedef typename T::Element Type; }; template class T, typename U, typename ...A> struct PointerElementBase, false> { typedef U Type; }; template struct PointerElementType { typedef typename PointerElementBase::Type Type; }; template struct PointerElementType { typedef T Type; }; template struct HasDifference { template static int test(...); template static char test(typename U::Difference * = 0); static constexpr bool value = (sizeof(test(0)) == 1); }; template::value> struct PointerDifferenceBase { typedef ptrdiff_t Type; }; template struct PointerDifferenceBase { typedef typename T::Difference Type; }; template struct PointerDifferenceType { typedef typename PointerDifferenceBase::Type Type; }; template struct PointerDifferenceType { typedef ptrdiff_t Type; }; template struct HasRebind { template static int test(...); template static char test( typename V::template Rebind * = 0); static constexpr bool value = (sizeof(test(0)) == 1); }; template::value> struct PointerRebindBase { typedef typename T::template Rebind Type; }; template class T, typename U, typename ...A, typename V > struct PointerRebindBase, V, true> { typedef typename T::template Rebind Type; }; template class T, typename U, typename ...A, typename V > struct PointerRebindBase, V, false> { typedef T Type; }; template struct PointerRebindType { using type = typename PointerRebindBase::Type; }; template struct PointerRebindType { using type = U *; }; template struct PointerPointer { typedef T Type; }; template struct PointerPointer { typedef T *Type; }; } /*namespace detail */ template using Pointer = typename octa::detail::PointerPointer::Type; template using PointerElement = typename octa::detail::PointerElementType::Type; template using PointerDifference = typename octa::detail::PointerDifferenceType::Type; template using PointerRebind = typename octa::detail::PointerRebindType::Type; /* pointer to */ namespace detail { struct PointerToNat {}; template struct PointerTo { static T pointer_to(octa::Conditional< octa::IsVoid>::value, PointerToNat, PointerElement > &r) { return T::pointer_to(r); } }; template struct PointerTo { static T pointer_to(octa::Conditional< octa::IsVoid::value, PointerToNat, T > &r) { return octa::address_of(r); } }; } template static T pointer_to(octa::Conditional< octa::IsVoid>::value, octa::detail::PointerToNat, PointerElement > &r) { return octa::detail::PointerTo::pointer_to(r); } /* default deleter */ template struct DefaultDelete { constexpr DefaultDelete() = default; template DefaultDelete(const DefaultDelete &) {}; void operator()(T *p) const { delete p; } }; template struct DefaultDelete { constexpr DefaultDelete() = default; template DefaultDelete(const DefaultDelete &) {}; void operator()(T *p) const { delete[] p; } template void operator()(U *) const = delete; }; /* box */ namespace detail { template::value> struct BoxPair; template struct BoxPair { /* non-empty deleter */ T *p_ptr; private: U p_del; public: template BoxPair(T *ptr, D &&dltr): p_ptr(ptr), p_del(octa::forward(dltr)) {} U &get_deleter() { return p_del; } const U &get_deleter() const { return p_del; } void swap(BoxPair &v) { octa::swap(p_ptr, v.p_ptr); octa::swap(p_del, v.p_del); } }; template struct BoxPair: U { /* empty deleter */ T *p_ptr; template BoxPair(T *ptr, D &&dltr): U(octa::forward(dltr)), p_ptr(ptr) {} U &get_deleter() { return *this; } const U &get_deleter() const { return *this; } void swap(BoxPair &v) { octa::swap(p_ptr, v.p_ptr); } }; template static int ptr_test(...); template static char ptr_test(typename T::Pointer * = 0); template struct HasPtr: octa::IntegralConstant(0)) == 1) > {}; template::value> struct PointerBase { typedef typename D::Pointer Type; }; template struct PointerBase { typedef T *Type; }; template struct PointerType { typedef typename PointerBase>::Type Type; }; } /* namespace detail */ template> struct Box { typedef T Element; typedef D Deleter; typedef typename octa::detail::PointerType::Type Pointer; private: struct Nat { int x; }; typedef RemoveReference &D_ref; typedef const RemoveReference &D_cref; public: constexpr Box(): p_stor(nullptr, D()) { static_assert(!octa::IsPointer::value, "Box constructed with null fptr deleter"); } constexpr Box(nullptr_t): p_stor(nullptr, D()) { static_assert(!octa::IsPointer::value, "Box constructed with null fptr deleter"); } explicit Box(Pointer p): p_stor(p, D()) { static_assert(!octa::IsPointer::value, "Box constructed with null fptr deleter"); } Box(Pointer p, octa::Conditional::value, D, octa::AddLvalueReference > d): p_stor(p, d) {} Box(Pointer p, octa::RemoveReference &&d): p_stor(p, octa::move(d)) { static_assert(!octa::IsReference::value, "rvalue deleter cannot be a ref"); } Box(Box &&u): p_stor(u.release(), octa::forward(u.get_deleter())) {} template Box(Box &&u, octa::EnableIf::value && octa::IsConvertible::Pointer, Pointer>::value && octa::IsConvertible::value && (!octa::IsReference::value || octa::IsSame::value) > = Nat()): p_stor(u.release(), octa::forward
(u.get_deleter())) {} Box &operator=(Box &&u) { reset(u.release()); p_stor.get_deleter() = octa::forward(u.get_deleter()); return *this; } template EnableIf::value && octa::IsConvertible::Pointer, Pointer>::value && octa::IsAssignable::value, Box & > operator=(Box &&u) { reset(u.release()); p_stor.get_deleter() = octa::forward
(u.get_deleter()); return *this; } Box &operator=(nullptr_t) { reset(); return *this; } ~Box() { reset(); } octa::AddLvalueReference operator*() const { return *p_stor.p_ptr; } Pointer operator->() const { return p_stor.p_ptr; } explicit operator bool() const { return p_stor.p_ptr != nullptr; } Pointer get() const { return p_stor.p_ptr; } D_ref get_deleter() { return p_stor.get_deleter(); } D_cref get_deleter() const { return p_stor.get_deleter(); } Pointer release() { Pointer p = p_stor.p_ptr; p_stor.p_ptr = nullptr; return p; } void reset(Pointer p = nullptr) { Pointer tmp = p_stor.p_ptr; p_stor.p_ptr = p; if (tmp) p_stor.get_deleter()(tmp); } void swap(Box &u) { p_stor.swap(u.p_stor); } private: octa::detail::BoxPair p_stor; }; namespace detail { template>, octa::RemoveCv> >::value> struct SameOrLessCvQualifiedBase: octa::IsConvertible {}; template struct SameOrLessCvQualifiedBase: octa::False {}; template::value || octa::IsSame::value || octa::detail::HasElement::value > struct SameOrLessCvQualified: SameOrLessCvQualifiedBase {}; template struct SameOrLessCvQualified: octa::False {}; } /* namespace detail */ template struct Box { typedef T Element; typedef D Deleter; typedef typename octa::detail::PointerType::Type Pointer; private: struct Nat { int x; }; typedef RemoveReference &D_ref; typedef const RemoveReference &D_cref; public: constexpr Box(): p_stor(nullptr, D()) { static_assert(!octa::IsPointer::value, "Box constructed with null fptr deleter"); } constexpr Box(nullptr_t): p_stor(nullptr, D()) { static_assert(!octa::IsPointer::value, "Box constructed with null fptr deleter"); } template explicit Box(U p, octa::EnableIf< octa::detail::SameOrLessCvQualified::value, Nat > = Nat()): p_stor(p, D()) { static_assert(!octa::IsPointer::value, "Box constructed with null fptr deleter"); } template Box(U p, octa::Conditional< octa::IsReference::value, D, AddLvalueReference > d, octa::EnableIf::value, Nat> = Nat()): p_stor(p, d) {} Box(nullptr_t, octa::Conditional::value, D, AddLvalueReference > d): p_stor(nullptr, d) {} template Box(U p, octa::RemoveReference &&d, octa::EnableIf< octa::detail::SameOrLessCvQualified::value, Nat > = Nat()): p_stor(p, octa::move(d)) { static_assert(!octa::IsReference::value, "rvalue deleter cannot be a ref"); } Box(nullptr_t, octa::RemoveReference &&d): p_stor(nullptr, octa::move(d)) { static_assert(!octa::IsReference::value, "rvalue deleter cannot be a ref"); } Box(Box &&u): p_stor(u.release(), octa::forward(u.get_deleter())) {} template Box(Box &&u, EnableIf::value && octa::detail::SameOrLessCvQualified::Pointer, Pointer>::value && octa::IsConvertible::value && (!octa::IsReference::value || octa::IsSame::value)> = Nat() ): p_stor(u.release(), octa::forward
(u.get_deleter())) {} Box &operator=(Box &&u) { reset(u.release()); p_stor.get_deleter() = octa::forward(u.get_deleter()); return *this; } template EnableIf::value && octa::detail::SameOrLessCvQualified::Pointer, Pointer>::value && IsAssignable::value, Box & > operator=(Box &&u) { reset(u.release()); p_stor.get_deleter() = octa::forward
(u.get_deleter()); return *this; } Box &operator=(nullptr_t) { reset(); return *this; } ~Box() { reset(); } octa::AddLvalueReference operator[](size_t idx) const { return p_stor.p_ptr[idx]; } explicit operator bool() const { return p_stor.p_ptr != nullptr; } Pointer get() const { return p_stor.p_ptr; } D_ref get_deleter() { return p_stor.get_deleter(); } D_cref get_deleter() const { return p_stor.get_deleter(); } Pointer release() { Pointer p = p_stor.p_ptr; p_stor.p_ptr = nullptr; return p; } template EnableIf< octa::detail::SameOrLessCvQualified::value, void > reset(U p) { Pointer tmp = p_stor.p_ptr; p_stor.p_ptr = p; if (tmp) p_stor.get_deleter()(tmp); } void reset(nullptr_t) { Pointer tmp = p_stor.p_ptr; p_stor.p_ptr = nullptr; if (tmp) p_stor.get_deleter()(tmp); } void reset() { reset(nullptr); } void swap(Box &u) { p_stor.swap(u.p_stor); } private: octa::detail::BoxPair p_stor; }; namespace detail { template struct BoxIf { typedef octa::Box Box; }; template struct BoxIf { typedef octa::Box BoxUnknownSize; }; template struct BoxIf { typedef void BoxKnownSize; }; } template typename octa::detail::BoxIf::Box make_box(A &&...args) { return Box(new T(octa::forward(args)...)); } template typename octa::detail::BoxIf::BoxUnknownSize make_box(size_t n) { return Box(new octa::RemoveExtent[n]()); } template typename octa::detail::BoxIf::BoxKnownSize make_box(A &&...args) = delete; /* allocator */ template struct Allocator; template<> struct Allocator { typedef void Value; typedef void *Pointer; typedef const void *ConstPointer; template using Rebind = Allocator; }; template<> struct Allocator { typedef const void Value; typedef const void *Pointer; typedef const void *ConstPointer; template using Rebind = Allocator; }; template struct Allocator { typedef size_t Size; typedef ptrdiff_t Difference; typedef T Value; typedef T &Reference; typedef const T &ConstReference; typedef T *Pointer; typedef const T *ConstPointer; template using Rebind = Allocator; Pointer address(Reference v) const { return address_of(v); }; ConstPointer address(ConstReference v) const { return address_of(v); }; Size max_size() const { return Size(~0) / sizeof(T); } Pointer allocate(Size n, Allocator::ConstPointer = nullptr) { return (Pointer) ::new uchar[n * sizeof(T)]; } void deallocate(Pointer p, Size) { ::delete[] (uchar *) p; } template void construct(U *p, A &&...args) { ::new((void *)p) U(octa::forward(args)...); } void destroy(Pointer p) { p->~T(); } }; template struct Allocator { typedef size_t Size; typedef ptrdiff_t Difference; typedef const T Value; typedef const T &Reference; typedef const T &ConstReference; typedef const T *Pointer; typedef const T *ConstPointer; template using Rebind = Allocator; ConstPointer address(ConstReference v) const { return address_of(v); }; Size max_size() const { return Size(~0) / sizeof(T); } Pointer allocate(Size n, Allocator::ConstPointer = nullptr) { return (Pointer) ::new uchar[n * sizeof(T)]; } void deallocate(Pointer p, Size) { ::delete[] (uchar *) p; } template void construct(U *p, A &&...args) { ::new((void *)p) U(octa::forward(args)...); } void destroy(Pointer p) { p->~T(); } }; template bool operator==(const Allocator &, const Allocator &) { return true; } template bool operator!=(const Allocator &, const Allocator &) { return false; } /* allocator traits - modeled after libc++ */ namespace detail { template struct ConstPtrTest { template static char test( typename U::ConstPointer * = 0); template static int test(...); static constexpr bool value = (sizeof(test(0)) == 1); }; template::value> struct ConstPointer { typedef typename A::ConstPointer Type; }; template struct ConstPointer { typedef PointerRebind Type; }; template struct VoidPtrTest { template static char test( typename U::VoidPointer * = 0); template static int test(...); static constexpr bool value = (sizeof(test(0)) == 1); }; template::value> struct VoidPointer { typedef typename A::VoidPointer Type; }; template struct VoidPointer { typedef PointerRebind Type; }; template struct ConstVoidPtrTest { template static char test( typename U::ConstVoidPointer * = 0); template static int test(...); static constexpr bool value = (sizeof(test(0)) == 1); }; template::value> struct ConstVoidPointer { typedef typename A::ConstVoidPointer Type; }; template struct ConstVoidPointer { typedef PointerRebind Type; }; template struct SizeTest { template static char test(typename U::Size * = 0); template static int test(...); static constexpr bool value = (sizeof(test(0)) == 1); }; template::value> struct SizeBase { typedef octa::MakeUnsigned Type; }; template struct SizeBase { typedef typename A::Size Type; }; } /* namespace detail */ /* allocator type traits */ template using AllocatorType = A; template using AllocatorValue = typename AllocatorType::Value; template using AllocatorPointer = typename octa::detail::PointerType< AllocatorValue, AllocatorType >::Type; template using AllocatorConstPointer = typename octa::detail::ConstPointer< AllocatorValue, AllocatorPointer, AllocatorType >::Type; template using AllocatorVoidPointer = typename octa::detail::VoidPointer< AllocatorPointer, AllocatorType >::Type; template using AllocatorConstVoidPointer = typename octa::detail::ConstVoidPointer< AllocatorPointer, AllocatorType >::Type; /* allocator difference */ namespace detail { template struct DiffTest { template static char test(typename U::Difference * = 0); template static int test(...); static constexpr bool value = (sizeof(test(0)) == 1); }; template::value> struct AllocDifference { typedef PointerDifference

Type; }; template struct AllocDifference { typedef typename A::Difference Type; }; } template using AllocatorDifference = typename octa::detail::AllocDifference< A, AllocatorPointer >::Type; /* allocator size */ template using AllocatorSize = typename octa::detail::SizeBase< A, AllocatorDifference >::Type; /* allocator rebind */ namespace detail { template::value> struct AllocTraitsRebindType { typedef typename T::template Rebind Type; }; template class A, typename T, typename ...Args, typename U > struct AllocTraitsRebindType, U, true> { typedef typename A::template Rebind Type; }; template class A, typename T, typename ...Args, typename U > struct AllocTraitsRebindType, U, false> { typedef A Type; }; } /* namespace detail */ template using AllocatorRebind = typename octa::detail::AllocTraitsRebindType< AllocatorType, T >::Type; /* allocator propagate on container copy assignment */ namespace detail { template struct PropagateOnContainerCopyAssignmentTest { template static char test( typename U::PropagateOnContainerCopyAssignment * = 0); template static int test(...); static constexpr bool value = (sizeof(test(0)) == 1); }; template::value> struct PropagateOnContainerCopyAssignmentBase { typedef octa::False Type; }; template struct PropagateOnContainerCopyAssignmentBase { typedef typename A::PropagateOnContainerCopyAssignment Type; }; } /* namespace detail */ template using PropagateOnContainerCopyAssignment = typename octa::detail::PropagateOnContainerCopyAssignmentBase::Type; /* allocator propagate on container move assignment */ namespace detail { template struct PropagateOnContainerMoveAssignmentTest { template static char test( typename U::PropagateOnContainerMoveAssignment * = 0); template static int test(...); static constexpr bool value = (sizeof(test(0)) == 1); }; template::value> struct PropagateOnContainerMoveAssignmentBase { typedef octa::False Type; }; template struct PropagateOnContainerMoveAssignmentBase { typedef typename A::PropagateOnContainerMoveAssignment Type; }; } /* namespace detail */ template using PropagateOnContainerMoveAssignment = typename octa::detail::PropagateOnContainerMoveAssignmentBase::Type; /* allocator propagate on container swap */ namespace detail { template struct PropagateOnContainerSwapTest { template static char test( typename U::PropagateOnContainerSwap * = 0); template static int test(...); static constexpr bool value = (sizeof(test(0)) == 1); }; template::value> struct PropagateOnContainerSwapBase { typedef octa::False Type; }; template struct PropagateOnContainerSwapBase { typedef typename A::PropagateOnContainerSwap Type; }; } /* namespace detail */ template using PropagateOnContainerSwap = typename octa::detail::PropagateOnContainerSwapBase::Type; /* allocator is always equal */ namespace detail { template struct IsAlwaysEqualTest { template static char test(typename U::IsAlwaysEqual * = 0); template static int test(...); static constexpr bool value = (sizeof(test(0)) == 1); }; template::value> struct IsAlwaysEqualBase { typedef typename octa::IsEmpty::Type Type; }; template struct IsAlwaysEqualBase { typedef typename A::IsAlwaysEqual Type; }; } /* namespace detail */ template using IsAlwaysEqual = typename octa::detail::IsAlwaysEqualBase::Type; /* allocator allocate */ template inline AllocatorPointer allocator_allocate(A &a, AllocatorSize n) { return a.allocate(n); } namespace detail { template auto allocate_hint_test(A &&a, S &&sz, CVP &&p) -> decltype(a.allocate(sz, p), octa::True()); template auto allocate_hint_test(const A &, S &&, CVP &&) -> octa::False; template struct AllocateHintTest: octa::IntegralConstant(), octa::declval(), octa::declval())), octa::True >::value > {}; template inline AllocatorPointer allocate(A &a, AllocatorSize n, AllocatorConstVoidPointer h, octa::True) { return a.allocate(n, h); } template inline AllocatorPointer allocate(A &a, AllocatorSize n, AllocatorConstVoidPointer, octa::False) { return a.allocate(n); } } /* namespace detail */ template inline AllocatorPointer allocator_allocate(A &a, AllocatorSize n, AllocatorConstVoidPointer h) { return octa::detail::allocate(a, n, h, octa::detail::AllocateHintTest< A, AllocatorSize, AllocatorConstVoidPointer >()); } /* allocator deallocate */ template inline void allocator_deallocate(A &a, AllocatorPointer p, AllocatorSize n) { a.deallocate(p, n); } /* allocator construct */ namespace detail { template auto construct_test(A &&a, T *p, Args &&...args) -> decltype(a.construct(p, octa::forward(args)...), octa::True()); template auto construct_test(const A &, T *, Args &&...) -> octa::False; template struct ConstructTest: octa::IntegralConstant(), octa::declval(), octa::declval()...)), octa::True >::value > {}; template inline void construct(octa::True, A &a, T *p, Args &&...args) { a.construct(p, octa::forward(args)...); } template inline void construct(octa::False, A &, T *p, Args &&...args) { ::new ((void *)p) T(octa::forward(args)...); } } /* namespace detail */ template inline void allocator_construct(A &a, T *p, Args &&...args) { octa::detail::construct(octa::detail::ConstructTest< A, T *, Args... >(), a, p, octa::forward(args)...); } /* allocator destroy */ namespace detail { template auto destroy_test(A &&a, P &&p) -> decltype(a.destroy(p), octa::True()); template auto destroy_test(const A &, P &&) -> octa::False; template struct DestroyTest: octa::IntegralConstant(), octa::declval

())), octa::True >::value > {}; template inline void destroy(octa::True, A &a, T *p) { a.destroy(p); } template inline void destroy(octa::False, A &, T *p) { p->~T(); } } /* namespace detail */ template inline void allocator_destroy(A &a, T *p) { octa::detail::destroy(octa::detail::DestroyTest(), a, p); } /* allocator max size */ namespace detail { template auto alloc_max_size_test(A &&a) -> decltype(a.max_size(), octa::True()); template auto alloc_max_size_test(const A &) -> octa::False; template struct AllocMaxSizeTest: octa::IntegralConstant())), octa::True >::value > {}; template inline AllocatorSize alloc_max_size(octa::True, const A &a) { return a.max_size(); } template inline AllocatorSize alloc_max_size(octa::False, const A &) { return AllocatorSize(~0); } } /* namespace detail */ template inline AllocatorSize allocator_max_size(const A &a) { return octa::detail::alloc_max_size(octa::detail::AllocMaxSizeTest< const A >(), a); } /* allocator container copy */ namespace detail { template auto alloc_copy_test(A &&a) -> decltype(a.container_copy(), octa::True()); template auto alloc_copy_test(const A &) -> octa::False; template struct AllocCopyTest: octa::IntegralConstant())), octa::True >::value > {}; template inline AllocatorType alloc_container_copy(octa::True, const A &a) { return a.container_copy(); } template inline AllocatorType alloc_container_copy(octa::False, const A &a) { return a; } } /* namespace detail */ template inline AllocatorType allocator_container_copy(const A &a) { return octa::detail::alloc_container_copy(octa::detail::AllocCopyTest< const A >(), a); } } #endif