// Copyright 2025 Steven Le Rouzic // // SPDX-License-Identifier: BSD-3-Clause #pragma once #include "asl/base/integers.hpp" #include "asl/base/meta.hpp" #include "asl/base/utility.hpp" #include "asl/memory/layout.hpp" constexpr void* operator new(size_t, void* ptr) noexcept { return ptr; } namespace asl { constexpr isize_t memcmp(const void* a, const void* b, isize_t size) { return __builtin_memcmp(a, b, static_cast(size)); } constexpr void memcpy(void* dst, const void* src, isize_t size) { __builtin_memcpy(dst, src, static_cast(size)); } inline void memzero(void* dst, isize_t size) { __builtin_memset(dst, 0, static_cast(size)); } constexpr isize_t strlen(const char* s) { return static_cast(__builtin_strlen(s)); } template constexpr T* construct_at(void* ptr, Args&&... args) requires constructible_from { return new (ptr) T{ std::forward(args)... }; // NOLINT(*-owning-memory) } template constexpr void destroy(T* data) { if constexpr (!trivially_destructible) { data->~T(); } } template constexpr void destroy_n(T* data, isize_t n) { if constexpr (!trivially_destructible) { for (isize_t i = 0; i < n; ++i) { destroy(data + i); } } } template constexpr void copy_uninit_n(T* to, const T* from, isize_t n) { if constexpr (trivially_copy_constructible) { memcpy(to, from, size_of * n); } else { for (isize_t i = 0; i < n; ++i) { // NOLINTNEXTLINE(*-pointer-arithmetic) construct_at(to + i, from[i]); } } } template constexpr void copy_assign_n(T* to, const T* from, isize_t n) { if constexpr (trivially_copy_constructible) { memcpy(to, from, size_of * n); } else { for (isize_t i = 0; i < n; ++i) { // NOLINTNEXTLINE(*-pointer-arithmetic) to[i] = from[i]; } } } template constexpr void relocate_uninit_n(T* to, T* from, isize_t n) { if constexpr (trivially_move_constructible) { static_assert(trivially_destructible); memcpy(to, from, size_of * n); } else { for (isize_t i = 0; i < n; ++i) { // NOLINTNEXTLINE(*-pointer-arithmetic) construct_at(to + i, std::move(from[i])); } destroy_n(from, n); } } template constexpr void relocate_assign_n(T* to, T* from, isize_t n) { if constexpr (trivially_move_assignable) { static_assert(trivially_destructible); memcpy(to, from, size_of * n); } else { for (isize_t i = 0; i < n; ++i) { // NOLINTNEXTLINE(*-pointer-arithmetic) to[i] = std::move(from[i]); } destroy_n(from, n); } } } // namespace asl