WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
lowlevel_allocator.hpp
Go to the documentation of this file.
1// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors
2// SPDX-License-Identifier: Zlib
3
4#ifndef WPI_MEMORY_DETAIL_LOWLEVEL_ALLOCATOR_HPP_INCLUDED
5#define WPI_MEMORY_DETAIL_LOWLEVEL_ALLOCATOR_HPP_INCLUDED
6
7#include <type_traits>
8
9#include "../config.hpp"
10#include "../error.hpp"
11#include "align.hpp"
12#include "debug_helpers.hpp"
13#include "assert.hpp"
14
15namespace wpi
16{
17 namespace memory
18 {
19 namespace detail
20 {
21 template <class Functor>
23 {
24 void operator()(std::ptrdiff_t amount)
25 {
26 debug_handle_memory_leak(Functor::info(), amount);
27 }
28 };
29
30 // Functor controls low-level allocation:
31 // static allocator_info info()
32 // static void* allocate(std::size_t size, std::size_t alignment);
33 // static void deallocate(void *memory, std::size_t size, std::size_t alignment);
34 // static std::size_t max_node_size();
35 template <class Functor>
36 class lowlevel_allocator : global_leak_checker<lowlevel_allocator_leak_handler<Functor>>
37 {
38 public:
39 using is_stateful = std::false_type;
40
41 lowlevel_allocator() noexcept {}
43 ~lowlevel_allocator() noexcept {}
44
46 {
47 return *this;
48 }
49
50 void* allocate_node(std::size_t size, std::size_t alignment)
51 {
52 auto actual_size = size + (debug_fence_size ? 2 * max_alignment : 0u);
53
54 auto memory = Functor::allocate(actual_size, alignment);
55 if (!memory)
56 WPI_THROW(out_of_memory(Functor::info(), actual_size));
57
58 this->on_allocate(actual_size);
59
60 return debug_fill_new(memory, size, max_alignment);
61 }
62
63 void deallocate_node(void* node, std::size_t size, std::size_t alignment) noexcept
64 {
65 auto actual_size = size + (debug_fence_size ? 2 * max_alignment : 0u);
66
67 auto memory = debug_fill_free(node, size, max_alignment);
68 Functor::deallocate(memory, actual_size, alignment);
69
70 this->on_deallocate(actual_size);
71 }
72
73 std::size_t max_node_size() const noexcept
74 {
75 return Functor::max_node_size();
76 }
77 };
78
79#define WPI_MEMORY_LL_ALLOCATOR_LEAK_CHECKER(functor, var_name) \
80 WPI_MEMORY_GLOBAL_LEAK_CHECKER(lowlevel_allocator_leak_handler<functor>, var_name)
81 } // namespace detail
82 } // namespace memory
83} // namespace wpi
84
85#endif // WPI_MEMORY_DETAIL_LOWLEVEL_ALLOCATOR_HPP_INCLUDED
Definition lowlevel_allocator.hpp:37
~lowlevel_allocator() noexcept
Definition lowlevel_allocator.hpp:43
std::false_type is_stateful
Definition lowlevel_allocator.hpp:39
void deallocate_node(void *node, std::size_t size, std::size_t alignment) noexcept
Definition lowlevel_allocator.hpp:63
lowlevel_allocator() noexcept
Definition lowlevel_allocator.hpp:41
lowlevel_allocator & operator=(lowlevel_allocator &&) noexcept
Definition lowlevel_allocator.hpp:45
void * allocate_node(std::size_t size, std::size_t alignment)
Definition lowlevel_allocator.hpp:50
lowlevel_allocator(lowlevel_allocator &&) noexcept
Definition lowlevel_allocator.hpp:42
std::size_t max_node_size() const noexcept
Definition lowlevel_allocator.hpp:73
Definition debug_helpers.hpp:102
void on_allocate(std::size_t) noexcept
Definition debug_helpers.hpp:113
void on_deallocate(std::size_t) noexcept
Definition debug_helpers.hpp:114
The exception class thrown when a low level allocator runs out of memory.
Definition error.hpp:65
Configuration macros.
#define WPI_THROW(Ex)
Definition config.hpp:33
The exception classes.
detail namespace with internal helper functions
Definition input_adapters.h:32
void * debug_fill_new(void *memory, std::size_t, std::size_t) noexcept
Definition debug_helpers.hpp:52
constexpr std::size_t max_alignment
Definition align.hpp:42
void * debug_fill_free(void *memory, std::size_t, std::size_t) noexcept
Definition debug_helpers.hpp:57
void debug_handle_memory_leak(const allocator_info &info, std::ptrdiff_t amount)
constexpr std::size_t debug_fence_size
Definition debug_helpers.hpp:22
Memory namespace.
Definition heap_allocator.hpp:20
Foonathan namespace.
Definition ntcore_cpp.h:26
Definition lowlevel_allocator.hpp:23
void operator()(std::ptrdiff_t amount)
Definition lowlevel_allocator.hpp:24