42 #include <type_traits>
53 template <
typename Type>
56 template <typename Type>
83 template <typename Type>
103 template <
typename Type>
105 return alignof(Type);
121 #if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
123 #elif defined(__aarch64__) || defined(_M_ARM64) || defined(__arm__) || defined(_M_ARM)
125 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)
167 template <
typename Type>
170 static constexpr
size_t size =
sizeof(Type);
173 static constexpr
size_t alignment =
alignof(Type);
179 static constexpr
bool is_trivially_copyable = std::is_trivially_copyable_v<Type>;
182 static constexpr
bool is_standard_layout = std::is_standard_layout_v<Type>;
185 static constexpr
bool is_pod = is_trivially_copyable && is_standard_layout;
188 static constexpr
bool is_empty = std::is_empty_v<Type>;
191 static constexpr
bool is_fundamental = std::is_fundamental_v<Type>;
194 static constexpr
bool is_integral = std::is_integral_v<Type>;
197 static constexpr
bool is_floating_point = std::is_floating_point_v<Type>;
200 static constexpr
bool is_pointer = std::is_pointer_v<Type>;
203 static constexpr
bool is_array = std::is_array_v<Type>;
229 template <
typename Type>
231 return detail::calculateTypePadding<Type>();
253 template <
typename Type>
255 return detail::hasTypePadding<Type>();
275 template <
size_t TAlignment>
277 static_assert(TAlignment > 0,
"Alignment must be greater than zero");
278 static_assert((TAlignment & (TAlignment - 1)) == 0,
"Alignment must be a power of two");
281 alignas(TAlignment)
char data[TAlignment];
284 void*
get() noexcept {
return data; }
287 const void*
get() const noexcept {
return data; }
290 template <
typename T>
292 static_assert(
alignof(T) <= TAlignment,
"Type alignment exceeds storage alignment");
293 return reinterpret_cast<T*
>(data);
297 template <
typename T>
298 const T*
as() const noexcept {
299 static_assert(
alignof(T) <= TAlignment,
"Type alignment exceeds storage alignment");
300 return reinterpret_cast<const T*
>(data);
354 template <
typename Type,
size_t TExpectedSize>
356 return sizeof(Type) == TExpectedSize;
375 template <
typename Type,
size_t TExpectedAlignment>
377 return alignof(Type) == TExpectedAlignment;
388 template <
typename Type>
401 template <
typename Type>
419 constexpr
size_t alignedSize(
size_t size,
size_t alignment) noexcept {
420 return (size + alignment - 1) & ~(alignment - 1);
433 return (addr + alignment - 1) & ~(alignment - 1);
443 constexpr
bool isAligned(uintptr_t addr,
size_t alignment) noexcept {
444 return (addr & (alignment - 1)) == 0;
456 inline bool isAligned(
const void* ptr,
size_t alignment) noexcept {
457 return isAligned(
reinterpret_cast<uintptr_t
>(ptr), alignment);
474 template <
typename Type>
476 if constexpr (std::is_fundamental_v<Type> || std::is_pointer_v<Type>) {
479 }
else if constexpr (std::is_empty_v<Type>) {
486 constexpr
size_t type_size =
sizeof(Type);
487 constexpr
size_t type_alignment =
alignof(Type);
490 return (type_alignment - (type_size % type_alignment)) % type_alignment;
497 template <
typename Type>
499 return calculateTypePadding<Type>() > 0;
512 #if defined(_WIN32) || defined(_WIN64)
515 #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
519 #elif defined(__sparc__)
522 #elif defined(__ia64__)
546 #define TRLC_ALIGNOF(type) (alignof(type))
554 #define TRLC_SIZEOF(type) (sizeof(type))
561 #define TRLC_CACHE_LINE_SIZE (trlc::platform::getCacheLineSize())
568 #define TRLC_PAGE_SIZE (trlc::platform::getPageSize())
585 #define TRLC_ALIGN_TO_CACHE_LINE alignas(trlc::platform::getCacheLineSize())
593 #define TRLC_ALIGN_TO_PAGE alignas(trlc::platform::getPageSize())
613 #define TRLC_PAGE_ALIGNED(type, name) TRLC_ALIGN_TO_PAGE type name
621 "CacheLineAligned should be at least 64-byte aligned");
623 "PageAligned should be at least 4096-byte aligned");
626 static_assert(trlc::platform::getTypeSize<char>() == 1,
"char should be 1 byte");
627 static_assert(trlc::platform::getTypeAlignment<char>() == 1,
"char should be 1-byte aligned");
630 static_assert(trlc::platform::verifyTypeSize<char, 1>(),
"Type size verification should work");
631 static_assert(trlc::platform::verifyTypeAlignment<char, 1>(),
632 "Type alignment verification should work");
636 "TypeInfo size should match sizeof");
638 "TypeInfo alignment should match alignof");
642 "int should not be floating point");