co_ecs 0.9.0
Cobalt ECS
Loading...
Searching...
No Matches
temp_allocator.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace co_ecs::detail {
6
7constexpr auto global_stack_allocator_size = 16ull * 1024 * 1024; // 16 MB
8
9static thread_local inline std::unique_ptr<uint8_t[]> global_stack_allocator_buffer(
10 new uint8_t[global_stack_allocator_size]);
11
13static thread_local inline stack_allocator global_stack_allocator{ global_stack_allocator_buffer.get(),
15
18template<typename T>
19class temp_allocator {
20public:
21 using value_type = T;
22 using size_type = std::size_t;
23 using difference_type = std::ptrdiff_t;
24 using propagate_on_container_move_assignment = std::true_type;
25
27 temp_allocator() = default;
28
32 template<class U>
33 constexpr temp_allocator(const temp_allocator<U>& other) noexcept {
34 }
35
39 [[nodiscard]] constexpr T* allocate(std::size_t n) {
40 auto* ptr = static_cast<T*>(global_stack_allocator.allocate(n * sizeof(T), alignof(T)));
41 if (!ptr) {
42 throw std::bad_alloc{};
43 }
44 return ptr;
45 }
46
51 constexpr void deallocate(T* p, [[maybe_unused]] std::size_t n) {
52 return global_stack_allocator.deallocate(p);
53 }
54};
55
56} // namespace co_ecs::detail
57
58
59template<class T, class U>
60constexpr bool operator==(const co_ecs::detail::temp_allocator<T>&, const co_ecs::detail::temp_allocator<U>&) noexcept {
61 return true;
62}
63
64template<class T, class U>
65constexpr bool operator!=(const co_ecs::detail::temp_allocator<T>&, const co_ecs::detail::temp_allocator<U>&) noexcept {
66 return false;
67}
Definition component.hpp:17
constexpr auto global_stack_allocator_size
Definition temp_allocator.hpp:7
constexpr bool operator==(const co_ecs::detail::temp_allocator< T > &, const co_ecs::detail::temp_allocator< U > &) noexcept
Definition temp_allocator.hpp:60
constexpr bool operator!=(const co_ecs::detail::temp_allocator< T > &, const co_ecs::detail::temp_allocator< U > &) noexcept
Definition temp_allocator.hpp:65