diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-01-26 00:40:51 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-01-26 00:42:44 +0100 |
commit | cf7db48c261ee9c896c813a38ff8c59da5b8fe07 (patch) | |
tree | 41d827be5db5f3322d745215fe38a9c395a52fa3 /asl | |
parent | 79aaec3d7d12bc1a0483f19eadeda325a2d65920 (diff) |
Fix line endings
Diffstat (limited to 'asl')
55 files changed, 6274 insertions, 6274 deletions
diff --git a/asl/BUILD.bazel b/asl/BUILD.bazel index 2d82f4b..2cc4af5 100644 --- a/asl/BUILD.bazel +++ b/asl/BUILD.bazel @@ -1,78 +1,78 @@ -cc_library(
- name = "asl",
- hdrs = [
- "allocator.hpp",
- "annotations.hpp",
- "assert.hpp",
- "atomic.hpp",
- "box.hpp",
- "buffer.hpp",
- "config.hpp",
- "float.hpp",
- "format.hpp",
- "functional.hpp",
- "hash.hpp",
- "hash_map.hpp",
- "hash_set.hpp",
- "integers.hpp",
- "io.hpp",
- "layout.hpp",
- "maybe_uninit.hpp",
- "memory.hpp",
- "meta.hpp",
- "option.hpp",
- "print.hpp",
- "span.hpp",
- "status.hpp",
- "status_or.hpp",
- "string.hpp",
- "string_builder.hpp",
- "string_view.hpp",
- "utility.hpp",
- ],
- srcs = [
- "allocator.cpp",
- "assert.cpp",
- "format.cpp",
- "format_float.cpp",
- "hash_cityhash.cpp",
- "print.cpp",
- "status.cpp",
- ],
- deps = [
- "//vendor/dragonbox",
- ],
- visibility = ["//visibility:public"],
-)
-
-[cc_test(
- name = "%s_tests" % name,
- srcs = [
- "tests/%s_tests.cpp" % name,
- "tests/test_types.hpp",
- ],
- deps = [
- ":asl",
- "//asl/testing",
- ],
-) for name in [
- "box",
- "buffer",
- "float",
- "format",
- "functional",
- "hash",
- "hash_map",
- "hash_set",
- "integers",
- "maybe_uninit",
- "meta",
- "option",
- "span",
- "status",
- "status_or",
- "string",
- "string_builder",
- "string_view",
- "utility",
-]]
+cc_library( + name = "asl", + hdrs = [ + "allocator.hpp", + "annotations.hpp", + "assert.hpp", + "atomic.hpp", + "box.hpp", + "buffer.hpp", + "config.hpp", + "float.hpp", + "format.hpp", + "functional.hpp", + "hash.hpp", + "hash_map.hpp", + "hash_set.hpp", + "integers.hpp", + "io.hpp", + "layout.hpp", + "maybe_uninit.hpp", + "memory.hpp", + "meta.hpp", + "option.hpp", + "print.hpp", + "span.hpp", + "status.hpp", + "status_or.hpp", + "string.hpp", + "string_builder.hpp", + "string_view.hpp", + "utility.hpp", + ], + srcs = [ + "allocator.cpp", + "assert.cpp", + "format.cpp", + "format_float.cpp", + "hash_cityhash.cpp", + "print.cpp", + "status.cpp", + ], + deps = [ + "//vendor/dragonbox", + ], + visibility = ["//visibility:public"], +) + +[cc_test( + name = "%s_tests" % name, + srcs = [ + "tests/%s_tests.cpp" % name, + "tests/test_types.hpp", + ], + deps = [ + ":asl", + "//asl/testing", + ], +) for name in [ + "box", + "buffer", + "float", + "format", + "functional", + "hash", + "hash_map", + "hash_set", + "integers", + "maybe_uninit", + "meta", + "option", + "span", + "status", + "status_or", + "string", + "string_builder", + "string_view", + "utility", +]] diff --git a/asl/allocator.cpp b/asl/allocator.cpp index 5dbdce0..59bff76 100644 --- a/asl/allocator.cpp +++ b/asl/allocator.cpp @@ -1,56 +1,56 @@ -#include "asl/allocator.hpp"
-#include "asl/assert.hpp"
-#include "asl/utility.hpp"
-#include "asl/memory.hpp"
-#include "asl/print.hpp"
-
-#include <cstdlib>
-
-// @Todo zalloc
-// @Todo Cookies
-// @Todo Debug values
-
-void* asl::GlobalHeap::alloc(const layout& layout)
-{
-#if ASL_OS_WINDOWS
- void* ptr = ::_aligned_malloc(
- static_cast<size_t>(layout.size),
- static_cast<size_t>(layout.align));
-#elif ASL_OS_LINUX
- void* ptr = ::aligned_alloc(
- static_cast<size_t>(layout.align),
- static_cast<size_t>(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<size_t>(new_layout.size),
- static_cast<size_t>(new_layout.align));
-#elif ASL_OS_LINUX
- if (new_layout.align <= old_layout.align)
- {
- void* new_ptr = ::realloc(old_ptr, static_cast<size_t>(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
-}
+#include "asl/allocator.hpp" +#include "asl/assert.hpp" +#include "asl/utility.hpp" +#include "asl/memory.hpp" +#include "asl/print.hpp" + +#include <cstdlib> + +// @Todo zalloc +// @Todo Cookies +// @Todo Debug values + +void* asl::GlobalHeap::alloc(const layout& layout) +{ +#if ASL_OS_WINDOWS + void* ptr = ::_aligned_malloc( + static_cast<size_t>(layout.size), + static_cast<size_t>(layout.align)); +#elif ASL_OS_LINUX + void* ptr = ::aligned_alloc( + static_cast<size_t>(layout.align), + static_cast<size_t>(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<size_t>(new_layout.size), + static_cast<size_t>(new_layout.align)); +#elif ASL_OS_LINUX + if (new_layout.align <= old_layout.align) + { + void* new_ptr = ::realloc(old_ptr, static_cast<size_t>(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/allocator.hpp b/asl/allocator.hpp index 265378b..90793dd 100644 --- a/asl/allocator.hpp +++ b/asl/allocator.hpp @@ -1,58 +1,58 @@ -#pragma once
-
-#include "asl/layout.hpp"
-#include "asl/meta.hpp"
-#include "asl/memory.hpp"
-
-namespace asl
-{
-
-template<typename T>
-concept allocator = moveable<T> && equality_comparable<T> &&
- requires(T& alloc, layout layout, void* ptr)
- {
- { alloc.alloc(layout) } -> same_as<void*>;
- { alloc.realloc(ptr, layout, layout) } -> same_as<void*>;
- 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<GlobalHeap>);
-
-using DefaultAllocator = GlobalHeap;
-
-template<typename T>
-T* alloc_new(allocator auto& a, auto&&... args)
-{
- void* ptr = a.alloc(layout::of<T>());
- return construct_at<T>(ptr, ASL_FWD(args)...);
-}
-
-template<typename T>
-void alloc_delete(allocator auto& a, T* ptr)
-{
- destroy(ptr);
- a.dealloc(ptr, layout::of<T>());
-}
-
-template<typename T>
-constexpr T* alloc_new_default(auto&&... args)
-{
- return alloc_new<T>(DefaultAllocator{}, ASL_FWD(args)...);
-}
-
-template<typename T>
-void alloc_delete_default(T* ptr)
-{
- alloc_delete(DefaultAllocator{}, ptr);
-}
-
-} // namespace asl
+#pragma once + +#include "asl/layout.hpp" +#include "asl/meta.hpp" +#include "asl/memory.hpp" + +namespace asl +{ + +template<typename T> +concept allocator = moveable<T> && equality_comparable<T> && + requires(T& alloc, layout layout, void* ptr) + { + { alloc.alloc(layout) } -> same_as<void*>; + { alloc.realloc(ptr, layout, layout) } -> same_as<void*>; + 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<GlobalHeap>); + +using DefaultAllocator = GlobalHeap; + +template<typename T> +T* alloc_new(allocator auto& a, auto&&... args) +{ + void* ptr = a.alloc(layout::of<T>()); + return construct_at<T>(ptr, ASL_FWD(args)...); +} + +template<typename T> +void alloc_delete(allocator auto& a, T* ptr) +{ + destroy(ptr); + a.dealloc(ptr, layout::of<T>()); +} + +template<typename T> +constexpr T* alloc_new_default(auto&&... args) +{ + return alloc_new<T>(DefaultAllocator{}, ASL_FWD(args)...); +} + +template<typename T> +void alloc_delete_default(T* ptr) +{ + alloc_delete(DefaultAllocator{}, ptr); +} + +} // namespace asl diff --git a/asl/annotations.hpp b/asl/annotations.hpp index fc65378..6ea3a84 100644 --- a/asl/annotations.hpp +++ b/asl/annotations.hpp @@ -1,9 +1,9 @@ -#pragma once
-
-#include "asl/config.hpp"
-
-#if ASL_COMPILER_CLANG_CL
- #define ASL_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
-#elif ASL_COMPILER_CLANG
- #define ASL_NO_UNIQUE_ADDRESS [[no_unique_address]]
-#endif
+#pragma once + +#include "asl/config.hpp" + +#if ASL_COMPILER_CLANG_CL + #define ASL_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]] +#elif ASL_COMPILER_CLANG + #define ASL_NO_UNIQUE_ADDRESS [[no_unique_address]] +#endif diff --git a/asl/assert.hpp b/asl/assert.hpp index bf6a795..7a3ae00 100644 --- a/asl/assert.hpp +++ b/asl/assert.hpp @@ -1,33 +1,33 @@ -#pragma once
-
-#include "asl/config.hpp"
-#include "asl/meta.hpp"
-
-namespace asl
-{
-
-void report_assert_failure( const char* msg, const source_location& sl = source_location{});
-
-} // namespace asl
-
-#if ASL_COMPILER_CLANG_CL
- #define ASL_DEBUG_BREAK() __debugbreak()
-#elif ASL_COMPILER_CLANG
- #define ASL_DEBUG_BREAK() __builtin_debugtrap()
-#endif
-
-#define ASL_ASSERT(...) \
- if (__VA_ARGS__) {} \
- else \
- { \
- ::asl::report_assert_failure(#__VA_ARGS__); \
- ASL_DEBUG_BREAK(); \
- }
-
-#define ASL_ASSERT_RELEASE(...) \
- if (__VA_ARGS__) {} \
- else \
- { \
- ::asl::report_assert_failure(#__VA_ARGS__); \
- ASL_DEBUG_BREAK(); \
- }
+#pragma once + +#include "asl/config.hpp" +#include "asl/meta.hpp" + +namespace asl +{ + +void report_assert_failure( const char* msg, const source_location& sl = source_location{}); + +} // namespace asl + +#if ASL_COMPILER_CLANG_CL + #define ASL_DEBUG_BREAK() __debugbreak() +#elif ASL_COMPILER_CLANG + #define ASL_DEBUG_BREAK() __builtin_debugtrap() +#endif + +#define ASL_ASSERT(...) \ + if (__VA_ARGS__) {} \ + else \ + { \ + ::asl::report_assert_failure(#__VA_ARGS__); \ + ASL_DEBUG_BREAK(); \ + } + +#define ASL_ASSERT_RELEASE(...) \ + if (__VA_ARGS__) {} \ + else \ + { \ + ::asl::report_assert_failure(#__VA_ARGS__); \ + ASL_DEBUG_BREAK(); \ + } diff --git a/asl/box.hpp b/asl/box.hpp index 340a37c..f193853 100644 --- a/asl/box.hpp +++ b/asl/box.hpp @@ -1,115 +1,115 @@ -#pragma once
-
-#include "asl/allocator.hpp"
-#include "asl/assert.hpp"
-#include "asl/annotations.hpp"
-#include "asl/memory.hpp"
-#include "asl/utility.hpp"
-#include "asl/hash.hpp"
-
-namespace asl
-{
-
-template<is_object T, allocator Allocator = DefaultAllocator>
-class box
-{
- T* m_ptr;
- ASL_NO_UNIQUE_ADDRESS Allocator m_alloc;
-
-public:
- explicit constexpr box(niche_t)
- requires default_constructible<Allocator>
- : m_ptr{nullptr}
- , m_alloc{}
- {}
-
- constexpr box(T* ptr, Allocator alloc)
- : m_ptr{ptr}
- , m_alloc{ASL_MOVE(alloc)}
- {
- ASL_ASSERT(m_ptr != nullptr);
- }
-
- constexpr box(box&& other)
- : m_ptr{exchange(other.m_ptr, nullptr)}
- , m_alloc{ASL_MOVE(other.m_alloc)}
- {}
-
- constexpr box& operator=(box&& other)
- {
- if (this == &other) { return *this; }
-
- if (m_ptr != nullptr) { reset(); }
-
- m_ptr = exchange(other.m_ptr, nullptr);
- m_alloc = ASL_MOVE(other.m_alloc);
-
- return *this;
- }
-
- box(const box&) = delete;
- box& operator=(const box&) = delete;
-
- constexpr ~box()
- {
- reset();
- }
-
- constexpr void reset()
- {
- if (m_ptr != nullptr)
- {
- destroy(m_ptr);
- m_alloc.dealloc(m_ptr, layout::of<T>());
- m_ptr = nullptr;
- }
- }
-
- constexpr T* get() const { return m_ptr; }
-
- constexpr T& operator*() const
- {
- ASL_ASSERT(m_ptr != nullptr);
- return *m_ptr;
- }
-
- constexpr T* operator->() const
- {
- ASL_ASSERT(m_ptr != nullptr);
- return m_ptr;
- }
-
- constexpr bool operator==(niche_t) const
- {
- return m_ptr == nullptr;
- }
-
- template<typename H>
- requires hashable<T>
- friend H AslHashValue(H h, const box& b)
- {
- return H::combine(ASL_MOVE(h), *b);
- }
-};
-
-template<is_object T, allocator Allocator = DefaultAllocator, typename... Args>
-constexpr box<T, Allocator> make_box_in(Allocator allocator, Args&&... args)
- requires constructible_from<T, Args&&...>
-{
- void* raw_ptr = allocator.alloc(layout::of<T>());
- auto* ptr = construct_at<T>(raw_ptr, ASL_FWD(args)...);
- return box(ptr, ASL_MOVE(allocator));
-}
-
-template<is_object T, allocator Allocator = DefaultAllocator, typename... Args>
-constexpr box<T, Allocator> make_box(Args&&... args)
- requires default_constructible<Allocator> && constructible_from<T, Args&&...>
-{
- Allocator allocator{};
- void* raw_ptr = allocator.alloc(layout::of<T>());
- auto* ptr = construct_at<T>(raw_ptr, ASL_FWD(args)...);
- return box<T>(ptr, ASL_MOVE(allocator));
-}
-
-} // namespace asl
-
+#pragma once + +#include "asl/allocator.hpp" +#include "asl/assert.hpp" +#include "asl/annotations.hpp" +#include "asl/memory.hpp" +#include "asl/utility.hpp" +#include "asl/hash.hpp" + +namespace asl +{ + +template<is_object T, allocator Allocator = DefaultAllocator> +class box +{ + T* m_ptr; + ASL_NO_UNIQUE_ADDRESS Allocator m_alloc; + +public: + explicit constexpr box(niche_t) + requires default_constructible<Allocator> + : m_ptr{nullptr} + , m_alloc{} + {} + + constexpr box(T* ptr, Allocator alloc) + : m_ptr{ptr} + , m_alloc{ASL_MOVE(alloc)} + { + ASL_ASSERT(m_ptr != nullptr); + } + + constexpr box(box&& other) + : m_ptr{exchange(other.m_ptr, nullptr)} + , m_alloc{ASL_MOVE(other.m_alloc)} + {} + + constexpr box& operator=(box&& other) + { + if (this == &other) { return *this; } + + if (m_ptr != nullptr) { reset(); } + + m_ptr = exchange(other.m_ptr, nullptr); + m_alloc = ASL_MOVE(other.m_alloc); + + return *this; + } + + box(const box&) = delete; + box& operator=(const box&) = delete; + + constexpr ~box() + { + reset(); + } + + constexpr void reset() + { + if (m_ptr != nullptr) + { + destroy(m_ptr); + m_alloc.dealloc(m_ptr, layout::of<T>()); + m_ptr = nullptr; + } + } + + constexpr T* get() const { return m_ptr; } + + constexpr T& operator*() const + { + ASL_ASSERT(m_ptr != nullptr); + return *m_ptr; + } + + constexpr T* operator->() const + { + ASL_ASSERT(m_ptr != nullptr); + return m_ptr; + } + + constexpr bool operator==(niche_t) const + { + return m_ptr == nullptr; + } + + template<typename H> + requires hashable<T> + friend H AslHashValue(H h, const box& b) + { + return H::combine(ASL_MOVE(h), *b); + } +}; + +template<is_object T, allocator Allocator = DefaultAllocator, typename... Args> +constexpr box<T, Allocator> make_box_in(Allocator allocator, Args&&... args) + requires constructible_from<T, Args&&...> +{ + void* raw_ptr = allocator.alloc(layout::of<T>()); + auto* ptr = construct_at<T>(raw_ptr, ASL_FWD(args)...); + return box(ptr, ASL_MOVE(allocator)); +} + +template<is_object T, allocator Allocator = DefaultAllocator, typename... Args> +constexpr box<T, Allocator> make_box(Args&&... args) + requires default_constructible<Allocator> && constructible_from<T, Args&&...> +{ + Allocator allocator{}; + void* raw_ptr = allocator.alloc(layout::of<T>()); + auto* ptr = construct_at<T>(raw_ptr, ASL_FWD(args)...); + return box<T>(ptr, ASL_MOVE(allocator)); +} + +} // namespace asl + |