#pragma once //////////////////////////////////////////////////////////////////////////////// // The MIT License (MIT) // // Copyright (c) 2017 Nicholas Frechette & Animation Compression Library contributors // Copyright (c) 2018 Nicholas Frechette & Realtime Math contributors // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. //////////////////////////////////////////////////////////////////////////////// #include "rtm/impl/compiler_utils.h" #include "rtm/impl/error.h" #include #include #include #include #include #include ////////////////////////////////////////////////////////////////////////// // This file contains various memory related utility functions and other misc helpers. // Everything is hidden in an impl namespace even though they could be useful and used // by anyone only to avoid polluting the rtm namespace. If these become useful enough, // they might be moved into their own external dependency. ////////////////////////////////////////////////////////////////////////// RTM_IMPL_FILE_PRAGMA_PUSH namespace rtm { namespace rtm_impl { ////////////////////////////////////////////////////////////////////////// // Allows static branching without any warnings ////////////////////////////////////////////////////////////////////////// template struct static_condition { static constexpr bool test() RTM_NO_EXCEPT { return true; } }; template<> struct static_condition { static constexpr bool test() RTM_NO_EXCEPT { return false; } }; ////////////////////////////////////////////////////////////////////////// // Returns true if the input is a power of two, false otherwise. ////////////////////////////////////////////////////////////////////////// RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE constexpr bool is_power_of_two(size_t input) RTM_NO_EXCEPT { return input != 0 && (input & (input - 1)) == 0; } ////////////////////////////////////////////////////////////////////////// // Returns true if the alignment provided satisfies the requirement for the provided Type, false otherwise. ////////////////////////////////////////////////////////////////////////// template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE constexpr bool is_alignment_valid(size_t alignment) RTM_NO_EXCEPT { return is_power_of_two(alignment) && alignment >= alignof(Type); } ////////////////////////////////////////////////////////////////////////// // Returns true if the pointer provided satisfies the specified alignment, false otherwise. ////////////////////////////////////////////////////////////////////////// template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE bool is_aligned_to(PtrType* value, size_t alignment) RTM_NO_EXCEPT { RTM_ASSERT(is_power_of_two(alignment), "Alignment value must be a power of two"); return (reinterpret_cast(value) & (alignment - 1)) == 0; } ////////////////////////////////////////////////////////////////////////// // Returns true if the integral value provided satisfies the specified alignment, false otherwise. ////////////////////////////////////////////////////////////////////////// template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE bool is_aligned_to(IntegralType value, size_t alignment) RTM_NO_EXCEPT { RTM_ASSERT(is_power_of_two(alignment), "Alignment value must be a power of two"); return (static_cast(value) & (alignment - 1)) == 0; } ////////////////////////////////////////////////////////////////////////// // Returns true if the provided pointer satisfies the alignment of PtrType, false otherwise. ////////////////////////////////////////////////////////////////////////// template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE constexpr bool is_aligned(PtrType* value) RTM_NO_EXCEPT { return is_aligned_to(value, alignof(PtrType)); } ////////////////////////////////////////////////////////////////////////// // The input pointer is rounded up to the desired alignment. ////////////////////////////////////////////////////////////////////////// template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE PtrType* align_to(PtrType* value, size_t alignment) RTM_NO_EXCEPT { RTM_ASSERT(is_power_of_two(alignment), "Alignment value must be a power of two"); return reinterpret_cast((reinterpret_cast(value) + (alignment - 1)) & ~(alignment - 1)); } ////////////////////////////////////////////////////////////////////////// // The input integral value is rounded up to the desired alignment. ////////////////////////////////////////////////////////////////////////// template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE IntegralType align_to(IntegralType value, size_t alignment) RTM_NO_EXCEPT { RTM_ASSERT(is_power_of_two(alignment), "Alignment value must be a power of two"); return static_cast((static_cast(value) + (alignment - 1)) & ~(alignment - 1)); } ////////////////////////////////////////////////////////////////////////// // Returns the array size for the provided array. ////////////////////////////////////////////////////////////////////////// template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE constexpr size_t get_array_size(ElementType const (&)[num_elements]) RTM_NO_EXCEPT { return num_elements; } ////////////////////////////////////////////////////////////////////////// // Type safe casting ////////////////////////////////////////////////////////////////////////// template struct safe_ptr_to_ptr_cast_impl { RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE static DestPtrType* cast(SrcType* input) RTM_NO_EXCEPT { RTM_ASSERT(is_aligned_to(input, alignof(DestPtrType)), "reinterpret_cast would result in an unaligned pointer"); return reinterpret_cast(input); } }; template struct safe_ptr_to_ptr_cast_impl { RTM_DISABLE_SECURITY_COOKIE_CHECK static RTM_FORCE_INLINE constexpr void* cast(SrcType* input) RTM_NO_EXCEPT { return input; } }; template struct safe_int_to_ptr_cast_impl { RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE static DestPtrType* cast(SrcType input) RTM_NO_EXCEPT { RTM_ASSERT(is_aligned_to(input, alignof(DestPtrType)), "reinterpret_cast would result in an unaligned pointer"); return reinterpret_cast(input); } }; template struct safe_int_to_ptr_cast_impl { RTM_DISABLE_SECURITY_COOKIE_CHECK static RTM_FORCE_INLINE constexpr void* cast(SrcType input) RTM_NO_EXCEPT { return reinterpret_cast(input); } }; template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE DestPtrType* safe_ptr_cast(SrcType* input) RTM_NO_EXCEPT { return safe_ptr_to_ptr_cast_impl::cast(input); } template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE DestPtrType* safe_ptr_cast(SrcType input) RTM_NO_EXCEPT { return safe_int_to_ptr_cast_impl::cast(input); } #if defined(RTM_COMPILER_GCC) // GCC sometimes complains about comparisons being always true due to partial template // evaluation. Disable that warning since we know it is safe. #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtype-limits" #endif template struct safe_underlying_type { using type = typename std::underlying_type::type; }; template struct safe_underlying_type { using type = Type; }; template struct is_static_cast_safe_s { RTM_DISABLE_SECURITY_COOKIE_CHECK static RTM_FORCE_INLINE bool test(SrcType input) RTM_NO_EXCEPT { using SrcRealType = typename safe_underlying_type::value>::type; if (static_condition<(std::is_signed::value == std::is_signed::value)>::test()) return SrcType(DstType(input)) == input; else if (static_condition<(std::is_signed::value)>::test()) return int64_t(input) >= 0 && SrcType(DstType(input)) == input; else return uint64_t(input) <= uint64_t(std::numeric_limits::max()); }; }; template struct is_static_cast_safe_s { RTM_DISABLE_SECURITY_COOKIE_CHECK static RTM_FORCE_INLINE bool test(SrcType input) RTM_NO_EXCEPT { return SrcType(DstType(input)) == input; } }; template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE bool is_static_cast_safe(SrcType input) RTM_NO_EXCEPT { // TODO: In C++17 this should be folded to constexpr if return is_static_cast_safe_s::value || std::is_floating_point::value)>::test()>::test(input); } template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE DstType safe_static_cast(SrcType input) RTM_NO_EXCEPT { #if defined(RTM_HAS_ASSERT_CHECKS) const bool is_safe = is_static_cast_safe(input); RTM_ASSERT(is_safe, "Unsafe static cast resulted in data loss"); #endif return static_cast(input); } #if defined(RTM_COMPILER_GCC) #pragma GCC diagnostic pop #endif ////////////////////////////////////////////////////////////////////////// // Reads a DataType from the input pointer regardless of its alignment. ////////////////////////////////////////////////////////////////////////// template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE DataType unaligned_read(const void* input) RTM_NO_EXCEPT { DataType result; std::memcpy(&result, input, sizeof(DataType)); return result; } ////////////////////////////////////////////////////////////////////////// // Reads a DataType from the input pointer with an alignment check. ////////////////////////////////////////////////////////////////////////// template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE DataType aligned_read(const void* input) RTM_NO_EXCEPT { return *safe_ptr_cast(input); } ////////////////////////////////////////////////////////////////////////// // Writes a DataType into the output pointer regardless of its alignment. ////////////////////////////////////////////////////////////////////////// template RTM_DISABLE_SECURITY_COOKIE_CHECK RTM_FORCE_INLINE void unaligned_write(DataType input, void* output) RTM_NO_EXCEPT { std::memcpy(output, &input, sizeof(DataType)); } } } RTM_IMPL_FILE_PRAGMA_POP