mirror of
https://github.com/MariaDB/server.git
synced 2025-08-08 11:22:35 +03:00
fix aligned memcpy()-like functions usage
I found that memcpy_aligned was used incorrectly at redo log and decided to put assertions in aligned functions. And found even more incorrect cases. Given the amount discovered of bugs, I left assertions to prevent future bugs. my_assume_aligned(): instead of MY_ASSUME_ALIGNED macro
This commit is contained in:
@@ -197,37 +197,50 @@ extern ulonglong strtoull(const char *str, char **ptr, int base);
|
||||
}
|
||||
|
||||
# ifdef _MSC_VER
|
||||
# define MY_ASSUME_ALIGNED(x,n) x
|
||||
template <size_t Alignment, class T> static inline T my_assume_aligned(T ptr)
|
||||
{
|
||||
DBUG_ASSERT(reinterpret_cast<size_t>(ptr) % Alignment == 0);
|
||||
__assume(reinterpret_cast<size_t>(ptr) % Alignment == 0);
|
||||
return ptr;
|
||||
}
|
||||
# else
|
||||
# define MY_ASSUME_ALIGNED(x,n) __builtin_assume_aligned(x,n)
|
||||
template <size_t Alignment, class T> static inline T my_assume_aligned(T ptr)
|
||||
{
|
||||
DBUG_ASSERT(reinterpret_cast<size_t>(ptr) % Alignment == 0);
|
||||
return static_cast<T>(__builtin_assume_aligned(ptr, Alignment));
|
||||
}
|
||||
# endif
|
||||
|
||||
template <size_t Alignment>
|
||||
inline void *memcpy_aligned(void *dest, const void *src, size_t n)
|
||||
{
|
||||
static_assert(Alignment && !(Alignment & (Alignment - 1)), "power of 2");
|
||||
return memcpy(MY_ASSUME_ALIGNED(dest, Alignment),
|
||||
MY_ASSUME_ALIGNED(src, Alignment), n);
|
||||
|
||||
return memcpy(my_assume_aligned<Alignment>(dest),
|
||||
my_assume_aligned<Alignment>(src), n);
|
||||
}
|
||||
template <size_t Alignment>
|
||||
inline void *memmove_aligned(void *dest, const void *src, size_t n)
|
||||
{
|
||||
static_assert(Alignment && !(Alignment & (Alignment - 1)), "power of 2");
|
||||
return memmove(MY_ASSUME_ALIGNED(dest, Alignment),
|
||||
MY_ASSUME_ALIGNED(src, Alignment), n);
|
||||
|
||||
return memmove(my_assume_aligned<Alignment>(dest),
|
||||
my_assume_aligned<Alignment>(src), n);
|
||||
}
|
||||
template <size_t Alignment>
|
||||
inline int memcmp_aligned(const void *s1, const void *s2, size_t n)
|
||||
{
|
||||
static_assert(Alignment && !(Alignment & (Alignment - 1)), "power of 2");
|
||||
return memcmp(MY_ASSUME_ALIGNED(s1, Alignment),
|
||||
MY_ASSUME_ALIGNED(s2, Alignment), n);
|
||||
|
||||
return memcmp(my_assume_aligned<Alignment>(s1),
|
||||
my_assume_aligned<Alignment>(s2), n);
|
||||
}
|
||||
template <size_t Alignment>
|
||||
inline void *memset_aligned(void *s, int c, size_t n)
|
||||
{
|
||||
static_assert(Alignment && !(Alignment & (Alignment - 1)), "power of 2");
|
||||
return memset(MY_ASSUME_ALIGNED(s, Alignment), c, n);
|
||||
|
||||
return memset(my_assume_aligned<Alignment>(s), c, n);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user