From 2c457c42758f1aeb5e2ec15f89b9702586328220 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Wed, 26 Mar 2025 18:54:49 +0100 Subject: Add address_of --- asl/memory/memory.hpp | 11 +++++++++++ asl/types/BUILD.bazel | 1 + asl/types/function_ref.hpp | 5 +++-- todo.txt | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/asl/memory/memory.hpp b/asl/memory/memory.hpp index 2c5301e..5ea4766 100644 --- a/asl/memory/memory.hpp +++ b/asl/memory/memory.hpp @@ -17,6 +17,17 @@ constexpr void* operator new(size_t, void* ptr) noexcept namespace asl { +template +[[nodiscard]] +constexpr T* address_of(T& obj) +{ + return __builtin_addressof(obj); +} + +template +void address_of(const T&& obj) = delete; + +[[nodiscard]] constexpr isize_t memcmp(const void* a, const void* b, isize_t size) { return __builtin_memcmp(a, b, static_cast(size)); diff --git a/asl/types/BUILD.bazel b/asl/types/BUILD.bazel index 198d0a2..53905a3 100644 --- a/asl/types/BUILD.bazel +++ b/asl/types/BUILD.bazel @@ -97,6 +97,7 @@ cc_library( ], deps = [ "//asl/base", + "//asl/memory", ], visibility = ["//visibility:public"], ) diff --git a/asl/types/function_ref.hpp b/asl/types/function_ref.hpp index 8daa5bd..8d874f1 100644 --- a/asl/types/function_ref.hpp +++ b/asl/types/function_ref.hpp @@ -7,6 +7,7 @@ #include "asl/base/utility.hpp" #include "asl/base/meta.hpp" #include "asl/base/functional.hpp" +#include "asl/memory/memory.hpp" namespace asl { @@ -43,7 +44,7 @@ public: && same_as, R> ) // NOLINTNEXTLINE(*cast*) - : m_obj{const_cast(reinterpret_cast(&t))} + : m_obj{const_cast(reinterpret_cast(address_of(t)))} , m_invoke{invoke>} {} @@ -56,7 +57,7 @@ public: ) { // NOLINTNEXTLINE(*cast*) - m_obj = const_cast(reinterpret_cast(&t)); + m_obj = const_cast(reinterpret_cast(address_of(t))); m_invoke = invoke>; return *this; diff --git a/todo.txt b/todo.txt index 8b13789..5ea5980 100644 --- a/todo.txt +++ b/todo.txt @@ -1 +1 @@ - +Separate memory and allocator -- cgit