1 //===- ThreadSafetyUtil.h --------------------------------------*- C++ --*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines some basic utility classes for use by ThreadSafetyTIL.h
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_THREAD_SAFETY_UTIL_H
15 #define LLVM_CLANG_THREAD_SAFETY_UTIL_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/AlignOf.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/Compiler.h"
21 #include "clang/AST/ExprCXX.h"
29 namespace threadSafety {
32 // Simple wrapper class to abstract away from the details of memory management.
33 // SExprs are allocated in pools, and deallocated all at once.
44 MemRegionRef() : Allocator(nullptr) {}
45 MemRegionRef(llvm::BumpPtrAllocator *A) : Allocator(A) {}
47 void *allocate(size_t Sz) {
48 return Allocator->Allocate(Sz, llvm::AlignOf<AlignmentType>::Alignment);
51 template <typename T> T *allocateT() { return Allocator->Allocate<T>(); }
53 template <typename T> T *allocateT(size_t NumElems) {
54 return Allocator->Allocate<T>(NumElems);
58 llvm::BumpPtrAllocator *Allocator;
62 } // end namespace til
63 } // end namespace threadSafety
64 } // end namespace clang
67 inline void *operator new(size_t Sz,
68 clang::threadSafety::til::MemRegionRef &R) {
69 return R.allocate(Sz);
74 namespace threadSafety {
76 std::string getSourceLiteralString(const clang::Expr *CE);
78 using llvm::StringRef;
79 using clang::SourceLocation;
84 // A simple fixed size array class that does not manage its own memory,
85 // suitable for use with bump pointer allocation.
86 template <class T> class SimpleArray {
88 SimpleArray() : Data(nullptr), Size(0), Capacity(0) {}
89 SimpleArray(T *Dat, size_t Cp, size_t Sz = 0)
90 : Data(Dat), Size(Sz), Capacity(Cp) {}
91 SimpleArray(MemRegionRef A, size_t Cp)
92 : Data(Cp == 0 ? nullptr : A.allocateT<T>(Cp)), Size(0), Capacity(Cp) {}
93 SimpleArray(SimpleArray<T> &&A)
94 : Data(A.Data), Size(A.Size), Capacity(A.Capacity) {
100 SimpleArray &operator=(SimpleArray &&RHS) {
104 Capacity = RHS.Capacity;
107 RHS.Size = RHS.Capacity = 0;
112 // Reserve space for at least Ncp items, reallocating if necessary.
113 void reserve(size_t Ncp, MemRegionRef A) {
117 Data = A.allocateT<T>(Ncp);
119 memcpy(Data, Odata, sizeof(T) * Size);
123 // Reserve space for at least N more items.
124 void reserveCheck(size_t N, MemRegionRef A) {
126 reserve(u_max(InitialCapacity, N), A);
127 else if (Size + N < Capacity)
128 reserve(u_max(Size + N, Capacity * 2), A);
132 typedef const T *const_iterator;
134 size_t size() const { return Size; }
135 size_t capacity() const { return Capacity; }
137 T &operator[](unsigned i) {
138 assert(i < Size && "Array index out of bounds.");
141 const T &operator[](unsigned i) const {
142 assert(i < Size && "Array index out of bounds.");
146 iterator begin() { return Data; }
147 iterator end() { return Data + Size; }
149 const_iterator cbegin() const { return Data; }
150 const_iterator cend() const { return Data + Size; }
152 void push_back(const T &Elem) {
153 assert(Size < Capacity);
157 void setValues(unsigned Sz, const T& C) {
158 assert(Sz <= Capacity);
160 for (unsigned i = 0; i < Sz; ++i) {
165 template <class Iter> unsigned append(Iter I, Iter E) {
168 for (; J < Capacity && I != E; ++J, ++I)
175 // std::max is annoying here, because it requires a reference,
176 // thus forcing InitialCapacity to be initialized outside the .h file.
177 size_t u_max(size_t i, size_t j) { return (i < j) ? j : i; }
179 static const size_t InitialCapacity = 4;
181 SimpleArray(const SimpleArray<T> &A) LLVM_DELETED_FUNCTION;
188 } // end namespace til
191 // A copy on write vector.
192 // The vector can be in one of three states:
193 // * invalid -- no operations are permitted.
194 // * read-only -- read operations are permitted.
195 // * writable -- read and write operations are permitted.
196 // The init(), destroy(), and makeWritable() methods will change state.
198 class CopyOnWriteVector {
201 VectorData() : NumRefs(1) { }
202 VectorData(const VectorData &VD) : NumRefs(1), Vect(VD.Vect) { }
208 // No copy constructor or copy assignment. Use clone() with move assignment.
209 CopyOnWriteVector(const CopyOnWriteVector &V) LLVM_DELETED_FUNCTION;
210 void operator=(const CopyOnWriteVector &V) LLVM_DELETED_FUNCTION;
213 CopyOnWriteVector() : Data(nullptr) {}
214 CopyOnWriteVector(CopyOnWriteVector &&V) : Data(V.Data) { V.Data = nullptr; }
215 ~CopyOnWriteVector() { destroy(); }
217 // Returns true if this holds a valid vector.
218 bool valid() const { return Data; }
220 // Returns true if this vector is writable.
221 bool writable() const { return Data && Data->NumRefs == 1; }
223 // If this vector is not valid, initialize it to a valid vector.
226 Data = new VectorData();
230 // Destroy this vector; thus making it invalid.
234 if (Data->NumRefs <= 1)
241 // Make this vector writable, creating a copy if needed.
242 void makeWritable() {
244 Data = new VectorData();
247 if (Data->NumRefs == 1)
248 return; // already writeable.
250 Data = new VectorData(*Data);
253 // Create a lazy copy of this vector.
254 CopyOnWriteVector clone() { return CopyOnWriteVector(Data); }
256 CopyOnWriteVector &operator=(CopyOnWriteVector &&V) {
263 typedef typename std::vector<T>::const_iterator const_iterator;
265 const std::vector<T> &elements() const { return Data->Vect; }
267 const_iterator begin() const { return elements().cbegin(); }
268 const_iterator end() const { return elements().cend(); }
270 const T& operator[](unsigned i) const { return elements()[i]; }
272 unsigned size() const { return Data ? elements().size() : 0; }
274 // Return true if V and this vector refer to the same data.
275 bool sameAs(const CopyOnWriteVector &V) const { return Data == V.Data; }
277 // Clear vector. The vector must be writable.
279 assert(writable() && "Vector is not writable!");
283 // Push a new element onto the end. The vector must be writable.
284 void push_back(const T &Elem) {
285 assert(writable() && "Vector is not writable!");
286 Data->Vect.push_back(Elem);
289 // Gets a mutable reference to the element at index(i).
290 // The vector must be writable.
291 T& elem(unsigned i) {
292 assert(writable() && "Vector is not writable!");
293 return Data->Vect[i];
296 // Drops elements from the back until the vector has size i.
297 void downsize(unsigned i) {
298 assert(writable() && "Vector is not writable!");
299 Data->Vect.erase(Data->Vect.begin() + i, Data->Vect.end());
303 CopyOnWriteVector(VectorData *D) : Data(D) {
313 } // end namespace threadSafety
314 } // end namespace clang
316 #endif // LLVM_CLANG_THREAD_SAFETY_UTIL_H