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_ANALYSIS_ANALYSES_THREADSAFETYUTIL_H
15 #define LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYUTIL_H
17 #include "clang/AST/Decl.h"
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/iterator_range.h"
21 #include "llvm/Support/Allocator.h"
34 namespace threadSafety {
37 // Simple wrapper class to abstract away from the details of memory management.
38 // SExprs are allocated in pools, and deallocated all at once.
49 MemRegionRef() = default;
50 MemRegionRef(llvm::BumpPtrAllocator *A) : Allocator(A) {}
52 void *allocate(size_t Sz) {
53 return Allocator->Allocate(Sz, alignof(AlignmentType));
56 template <typename T> T *allocateT() { return Allocator->Allocate<T>(); }
58 template <typename T> T *allocateT(size_t NumElems) {
59 return Allocator->Allocate<T>(NumElems);
63 llvm::BumpPtrAllocator *Allocator = nullptr;
67 } // namespace threadSafety
71 inline void *operator new(size_t Sz,
72 clang::threadSafety::til::MemRegionRef &R) {
73 return R.allocate(Sz);
77 namespace threadSafety {
79 std::string getSourceLiteralString(const Expr *CE);
83 // A simple fixed size array class that does not manage its own memory,
84 // suitable for use with bump pointer allocation.
85 template <class T> class SimpleArray {
87 SimpleArray() = default;
88 SimpleArray(T *Dat, size_t Cp, size_t Sz = 0)
89 : Data(Dat), Size(Sz), Capacity(Cp) {}
90 SimpleArray(MemRegionRef A, size_t Cp)
91 : Data(Cp == 0 ? nullptr : A.allocateT<T>(Cp)), Capacity(Cp) {}
92 SimpleArray(const SimpleArray<T> &A) = delete;
94 SimpleArray(SimpleArray<T> &&A)
95 : Data(A.Data), Size(A.Size), Capacity(A.Capacity) {
101 SimpleArray &operator=(SimpleArray &&RHS) {
105 Capacity = RHS.Capacity;
108 RHS.Size = RHS.Capacity = 0;
113 // Reserve space for at least Ncp items, reallocating if necessary.
114 void reserve(size_t Ncp, MemRegionRef A) {
118 Data = A.allocateT<T>(Ncp);
120 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);
131 using iterator = T *;
132 using const_iterator = const T *;
133 using reverse_iterator = std::reverse_iterator<iterator>;
134 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
136 size_t size() const { return Size; }
137 size_t capacity() const { return Capacity; }
139 T &operator[](unsigned i) {
140 assert(i < Size && "Array index out of bounds.");
144 const T &operator[](unsigned i) const {
145 assert(i < Size && "Array index out of bounds.");
150 assert(Size && "No elements in the array.");
151 return Data[Size - 1];
154 const T &back() const {
155 assert(Size && "No elements in the array.");
156 return Data[Size - 1];
159 iterator begin() { return Data; }
160 iterator end() { return Data + Size; }
162 const_iterator begin() const { return Data; }
163 const_iterator end() const { return Data + Size; }
165 const_iterator cbegin() const { return Data; }
166 const_iterator cend() const { return Data + Size; }
168 reverse_iterator rbegin() { return reverse_iterator(end()); }
169 reverse_iterator rend() { return reverse_iterator(begin()); }
171 const_reverse_iterator rbegin() const {
172 return const_reverse_iterator(end());
175 const_reverse_iterator rend() const {
176 return const_reverse_iterator(begin());
179 void push_back(const T &Elem) {
180 assert(Size < Capacity);
184 // drop last n elements from array
185 void drop(unsigned n = 0) {
190 void setValues(unsigned Sz, const T& C) {
191 assert(Sz <= Capacity);
193 for (unsigned i = 0; i < Sz; ++i) {
198 template <class Iter> unsigned append(Iter I, Iter E) {
201 for (; J < Capacity && I != E; ++J, ++I)
207 llvm::iterator_range<reverse_iterator> reverse() {
208 return llvm::make_range(rbegin(), rend());
211 llvm::iterator_range<const_reverse_iterator> reverse() const {
212 return llvm::make_range(rbegin(), rend());
216 // std::max is annoying here, because it requires a reference,
217 // thus forcing InitialCapacity to be initialized outside the .h file.
218 size_t u_max(size_t i, size_t j) { return (i < j) ? j : i; }
220 static const size_t InitialCapacity = 4;
229 // A copy on write vector.
230 // The vector can be in one of three states:
231 // * invalid -- no operations are permitted.
232 // * read-only -- read operations are permitted.
233 // * writable -- read and write operations are permitted.
234 // The init(), destroy(), and makeWritable() methods will change state.
236 class CopyOnWriteVector {
239 unsigned NumRefs = 1;
242 VectorData() = default;
243 VectorData(const VectorData &VD) : Vect(VD.Vect) {}
247 CopyOnWriteVector() = default;
248 CopyOnWriteVector(CopyOnWriteVector &&V) : Data(V.Data) { V.Data = nullptr; }
250 CopyOnWriteVector &operator=(CopyOnWriteVector &&V) {
257 // No copy constructor or copy assignment. Use clone() with move assignment.
258 CopyOnWriteVector(const CopyOnWriteVector &) = delete;
259 CopyOnWriteVector &operator=(const CopyOnWriteVector &) = delete;
261 ~CopyOnWriteVector() { destroy(); }
263 // Returns true if this holds a valid vector.
264 bool valid() const { return Data; }
266 // Returns true if this vector is writable.
267 bool writable() const { return Data && Data->NumRefs == 1; }
269 // If this vector is not valid, initialize it to a valid vector.
272 Data = new VectorData();
276 // Destroy this vector; thus making it invalid.
280 if (Data->NumRefs <= 1)
287 // Make this vector writable, creating a copy if needed.
288 void makeWritable() {
290 Data = new VectorData();
293 if (Data->NumRefs == 1)
294 return; // already writeable.
296 Data = new VectorData(*Data);
299 // Create a lazy copy of this vector.
300 CopyOnWriteVector clone() { return CopyOnWriteVector(Data); }
302 using const_iterator = typename std::vector<T>::const_iterator;
304 const std::vector<T> &elements() const { return Data->Vect; }
306 const_iterator begin() const { return elements().cbegin(); }
307 const_iterator end() const { return elements().cend(); }
309 const T& operator[](unsigned i) const { return elements()[i]; }
311 unsigned size() const { return Data ? elements().size() : 0; }
313 // Return true if V and this vector refer to the same data.
314 bool sameAs(const CopyOnWriteVector &V) const { return Data == V.Data; }
316 // Clear vector. The vector must be writable.
318 assert(writable() && "Vector is not writable!");
322 // Push a new element onto the end. The vector must be writable.
323 void push_back(const T &Elem) {
324 assert(writable() && "Vector is not writable!");
325 Data->Vect.push_back(Elem);
328 // Gets a mutable reference to the element at index(i).
329 // The vector must be writable.
330 T& elem(unsigned i) {
331 assert(writable() && "Vector is not writable!");
332 return Data->Vect[i];
335 // Drops elements from the back until the vector has size i.
336 void downsize(unsigned i) {
337 assert(writable() && "Vector is not writable!");
338 Data->Vect.erase(Data->Vect.begin() + i, Data->Vect.end());
342 CopyOnWriteVector(VectorData *D) : Data(D) {
348 VectorData *Data = nullptr;
351 inline std::ostream& operator<<(std::ostream& ss, const StringRef str) {
352 return ss.write(str.data(), str.size());
355 } // namespace threadSafety
358 #endif // LLVM_CLANG_THREAD_SAFETY_UTIL_H