tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
monotonic_allocator.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_MEMORY_MONOTONIC_ALLOCATOR_HPP
4#define TETL_MEMORY_MONOTONIC_ALLOCATOR_HPP
5
10#include <etl/_span/span.hpp>
11
12namespace etl {
13template <typename T>
15 using value_type = T;
18
20 : _memory{memory}
21 {
22 }
23
24 [[nodiscard]] auto allocate(etl::size_t n) -> T*
25 {
26 if (etl::align(alignof(T), sizeof(T) * n, _ptr, _sz) != nullptr) {
27 auto* result = reinterpret_cast<T*>(_ptr);
28 _ptr = reinterpret_cast<char*>(_ptr) + sizeof(T) * n;
29 _sz -= sizeof(T) * n;
30 return result;
31 }
32 return nullptr;
33 }
34
35 auto deallocate(T* p, etl::size_t n) -> void { etl::ignore_unused(p, n); }
36
37private:
39 void* _ptr{_memory.data()};
40 etl::size_t _sz{_memory.size()};
41};
42
43} // namespace etl
44
45#endif // TETL_MEMORY_MONOTONIC_ALLOCATOR_HPP
Definition adjacent_find.hpp:8
auto align(etl::size_t alignment, etl::size_t size, void *&ptr, etl::size_t &space) noexcept -> void *
Given a pointer ptr to a buffer of size space, returns a pointer aligned by the specified alignment f...
Definition align.hpp:22
TETL_BUILTIN_PTRDIFF ptrdiff_t
etl::ptrdiff_t is the signed integer type of the result of subtracting two pointers.
Definition ptrdiff_t.hpp:14
constexpr auto ignore_unused(Types &&...) -> void
Explicitly ignore arguments or variables.
Definition ignore_unused.hpp:17
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:14
etl::ptrdiff_t difference_type
Definition monotonic_allocator.hpp:17
etl::size_t size_type
Definition monotonic_allocator.hpp:16
auto deallocate(T *p, etl::size_t n) -> void
Definition monotonic_allocator.hpp:35
monotonic_allocator(etl::span< etl::byte > memory)
Definition monotonic_allocator.hpp:19
auto allocate(etl::size_t n) -> T *
Definition monotonic_allocator.hpp:24
T value_type
Definition monotonic_allocator.hpp:15
A non-owning view over a contiguous sequence of objects.
Definition span.hpp:83
constexpr auto data() const noexcept -> pointer
Returns a pointer to the beginning of the sequence.
Definition span.hpp:214
constexpr auto size() const noexcept -> size_type
Returns the number of elements in the span.
Definition span.hpp:217