WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
malloc_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_MALLOC_ALLOCATOR_HPP_INCLUDED
5#define WPI_MEMORY_MALLOC_ALLOCATOR_HPP_INCLUDED
6
7/// \file
8/// Class \ref wpi::memory::malloc_allocator.
9/// \note Only available on a hosted implementation.
10
11#include "config.hpp"
12#if !WPI_HOSTED_IMPLEMENTATION
13#error "This header is only available for a hosted implementation."
14#endif
15
16#include <cstdlib>
17#include <memory>
18
20
21#if WPI_MEMORY_EXTERN_TEMPLATE
22#include "allocator_traits.hpp"
23#endif
24
25namespace wpi
26{
27 namespace memory
28 {
29 struct allocator_info;
30
31 namespace detail
32 {
34 {
35 static allocator_info info() noexcept;
36
37 static void* allocate(std::size_t size, std::size_t) noexcept
38 {
39 return std::malloc(size);
40 }
41
42 static void deallocate(void* ptr, std::size_t, std::size_t) noexcept
43 {
44 std::free(ptr);
45 }
46
47 static std::size_t max_node_size() noexcept
48 {
49 return std::allocator_traits<std::allocator<char>>::max_size({});
50 }
51 };
52
53 WPI_MEMORY_LL_ALLOCATOR_LEAK_CHECKER(malloc_allocator_impl,
54 malloc_alloator_leak_checker)
55 } // namespace detail
56
57 /// A stateless RawAllocator that allocates memory using <tt>std::malloc()</tt>.
58 /// It throws \ref out_of_memory when the allocation fails.
59 /// \ingroup memory_allocator
62
63#if WPI_MEMORY_EXTERN_TEMPLATE
65 extern template class allocator_traits<malloc_allocator>;
66#endif
67 } // namespace memory
68} // namespace wpi
69
70#endif //WPI_MEMORY_MALLOC_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
implementation_defined malloc_allocator
A stateless RawAllocator that allocates memory using std::malloc().
Definition malloc_allocator.hpp:60
#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 malloc_allocator.hpp:34
static allocator_info info() noexcept
static std::size_t max_node_size() noexcept
Definition malloc_allocator.hpp:47
static void deallocate(void *ptr, std::size_t, std::size_t) noexcept
Definition malloc_allocator.hpp:42
static void * allocate(std::size_t size, std::size_t) noexcept
Definition malloc_allocator.hpp:37