From cf7db48c261ee9c896c813a38ff8c59da5b8fe07 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Sun, 26 Jan 2025 00:40:51 +0100 Subject: Fix line endings --- .bazelrc | 88 +-- .clang-tidy | 68 +-- .gitignore | 6 +- BUILD.bazel | 36 +- MODULE.bazel | 30 +- asl/BUILD.bazel | 156 ++--- asl/allocator.cpp | 112 ++-- asl/allocator.hpp | 116 ++-- asl/annotations.hpp | 18 +- asl/assert.hpp | 66 +-- asl/box.hpp | 230 ++++---- asl/buffer.hpp | 918 ++++++++++++++--------------- asl/config.hpp | 34 +- asl/float.hpp | 34 +- asl/format.cpp | 402 ++++++------- asl/format.hpp | 216 +++---- asl/format_float.cpp | 196 +++---- asl/functional.hpp | 130 ++-- asl/hash.hpp | 276 ++++----- asl/hash_cityhash.cpp | 1034 ++++++++++++++++---------------- asl/hash_map.hpp | 356 +++++------ asl/hash_set.hpp | 836 +++++++++++++------------- asl/integers.hpp | 80 +-- asl/io.hpp | 38 +- asl/layout.hpp | 74 +-- asl/log/BUILD.bazel | 52 +- asl/maybe_uninit.hpp | 144 ++--- asl/memory.hpp | 274 ++++----- asl/meta.hpp | 442 +++++++------- asl/option.hpp | 1016 ++++++++++++++++---------------- asl/print.cpp | 62 +- asl/print.hpp | 58 +- asl/span.hpp | 6 +- asl/status.cpp | 2 +- asl/status.hpp | 4 +- asl/status_or.hpp | 358 +++++------ asl/string.hpp | 2 +- asl/string_builder.hpp | 2 +- asl/string_view.hpp | 2 +- asl/testing/BUILD.bazel | 26 +- asl/testing/testing.cpp | 162 ++--- asl/testing/testing.hpp | 92 +-- asl/tests/box_tests.cpp | 156 ++--- asl/tests/buffer_tests.cpp | 1206 +++++++++++++++++++------------------- asl/tests/float_tests.cpp | 46 +- asl/tests/format_tests.cpp | 220 +++---- asl/tests/functional_tests.cpp | 146 ++--- asl/tests/hash_map_tests.cpp | 96 +-- asl/tests/hash_set_tests.cpp | 370 ++++++------ asl/tests/hash_tests.cpp | 520 ++++++++-------- asl/tests/integers_tests.cpp | 30 +- asl/tests/maybe_uninit_tests.cpp | 44 +- asl/tests/meta_tests.cpp | 498 ++++++++-------- asl/tests/option_tests.cpp | 658 ++++++++++----------- asl/tests/status_or_tests.cpp | 168 +++--- asl/tests/status_tests.cpp | 4 +- asl/tests/string_view_tests.cpp | 8 +- asl/tests/test_types.hpp | 182 +++--- asl/tests/utility_tests.cpp | 2 +- asl/utility.hpp | 168 +++--- tools/BUILD.bazel | 8 + tools/fix_line_endings.py | 68 +++ vendor/cityhash/LICENSE.txt | 38 +- vendor/dragonbox/BUILD.bazel | 12 +- 64 files changed, 6490 insertions(+), 6412 deletions(-) create mode 100644 tools/BUILD.bazel create mode 100644 tools/fix_line_endings.py diff --git a/.bazelrc b/.bazelrc index 97a7861..19154d5 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,44 +1,44 @@ -startup --windows_enable_symlinks -build:windows --enable_runfiles=true - -build --build_python_zip=false - -build:windows --extra_execution_platforms=//:x64_windows-clang-cl -# @Todo(bazel) We should be able to use @local_config_cc... -build:windows --extra_toolchains=@@rules_cc++cc_configure_extension+local_config_cc//:cc-toolchain-x64_windows-clang-cl - -build:linux --repo_env=CC=clang - -build:windows --cxxopt=-Xclang=-std=c++20 -build:linux --cxxopt=-std=c++20 - -build --cxxopt=-Wall -build --cxxopt=-Wno-c++98-compat -build --cxxopt=-Wno-c++98-compat-pedantic -build --cxxopt=-Wno-pre-c++17-compat -build --cxxopt=-Wno-c++20-compat -build --cxxopt=-Wno-unused-macros -build --cxxopt=-Wno-documentation-unknown-command -build --cxxopt=-Wno-extra-semi-stmt -build --cxxopt=-Wno-extra-semi -build --cxxopt=-Wno-global-constructors -build --cxxopt=-Wno-unsafe-buffer-usage -build --cxxopt=-Wno-covered-switch-default - -build:windows_san --config=windows -build:windows_san --copt=-fno-sanitize-ignorelist -build:windows_san --copt=-fsanitize=address -build:windows_san --copt=-fsanitize=undefined -build:windows_san --copt=-fno-sanitize-recover=all -build:windows_san --linkopt=clang_rt.asan-x86_64.lib -build:windows_san --copt=/MT - -build:linux_san --config=linux -build:linux_san --copt=-fsanitize=address -build:linux_san --linkopt=-fsanitize=address -build:linux_san --copt=-fsanitize=undefined -build:linux_san --copt=-fno-sanitize-recover=all -build:linux_san --linkopt=-fsanitize=undefined -build:linux_san --linkopt=-fsanitize-link-c++-runtime - -test --test_output=errors +startup --windows_enable_symlinks +build:windows --enable_runfiles=true + +build --build_python_zip=false + +build:windows --extra_execution_platforms=//:x64_windows-clang-cl +# @Todo(bazel) We should be able to use @local_config_cc... +build:windows --extra_toolchains=@@rules_cc++cc_configure_extension+local_config_cc//:cc-toolchain-x64_windows-clang-cl + +build:linux --repo_env=CC=clang + +build:windows --cxxopt=-Xclang=-std=c++20 +build:linux --cxxopt=-std=c++20 + +build --cxxopt=-Wall +build --cxxopt=-Wno-c++98-compat +build --cxxopt=-Wno-c++98-compat-pedantic +build --cxxopt=-Wno-pre-c++17-compat +build --cxxopt=-Wno-c++20-compat +build --cxxopt=-Wno-unused-macros +build --cxxopt=-Wno-documentation-unknown-command +build --cxxopt=-Wno-extra-semi-stmt +build --cxxopt=-Wno-extra-semi +build --cxxopt=-Wno-global-constructors +build --cxxopt=-Wno-unsafe-buffer-usage +build --cxxopt=-Wno-covered-switch-default + +build:windows_san --config=windows +build:windows_san --copt=-fno-sanitize-ignorelist +build:windows_san --copt=-fsanitize=address +build:windows_san --copt=-fsanitize=undefined +build:windows_san --copt=-fno-sanitize-recover=all +build:windows_san --linkopt=clang_rt.asan-x86_64.lib +build:windows_san --copt=/MT + +build:linux_san --config=linux +build:linux_san --copt=-fsanitize=address +build:linux_san --linkopt=-fsanitize=address +build:linux_san --copt=-fsanitize=undefined +build:linux_san --copt=-fno-sanitize-recover=all +build:linux_san --linkopt=-fsanitize=undefined +build:linux_san --linkopt=-fsanitize-link-c++-runtime + +test --test_output=errors diff --git a/.clang-tidy b/.clang-tidy index d7337bd..d988d1b 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,34 +1,34 @@ -Checks: - - "hicpp-*" - - "cppcoreguidelines-*" - - "misc-*" - - "clang-analyzer-*" - - "-misc-include-cleaner" - - "performance-*" - - "readability-*" - - "-*-named-parameter" - - "-*-avoid-do-while" - - "-*-magic-numbers" - - "-*-identifier-length" - - "-*-union-access" - - "-*-vararg" - - "-*-macro-usage" - - "-*-non-private-member-variables-in-classes" - - "-*-avoid-non-const-global-variables" - - "-*-missing-std-forward" - - "-*-owning-memory" - - "-*-no-malloc" - - "-*-avoid-c-arrays" - - "-*-use-anonymous-namespace" - - "-*-reinterpret-cast" - - "-*-noexcept-swap" - - "-*-noexcept-move" - - "-*-noexcept-move-constructor" - - "-*-noexcept-move-operations" - - "-*-bounds-array-to-pointer-decay" - - "-*-no-array-decay" - - "-*-signed-bitwise" - - "-readability-use-anyofallof" - - "-readability-function-cognitive-complexity" - - "-readability-math-missing-parentheses" - - "-*-rvalue-reference-param-not-moved" +Checks: + - "hicpp-*" + - "cppcoreguidelines-*" + - "misc-*" + - "clang-analyzer-*" + - "-misc-include-cleaner" + - "performance-*" + - "readability-*" + - "-*-named-parameter" + - "-*-avoid-do-while" + - "-*-magic-numbers" + - "-*-identifier-length" + - "-*-union-access" + - "-*-vararg" + - "-*-macro-usage" + - "-*-non-private-member-variables-in-classes" + - "-*-avoid-non-const-global-variables" + - "-*-missing-std-forward" + - "-*-owning-memory" + - "-*-no-malloc" + - "-*-avoid-c-arrays" + - "-*-use-anonymous-namespace" + - "-*-reinterpret-cast" + - "-*-noexcept-swap" + - "-*-noexcept-move" + - "-*-noexcept-move-constructor" + - "-*-noexcept-move-operations" + - "-*-bounds-array-to-pointer-decay" + - "-*-no-array-decay" + - "-*-signed-bitwise" + - "-readability-use-anyofallof" + - "-readability-function-cognitive-complexity" + - "-readability-math-missing-parentheses" + - "-*-rvalue-reference-param-not-moved" diff --git a/.gitignore b/.gitignore index deb8fd6..fd35d2a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -bazel-*/ -compile_commands.json +bazel-*/ +compile_commands.json +.cache/ +external/ diff --git a/BUILD.bazel b/BUILD.bazel index d861b66..c5a2f79 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,18 +1,18 @@ -load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands") - -refresh_compile_commands( - name = "refresh_clangd", - targets = "//...", -) - -platform( - name = "x64_windows-clang-cl", - constraint_values = [ - "@platforms//cpu:x86_64", - "@platforms//os:windows", - # @Todo(bazel) Bit weird to use a private thing. - # We used to use @bazel_tools//tools/cpp:clang-cl but it's deprecated - # in favor of... this?... - "@rules_cc//cc/private/toolchain:clang-cl", - ], -) +load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands") + +refresh_compile_commands( + name = "refresh_clangd", + targets = "//...", +) + +platform( + name = "x64_windows-clang-cl", + constraint_values = [ + "@platforms//cpu:x86_64", + "@platforms//os:windows", + # @Todo(bazel) Bit weird to use a private thing. + # We used to use @bazel_tools//tools/cpp:clang-cl but it's deprecated + # in favor of... this?... + "@rules_cc//cc/private/toolchain:clang-cl", + ], +) diff --git a/MODULE.bazel b/MODULE.bazel index a10bf6f..197d3cc 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -1,15 +1,15 @@ -module(name = "asl") - -bazel_dep(name = "platforms", version = "0.0.10") -bazel_dep(name = "rules_cc", version = "0.0.17") - -bazel_dep(name = "hedron_compile_commands", dev_dependency = True) -git_override( - module_name = "hedron_compile_commands", - remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git", - commit = "4f28899228fb3ad0126897876f147ca15026151e", -) - -bazel_dep(name = "rules_python", version = "1.1.0", dev_dependency = True) -python = use_extension("@rules_python//python/extensions:python.bzl", "python") -python.toolchain(python_version = "3.13", is_default = True) +module(name = "asl") + +bazel_dep(name = "platforms", version = "0.0.10") +bazel_dep(name = "rules_cc", version = "0.0.17") + +bazel_dep(name = "hedron_compile_commands", dev_dependency = True) +git_override( + module_name = "hedron_compile_commands", + remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git", + commit = "4f28899228fb3ad0126897876f147ca15026151e", +) + +bazel_dep(name = "rules_python", version = "1.1.0", dev_dependency = True) +python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python.toolchain(python_version = "3.13", is_default = True) 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 - -// @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 -} +#include "asl/allocator.hpp" +#include "asl/assert.hpp" +#include "asl/utility.hpp" +#include "asl/memory.hpp" +#include "asl/print.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/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 -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 +#pragma once + +#include "asl/layout.hpp" +#include "asl/meta.hpp" +#include "asl/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/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 -class box -{ - T* m_ptr; - ASL_NO_UNIQUE_ADDRESS Allocator m_alloc; - -public: - explicit constexpr box(niche_t) - requires default_constructible - : 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()); - 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 - requires hashable - friend H AslHashValue(H h, const box& b) - { - return H::combine(ASL_MOVE(h), *b); - } -}; - -template -constexpr box make_box_in(Allocator allocator, Args&&... args) - requires constructible_from -{ - void* raw_ptr = allocator.alloc(layout::of()); - auto* ptr = construct_at(raw_ptr, ASL_FWD(args)...); - return box(ptr, ASL_MOVE(allocator)); -} - -template -constexpr box make_box(Args&&... args) - requires default_constructible && constructible_from -{ - Allocator allocator{}; - void* raw_ptr = allocator.alloc(layout::of()); - auto* ptr = construct_at(raw_ptr, ASL_FWD(args)...); - return box(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 +class box +{ + T* m_ptr; + ASL_NO_UNIQUE_ADDRESS Allocator m_alloc; + +public: + explicit constexpr box(niche_t) + requires default_constructible + : 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()); + 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 + requires hashable + friend H AslHashValue(H h, const box& b) + { + return H::combine(ASL_MOVE(h), *b); + } +}; + +template +constexpr box make_box_in(Allocator allocator, Args&&... args) + requires constructible_from +{ + void* raw_ptr = allocator.alloc(layout::of()); + auto* ptr = construct_at(raw_ptr, ASL_FWD(args)...); + return box(ptr, ASL_MOVE(allocator)); +} + +template +constexpr box make_box(Args&&... args) + requires default_constructible && constructible_from +{ + Allocator allocator{}; + void* raw_ptr = allocator.alloc(layout::of()); + auto* ptr = construct_at(raw_ptr, ASL_FWD(args)...); + return box(ptr, ASL_MOVE(allocator)); +} + +} // namespace asl + diff --git a/asl/buffer.hpp b/asl/buffer.hpp index b714e5a..5cf9964 100644 --- a/asl/buffer.hpp +++ b/asl/buffer.hpp @@ -1,459 +1,459 @@ -#pragma once - -#include "asl/meta.hpp" -#include "asl/allocator.hpp" -#include "asl/annotations.hpp" -#include "asl/memory.hpp" -#include "asl/assert.hpp" -#include "asl/span.hpp" -#include "asl/hash.hpp" - -namespace asl -{ - -template -class buffer -{ - T* m_data{}; - isize_t m_capacity{}; - - static constexpr size_t kOnHeapMask = 0x8000'0000'0000'0000ULL; - - // bit 63 : 1 = on heap, 0 = inline - // bits [62:56] : size when inline - // bits [62:0] : size when on heap - size_t m_size_encoded_{}; - - ASL_NO_UNIQUE_ADDRESS Allocator m_allocator; - - static constexpr isize_t kInlineRegionSize = size_of + size_of + size_of; - -public: - static constexpr isize_t kInlineCapacity = []() { - // 1 byte is used for size inline in m_size_encoded. - // This is enough because we have at most 24 bytes available, - // so 23 chars of capacity. - const isize_t available_size = kInlineRegionSize - 1; - return available_size / size_of; - }(); - -private: - static_assert(align_of <= align_of); - static_assert(align_of == align_of); - static_assert(align_of == align_of); - - constexpr size_t load_size_encoded() const - { - size_t s{}; - asl::memcpy(&s, &m_size_encoded_, sizeof(size_t)); - return s; - } - - constexpr void store_size_encoded(size_t encoded) - { - asl::memcpy(&m_size_encoded_, &encoded, sizeof(size_t)); - } - - static constexpr bool is_on_heap(size_t size_encoded) - { - return (size_encoded & kOnHeapMask) != 0; - } - - static constexpr size_t encode_size_heap(isize_t size) - { - return static_cast(size) | kOnHeapMask; - } - - static constexpr isize_t decode_size(size_t size_encoded) - { - if constexpr (kInlineCapacity == 0) - { - return is_on_heap(size_encoded) - ? static_cast(size_encoded & (~kOnHeapMask)) - : 0; - } - else - { - return is_on_heap(size_encoded) - ? static_cast(size_encoded & (~kOnHeapMask)) - : static_cast(size_encoded >> 56); - } - } - - constexpr bool is_on_heap() const - { - return is_on_heap(load_size_encoded()); - } - - constexpr T* push_uninit() - { - isize_t sz = size(); - resize_uninit_inner(sz + 1); - return data() + sz; - } - - constexpr void resize_uninit_inner(isize_t new_size) - { - isize_t old_size = size(); - if (!trivially_destructible && new_size < old_size) - { - destroy_n(data() + new_size, old_size - new_size); - } - reserve_capacity(new_size); - set_size(new_size); - } - - constexpr void set_size_inline(isize_t new_size) - { - ASL_ASSERT(new_size >= 0 && new_size <= kInlineCapacity); - size_t size_encoded = (load_size_encoded() & size_t{0x00ff'ffff'ffff'ffff}) | (bit_cast(new_size) << 56); - store_size_encoded(size_encoded); - } - - constexpr void set_size(isize_t new_size) - { - ASL_ASSERT(new_size >= 0 && new_size <= capacity()); - if (is_on_heap()) - { - store_size_encoded(encode_size_heap(new_size)); - } - else - { - set_size_inline(new_size); - } - } - - // NOLINTNEXTLINE(*-rvalue-reference-param-not-moved) - void move_from_other(buffer&& other, bool assign) - { - if (other.is_on_heap()) - { - destroy(); - m_data = other.m_data; - m_capacity = other.m_capacity; - store_size_encoded(other.load_size_encoded()); - } - else if (trivially_move_constructible) - { - destroy(); - asl::memcpy(this, &other, kInlineRegionSize); - } - else if (!assign || m_allocator == other.m_allocator) - { - isize_t other_n = other.size(); - isize_t this_n = size(); - resize_uninit_inner(other_n); - if (other_n <= this_n) - { - relocate_assign_n(data(), other.data(), other_n); - } - else - { - relocate_assign_n(data(), other.data(), this_n); - relocate_uninit_n(data() + this_n, other.data() + this_n, other_n - this_n); - } - } - else - { - destroy(); - isize_t n = other.size(); - ASL_ASSERT(n <= kInlineCapacity); - relocate_uninit_n(data(), other.data(), n); - set_size_inline(n); - } - - other.set_size_inline(0); - - if (assign) - { - m_allocator = ASL_MOVE(other.m_allocator); - } - } - - void copy_range(span to_copy) - { - isize_t this_size = size(); - isize_t new_size = to_copy.size(); - - resize_uninit_inner(to_copy.size()); - ASL_ASSERT(capacity() >= new_size); - ASL_ASSERT(size() == to_copy.size()); - - if (new_size <= this_size) - { - copy_assign_n(data(), to_copy.data(), new_size); - } - else - { - copy_assign_n(data(), to_copy.data(), this_size); - copy_uninit_n(data() + this_size, to_copy.data() + this_size, new_size - this_size); - } - } - - template - void resize_inner(isize_t new_size, Args&&... args) - requires constructible_from - { - ASL_ASSERT(new_size >= 0); - - isize_t old_size = size(); - resize_uninit_inner(new_size); - - T* data_ptr = data(); - T* end = data_ptr + new_size; - - // NOLINTNEXTLINE(*-pointer-arithmetic) - for (T* it = data_ptr + old_size; it < end; ++it) - { - construct_at(it, ASL_FWD(args)...); - } - } - -public: - constexpr buffer() requires default_constructible = default; - - explicit constexpr buffer(span s) - requires default_constructible - : buffer{} - { - copy_range(s); - } - - explicit constexpr buffer(Allocator allocator) - : m_allocator{ASL_MOVE(allocator)} - {} - - explicit constexpr buffer(span s, Allocator allocator) - : m_allocator{ASL_MOVE(allocator)} - { - copy_range(s); - } - - constexpr buffer(const buffer& other) - requires copy_constructible && copyable - : m_allocator{other.m_allocator} - { - copy_range(other); - } - - constexpr buffer(buffer&& other) - requires moveable - : buffer(ASL_MOVE(other.m_allocator)) - { - move_from_other(ASL_MOVE(other), false); - } - - constexpr buffer& operator=(const buffer& other) - requires copyable - { - if (&other == this) { return *this; } - copy_range(other); - return *this; - } - - constexpr buffer& operator=(buffer&& other) - requires moveable - { - if (&other == this) { return *this; } - move_from_other(ASL_MOVE(other), true); - return *this; - } - - ~buffer() - { - destroy(); - } - - constexpr isize_t size() const - { - return decode_size(load_size_encoded()); - } - - constexpr isize_t capacity() const - { - if constexpr (kInlineCapacity == 0) - { - return m_capacity; - } - else - { - return is_on_heap() ? m_capacity : kInlineCapacity; - } - } - - void clear() - { - isize_t current_size = size(); - if (current_size == 0) { return; } - - destroy_n(data(), current_size); - set_size(0); - } - - void destroy() - { - clear(); - if (is_on_heap()) - { - if (m_data != nullptr) - { - auto current_layout = layout::array(m_capacity); - m_allocator.dealloc(m_data, current_layout); - } - set_size_inline(0); - } - } - - void reserve_capacity(isize_t new_capacity) - { - ASL_ASSERT(new_capacity >= 0); - ASL_ASSERT_RELEASE(new_capacity <= 0x4000'0000'0000'0000); - - if (new_capacity <= capacity()) { return; } - ASL_ASSERT(new_capacity > kInlineCapacity); - - new_capacity = static_cast(round_up_pow2(static_cast(new_capacity))); - - T* old_data = data(); - const isize_t old_capacity = capacity(); - const isize_t current_size = size(); - const bool currently_on_heap = is_on_heap(); - - auto old_layout = layout::array(old_capacity); - auto new_layout = layout::array(new_capacity); - - if (currently_on_heap && trivially_move_constructible) - { - m_data = reinterpret_cast(m_allocator.realloc(m_data, old_layout, new_layout)); - m_capacity = new_capacity; - return; - } - - T* new_data = reinterpret_cast(m_allocator.alloc(new_layout)); - - relocate_uninit_n(new_data, old_data, current_size); - - if (currently_on_heap) - { - m_allocator.dealloc(old_data, old_layout); - } - - m_data = new_data; - m_capacity = new_capacity; - store_size_encoded(encode_size_heap(current_size)); - } - - constexpr void resize_uninit(isize_t new_size) - requires trivially_default_constructible && trivially_destructible - { - reserve_capacity(new_size); - set_size(new_size); - } - - constexpr void resize_zero(isize_t new_size) - requires trivially_default_constructible && trivially_destructible - { - isize_t old_size = size(); - resize_uninit(new_size); - - if (new_size > old_size) - { - memzero(data() + old_size, (new_size - old_size) * size_of); - } - } - - void resize(isize_t new_size) - requires default_constructible - { - resize_inner(new_size); - } - - void resize(isize_t new_size, const T& value) - { - resize_inner(new_size, value); - } - - constexpr T& push(auto&&... args) - requires constructible_from - { - T* uninit = push_uninit(); - T* init = construct_at(uninit, ASL_FWD(args)...); - return *init; - } - - // @Todo(C++23) Use deducing this - const T* data() const - { - if constexpr (kInlineCapacity == 0) - { - return m_data; - } - else - { - return is_on_heap() ? m_data : reinterpret_cast(this); - } - } - - T* data() - { - if constexpr (kInlineCapacity == 0) - { - return m_data; - } - else - { - return is_on_heap() ? m_data : reinterpret_cast(this); - } - } - - // @Todo(C++23) Use deducing this - constexpr contiguous_iterator begin() const { return contiguous_iterator{data()}; } - constexpr contiguous_iterator end() const { return contiguous_iterator{data() + size()}; } - - constexpr contiguous_iterator begin() { return contiguous_iterator{data()}; } - constexpr contiguous_iterator end() { return contiguous_iterator{data() + size()}; } - - // @Todo(C++23) Deducing this - constexpr operator span() const // NOLINT(*-explicit-conversions) - { - return as_span(); - } - - constexpr operator span() // NOLINT(*-explicit-conversions) - { - return as_span(); - } - - constexpr span as_span() const - { - return span{data(), size()}; - } - - constexpr span as_span() - { - return span{data(), size()}; - } - - // @Todo(C++23) Use deducing this - constexpr T& operator[](isize_t i) - { - ASL_ASSERT(i >= 0 && i <= size()); - return data()[i]; - } - - constexpr const T& operator[](isize_t i) const - { - ASL_ASSERT(i >= 0 && i <= size()); - return data()[i]; - } - - template - requires hashable - friend H AslHashValue(H h, const buffer& b) - { - return H::combine_contiguous(ASL_MOVE(h), b.as_span()); - } -}; - -} // namespace asl - +#pragma once + +#include "asl/meta.hpp" +#include "asl/allocator.hpp" +#include "asl/annotations.hpp" +#include "asl/memory.hpp" +#include "asl/assert.hpp" +#include "asl/span.hpp" +#include "asl/hash.hpp" + +namespace asl +{ + +template +class buffer +{ + T* m_data{}; + isize_t m_capacity{}; + + static constexpr size_t kOnHeapMask = 0x8000'0000'0000'0000ULL; + + // bit 63 : 1 = on heap, 0 = inline + // bits [62:56] : size when inline + // bits [62:0] : size when on heap + size_t m_size_encoded_{}; + + ASL_NO_UNIQUE_ADDRESS Allocator m_allocator; + + static constexpr isize_t kInlineRegionSize = size_of + size_of + size_of; + +public: + static constexpr isize_t kInlineCapacity = []() { + // 1 byte is used for size inline in m_size_encoded. + // This is enough because we have at most 24 bytes available, + // so 23 chars of capacity. + const isize_t available_size = kInlineRegionSize - 1; + return available_size / size_of; + }(); + +private: + static_assert(align_of <= align_of); + static_assert(align_of == align_of); + static_assert(align_of == align_of); + + constexpr size_t load_size_encoded() const + { + size_t s{}; + asl::memcpy(&s, &m_size_encoded_, sizeof(size_t)); + return s; + } + + constexpr void store_size_encoded(size_t encoded) + { + asl::memcpy(&m_size_encoded_, &encoded, sizeof(size_t)); + } + + static constexpr bool is_on_heap(size_t size_encoded) + { + return (size_encoded & kOnHeapMask) != 0; + } + + static constexpr size_t encode_size_heap(isize_t size) + { + return static_cast(size) | kOnHeapMask; + } + + static constexpr isize_t decode_size(size_t size_encoded) + { + if constexpr (kInlineCapacity == 0) + { + return is_on_heap(size_encoded) + ? static_cast(size_encoded & (~kOnHeapMask)) + : 0; + } + else + { + return is_on_heap(size_encoded) + ? static_cast(size_encoded & (~kOnHeapMask)) + : static_cast(size_encoded >> 56); + } + } + + constexpr bool is_on_heap() const + { + return is_on_heap(load_size_encoded()); + } + + constexpr T* push_uninit() + { + isize_t sz = size(); + resize_uninit_inner(sz + 1); + return data() + sz; + } + + constexpr void resize_uninit_inner(isize_t new_size) + { + isize_t old_size = size(); + if (!trivially_destructible && new_size < old_size) + { + destroy_n(data() + new_size, old_size - new_size); + } + reserve_capacity(new_size); + set_size(new_size); + } + + constexpr void set_size_inline(isize_t new_size) + { + ASL_ASSERT(new_size >= 0 && new_size <= kInlineCapacity); + size_t size_encoded = (load_size_encoded() & size_t{0x00ff'ffff'ffff'ffff}) | (bit_cast(new_size) << 56); + store_size_encoded(size_encoded); + } + + constexpr void set_size(isize_t new_size) + { + ASL_ASSERT(new_size >= 0 && new_size <= capacity()); + if (is_on_heap()) + { + store_size_encoded(encode_size_heap(new_size)); + } + else + { + set_size_inline(new_size); + } + } + + // NOLINTNEXTLINE(*-rvalue-reference-param-not-moved) + void move_from_other(buffer&& other, bool assign) + { + if (other.is_on_heap()) + { + destroy(); + m_data = other.m_data; + m_capacity = other.m_capacity; + store_size_encoded(other.load_size_encoded()); + } + else if (trivially_move_constructible) + { + destroy(); + asl::memcpy(this, &other, kInlineRegionSize); + } + else if (!assign || m_allocator == other.m_allocator) + { + isize_t other_n = other.size(); + isize_t this_n = size(); + resize_uninit_inner(other_n); + if (other_n <= this_n) + { + relocate_assign_n(data(), other.data(), other_n); + } + else + { + relocate_assign_n(data(), other.data(), this_n); + relocate_uninit_n(data() + this_n, other.data() + this_n, other_n - this_n); + } + } + else + { + destroy(); + isize_t n = other.size(); + ASL_ASSERT(n <= kInlineCapacity); + relocate_uninit_n(data(), other.data(), n); + set_size_inline(n); + } + + other.set_size_inline(0); + + if (assign) + { + m_allocator = ASL_MOVE(other.m_allocator); + } + } + + void copy_range(span to_copy) + { + isize_t this_size = size(); + isize_t new_size = to_copy.size(); + + resize_uninit_inner(to_copy.size()); + ASL_ASSERT(capacity() >= new_size); + ASL_ASSERT(size() == to_copy.size()); + + if (new_size <= this_size) + { + copy_assign_n(data(), to_copy.data(), new_size); + } + else + { + copy_assign_n(data(), to_copy.data(), this_size); + copy_uninit_n(data() + this_size, to_copy.data() + this_size, new_size - this_size); + } + } + + template + void resize_inner(isize_t new_size, Args&&... args) + requires constructible_from + { + ASL_ASSERT(new_size >= 0); + + isize_t old_size = size(); + resize_uninit_inner(new_size); + + T* data_ptr = data(); + T* end = data_ptr + new_size; + + // NOLINTNEXTLINE(*-pointer-arithmetic) + for (T* it = data_ptr + old_size; it < end; ++it) + { + construct_at(it, ASL_FWD(args)...); + } + } + +public: + constexpr buffer() requires default_constructible = default; + + explicit constexpr buffer(span s) + requires default_constructible + : buffer{} + { + copy_range(s); + } + + explicit constexpr buffer(Allocator allocator) + : m_allocator{ASL_MOVE(allocator)} + {} + + explicit constexpr buffer(span s, Allocator allocator) + : m_allocator{ASL_MOVE(allocator)} + { + copy_range(s); + } + + constexpr buffer(const buffer& other) + requires copy_constructible && copyable + : m_allocator{other.m_allocator} + { + copy_range(other); + } + + constexpr buffer(buffer&& other) + requires moveable + : buffer(ASL_MOVE(other.m_allocator)) + { + move_from_other(ASL_MOVE(other), false); + } + + constexpr buffer& operator=(const buffer& other) + requires copyable + { + if (&other == this) { return *this; } + copy_range(other); + return *this; + } + + constexpr buffer& operator=(buffer&& other) + requires moveable + { + if (&other == this) { return *this; } + move_from_other(ASL_MOVE(other), true); + return *this; + } + + ~buffer() + { + destroy(); + } + + constexpr isize_t size() const + { + return decode_size(load_size_encoded()); + } + + constexpr isize_t capacity() const + { + if constexpr (kInlineCapacity == 0) + { + return m_capacity; + } + else + { + return is_on_heap() ? m_capacity : kInlineCapacity; + } + } + + void clear() + { + isize_t current_size = size(); + if (current_size == 0) { return; } + + destroy_n(data(), current_size); + set_size(0); + } + + void destroy() + { + clear(); + if (is_on_heap()) + { + if (m_data != nullptr) + { + auto current_layout = layout::array(m_capacity); + m_allocator.dealloc(m_data, current_layout); + } + set_size_inline(0); + } + } + + void reserve_capacity(isize_t new_capacity) + { + ASL_ASSERT(new_capacity >= 0); + ASL_ASSERT_RELEASE(new_capacity <= 0x4000'0000'0000'0000); + + if (new_capacity <= capacity()) { return; } + ASL_ASSERT(new_capacity > kInlineCapacity); + + new_capacity = static_cast(round_up_pow2(static_cast(new_capacity))); + + T* old_data = data(); + const isize_t old_capacity = capacity(); + const isize_t current_size = size(); + const bool currently_on_heap = is_on_heap(); + + auto old_layout = layout::array(old_capacity); + auto new_layout = layout::array(new_capacity); + + if (currently_on_heap && trivially_move_constructible) + { + m_data = reinterpret_cast(m_allocator.realloc(m_data, old_layout, new_layout)); + m_capacity = new_capacity; + return; + } + + T* new_data = reinterpret_cast(m_allocator.alloc(new_layout)); + + relocate_uninit_n(new_data, old_data, current_size); + + if (currently_on_heap) + { + m_allocator.dealloc(old_data, old_layout); + } + + m_data = new_data; + m_capacity = new_capacity; + store_size_encoded(encode_size_heap(current_size)); + } + + constexpr void resize_uninit(isize_t new_size) + requires trivially_default_constructible && trivially_destructible + { + reserve_capacity(new_size); + set_size(new_size); + } + + constexpr void resize_zero(isize_t new_size) + requires trivially_default_constructible && trivially_destructible + { + isize_t old_size = size(); + resize_uninit(new_size); + + if (new_size > old_size) + { + memzero(data() + old_size, (new_size - old_size) * size_of); + } + } + + void resize(isize_t new_size) + requires default_constructible + { + resize_inner(new_size); + } + + void resize(isize_t new_size, const T& value) + { + resize_inner(new_size, value); + } + + constexpr T& push(auto&&... args) + requires constructible_from + { + T* uninit = push_uninit(); + T* init = construct_at(uninit, ASL_FWD(args)...); + return *init; + } + + // @Todo(C++23) Use deducing this + const T* data() const + { + if constexpr (kInlineCapacity == 0) + { + return m_data; + } + else + { + return is_on_heap() ? m_data : reinterpret_cast(this); + } + } + + T* data() + { + if constexpr (kInlineCapacity == 0) + { + return m_data; + } + else + { + return is_on_heap() ? m_data : reinterpret_cast(this); + } + } + + // @Todo(C++23) Use deducing this + constexpr contiguous_iterator begin() const { return contiguous_iterator{data()}; } + constexpr contiguous_iterator end() const { return contiguous_iterator{data() + size()}; } + + constexpr contiguous_iterator begin() { return contiguous_iterator{data()}; } + constexpr contiguous_iterator end() { return contiguous_iterator{data() + size()}; } + + // @Todo(C++23) Deducing this + constexpr operator span() const // NOLINT(*-explicit-conversions) + { + return as_span(); + } + + constexpr operator span() // NOLINT(*-explicit-conversions) + { + return as_span(); + } + + constexpr span as_span() const + { + return span{data(), size()}; + } + + constexpr span as_span() + { + return span{data(), size()}; + } + + // @Todo(C++23) Use deducing this + constexpr T& operator[](isize_t i) + { + ASL_ASSERT(i >= 0 && i <= size()); + return data()[i]; + } + + constexpr const T& operator[](isize_t i) const + { + ASL_ASSERT(i >= 0 && i <= size()); + return data()[i]; + } + + template + requires hashable + friend H AslHashValue(H h, const buffer& b) + { + return H::combine_contiguous(ASL_MOVE(h), b.as_span()); + } +}; + +} // namespace asl + diff --git a/asl/config.hpp b/asl/config.hpp index 1e28252..e182569 100644 --- a/asl/config.hpp +++ b/asl/config.hpp @@ -1,17 +1,17 @@ -#pragma once - -#if defined(_WIN32) - #define ASL_OS_WINDOWS 1 -#elif defined(__linux__) - #define ASL_OS_LINUX 1 -#else - #error Unknown OS -#endif - -#if defined(__clang__) && defined(_MSC_VER) - #define ASL_COMPILER_CLANG_CL 1 -#elif defined(__clang__) - #define ASL_COMPILER_CLANG 1 -#else - #error Unknown compiler -#endif +#pragma once + +#if defined(_WIN32) + #define ASL_OS_WINDOWS 1 +#elif defined(__linux__) + #define ASL_OS_LINUX 1 +#else + #error Unknown OS +#endif + +#if defined(__clang__) && defined(_MSC_VER) + #define ASL_COMPILER_CLANG_CL 1 +#elif defined(__clang__) + #define ASL_COMPILER_CLANG 1 +#else + #error Unknown compiler +#endif diff --git a/asl/float.hpp b/asl/float.hpp index 8ecf5fa..99c3f32 100644 --- a/asl/float.hpp +++ b/asl/float.hpp @@ -1,17 +1,17 @@ -#pragma once - -#include "asl/meta.hpp" - -namespace asl -{ - -template constexpr T infinity() { return __builtin_inf(); } - -template constexpr T nan() { return static_cast(__builtin_nanf("")); } - -template constexpr bool is_infinity(T f) { return __builtin_isinf(f); } - -template constexpr bool is_nan(T f) { return __builtin_isnan(f); } - -} // namespace asl - +#pragma once + +#include "asl/meta.hpp" + +namespace asl +{ + +template constexpr T infinity() { return __builtin_inf(); } + +template constexpr T nan() { return static_cast(__builtin_nanf("")); } + +template constexpr bool is_infinity(T f) { return __builtin_isinf(f); } + +template constexpr bool is_nan(T f) { return __builtin_isnan(f); } + +} // namespace asl + diff --git a/asl/format.cpp b/asl/format.cpp index 9e31888..91a7be9 100644 --- a/asl/format.cpp +++ b/asl/format.cpp @@ -1,201 +1,201 @@ -#include "asl/format.hpp" -#include "asl/utility.hpp" -#include "asl/assert.hpp" -#include "asl/memory.hpp" - -void asl::format_internals::format( - Writer* writer, - string_view fmt, - span args) -{ - Formatter f(writer); - - auto arg_it = args.begin(); - auto arg_end = args.end(); - - isize_t i = 0; - while (i < fmt.size()) - { - if (fmt[i] == '{') - { - if (i + 1 < fmt.size()) - { - if (fmt[i + 1] == '}') - { - f.write(fmt.substr(0, i)); - fmt = fmt.substr(i + 2); - i = 0; - - if (arg_it == arg_end) - { - f.write(""); - } - else - { - arg_it->fn(f, arg_it->data); - arg_it++; - } - - continue; - } - - if (fmt[i + 1] == '{') - { - f.write(fmt.substr(0, i + 1)); - fmt = fmt.substr(i + 2); - i = 0; - - continue; - } - } - - f.write(fmt.substr(0, i)); - fmt = fmt.substr(i + 1); - i = 0; - - f.write(""); - } - else if (i + 1 < fmt.size() && fmt[i] == '}' && fmt[i + 1] == '}') - { - f.write(fmt.substr(0, i + 1)); - fmt = fmt.substr(i + 2); - i = 0; - } - else - { - i += 1; - } - } - - f.write(fmt); -} - -void asl::AslFormat(Formatter& f, const char* str) -{ - f.write({str, asl::strlen(str)}); -} - -void asl::AslFormat(Formatter& f, bool v) -{ - if (v) - { - f.write("true"); - } - else - { - f.write("false"); - } -} - -void asl::AslFormat(Formatter& f, uint8_t v) -{ - AslFormat(f, static_cast(v)); -} - -void asl::AslFormat(Formatter& f, uint16_t v) -{ - AslFormat(f, static_cast(v)); -} - -void asl::AslFormat(Formatter& f, uint32_t v) -{ - AslFormat(f, static_cast(v)); -} - -static constexpr int32_t kMaxUint64Digits = 20; - -asl::string_view asl::format_uint64(uint64_t v, asl::span buffer) -{ - static constexpr char s_pairs_storage[] = { - '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', - '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', - '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', - '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', - '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', - '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', - '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', - '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', - '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', - '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', - '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', - '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', - '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', - '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', - '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', - '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', - '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', - '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', - '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', - '9', '5', '9', '6', '9', '7', '9', '8', '9', '9', - }; - - static constexpr span s_pairs = s_pairs_storage; - int32_t cursor = kMaxUint64Digits; - - auto write_two = [&buffer, &cursor](span str) - { - ASL_ASSERT(cursor >= 2); - buffer[--cursor] = str[1]; - buffer[--cursor] = str[0]; - }; - - auto write_one = [&buffer, &cursor](char c) - { - ASL_ASSERT(cursor >= 1); - buffer[--cursor] = c; - }; - - while (v >= 100) - { - uint64_t x = v % 100; - v /= 100; - write_two(s_pairs.subspan(static_cast(x * 2)).first<2>()); - } - - if (v >= 10) - { - write_two(s_pairs.subspan(static_cast(v * 2)).first<2>()); - } - else if (v > 0 || cursor == kMaxUint64Digits) - { - ASL_ASSERT(v < 10); - write_one(static_cast('0' + v)); - } - - return string_view(buffer.data(), kMaxUint64Digits).substr(cursor); -} - -void asl::AslFormat(Formatter& f, uint64_t v) -{ - char buffer[kMaxUint64Digits]; - f.write(format_uint64(v, buffer)); -} - -void asl::AslFormat(Formatter& f, int8_t v) -{ - AslFormat(f, static_cast(v)); -} - -void asl::AslFormat(Formatter& f, int16_t v) -{ - AslFormat(f, static_cast(v)); -} - -void asl::AslFormat(Formatter& f, int32_t v) -{ - AslFormat(f, static_cast(v)); -} - -void asl::AslFormat(Formatter& f, int64_t v) -{ - if (v < 0) - { - f.write("-"); - uint64_t absolute_value = ~(bit_cast(v) - 1); - AslFormat(f, absolute_value); - } - else - { - AslFormat(f, static_cast(v)); - } -} +#include "asl/format.hpp" +#include "asl/utility.hpp" +#include "asl/assert.hpp" +#include "asl/memory.hpp" + +void asl::format_internals::format( + Writer* writer, + string_view fmt, + span args) +{ + Formatter f(writer); + + auto arg_it = args.begin(); + auto arg_end = args.end(); + + isize_t i = 0; + while (i < fmt.size()) + { + if (fmt[i] == '{') + { + if (i + 1 < fmt.size()) + { + if (fmt[i + 1] == '}') + { + f.write(fmt.substr(0, i)); + fmt = fmt.substr(i + 2); + i = 0; + + if (arg_it == arg_end) + { + f.write(""); + } + else + { + arg_it->fn(f, arg_it->data); + arg_it++; + } + + continue; + } + + if (fmt[i + 1] == '{') + { + f.write(fmt.substr(0, i + 1)); + fmt = fmt.substr(i + 2); + i = 0; + + continue; + } + } + + f.write(fmt.substr(0, i)); + fmt = fmt.substr(i + 1); + i = 0; + + f.write(""); + } + else if (i + 1 < fmt.size() && fmt[i] == '}' && fmt[i + 1] == '}') +