WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
heap_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_HEAP_ALLOCATOR_HPP_INCLUDED
5#define WPI_MEMORY_HEAP_ALLOCATOR_HPP_INCLUDED
6
7/// \file
8/// Class \ref wpi::memory::heap_allocator and related functions.
9
11#include "config.hpp"
12
13#if WPI_MEMORY_EXTERN_TEMPLATE
14#include "allocator_traits.hpp"
15#endif
16
17namespace wpi
18{
19 namespace memory
20 {
21 struct allocator_info;
22
23 /// Allocates heap memory.
24 /// This function is used by the \ref heap_allocator to allocate the heap memory.
25 /// It is not defined on a freestanding implementation, a definition must be provided by the library user.
26 /// \requiredbe This function shall return a block of uninitialized memory that is aligned for \c max_align_t and has the given size.
27 /// The size parameter will not be zero.
28 /// It shall return a \c nullptr if no memory is available.
29 /// It must be thread safe.
30 /// \defaultbe On a hosted implementation this function uses OS specific facilities, \c std::malloc is used as fallback.
31 /// \ingroup memory_allocator
32 void* heap_alloc(std::size_t size) noexcept;
33
34 /// Deallocates heap memory.
35 /// This function is used by the \ref heap_allocator to allocate the heap memory.
36 /// It is not defined on a freestanding implementation, a definition must be provided by the library user.
37 /// \requiredbe This function gets a pointer from a previous call to \ref heap_alloc with the same size.
38 /// It shall free the memory.
39 /// The pointer will not be zero.
40 /// It must be thread safe.
41 /// \defaultbe On a hosted implementation this function uses OS specific facilities, \c std::free is used as fallback.
42 /// \ingroup memory_allocator
43 void heap_dealloc(void* ptr, std::size_t size) noexcept;
44
45 namespace detail
46 {
48 {
49 static allocator_info info() noexcept;
50
51 static void* allocate(std::size_t size, std::size_t) noexcept
52 {
53 return heap_alloc(size);
54 }
55
56 static void deallocate(void* ptr, std::size_t size, std::size_t) noexcept
57 {
58 heap_dealloc(ptr, size);
59 }
60
61 static std::size_t max_node_size() noexcept;
62 };
63
65 heap_alloator_leak_checker)
66 } // namespace detail
67
68 /// A stateless RawAllocator that allocates memory from the heap.
69 /// It uses the two functions \ref heap_alloc and \ref heap_dealloc for the allocation,
70 /// which default to \c std::malloc and \c std::free.
71 /// \ingroup memory_allocator
73 WPI_IMPL_DEFINED(detail::lowlevel_allocator<detail::heap_allocator_impl>);
74
75#if WPI_MEMORY_EXTERN_TEMPLATE
77 extern template class allocator_traits<heap_allocator>;
78#endif
79 } // namespace memory
80} // namespace wpi
81
82#endif // WPI_MEMORY_HEAP_ALLOCATOR_HPP_INCLUDED
The default specialization of the wpi::memory::allocator_traits.
The default specialization of the allocator_traits for a RawAllocator.
Definition allocator_traits.hpp:292
Definition lowlevel_allocator.hpp:37
Configuration macros.
auto ptr(T p) -> const void *
Converts p to const void* for pointer formatting.
Definition format.h:3821
void heap_dealloc(void *ptr, std::size_t size) noexcept
Deallocates heap memory.
void * heap_alloc(std::size_t size) noexcept
Allocates heap memory.
implementation_defined heap_allocator
A stateless RawAllocator that allocates memory from the heap.
Definition heap_allocator.hpp:72
#define WPI_MEMORY_LL_ALLOCATOR_LEAK_CHECKER(functor, var_name)
Definition lowlevel_allocator.hpp:79
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
Memory namespace.
Definition heap_allocator.hpp:20
Foonathan namespace.
Definition ntcore_cpp.h:26
Contains information about an allocator.
Definition error.hpp:23
Definition heap_allocator.hpp:48
static allocator_info info() noexcept
static void * allocate(std::size_t size, std::size_t) noexcept
Definition heap_allocator.hpp:51
static void deallocate(void *ptr, std::size_t size, std::size_t) noexcept
Definition heap_allocator.hpp:56
static std::size_t max_node_size() noexcept