di 0.1.0
Loading...
Searching...
No Matches
allocate_many.h
Go to the documentation of this file.
1#pragma once
2
10#include "di/meta/vocab.h"
11#include "di/platform/prelude.h"
15
16namespace di::container {
17namespace detail {
18 template<typename T>
19 struct AllocateManyFunction {
20 template<concepts::Allocator Alloc>
21 constexpr auto operator()(Alloc& allocator, usize count) const
23 if consteval {
24 auto* result = std::allocator<T>().allocate(count);
25 return AllocationResult<T> { result, count };
26 }
27
28 if constexpr (concepts::FallibleAllocator<Alloc>) {
29 auto byte_size = math::Checked(count) * sizeof(T);
30 if (byte_size.invalid()) {
31 return vocab::Unexpected(BasicError::ValueTooLarge);
32 }
33
34 auto result = DI_TRY(di::allocate(allocator, *byte_size.value(), alignof(T)));
35 return AllocationResult<T> { static_cast<T*>(result.data), result.count / sizeof(T) };
36 } else {
37 auto byte_size = math::Checked(count) * sizeof(T);
38 DI_ASSERT(!byte_size.invalid());
39
40 auto result = di::allocate(allocator, *byte_size.value(), alignof(T));
41 return AllocationResult<T> { static_cast<T*>(result.data), result.count / sizeof(T) };
42 }
43 }
44 };
45}
46
47template<typename T>
48constexpr inline auto allocate_many = detail::AllocateManyFunction<T> {};
49}
50
51namespace di {
53}
#define DI_ASSERT(...)
Definition assert_bool.h:7
#define DI_TRY(...)
Definition monad_try.h:13
Definition sequence.h:12
constexpr auto count
Definition count.h:37
constexpr auto allocate_many
Definition allocate_many.h:48
meta::LikeExpected< decltype(di::allocate(util::declval< Alloc & >(), 0, 0)), T > AllocatorResult
Definition allocator.h:25
size_t usize
Definition integers.h:33
Unexpected(E &&) -> Unexpected< meta::UnwrapRefDecay< E > >
Definition any_storable.h:9
constexpr auto allocate
Definition allocate.h:26