Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
memory_diagnostics.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <uni20/config.hpp>
4
5#include <cstddef>
6
7// Use the active translation unit's instrumentation, independently of build type.
8#if defined(__has_feature)
9#if __has_feature(memory_sanitizer)
10#define UNI20_HAS_MEMORY_SANITIZER 1
11#endif
12#endif
13#ifndef UNI20_HAS_MEMORY_SANITIZER
14#define UNI20_HAS_MEMORY_SANITIZER 0
15#endif
16
17#if UNI20_HAS_MEMORY_SANITIZER
18#include <sanitizer/msan_interface.h>
19#endif
20#if UNI20_ENABLE_VALGRIND
21#include <valgrind/memcheck.h>
22#endif
23
24namespace uni20::detail::memory_diagnostics
25{
26
30inline void mark_uninitialized(void* data, std::size_t bytes) noexcept
31{
32 if (bytes == 0) return;
33#if UNI20_HAS_MEMORY_SANITIZER
34 __msan_allocated_memory(data, bytes);
35#endif
36#if UNI20_ENABLE_VALGRIND
37 (void)VALGRIND_MAKE_MEM_UNDEFINED(data, bytes);
38#endif
39 (void)data;
40}
41
44inline void check_initialized(void const* data, std::size_t bytes) noexcept
45{
46 if (bytes == 0) return;
47#if UNI20_HAS_MEMORY_SANITIZER
48 __msan_check_mem_is_initialized(data, bytes);
49#endif
50#if UNI20_ENABLE_VALGRIND
51 (void)VALGRIND_CHECK_MEM_IS_DEFINED(data, bytes);
52#endif
53 (void)data;
54}
55
59inline void mark_initialized(void* data, std::size_t bytes) noexcept
60{
61 if (bytes == 0) return;
62#if UNI20_HAS_MEMORY_SANITIZER
63 __msan_unpoison(data, bytes);
64#endif
65#if UNI20_ENABLE_VALGRIND
66 (void)VALGRIND_MAKE_MEM_DEFINED(data, bytes);
67#endif
68 (void)data;
69}
70
71} // namespace uni20::detail::memory_diagnostics
std::size_t bytes
Definition presentation.cpp:18