/* * Copyright (C) 2018-2025 Intel Corporation * * SPDX-License-Identifier: MIT * */ #pragma once #include "shared/source/helpers/basic_math.h" #include "shared/source/helpers/constants.h" #include "shared/source/helpers/debug_helpers.h" #include "shared/source/utilities/logger.h" #include #include #include #include #ifdef _MSC_VER #define ALIGNAS(x) __declspec(align(x)) #else #define ALIGNAS(x) alignas(x) #endif template ::type> constexpr inline TNoRef alignUp(T before, size_t alignment) { OPTIONAL_UNRECOVERABLE_IF(!Math::isPow2(alignment)); TNoRef mask = static_cast(alignment - 1); return (before + mask) & ~mask; } template ::type> constexpr inline TNoRef alignUpNonZero(T before, size_t alignment) { if (before == 0) { return alignment; } else { return alignUp(before, alignment); } } template constexpr inline T *alignUp(T *ptrBefore, size_t alignment) { return reinterpret_cast(alignUp(reinterpret_cast(ptrBefore), alignment)); } template ::type> constexpr inline TNoRef alignDown(T before, size_t alignment) { OPTIONAL_UNRECOVERABLE_IF(!Math::isPow2(alignment)); TNoRef mask = static_cast(alignment - 1); return before & ~mask; } template constexpr inline T *alignDown(T *ptrBefore, size_t alignment) { return reinterpret_cast(alignDown(reinterpret_cast(ptrBefore), alignment)); } inline void *alignedMalloc(size_t bytes, size_t alignment) { DEBUG_BREAK_IF(alignment <= 0); if (bytes == 0) { bytes = sizeof(void *); } // Make sure our alignment is at least the size of a pointer alignment = std::max(alignment, sizeof(void *)); // Allocate _bytes + _alignment size_t sizeToAlloc = bytes + alignment; auto pOriginalMemory = new (std::nothrow) char[sizeToAlloc]; // Add in the alignment auto pAlignedMemory = reinterpret_cast(pOriginalMemory); if (pAlignedMemory) { pAlignedMemory += alignment; pAlignedMemory -= pAlignedMemory % alignment; // Store the original pointer to facilitate deallocation reinterpret_cast(pAlignedMemory)[-1] = pOriginalMemory; } DBG_LOG(LogAlignedAllocations, __FUNCTION__, "Pointer:", reinterpret_cast(pOriginalMemory), "size:", sizeToAlloc); // Return result return reinterpret_cast(pAlignedMemory); // NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks) } inline void alignedFree(void *ptr) { if (ptr) { auto originalPtr = reinterpret_cast(ptr)[-1]; DBG_LOG(LogAlignedAllocations, __FUNCTION__, "Pointer:", reinterpret_cast(originalPtr)); delete[] originalPtr; } } inline size_t alignSizeWholePage(const void *ptr, size_t size) { uintptr_t startPageMisalignedAddressOffset = reinterpret_cast(ptr) & MemoryConstants::pageMask; size_t alignedSizeToPage = alignUp(startPageMisalignedAddressOffset + size, MemoryConstants::pageSize); return alignedSizeToPage; } template inline constexpr bool isAligned(T val) { return (static_cast(val) % alignment) == 0; } template inline bool isAligned(T *ptr) { return ((reinterpret_cast(ptr)) % alignment) == 0; } template inline bool isAligned(T1 ptr, T2 alignment) { OPTIONAL_UNRECOVERABLE_IF(!Math::isPow2(alignment)); return ((static_cast(ptr)) & (static_cast(alignment) - 1u)) == 0; } template inline bool isAligned(T *ptr) { // alignment requirement (returned by alignof) is always a power of 2 return (reinterpret_cast(ptr) & (alignof(T) - 1)) == 0; } inline auto allocateAlignedMemory(size_t bytes, size_t alignment) { return std::unique_ptr>(alignedMalloc(bytes, alignment), alignedFree); }