// This file is part of the mcl project. // Copyright (c) 2022 merryhime // SPDX-License-Identifier: MIT #pragma once #include #ifdef _MSC_VER # include #else # include #endif namespace mcl { namespace detail { struct aligned_alloc_deleter { template void operator()(T* p) const { #ifdef _MSC_VER _aligned_free(const_cast*>(p)); #else std::free(const_cast*>(p)); #endif } }; } // namespace detail template using overaligned_unique_ptr = std::unique_ptr; template auto make_overaligned_unique_ptr_array(size_t element_count) { const size_t min_size = element_count * sizeof(T); const size_t alloc_size = (min_size + alignment - 1) / alignment * alignment; #ifdef _MSC_VER return overaligned_unique_ptr{static_cast(_aligned_malloc(alloc_size, alignment))}; #else return overaligned_unique_ptr{static_cast(std::aligned_alloc(alignment, alloc_size))}; #endif } } // namespace mcl