aboutsummaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-03-19 08:08:32 +0100
committerFelix Morgner <felix.morgner@ost.ch>2026-03-19 08:08:32 +0100
commitcc55324b0f3a988befaecff15e0ff87e3c6a4dee (patch)
treea400b9101272c30718ea76f0543c17772ee66568 /libs
parentc049d767c7d41d04226f2c6b01ee2e07aae7ea1f (diff)
downloadteachos-cc55324b0f3a988befaecff15e0ff87e3c6a4dee.tar.xz
teachos-cc55324b0f3a988befaecff15e0ff87e3c6a4dee.zip
kstd: implement default allocator
Diffstat (limited to 'libs')
-rw-r--r--libs/kstd/include/kstd/allocator64
1 files changed, 64 insertions, 0 deletions
diff --git a/libs/kstd/include/kstd/allocator b/libs/kstd/include/kstd/allocator
new file mode 100644
index 0000000..0de0e10
--- /dev/null
+++ b/libs/kstd/include/kstd/allocator
@@ -0,0 +1,64 @@
+#ifndef KSTD_ALLOCATOR_HPP
+#define KSTD_ALLOCATOR_HPP
+
+#include <cstddef>
+#include <new>
+#include <type_traits>
+
+#if __has_builtin(__builtin_operator_new) >= 201'802L
+#define KSTD_OPERATOR_NEW __builtin_operator_new
+#define KSTD_OPERATOR_DELETE __builtin_operator_delete
+#else
+#define KSTD_OPERATOR_NEW ::operator new
+#define KSTD_OPERATOR_DELETE ::operator delete
+#endif
+
+namespace kstd
+{
+
+ template<typename T>
+ struct allocator
+ {
+ using value_type = T;
+ using size_type = std::size_t;
+ using difference_type = std::ptrdiff_t;
+ using propagate_on_container_move_assignment = std::true_type;
+ using is_always_equal = std::true_type;
+
+ constexpr allocator() noexcept = default;
+
+ template<typename U>
+ constexpr allocator(allocator<U> const &) noexcept
+ {}
+
+ constexpr auto allocate(std::size_t n) -> T *
+ {
+ if (alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+ {
+ return static_cast<T *>(KSTD_OPERATOR_NEW(n * sizeof(T), std::align_val_t{alignof(T)}));
+ }
+ return static_cast<T *>(KSTD_OPERATOR_NEW(n * sizeof(T)));
+ }
+
+ constexpr void deallocate(T * p, std::size_t n) noexcept
+ {
+ if (alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+ {
+ KSTD_OPERATOR_DELETE(p, n * sizeof(T), std::align_val_t{alignof(T)});
+ }
+ KSTD_OPERATOR_DELETE(p, n * sizeof(T));
+ }
+ };
+
+ template<typename T, typename U>
+ constexpr auto operator==(allocator<T> const &, allocator<U> const &) noexcept -> bool
+ {
+ return true;
+ }
+
+} // namespace kstd
+
+#undef KSTD_OPERATOR_NEW
+#undef KSTD_OPERATOR_DELETE
+
+#endif \ No newline at end of file