WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
debugging.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_DEBUGGING_HPP_INCLUDED
5#define WPI_MEMORY_DEBUGGING_HPP_INCLUDED
6
7/// \file
8/// Debugging facilities.
9
10#include "config.hpp"
11
12namespace wpi
14 namespace memory
15 {
16 struct allocator_info;
17
18 /// The magic values that are used for debug filling.
19 /// If \ref WPI_MEMORY_DEBUG_FILL is \c true, memory will be filled to help detect use-after-free or missing initialization errors.
20 /// These are the constants for the different types.
21 /// \ingroup memory_core
22 enum class debug_magic : unsigned char
23 {
24 /// Marks internal memory used by the allocator - "allocated block".
25 internal_memory = 0xAB,
26 /// Marks internal memory currently not used by the allocator - "freed block".
28 /// Marks allocated, but not yet used memory - "clean memory".
29 new_memory = 0xCD,
30 /// Marks freed memory - "dead memory".
31 freed_memory = 0xDD,
32 /// Marks buffer memory used to ensure proper alignment.
33 /// This memory can also serve as \ref debug_magic::fence_memory.
34 alignment_memory = 0xED,
35 /// Marks buffer memory used to protect against overflow - "fence memory".
36 /// The option \ref WPI_MEMORY_DEBUG_FENCE controls the size of a memory fence that will be placed before or after a memory block.
37 /// It helps catching buffer overflows.
38 fence_memory = 0xFD
39 };
40
41 /// The type of the handler called when a memory leak is detected.
42 /// Leak checking can be controlled via the option \ref WPI_MEMORY_DEBUG_LEAK_CHECK
43 /// and only affects calls through the \ref allocator_traits, not direct calls.
44 /// The handler gets the \ref allocator_info and the amount of memory leaked.
45 /// This can also be negative, meaning that more memory has been freed than allocated.
46 /// \requiredbe A leak handler shall log the leak, abort the program, do nothing or anything else that seems appropriate.
47 /// It must not throw any exceptions since it is called in the cleanup process.
48 /// \defaultbe On a hosted implementation it logs the leak to \c stderr and returns, continuing execution.
49 /// On a freestanding implementation it does nothing.
50 /// \ingroup memory_core
51 using leak_handler = void (*)(const allocator_info& info, std::ptrdiff_t amount);
52
53 /// Exchanges the \ref leak_handler.
54 /// \effects Sets \c h as the new \ref leak_handler in an atomic operation.
55 /// A \c nullptr sets the default \ref leak_handler.
56 /// \returns The previous \ref leak_handler. This is never \c nullptr.
57 /// \ingroup memory_core
59
60 /// Returns the \ref leak_handler.
61 /// \returns The current \ref leak_handler. This is never \c nullptr.
62 /// \ingroup memory_core
64
65 /// The type of the handler called when an invalid pointer is passed to a deallocation function.
66 /// Pointer checking can be controlled via the options \ref WPI_MEMORY_DEBUG_POINTER_CHECK and \ref WPI_MEMORY_DEBUG_DOUBLE_DEALLOC_CHECK.
67 /// The handler gets the \ref allocator_info and the invalid pointer.
68 /// \requiredbe An invalid pointer handler shall terminate the program.
69 /// It must not throw any exceptions since it might be called in the cleanup process.
70 /// \defaultbe On a hosted implementation it logs the information to \c stderr and calls \c std::abort().
71 /// On a freestanding implementation it only calls \c std::abort().
72 /// \ingroup memory_core
73 using invalid_pointer_handler = void (*)(const allocator_info& info, const void* ptr);
74
75 /// Exchanges the \ref invalid_pointer_handler.
76 /// \effects Sets \c h as the new \ref invalid_pointer_handler in an atomic operation.
77 /// A \c nullptr sets the default \ref invalid_pointer_handler.
78 /// \returns The previous \ref invalid_pointer_handler. This is never \c nullptr.
79 /// \ingroup memory_core
81
82 /// Returns the \ref invalid_pointer_handler.
83 /// \returns The current \ref invalid_pointer_handler. This is never \c nullptr.
84 /// \ingroup memory_core
86
87 /// The type of the handler called when a buffer under/overflow is detected.
88 /// If \ref WPI_MEMORY_DEBUG_FILL is \c true and \ref WPI_MEMORY_DEBUG_FENCE has a non-zero value
89 /// the allocator classes check if a write into the fence has occured upon deallocation.
90 /// The handler gets the memory block belonging to the corrupted fence, its size and the exact address.
91 /// \requiredbe A buffer overflow handler shall terminate the program.
92 /// It must not throw any exceptions since it me be called in the cleanup process.
93 /// \defaultbe On a hosted implementation it logs the information to \c stderr and calls \c std::abort().
94 /// On a freestanding implementation it only calls \c std::abort().
95 /// \ingroup memory_core
96 using buffer_overflow_handler = void (*)(const void* memory, std::size_t size,
97 const void* write_ptr);
98
99 /// Exchanges the \ref buffer_overflow_handler.
100 /// \effects Sets \c h as the new \ref buffer_overflow_handler in an atomic operation.
101 /// A \c nullptr sets the default \ref buffer_overflow_handler.
102 /// \returns The previous \ref buffer_overflow_handler. This is never \c nullptr.
103 /// \ingroup memory_core
105
106 /// Returns the \ref buffer_overflow_handler.
107 /// \returns The current \ref buffer_overflow_handler. This is never \c nullptr.
108 /// \ingroup memory_core
110 } // namespace memory
111} // namespace wpi
112
113#endif // WPI_MEMORY_DEBUGGING_HPP_INCLUDED
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or memory
Definition ThirdPartyNotices.txt:58
Configuration macros.
auto ptr(T p) -> const void *
Converts p to const void* for pointer formatting.
Definition format.h:3821
invalid_pointer_handler get_invalid_pointer_handler()
Returns the invalid_pointer_handler.
void(*)(const void *memory, std::size_t size, const void *write_ptr) buffer_overflow_handler
The type of the handler called when a buffer under/overflow is detected.
Definition debugging.hpp:96
leak_handler set_leak_handler(leak_handler h)
Exchanges the leak_handler.
void(*)(const allocator_info &info, std::ptrdiff_t amount) leak_handler
The type of the handler called when a memory leak is detected.
Definition debugging.hpp:51
buffer_overflow_handler set_buffer_overflow_handler(buffer_overflow_handler h)
Exchanges the buffer_overflow_handler.
leak_handler get_leak_handler()
Returns the leak_handler.
void(*)(const allocator_info &info, const void *ptr) invalid_pointer_handler
The type of the handler called when an invalid pointer is passed to a deallocation function.
Definition debugging.hpp:73
invalid_pointer_handler set_invalid_pointer_handler(invalid_pointer_handler h)
Exchanges the invalid_pointer_handler.
buffer_overflow_handler get_buffer_overflow_handler()
Returns the buffer_overflow_handler.
debug_magic
The magic values that are used for debug filling.
Definition debugging.hpp:23
@ freed_memory
Marks freed memory - "dead memory".
@ alignment_memory
Marks buffer memory used to ensure proper alignment.
@ internal_memory
Marks internal memory used by the allocator - "allocated block".
@ fence_memory
Marks buffer memory used to protect against overflow - "fence memory".
@ new_memory
Marks allocated, but not yet used memory - "clean memory".
@ internal_freed_memory
Marks internal memory currently not used by the allocator - "freed block".
Memory namespace.
Definition heap_allocator.hpp:20
Foonathan namespace.
Definition ntcore_cpp.h:26
Contains information about an allocator.
Definition error.hpp:23