cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
use_pointer.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
23
24#if defined(__GNUC__) || defined(__clang__)
25
26/* *****************************************************************
27 * For GCC and Clang with inline assembly.
28 * *****************************************************************/
29
30namespace stat_bench {
31namespace impl {
32
33void use_pointer(void* ptr) noexcept {
34 // NOLINTNEXTLINE
35 asm volatile("" : : "g"(ptr) : "memory");
36}
37
38} // namespace impl
39} // namespace stat_bench
40
41#else
42
43/* *****************************************************************
44 * Fallback implementation using volatile.
45 * *****************************************************************/
46
47#include <atomic>
48
49namespace stat_bench {
50namespace impl {
51
52void use_pointer(void* ptr) noexcept {
53 std::atomic_signal_fence(std::memory_order_acq_rel);
54 *reinterpret_cast<char volatile*>(ptr) =
55 *reinterpret_cast<char volatile*>(ptr);
56 std::atomic_signal_fence(std::memory_order_acq_rel);
57}
58
59} // namespace impl
60} // namespace stat_bench
61
62#endif
Definition of do_not_optimize function.
Namespace of internal implementation.
void use_pointer(void *ptr) noexcept
Use a pointer to prevent deletion of a variable.
Namespace of stat_bench source codes.