diff --git a/include/assume_aligned.h b/include/assume_aligned.h new file mode 100644 index 00000000000..89f88c1b728 --- /dev/null +++ b/include/assume_aligned.h @@ -0,0 +1,70 @@ +/* + Copyright (c) 2000, 2012, Oracle and/or its affiliates. + Copyright (c) 2020, MariaDB Corporation. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ + +#pragma once + +#include +#include + +#ifdef _MSC_VER +template +static inline T my_assume_aligned(T ptr) +{ + assert(reinterpret_cast(ptr) % Alignment == 0); + __assume(reinterpret_cast(ptr) % Alignment == 0); + return ptr; +} +#else +template +static inline T my_assume_aligned(T ptr) +{ + assert(reinterpret_cast(ptr) % Alignment == 0); + return static_cast(__builtin_assume_aligned(ptr, Alignment)); +} +#endif + +template +inline void *memcpy_aligned(void *dest, const void *src, size_t n) +{ + static_assert(Alignment && !(Alignment & (Alignment - 1)), "power of 2"); + + return std::memcpy(my_assume_aligned(dest), + my_assume_aligned(src), n); +} +template +inline void *memmove_aligned(void *dest, const void *src, size_t n) +{ + static_assert(Alignment && !(Alignment & (Alignment - 1)), "power of 2"); + + return std::memmove(my_assume_aligned(dest), + my_assume_aligned(src), n); +} +template +inline int memcmp_aligned(const void *s1, const void *s2, size_t n) +{ + static_assert(Alignment && !(Alignment & (Alignment - 1)), "power of 2"); + + return std::memcmp(my_assume_aligned(s1), + my_assume_aligned(s2), n); +} +template +inline void *memset_aligned(void *s, int c, size_t n) +{ + static_assert(Alignment && !(Alignment & (Alignment - 1)), "power of 2"); + + return std::memset(my_assume_aligned(s), c, n); +} diff --git a/include/m_string.h b/include/m_string.h index 9e786908dc6..e4c11d24c0b 100644 --- a/include/m_string.h +++ b/include/m_string.h @@ -195,53 +195,6 @@ extern ulonglong strtoull(const char *str, char **ptr, int base); #if defined(__cplusplus) } - -# ifdef _MSC_VER -template static inline T my_assume_aligned(T ptr) -{ - DBUG_ASSERT(reinterpret_cast(ptr) % Alignment == 0); - __assume(reinterpret_cast(ptr) % Alignment == 0); - return ptr; -} -# else -template static inline T my_assume_aligned(T ptr) -{ - DBUG_ASSERT(reinterpret_cast(ptr) % Alignment == 0); - return static_cast(__builtin_assume_aligned(ptr, Alignment)); -} -# endif - -template -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), - my_assume_aligned(src), n); -} -template -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), - my_assume_aligned(src), n); -} -template -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), - my_assume_aligned(s2), n); -} -template -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), c, n); -} #endif #include diff --git a/storage/innobase/buf/buf0buf.cc b/storage/innobase/buf/buf0buf.cc index 1e181872e87..7df8815d73f 100644 --- a/storage/innobase/buf/buf0buf.cc +++ b/storage/innobase/buf/buf0buf.cc @@ -31,6 +31,7 @@ The database buffer buf_pool Created 11/5/1995 Heikki Tuuri *******************************************************/ +#include "assume_aligned.h" #include "mtr0types.h" #include "mach0data.h" #include "buf0buf.h" diff --git a/storage/innobase/include/buf0flu.ic b/storage/innobase/include/buf0flu.ic index 6f57bcb6fc8..4a3ecb57938 100644 --- a/storage/innobase/include/buf0flu.ic +++ b/storage/innobase/include/buf0flu.ic @@ -24,6 +24,7 @@ The database buffer pool flush algorithm Created 11/5/1995 Heikki Tuuri *******************************************************/ +#include "assume_aligned.h" #include "buf0buf.h" #include "mtr0mtr.h" #include "srv0srv.h" diff --git a/storage/innobase/include/fsp0fsp.h b/storage/innobase/include/fsp0fsp.h index eef8f94020f..983b7e11c95 100644 --- a/storage/innobase/include/fsp0fsp.h +++ b/storage/innobase/include/fsp0fsp.h @@ -27,6 +27,7 @@ Created 12/18/1995 Heikki Tuuri #ifndef fsp0fsp_h #define fsp0fsp_h +#include "assume_aligned.h" #include "fsp0types.h" #include "fut0lst.h" #include "ut0byte.h" diff --git a/storage/innobase/log/log0crypt.cc b/storage/innobase/log/log0crypt.cc index 3bdc375b229..e22ef10f44b 100644 --- a/storage/innobase/log/log0crypt.cc +++ b/storage/innobase/log/log0crypt.cc @@ -27,6 +27,7 @@ MDEV-11782: Rewritten for MariaDB 10.2 by Marko Mäkelä, MariaDB Corporation. #include #include "log0crypt.h" #include +#include "assume_aligned.h" #include "log0crypt.h" #include "log0recv.h" // for recv_sys