ClickHouse/libs/libcommon/include/common/unaligned.h
Alexander Kuzmenkov 0116c10e41 Require explicit type in unalignedStore
This is a follow-up to PR #5786, which fixed a segfault caused by
an unexpected deduced type for unalignedStore. To prevent future errors
of this kind, require a caller to specify the stored type explicitly.
2019-06-28 20:33:14 +03:00

26 lines
742 B
C++

#pragma once
#include <string.h>
#include <type_traits>
template <typename T>
inline T unalignedLoad(const void * address)
{
T res {};
memcpy(&res, address, sizeof(res));
return res;
}
/// We've had troubles before with wrong store size due to integral promotions
/// (e.g., unalignedStore(dest, uint16_t + uint16_t) stores an uint32_t).
/// To prevent this, make the caller specify the stored type explicitly.
/// To disable deduction of T, wrap the argument type with std::enable_if.
template <typename T>
inline void unalignedStore(void * address,
const typename std::enable_if<true, T>::type & src)
{
static_assert(std::is_trivially_copyable_v<T>);
memcpy(address, &src, sizeof(src));
}