/** * @file include/ActivMiddleware/Misc/MemoryHelper.h * * @brief Header file for the memory helper functions. */ #if (!defined ACTIV_MISC_MEMORY_HELPER_H) #define ACTIV_MISC_MEMORY_HELPER_H #include "ActivMiddleware/ActivMiddleware.h" #include //! Activ namespace. namespace Activ { /** * @brief Get built-in type value from a (potentially) unaligned pointer. * * @param p pointer to copy from. * * @return Value at location p. */ template inline T GetValueFromUnalignedPointer(const void *p) { #if (defined ACTIV_REQUIRE_ALIGNED_MEMORY_ACCESS) T tmp; // NB not using MemoryCopy() to allow special compiler optimizations ::memcpy(&tmp, p, sizeof(T)); return tmp; #else return *reinterpret_cast(p); #endif } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Copy a built-in type value to a (potentially) unaligned pointer. * * @param p pointer to copy to. * @param value value to copy. */ template inline void CopyValueToUnalignedPointer(void *p, const T &value) { #if (defined ACTIV_REQUIRE_ALIGNED_MEMORY_ACCESS) // NB not using MemoryCopy() to allow special compiler optimizations ::memcpy(p, &value, sizeof(T)); #else *reinterpret_cast(p) = value; #endif } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Compile-time template to align a value to an arbitrary byte boundary. */ template class StaticAlignValueToByteBoundary { // Doesn't make sense to pass a signed type. ACTIV_BOOST_STATIC_ASSERT(Boost::is_signed::value == false); typedef typename TypeToIntegralType::Type IntegralType; static const IntegralType integralValue = static_cast(input); static const IntegralType integralNumberOfBytes = static_cast(numberOfBytes); public: static const T value = (((integralValue - 1) / integralNumberOfBytes) + 1) * integralNumberOfBytes; }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Align a value to a power of two byte boundary. * * @tparam T input type. * @tparam numberOfBytes the byte alignment. * * @param value * * @return The value rounded up to a @a numberOfBytes boundary. */ template inline T AlignValueToPowerOfTwoByteBoundary(const T value) { // Doesn't make sense to pass a signed type. ACTIV_BOOST_STATIC_ASSERT(Boost::is_signed::value == false); ACTIV_BOOST_STATIC_ASSERT(StaticIsPowerOfTwo::result); return (value + T(numberOfBytes - 1)) & ~T(numberOfBytes - 1); } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Align a value to an arbitrary byte boundary. * * @param value * @param numberOfBytes * * @return The value rounded up to a @a numberOfBytes boundary. */ template inline T AlignValueToByteBoundary(const T value, const size_t numberOfBytes) { // Doesn't make sense to pass a signed type. ACTIV_BOOST_STATIC_ASSERT(Boost::is_signed::value == false); typedef typename TypeToIntegralType::Type IntegralType; // T may be a pointer, so use integral arithmetic. const IntegralType integralValue = static_cast(value); if (0 == integralValue) return value; return static_cast((((integralValue - 1) / static_cast(numberOfBytes)) + 1) * static_cast(numberOfBytes)); } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Compile-time template to align a value to a processor word size boundary. */ template class StaticAlignValueToProcessorWordBoundary : public StaticAlignValueToByteBoundary { }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Align a value to a processor word size boundary. * * @param value * * @return The value rounded up to a processor word size boundary. */ template inline T AlignValueToProcessorWordBoundary(const T value) { return AlignValueToPowerOfTwoByteBoundary(value); } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Asymmetrical memory swap. * * @param pBuffer pointer to memory to reverse. * @param bufferSize number of bytes to reverse. */ inline void MemoryReverse(void *pBuffer, size_t bufferSize) { for (byte_t *ptr = static_cast(pBuffer) + bufferSize - 1; static_cast(pBuffer) < ptr; pBuffer = (static_cast(pBuffer)) + sizeof(byte_t), --ptr) { *static_cast(pBuffer) ^= *ptr; *ptr ^= *static_cast(pBuffer); *static_cast(pBuffer) ^= *ptr; } } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Asymmetrical memory copy. * * @param pDestination pointer to target to copy to. * @param pSource pointer to source to copy from. * @param sourceSize number of bytes to copy from @a pSource to @a pDestination. */ inline void MemoryReverseCopy(void *pDestination, const void *pSource, size_t sourceSize) { for (size_t i = 0; i < sourceSize; ++i) { static_cast(pDestination)[i] = static_cast(pSource)[sourceSize - i - 1]; } } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Fast memory copy. * * Note: When @a sourceSize is known at compile time, it will most likely be better to use memcpy() directly as the compiler * can optimize better (see CopyValueToUnalignedPointer(), for instance). * * @param pDestination pointer to target to copy to. * @param pSource pointer to source to copy from. * @param sourceSize number of bytes to copy from @a pSource to @a pDestination. */ inline void MemoryCopy(void *pDestination, const void *pSource, size_t sourceSize) { // HACK magic numbers where bytewise copy becomes slower than memcpy. #if (defined ACTIV_PROCESSOR_X86) #if (defined ACTIV_COMPILER_VISUAL_C) #define ACTIV_MEMORY_HELPER_MEMORY_COPY_LIMIT 10 #elif (defined ACTIV_COMPILER_GCC) || (defined ACTIV_COMPILER_SUNPRO) #define ACTIV_MEMORY_HELPER_MEMORY_COPY_LIMIT 20 #else #error Configure for this compiler on x86 #endif // compilers for x86 // memcpy is as fast as bytewise with x86-64. #elif (defined ACTIV_PROCESSOR_X86_64) || (defined ACTIV_PROCESSOR_SPARC) || (defined ACTIV_PROCESSOR_SPARC64) || \ (defined ACTIV_PROCESSOR_ARM) || (defined ACTIV_PROCESSOR_WASM) #elif (defined ACTIV_PROCESSOR_IA64) #if (defined ACTIV_COMPILER_VISUAL_C) #define ACTIV_MEMORY_HELPER_MEMORY_COPY_LIMIT 14 #elif (defined ACTIV_COMPILER_GCC) #define ACTIV_MEMORY_HELPER_MEMORY_COPY_LIMIT 5 #else #error Configure for this compiler on ia64 #endif #elif (defined ACTIV_PROCESSOR_PPC) #if (defined ACTIV_COMPILER_GCC) #define ACTIV_MEMORY_HELPER_MEMORY_COPY_LIMIT 10 #else #error Configure for this compiler on ppc #endif #else #error Configure for this processor #endif // processors #if (defined ACTIV_MEMORY_HELPER_MEMORY_COPY_LIMIT) if (sourceSize >= ACTIV_MEMORY_HELPER_MEMORY_COPY_LIMIT) { ::memcpy(pDestination, pSource, sourceSize); } else { for (size_t i = 0; i < sourceSize; ++i) { static_cast(pDestination)[i] = static_cast(pSource)[i]; } } #else ::memcpy(pDestination, pSource, sourceSize); #endif } } // namespace Activ #endif // (!defined ACTIV_MISC_MEMORY_HELPER_H)