/* Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2024 The Stockfish developers (see AUTHORS file) Stockfish 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, either version 3 of the License, or (at your option) any later version. Stockfish 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, see . */ #ifndef MEMORY_H_INCLUDED #define MEMORY_H_INCLUDED #include #include #include #include #include #include #include #include "types.h" namespace Stockfish { void* std_aligned_alloc(size_t alignment, size_t size); void std_aligned_free(void* ptr); // Memory aligned by page size, min alignment: 4096 bytes void* aligned_large_pages_alloc(size_t size); void aligned_large_pages_free(void* mem); // Frees memory which was placed there with placement new. // Works for both single objects and arrays of unknown bound. template void memory_deleter(T* ptr, FREE_FUNC free_func) { if (!ptr) return; // Explicitly needed to call the destructor if constexpr (!std::is_trivially_destructible_v) ptr->~T(); free_func(ptr); return; } // Frees memory which was placed there with placement new. // Works for both single objects and arrays of unknown bound. template void memory_deleter_array(T* ptr, FREE_FUNC free_func) { if (!ptr) return; // Move back on the pointer to where the size is allocated const size_t array_offset = std::max(sizeof(size_t), alignof(T)); char* raw_memory = reinterpret_cast(ptr) - array_offset; if constexpr (!std::is_trivially_destructible_v) { const size_t size = *reinterpret_cast(raw_memory); // Explicitly call the destructor for each element in reverse order for (size_t i = size; i-- > 0;) ptr[i].~T(); } free_func(raw_memory); } // Allocates memory for a single object and places it there with placement new template inline std::enable_if_t, T*> memory_allocator(ALLOC_FUNC alloc_func, Args&&... args) { void* raw_memory = alloc_func(sizeof(T)); ASSERT_ALIGNED(raw_memory, alignof(T)); return new (raw_memory) T(std::forward(args)...); } // Allocates memory for an array of unknown bound and places it there with placement new template inline std::enable_if_t, std::remove_extent_t*> memory_allocator(ALLOC_FUNC alloc_func, size_t num) { using ElementType = std::remove_extent_t; const size_t array_offset = std::max(sizeof(size_t), alignof(ElementType)); // Save the array size in the memory location char* raw_memory = reinterpret_cast(alloc_func(array_offset + num * sizeof(ElementType))); ASSERT_ALIGNED(raw_memory, alignof(T)); new (raw_memory) size_t(num); for (size_t i = 0; i < num; ++i) new (raw_memory + array_offset + i * sizeof(ElementType)) ElementType(); // Need to return the pointer at the start of the array so that // the indexing in unique_ptr works. return reinterpret_cast(raw_memory + array_offset); } // // // aligned large page unique ptr // // template struct LargePageDeleter { void operator()(T* ptr) const { return memory_deleter(ptr, aligned_large_pages_free); } }; template struct LargePageArrayDeleter { void operator()(T* ptr) const { return memory_deleter_array(ptr, aligned_large_pages_free); } }; template using LargePagePtr = std::conditional_t, std::unique_ptr>>, std::unique_ptr>>; // make_unique_large_page for single objects template std::enable_if_t, LargePagePtr> make_unique_large_page(Args&&... args) { static_assert(alignof(T) <= 4096, "aligned_large_pages_alloc() may fail for such a big alignment requirement of T"); T* obj = memory_allocator(aligned_large_pages_alloc, std::forward(args)...); return LargePagePtr(obj); } // make_unique_large_page for arrays of unknown bound template std::enable_if_t, LargePagePtr> make_unique_large_page(size_t num) { using ElementType = std::remove_extent_t; static_assert(alignof(ElementType) <= 4096, "aligned_large_pages_alloc() may fail for such a big alignment requirement of T"); ElementType* memory = memory_allocator(aligned_large_pages_alloc, num); return LargePagePtr(memory); } // // // aligned unique ptr // // template struct AlignedDeleter { void operator()(T* ptr) const { return memory_deleter(ptr, std_aligned_free); } }; template struct AlignedArrayDeleter { void operator()(T* ptr) const { return memory_deleter_array(ptr, std_aligned_free); } }; template using AlignedPtr = std::conditional_t, std::unique_ptr>>, std::unique_ptr>>; // make_unique_aligned for single objects template std::enable_if_t, AlignedPtr> make_unique_aligned(Args&&... args) { const auto func = [](size_t size) { return std_aligned_alloc(alignof(T), size); }; T* obj = memory_allocator(func, std::forward(args)...); return AlignedPtr(obj); } // make_unique_aligned for arrays of unknown bound template std::enable_if_t, AlignedPtr> make_unique_aligned(size_t num) { using ElementType = std::remove_extent_t; const auto func = [](size_t size) { return std_aligned_alloc(alignof(ElementType), size); }; ElementType* memory = memory_allocator(func, num); return AlignedPtr(memory); } // Get the first aligned element of an array. // ptr must point to an array of size at least `sizeof(T) * N + alignment` bytes, // where N is the number of elements in the array. template T* align_ptr_up(T* ptr) { static_assert(alignof(T) < Alignment); const uintptr_t ptrint = reinterpret_cast(reinterpret_cast(ptr)); return reinterpret_cast( reinterpret_cast((ptrint + (Alignment - 1)) / Alignment * Alignment)); } } // namespace Stockfish #endif // #ifndef MEMORY_H_INCLUDED