From a141c401f78467bc15f62882fca5d55a007cacbb Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Mon, 17 Feb 2025 00:21:48 +0100 Subject: Reorganize everything --- asl/memory/BUILD.bazel | 16 ++++++ asl/memory/allocator.cpp | 54 +++++++++++++++++++ asl/memory/allocator.hpp | 58 ++++++++++++++++++++ asl/memory/layout.hpp | 37 +++++++++++++ asl/memory/memory.hpp | 137 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 302 insertions(+) create mode 100644 asl/memory/BUILD.bazel create mode 100644 asl/memory/allocator.cpp create mode 100644 asl/memory/allocator.hpp create mode 100644 asl/memory/layout.hpp create mode 100644 asl/memory/memory.hpp (limited to 'asl/memory') diff --git a/asl/memory/BUILD.bazel b/asl/memory/BUILD.bazel new file mode 100644 index 0000000..0a198bc --- /dev/null +++ b/asl/memory/BUILD.bazel @@ -0,0 +1,16 @@ +cc_library( + name = "memory", + hdrs = [ + "allocator.hpp", + "layout.hpp", + "memory.hpp", + ], + srcs = [ + "allocator.cpp", + ], + deps = [ + "//asl/base", + ], + visibility = ["//visibility:public"], +) + diff --git a/asl/memory/allocator.cpp b/asl/memory/allocator.cpp new file mode 100644 index 0000000..504202d --- /dev/null +++ b/asl/memory/allocator.cpp @@ -0,0 +1,54 @@ +#include "asl/memory/allocator.hpp" +#include "asl/base/assert.hpp" +#include "asl/memory/layout.hpp" + +#include + +// @Todo zalloc +// @Todo Cookies +// @Todo Debug values + +void* asl::GlobalHeap::alloc(const layout& layout) +{ +#if ASL_OS_WINDOWS + void* ptr = ::_aligned_malloc( + static_cast(layout.size), + static_cast(layout.align)); +#elif ASL_OS_LINUX + void* ptr = ::aligned_alloc( + static_cast(layout.align), + static_cast(layout.size)); +#endif + ASL_ASSERT(ptr != nullptr); // @Todo panic + return ptr; +} + +void* asl::GlobalHeap::realloc(void* old_ptr, [[maybe_unused]] const layout& old_layout, const layout& new_layout) +{ +#if ASL_OS_WINDOWS + return ::_aligned_realloc(old_ptr, + static_cast(new_layout.size), + static_cast(new_layout.align)); +#elif ASL_OS_LINUX + if (new_layout.align <= old_layout.align) + { + void* new_ptr = ::realloc(old_ptr, static_cast(new_layout.size)); + ASL_ASSERT(new_ptr != nullptr); // @Todo panic + return new_ptr; + } + + void* new_ptr = alloc(new_layout); + asl::memcpy(new_ptr, old_ptr, asl::min(old_layout.size, new_layout.size)); + dealloc(old_ptr, old_layout); + return new_ptr; +#endif +} + +void asl::GlobalHeap::dealloc(void* ptr, const layout&) +{ +#if ASL_OS_WINDOWS + ::_aligned_free(ptr); +#elif ASL_OS_LINUX + ::free(ptr); +#endif +} diff --git a/asl/memory/allocator.hpp b/asl/memory/allocator.hpp new file mode 100644 index 0000000..d39fa23 --- /dev/null +++ b/asl/memory/allocator.hpp @@ -0,0 +1,58 @@ +#pragma once + +#include "asl/base/meta.hpp" +#include "asl/memory/layout.hpp" +#include "asl/memory/memory.hpp" + +namespace asl +{ + +template +concept allocator = moveable && equality_comparable && + requires(T& alloc, layout layout, void* ptr) + { + { alloc.alloc(layout) } -> same_as; + { alloc.realloc(ptr, layout, layout) } -> same_as; + alloc.dealloc(ptr, layout); + }; + +class GlobalHeap +{ +public: + static void* alloc(const layout&); + static void* realloc(void* ptr, const layout& old, const layout& new_layout); + static void dealloc(void* ptr, const layout&); + + constexpr bool operator==(const GlobalHeap&) const { return true; } +}; +static_assert(allocator); + +using DefaultAllocator = GlobalHeap; + +template +T* alloc_new(allocator auto& a, auto&&... args) +{ + void* ptr = a.alloc(layout::of()); + return construct_at(ptr, ASL_FWD(args)...); +} + +template +void alloc_delete(allocator auto& a, T* ptr) +{ + destroy(ptr); + a.dealloc(ptr, layout::of()); +} + +template +constexpr T* alloc_new_default(auto&&... args) +{ + return alloc_new(DefaultAllocator{}, ASL_FWD(args)...); +} + +template +void alloc_delete_default(T* ptr) +{ + alloc_delete(DefaultAllocator{}, ptr); +} + +} // namespace asl diff --git a/asl/memory/layout.hpp b/asl/memory/layout.hpp new file mode 100644 index 0000000..af867d8 --- /dev/null +++ b/asl/memory/layout.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "asl/base/integers.hpp" +#include "asl/base/meta.hpp" + +namespace asl +{ + +template +inline constexpr isize_t size_of = static_cast(sizeof(T)); + +template +inline constexpr isize_t align_of = static_cast(alignof(T)); + +struct layout +{ + isize_t size; + isize_t align; + + constexpr bool operator==(const layout&) const = default; + + template + static constexpr layout of() + { + return layout{ size_of, align_of }; + } + + template + static constexpr layout array(isize_t size) + { + return layout{ size_of * size, align_of }; + } +}; + +} // namespace asl + +#define AslOffsetOf(S, M) (static_cast(__builtin_offsetof(S, M))) diff --git a/asl/memory/memory.hpp b/asl/memory/memory.hpp new file mode 100644 index 0000000..de8e07d --- /dev/null +++ b/asl/memory/memory.hpp @@ -0,0 +1,137 @@ +#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) +{ + 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{ ASL_FWD(args)... }; +} + +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, ASL_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] = ASL_MOVE(from[i]); + } + destroy_n(from, n); + } +} + +} // namespace asl + -- cgit