[ADT] fail-fast iterators for DenseMap
[oota-llvm.git] / include / llvm / ADT / EpochTracker.h
1 //===- llvm/ADT/EpochTracker.h - ADT epoch tracking --------------*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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 LLVM_ADT_EPOCH_TRACKER_H
17 #define LLVM_ADT_EPOCH_TRACKER_H
18
19 #include <cstdint>
20
21 namespace llvm {
22
23 #ifdef NDEBUG
24
25 class DebugEpochBase {
26 public:
27   void incrementEpoch() {}
28
29   class HandleBase {
30   public:
31     HandleBase() {}
32     explicit HandleBase(const DebugEpochBase *) {}
33     bool isHandleInSync() { return true; }
34   };
35 };
36
37 #else
38
39 /// \brief A base class for data structure classes wishing to make iterators
40 /// ("handles") pointing into themselves fail-fast.  When building without
41 /// asserts, this class is empty and does nothing.
42 ///
43 /// DebugEpochBase does not by itself track handles pointing into itself.  The
44 /// expectation is that routines touching the handles will poll on
45 /// isHandleInSync at appropriate points to assert that the handle they're using
46 /// is still valid.
47 ///
48 class DebugEpochBase {
49   uint64_t Epoch;
50
51 public:
52   DebugEpochBase() : Epoch(0) {}
53
54   /// \brief Calling incrementEpoch invalidates all handles pointing into the
55   /// calling instance.
56   void incrementEpoch() { ++Epoch; }
57
58   /// \brief The destructor calls incrementEpoch to make use-after-free bugs
59   /// more likely to crash deterministically.
60   ~DebugEpochBase() { incrementEpoch(); }
61
62   /// \brief A base class for iterator classes ("handles") that wish to poll for
63   /// iterator invalidating modifications in the underlying data structure.
64   /// When LLVM is built without asserts, this class is empty and does nothing.
65   ///
66   /// HandleBase does not track the parent data structure by itself.  It expects
67   /// the routines modifying the data structure to call incrementEpoch when they
68   /// make an iterator-invalidating modification.
69   ///
70   class HandleBase {
71     const uint64_t *EpochAddress;
72     uint64_t EpochAtCreation;
73
74   public:
75     HandleBase() : EpochAddress(nullptr), EpochAtCreation(UINT64_MAX) {}
76
77     explicit HandleBase(const DebugEpochBase *Parent)
78         : EpochAddress(&Parent->Epoch), EpochAtCreation(Parent->Epoch) {}
79
80     /// \brief Returns true if the DebugEpochBase this Handle is linked to has
81     /// not called incrementEpoch on itself since the creation of this
82     /// HandleBase instance.
83     bool isHandleInSync() const { return *EpochAddress == EpochAtCreation; }
84
85     /// \brief Returns a pointer to the epoch word stored in the data structure
86     /// this handle points into.
87     const uint64_t *getEpochAddress() const { return EpochAddress; }
88   };
89 };
90
91 #endif
92
93 } // namespace llvm
94
95 #endif