WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
small_free_list.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_SMALL_FREE_LIST_HPP_INCLUDED
5#define WPI_MEMORY_DETAIL_SMALL_FREE_LIST_HPP_INCLUDED
6
7#include <cstddef>
8#include <climits>
9
10#include "../config.hpp"
11#include "utility.hpp"
12#include "align.hpp"
13
14namespace wpi
15{
16 namespace memory
17 {
18 namespace detail
19 {
21 {
24
25 unsigned char first_free = 0; // first free node for the linked list
26 unsigned char capacity = 0; // total number of free nodes available
27 unsigned char no_nodes = 0; // total number of nodes in memory
28
29 chunk_base() noexcept = default;
30
31 chunk_base(unsigned char no) noexcept : capacity(no), no_nodes(no) {}
32 };
33
34 constexpr std::size_t chunk_memory_offset =
35 sizeof(chunk_base) % detail::max_alignment == 0 ?
36 sizeof(chunk_base) :
38 constexpr std::size_t chunk_max_nodes = UCHAR_MAX;
39
40 struct chunk;
41
42 // the same as free_memory_list but optimized for small node sizes
43 // it is slower and does not support arrays
44 // but has very small overhead
45 // debug: allocate() and deallocate() mark memory as new and freed, respectively
46 // node_size is increased via two times fence size and fence is put in front and after
48 {
49 static constexpr std::size_t chunk_count(std::size_t number_of_nodes)
50 {
51 return number_of_nodes / chunk_max_nodes
52 + (number_of_nodes % chunk_max_nodes == 0 ? 0 : 1);
53 }
54
55 public:
56 // minimum element size
57 static constexpr std::size_t min_element_size = 1;
58 // alignment
59 static constexpr std::size_t min_element_alignment = 1;
60
61 // minimal size of the block that needs to be inserted
62 static constexpr std::size_t min_block_size(std::size_t node_size,
63 std::size_t number_of_nodes)
64 {
65 return chunk_count(number_of_nodes)
67 }
68
69 //=== constructor ===//
70 small_free_memory_list(std::size_t node_size) noexcept;
71
72 // does not own memory!
73 small_free_memory_list(std::size_t node_size, void* mem, std::size_t size) noexcept;
74
76
77 ~small_free_memory_list() noexcept = default;
78
80 {
82 swap(*this, tmp);
83 return *this;
84 }
85
87
88 //=== insert/alloc/dealloc ===//
89 // inserts new memory of given size into the free list
90 // mem must be aligned for maximum alignment
91 void insert(void* mem, std::size_t size) noexcept;
92
93 // returns the usable size
94 // i.e. how many memory will be actually inserted and usable on a call to insert()
95 std::size_t usable_size(std::size_t size) const noexcept;
96
97 // allocates a node big enough for the node size
98 // pre: !empty()
99 void* allocate() noexcept;
100
101 // always returns nullptr, because array allocations are not supported
102 void* allocate(std::size_t) noexcept
103 {
104 return nullptr;
105 }
106
107 // deallocates the node previously allocated via allocate()
108 void deallocate(void* node) noexcept;
109
110 // forwards to insert()
111 void deallocate(void* mem, std::size_t size) noexcept
112 {
113 insert(mem, size);
114 }
115
116 // hint for allocate() to be prepared to allocate n nodes
117 // it searches for a chunk that has n nodes free
118 // returns false, if there is none like that
119 // never fails for n == 1 if not empty()
120 // pre: capacity() >= n * node_size()
121 bool find_chunk(std::size_t n) noexcept
122 {
123 return find_chunk_impl(n) != nullptr;
124 }
125
126 //=== getter ===//
127 std::size_t node_size() const noexcept
128 {
129 return node_size_;
130 }
131
132 // the alignment of all nodes
133 std::size_t alignment() const noexcept;
134
135 // number of nodes remaining
136 std::size_t capacity() const noexcept
137 {
138 return capacity_;
139 }
140
141 bool empty() const noexcept
142 {
143 return capacity_ == 0u;
144 }
145
146 private:
147 chunk* find_chunk_impl(std::size_t n = 1) noexcept;
148 chunk* find_chunk_impl(unsigned char* node, chunk_base* first,
149 chunk_base* last) noexcept;
150 chunk* find_chunk_impl(unsigned char* node) noexcept;
151
152 chunk_base base_;
153 std::size_t node_size_, capacity_;
154 chunk_base *alloc_chunk_, *dealloc_chunk_;
155 };
156
157 // for some reason, this is required in order to define it
159 } // namespace detail
160 } // namespace memory
161} // namespace wpi
162
163#endif // WPI_MEMORY_DETAIL_SMALL_FREE_LIST_HPP_INCLUDED
Definition small_free_list.hpp:48
bool empty() const noexcept
Definition small_free_list.hpp:141
small_free_memory_list(std::size_t node_size) noexcept
bool find_chunk(std::size_t n) noexcept
Definition small_free_list.hpp:121
std::size_t usable_size(std::size_t size) const noexcept
static constexpr std::size_t min_element_size
Definition small_free_list.hpp:57
small_free_memory_list(std::size_t node_size, void *mem, std::size_t size) noexcept
void insert(void *mem, std::size_t size) noexcept
small_free_memory_list(small_free_memory_list &&other) noexcept
std::size_t node_size() const noexcept
Definition small_free_list.hpp:127
static constexpr std::size_t min_block_size(std::size_t node_size, std::size_t number_of_nodes)
Definition small_free_list.hpp:62
static constexpr std::size_t min_element_alignment
Definition small_free_list.hpp:59
std::size_t capacity() const noexcept
Definition small_free_list.hpp:136
std::size_t alignment() const noexcept
void deallocate(void *mem, std::size_t size) noexcept
Definition small_free_list.hpp:111
friend void swap(small_free_memory_list &a, small_free_memory_list &b) noexcept
Configuration macros.
detail namespace with internal helper functions
Definition input_adapters.h:32
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
constexpr std::size_t chunk_max_nodes
Definition small_free_list.hpp:38
constexpr std::size_t max_alignment
Definition align.hpp:42
std::remove_reference< T >::type && move(T &&arg) noexcept
Definition utility.hpp:25
constexpr std::size_t chunk_memory_offset
Definition small_free_list.hpp:34
Memory namespace.
Definition heap_allocator.hpp:20
Foonathan namespace.
Definition ntcore_cpp.h:26
Definition small_free_list.hpp:21
unsigned char capacity
Definition small_free_list.hpp:26
unsigned char no_nodes
Definition small_free_list.hpp:27
chunk_base * next
Definition small_free_list.hpp:23
chunk_base() noexcept=default
chunk_base * prev
Definition small_free_list.hpp:22
unsigned char first_free
Definition small_free_list.hpp:25