WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
free_list_array.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_FREE_LIST_ARRAY_HPP
5#define WPI_MEMORY_DETAIL_FREE_LIST_ARRAY_HPP
6
7#include "align.hpp"
8#include "assert.hpp"
9#include "memory_stack.hpp"
10#include "../config.hpp"
11
12namespace wpi
13{
14 namespace memory
15 {
16 namespace detail
17 {
18 // an array of free_memory_list types
19 // indexed via size, AccessPolicy does necessary conversions
20 // requires trivial destructible FreeList type
21 template <class FreeList, class AccessPolicy>
23 {
24 // not supported on GCC 4.7
25 //static_assert(std::is_trivially_destructible<FreeList>::value,
26 // "free list must be trivially destructible");
27 public:
28 // creates sufficient elements to support up to given maximum node size
29 // all lists are initially empty
30 // actual number is calculated via policy
31 // memory is taken from fixed_memory_stack, it must be sufficient
33 std::size_t max_node_size) noexcept
34 : no_elements_(AccessPolicy::index_from_size(max_node_size) - min_size_index + 1)
35 {
36 array_ = static_cast<FreeList*>(
37 stack.allocate(end, no_elements_ * sizeof(FreeList), alignof(FreeList)));
38 WPI_MEMORY_ASSERT_MSG(array_, "insufficient memory for free lists");
39 for (std::size_t i = 0u; i != no_elements_; ++i)
40 {
41 auto node_size = AccessPolicy::size_from_index(i + min_size_index);
42 ::new (static_cast<void*>(array_ + i)) FreeList(node_size);
43 }
44 }
45
46 // move constructor, does not actually move the elements, just the pointer
48 : array_(other.array_), no_elements_(other.no_elements_)
49 {
50 other.array_ = nullptr;
51 other.no_elements_ = 0u;
52 }
53
54 // destructor, does nothing, list must be trivially destructible!
55 ~free_list_array() noexcept = default;
56
57 free_list_array& operator=(free_list_array&& other) noexcept
58 {
59 array_ = other.array_;
60 no_elements_ = other.no_elements_;
61
62 other.array_ = nullptr;
63 other.no_elements_ = 0u;
64 return *this;
65 }
66
67 // access free list for given size
68 FreeList& get(std::size_t node_size) const noexcept
69 {
70 auto i = AccessPolicy::index_from_size(node_size);
71 if (i < min_size_index)
72 i = min_size_index;
73 return array_[i - min_size_index];
74 }
75
76 // number of free lists
77 std::size_t size() const noexcept
78 {
79 return no_elements_;
80 }
81
82 // maximum supported node size
83 std::size_t max_node_size() const noexcept
84 {
85 return AccessPolicy::size_from_index(no_elements_ + min_size_index - 1);
86 }
87
88 private:
89 static const std::size_t min_size_index;
90
91 FreeList* array_;
92 std::size_t no_elements_;
93 };
94
95 template <class FL, class AP>
96 const std::size_t free_list_array<FL, AP>::min_size_index =
97 AP::index_from_size(FL::min_element_size);
98
99 // AccessPolicy that maps size to indices 1:1
100 // creates a free list for each size!
102 {
103 static std::size_t index_from_size(std::size_t size) noexcept
104 {
105 return size;
106 }
107
108 static std::size_t size_from_index(std::size_t index) noexcept
109 {
110 return index;
111 }
112 };
113
114 // AccessPolicy that maps sizes to the integral log2
115 // this creates more nodes and never wastes more than half the size
117 {
118 static std::size_t index_from_size(std::size_t size) noexcept;
119 static std::size_t size_from_index(std::size_t index) noexcept;
120 };
121 } // namespace detail
122 } // namespace memory
123} // namespace wpi
124
125#endif //WPI_MEMORY_DETAIL_FREE_LIST_ARRAY_HPP
Definition memory_stack.hpp:22
Definition free_list_array.hpp:23
std::size_t size() const noexcept
Definition free_list_array.hpp:77
free_list_array(free_list_array &&other) noexcept
Definition free_list_array.hpp:47
free_list_array(fixed_memory_stack &stack, const char *end, std::size_t max_node_size) noexcept
Definition free_list_array.hpp:32
std::size_t max_node_size() const noexcept
Definition free_list_array.hpp:83
FreeList & get(std::size_t node_size) const noexcept
Definition free_list_array.hpp:68
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:184
Configuration macros.
detail namespace with internal helper functions
Definition input_adapters.h:32
Memory namespace.
Definition heap_allocator.hpp:20
Foonathan namespace.
Definition ntcore_cpp.h:26
Definition free_list_array.hpp:102
static std::size_t index_from_size(std::size_t size) noexcept
Definition free_list_array.hpp:103
static std::size_t size_from_index(std::size_t index) noexcept
Definition free_list_array.hpp:108
Definition free_list_array.hpp:117
static std::size_t size_from_index(std::size_t index) noexcept
static std::size_t index_from_size(std::size_t size) noexcept
#define WPI_MEMORY_ASSERT_MSG(Expr, Msg)
Definition assert.hpp:47