/* * Copyright (c) 2017, Intel Corporation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ #pragma once #include "runtime/helpers/debug_helpers.h" #include "runtime/os_interface/debug_settings_manager.h" #include "runtime/memory_manager/memory_constants.h" #include #include #include #include #ifdef _MSC_VER #define ALIGNAS(x) __declspec(align(x)) #else #define ALIGNAS(x) alignas(x) #endif template constexpr inline T alignUp(T before, size_t alignment) { return static_cast((static_cast(before) + alignment - 1) & ~(alignment - 1)); } template constexpr inline T *alignUp(T *ptrBefore, size_t alignment) { return reinterpret_cast(alignUp(reinterpret_cast(ptrBefore), alignment)); } template constexpr inline T alignDown(T before, size_t alignment) { return static_cast(static_cast(before) & ~(alignment - 1)); } 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); } 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(T *ptr) { return (reinterpret_cast(ptr) & (alignof(T) - 1)) == 0; }