WPILibC++ 2024.3.2
EpochTracker.h
Go to the documentation of this file.
1//===- llvm/ADT/EpochTracker.h - ADT epoch tracking --------------*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file defines the DebugEpochBase and DebugEpochBase::HandleBase classes.
11/// These can be used to write iterators that are fail-fast when LLVM is built
12/// with asserts enabled.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef WPIUTIL_WPI_EPOCHTRACKER_H
17#define WPIUTIL_WPI_EPOCHTRACKER_H
18
19
20#include <cstdint>
21
22namespace wpi {
23
24#ifndef NDEBUG //ifndef LLVM_ENABLE_ABI_BREAKING_CHECKS
25#define LLVM_DEBUGEPOCHBASE_HANDLEBASE_EMPTYBASE
26
27/// A base class for data structure classes wishing to make iterators
28/// ("handles") pointing into themselves fail-fast. When building without
29/// asserts, this class is empty and does nothing.
30///
31/// DebugEpochBase does not by itself track handles pointing into itself. The
32/// expectation is that routines touching the handles will poll on
33/// isHandleInSync at appropriate points to assert that the handle they're using
34/// is still valid.
35///
37 uint64_t Epoch = 0;
38
39public:
40 DebugEpochBase() = default;
41
42 /// Calling incrementEpoch invalidates all handles pointing into the
43 /// calling instance.
44 void incrementEpoch() { ++Epoch; }
45
46 /// The destructor calls incrementEpoch to make use-after-free bugs
47 /// more likely to crash deterministically.
49
50 /// A base class for iterator classes ("handles") that wish to poll for
51 /// iterator invalidating modifications in the underlying data structure.
52 /// When LLVM is built without asserts, this class is empty and does nothing.
53 ///
54 /// HandleBase does not track the parent data structure by itself. It expects
55 /// the routines modifying the data structure to call incrementEpoch when they
56 /// make an iterator-invalidating modification.
57 ///
58 class HandleBase {
59 const uint64_t *EpochAddress = nullptr;
60 uint64_t EpochAtCreation = UINT64_MAX;
61
62 public:
63 HandleBase() = default;
64
65 explicit HandleBase(const DebugEpochBase *Parent)
66 : EpochAddress(&Parent->Epoch), EpochAtCreation(Parent->Epoch) {}
67
68 /// Returns true if the DebugEpochBase this Handle is linked to has
69 /// not called incrementEpoch on itself since the creation of this
70 /// HandleBase instance.
71 bool isHandleInSync() const { return *EpochAddress == EpochAtCreation; }
72
73 /// Returns a pointer to the epoch word stored in the data structure
74 /// this handle points into. Can be used to check if two iterators point
75 /// into the same data structure.
76 const void *getEpochAddress() const { return EpochAddress; }
77 };
78};
79
80#else
81#ifdef _MSC_VER
82#define LLVM_DEBUGEPOCHBASE_HANDLEBASE_EMPTYBASE __declspec(empty_bases)
83#else
84#define LLVM_DEBUGEPOCHBASE_HANDLEBASE_EMPTYBASE
85#endif // _MSC_VER
86
87class DebugEpochBase {
88public:
89 void incrementEpoch() {}
90
91 class HandleBase {
92 public:
93 HandleBase() = default;
94 explicit HandleBase(const DebugEpochBase *) {}
95 bool isHandleInSync() const { return true; }
96 const void *getEpochAddress() const { return nullptr; }
97 };
98};
99
100#endif // LLVM_ENABLE_ABI_BREAKING_CHECKS
101
102} // namespace wpi
103
104#endif
A base class for iterator classes ("handles") that wish to poll for iterator invalidating modificatio...
Definition: EpochTracker.h:58
bool isHandleInSync() const
Returns true if the DebugEpochBase this Handle is linked to has not called incrementEpoch on itself s...
Definition: EpochTracker.h:71
HandleBase(const DebugEpochBase *Parent)
Definition: EpochTracker.h:65
const void * getEpochAddress() const
Returns a pointer to the epoch word stored in the data structure this handle points into.
Definition: EpochTracker.h:76
A base class for data structure classes wishing to make iterators ("handles") pointing into themselve...
Definition: EpochTracker.h:36
void incrementEpoch()
Calling incrementEpoch invalidates all handles pointing into the calling instance.
Definition: EpochTracker.h:44
DebugEpochBase()=default
~DebugEpochBase()
The destructor calls incrementEpoch to make use-after-free bugs more likely to crash deterministicall...
Definition: EpochTracker.h:48
Definition: ntcore_cpp.h:26