aligator  0.13.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
 
Loading...
Searching...
No Matches
allocator.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <Eigen/Core>
5#include <memory_resource>
6
7namespace aligator {
8
9using byte_t = std::byte;
10
17class polymorphic_allocator : public std::pmr::polymorphic_allocator<byte_t> {
18public:
19 using base = std::pmr::polymorphic_allocator<byte_t>;
20 polymorphic_allocator() noexcept : base{std::pmr::get_default_resource()} {}
21
23
24 template <typename U>
26 const std::pmr::polymorphic_allocator<U> &other) noexcept
27 : base{other} {}
28
29 polymorphic_allocator(std::pmr::memory_resource *resource) noexcept
30 : std::pmr::polymorphic_allocator<byte_t>(resource) {}
31
32 template <typename T>
33 [[nodiscard]] T *allocate(size_t n,
34 size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
35 return static_cast<T *>(resource()->allocate(n * sizeof(T), alignment));
36 }
37
38 template <typename T>
39 void deallocate(T *p, size_t n,
40 size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
41 resource()->deallocate(p, n * sizeof(T), alignment);
42 }
43
44 [[nodiscard]] void *
45 allocate_bytes(size_t num_bytes,
46 size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
47 return resource()->allocate(num_bytes, alignment);
48 }
49
50 void deallocate_bytes(void *p, size_t num_bytes,
51 size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
52 resource()->deallocate(p, num_bytes, alignment);
53 }
54};
55
56} // namespace aligator
57
58template <>
59struct std::allocator_traits<aligator::polymorphic_allocator>
60 : std::allocator_traits<aligator::polymorphic_allocator::base> {
61 using allocator_type = aligator::polymorphic_allocator;
62};
polymorphic_allocator(std::pmr::memory_resource *resource) noexcept
Definition allocator.hpp:29
void * allocate_bytes(size_t num_bytes, size_t alignment=EIGEN_DEFAULT_ALIGN_BYTES)
Definition allocator.hpp:45
T * allocate(size_t n, size_t alignment=EIGEN_DEFAULT_ALIGN_BYTES)
Definition allocator.hpp:33
std::pmr::polymorphic_allocator< byte_t > base
Definition allocator.hpp:19
polymorphic_allocator(const polymorphic_allocator &other)=default
void deallocate_bytes(void *p, size_t num_bytes, size_t alignment=EIGEN_DEFAULT_ALIGN_BYTES)
Definition allocator.hpp:50
void deallocate(T *p, size_t n, size_t alignment=EIGEN_DEFAULT_ALIGN_BYTES)
Definition allocator.hpp:39
polymorphic_allocator(const std::pmr::polymorphic_allocator< U > &other) noexcept
Definition allocator.hpp:25
Main package namespace.
std::byte byte_t
Definition allocator.hpp:9