all: minor cleanups

This commit is contained in:
Markus F.X.J. Oberhumer
2024-05-28 10:26:39 +02:00
parent b0ce072370
commit 3495d1affd
17 changed files with 136 additions and 113 deletions
+15 -15
View File
@@ -141,6 +141,17 @@ private:
#define UPX_CXX_DISABLE_NEW_DELETE_NO_VIRTUAL(Klass) private:
#endif
class noncopyable {
protected:
forceinline constexpr noncopyable() noexcept {}
#if __cplusplus >= 202002L
forceinline constexpr ~noncopyable() noexcept = default;
#else
forceinline ~noncopyable() noexcept = default;
#endif
UPX_CXX_DISABLE_COPY_MOVE(noncopyable)
};
/*************************************************************************
// <type_traits>
**************************************************************************/
@@ -178,21 +189,21 @@ forceinline constexpr bool has_single_bit(T x) noexcept {
template <class T>
inline constexpr T align_down(const T &x, const T &alignment) noexcept {
// assert_noexcept(has_single_bit(alignment));
// assert_noexcept(has_single_bit(alignment)); // (not constexpr)
T r = {};
r = (x / alignment) * alignment;
return r;
}
template <class T>
inline constexpr T align_up(const T &x, const T &alignment) noexcept {
// assert_noexcept(has_single_bit(alignment));
// assert_noexcept(has_single_bit(alignment)); // (not constexpr)
T r = {};
r = ((x + (alignment - 1)) / alignment) * alignment;
return r;
}
template <class T>
inline constexpr T align_gap(const T &x, const T &alignment) noexcept {
// assert_noexcept(has_single_bit(alignment));
// assert_noexcept(has_single_bit(alignment)); // (not constexpr)
T r = {};
r = align_up(x, alignment) - x;
return r;
@@ -209,7 +220,7 @@ forceinline constexpr T max(const T &a, const T &b) noexcept {
template <class T>
inline constexpr bool is_uminmax_type =
is_same_any_v<T, upx_uint16_t, upx_uint32_t, upx_uint64_t, unsigned long, size_t>;
is_same_any_v<T, upx_uint8_t, upx_uint16_t, upx_uint32_t, upx_uint64_t, unsigned long, size_t>;
template <class T, class = std::enable_if_t<is_uminmax_type<T>, T> >
forceinline constexpr T umin(const T &a, const T &b) noexcept {
@@ -343,17 +354,6 @@ struct MallocDeleter final {
}
};
class noncopyable {
protected:
forceinline constexpr noncopyable() noexcept {}
#if __cplusplus >= 202002L
forceinline constexpr ~noncopyable() noexcept = default;
#else
forceinline ~noncopyable() noexcept = default;
#endif
UPX_CXX_DISABLE_COPY_MOVE(noncopyable)
};
/*************************************************************************
// compile_time
**************************************************************************/
+4 -3
View File
@@ -93,9 +93,9 @@ T *NewArray(upx_uint64_t n) may_throw {
// ptr util
**************************************************************************/
// also see CHERI cheri_address_get()
forceinline upx_ptraddr_t ptr_get_address(const void *p) { return (upx_uintptr_t) p; }
forceinline upx_ptraddr_t ptr_get_address(upx_uintptr_t p) { return p; }
// TODO later: CHERI; see cheri_address_get()
forceinline upx_ptraddr_t ptr_get_address(const void *p) noexcept { return (upx_uintptr_t) p; }
forceinline upx_ptraddr_t ptr_get_address(upx_uintptr_t p) noexcept { return p; }
// ptrdiff_t with nullptr checks and asserted size; will throw on failure
// NOTE: returns size_in_bytes, not number of elements!
@@ -138,6 +138,7 @@ forceinline void ptr_check_no_overlap(const void *a, size_t a_size, const void *
// - this should play nice with static analyzers like clang-tidy etc.
// NOTE: this is clearly UB (Undefined Behaviour), and stricter compilers or
// architectures may need a more advanced/costly implementation in the future
// TODO later: CHERI
template <class T>
inline void ptr_invalidate_and_poison(T *(&ptr)) noexcept {
ptr = (T *) (void *) 251; // 0x000000fb // NOLINT(performance-no-int-to-ptr)