2015-11-17 17:21:14 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2015-11-17 17:21:14 +11:00
|
|
|
*
|
2019-01-04 17:11:53 +11:00
|
|
|
* Copyright 2011-2019 Danny Robson <danny@nerdcruft.net>
|
2015-11-17 17:21:14 +11:00
|
|
|
*/
|
|
|
|
|
2018-12-17 14:45:54 +11:00
|
|
|
#pragma once
|
2018-03-02 12:15:43 +11:00
|
|
|
|
2020-11-27 09:58:30 +11:00
|
|
|
#include <utility>
|
2019-01-04 17:11:53 +11:00
|
|
|
#include <type_traits>
|
2015-11-17 17:21:14 +11:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
|
2020-11-25 15:21:20 +11:00
|
|
|
namespace cruft::ptr {
|
|
|
|
/// A utiliity function that deletes the provided pointer.
|
|
|
|
///
|
|
|
|
/// Useful as a means of moving `delete` calls inside a translation unit
|
|
|
|
/// and hence removing the need to include implementation headers for a
|
|
|
|
/// type that needs deletion.
|
|
|
|
template <typename ValueT>
|
|
|
|
void destroy (ValueT*);
|
|
|
|
|
|
|
|
|
|
|
|
/// A smart pointer that defers deletion to `destroy`.
|
|
|
|
///
|
|
|
|
/// This is solely useful as a way of avoid undefined type errors when
|
|
|
|
/// using std::unique_ptr and forward declarations.
|
|
|
|
template <typename ValueT>
|
|
|
|
class thin {
|
|
|
|
public:
|
|
|
|
explicit thin (ValueT *_value) { m_value = _value; }
|
|
|
|
~thin () { destroy (m_value); }
|
|
|
|
|
|
|
|
thin (thin const&) = delete;
|
|
|
|
thin& operator= (thin const&) = delete;
|
|
|
|
|
2021-04-19 14:52:22 +10:00
|
|
|
thin (thin &&rhs) noexcept
|
2020-11-25 15:21:20 +11:00
|
|
|
: m_value (rhs.m_value)
|
|
|
|
{ rhs.m_value = nullptr; }
|
|
|
|
|
|
|
|
thin& operator= (thin &&rhs) noexcept
|
|
|
|
{
|
|
|
|
std::swap (m_value, rhs.m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueT & operator* (void) &{ return *m_value; }
|
|
|
|
ValueT const& operator* (void) const &{ return *m_value; }
|
|
|
|
ValueT * operator->(void) &{ return m_value; }
|
|
|
|
ValueT const* operator->(void) const &{ return m_value; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ValueT *m_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename ValueT, typename ...ArgsT>
|
|
|
|
thin<ValueT>
|
|
|
|
make_thin (ArgsT &&...args)
|
|
|
|
{
|
|
|
|
return thin (new ValueT (std::forward<ArgsT> (args)...));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-19 17:16:57 +11:00
|
|
|
namespace cruft::align {
|
2015-11-17 17:21:14 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2017-08-30 15:38:13 +10:00
|
|
|
/// round the pointer upwards to satisfy the provided alignment
|
2018-12-17 14:45:54 +11:00
|
|
|
constexpr inline uintptr_t
|
2018-12-19 17:16:57 +11:00
|
|
|
up (uintptr_t ptr, size_t alignment)
|
2015-11-17 17:21:14 +11:00
|
|
|
{
|
2017-08-30 15:38:13 +10:00
|
|
|
// we perform this as two steps to avoid unnecessarily incrementing when
|
|
|
|
// remainder is zero.
|
2018-12-17 14:45:54 +11:00
|
|
|
if (auto mod = ptr % alignment; mod)
|
|
|
|
ptr += alignment - mod;
|
|
|
|
return ptr;
|
2015-11-17 17:21:14 +11:00
|
|
|
}
|
2016-03-11 09:40:12 +11:00
|
|
|
|
|
|
|
|
2018-03-02 12:15:43 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2017-08-30 15:38:13 +10:00
|
|
|
/// round the pointer upwards to satisfy the provided alignment
|
2018-12-17 14:45:54 +11:00
|
|
|
template <typename T>
|
|
|
|
constexpr T*
|
2018-12-19 17:16:57 +11:00
|
|
|
up (T *ptr, size_t alignment)
|
2016-03-11 09:40:12 +11:00
|
|
|
{
|
2017-08-30 15:38:13 +10:00
|
|
|
// we perform this as two steps to avoid unnecessarily incrementing when
|
|
|
|
// remainder is zero.
|
2018-12-17 14:45:54 +11:00
|
|
|
return reinterpret_cast<T*>(
|
2018-12-19 17:16:57 +11:00
|
|
|
up (reinterpret_cast<uintptr_t> (ptr), alignment)
|
2018-12-17 14:45:54 +11:00
|
|
|
);
|
2017-08-30 15:38:13 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-19 17:16:57 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
constexpr T*
|
|
|
|
up (T *ptr, std::align_val_t _alignment)
|
|
|
|
{
|
|
|
|
auto alignment = std::underlying_type_t<decltype(_alignment)> (_alignment);
|
|
|
|
return up (ptr, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-30 15:38:13 +10:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// round the pointer upwards to the nearest valid alignment for T
|
|
|
|
template <typename T>
|
|
|
|
constexpr auto
|
2018-12-19 17:16:57 +11:00
|
|
|
up (T *t)
|
2017-08-30 15:38:13 +10:00
|
|
|
{
|
2018-12-19 17:16:57 +11:00
|
|
|
return up (t, alignof (T));
|
2017-08-30 15:38:13 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// round the pointer upwards to the nearest valid alignment for T
|
|
|
|
template <typename T>
|
|
|
|
constexpr auto
|
2018-12-19 17:16:57 +11:00
|
|
|
up (uintptr_t ptr)
|
2017-08-30 15:38:13 +10:00
|
|
|
{
|
2018-12-19 17:16:57 +11:00
|
|
|
return up (ptr, alignof (T));
|
2018-12-17 14:45:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
constexpr inline uintptr_t
|
2018-12-19 17:16:57 +11:00
|
|
|
down (uintptr_t ptr, size_t alignment)
|
2018-12-17 14:45:54 +11:00
|
|
|
{
|
|
|
|
return ptr - ptr % alignment;
|
2016-03-11 09:40:12 +11:00
|
|
|
}
|
2015-11-17 17:21:14 +11:00
|
|
|
|
2018-12-17 14:45:54 +11:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
constexpr T*
|
2018-12-19 17:16:57 +11:00
|
|
|
down (T *ptr, size_t alignment)
|
2018-12-17 14:45:54 +11:00
|
|
|
{
|
|
|
|
return reinterpret_cast<T*> (
|
2018-12-19 17:16:57 +11:00
|
|
|
down (reinterpret_cast<uintptr_t> (ptr), alignment)
|
2018-12-17 14:45:54 +11:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|