1*0a6a1f1dSLionel Sambuc //===- ThreadSafetyUtil.h --------------------------------------*- C++ --*-===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc // The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // This file defines some basic utility classes for use by ThreadSafetyTIL.h
11*0a6a1f1dSLionel Sambuc //
12*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
13*0a6a1f1dSLionel Sambuc
14*0a6a1f1dSLionel Sambuc #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYUTIL_H
15*0a6a1f1dSLionel Sambuc #define LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYUTIL_H
16*0a6a1f1dSLionel Sambuc
17*0a6a1f1dSLionel Sambuc #include "clang/AST/ExprCXX.h"
18*0a6a1f1dSLionel Sambuc #include "llvm/ADT/StringRef.h"
19*0a6a1f1dSLionel Sambuc #include "llvm/Support/AlignOf.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/Support/Allocator.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/Support/Compiler.h"
22*0a6a1f1dSLionel Sambuc #include <cassert>
23*0a6a1f1dSLionel Sambuc #include <cstddef>
24*0a6a1f1dSLionel Sambuc #include <ostream>
25*0a6a1f1dSLionel Sambuc #include <utility>
26*0a6a1f1dSLionel Sambuc #include <vector>
27*0a6a1f1dSLionel Sambuc
28*0a6a1f1dSLionel Sambuc namespace clang {
29*0a6a1f1dSLionel Sambuc namespace threadSafety {
30*0a6a1f1dSLionel Sambuc namespace til {
31*0a6a1f1dSLionel Sambuc
32*0a6a1f1dSLionel Sambuc // Simple wrapper class to abstract away from the details of memory management.
33*0a6a1f1dSLionel Sambuc // SExprs are allocated in pools, and deallocated all at once.
34*0a6a1f1dSLionel Sambuc class MemRegionRef {
35*0a6a1f1dSLionel Sambuc private:
36*0a6a1f1dSLionel Sambuc union AlignmentType {
37*0a6a1f1dSLionel Sambuc double d;
38*0a6a1f1dSLionel Sambuc void *p;
39*0a6a1f1dSLionel Sambuc long double dd;
40*0a6a1f1dSLionel Sambuc long long ii;
41*0a6a1f1dSLionel Sambuc };
42*0a6a1f1dSLionel Sambuc
43*0a6a1f1dSLionel Sambuc public:
MemRegionRef()44*0a6a1f1dSLionel Sambuc MemRegionRef() : Allocator(nullptr) {}
MemRegionRef(llvm::BumpPtrAllocator * A)45*0a6a1f1dSLionel Sambuc MemRegionRef(llvm::BumpPtrAllocator *A) : Allocator(A) {}
46*0a6a1f1dSLionel Sambuc
allocate(size_t Sz)47*0a6a1f1dSLionel Sambuc void *allocate(size_t Sz) {
48*0a6a1f1dSLionel Sambuc return Allocator->Allocate(Sz, llvm::AlignOf<AlignmentType>::Alignment);
49*0a6a1f1dSLionel Sambuc }
50*0a6a1f1dSLionel Sambuc
allocateT()51*0a6a1f1dSLionel Sambuc template <typename T> T *allocateT() { return Allocator->Allocate<T>(); }
52*0a6a1f1dSLionel Sambuc
allocateT(size_t NumElems)53*0a6a1f1dSLionel Sambuc template <typename T> T *allocateT(size_t NumElems) {
54*0a6a1f1dSLionel Sambuc return Allocator->Allocate<T>(NumElems);
55*0a6a1f1dSLionel Sambuc }
56*0a6a1f1dSLionel Sambuc
57*0a6a1f1dSLionel Sambuc private:
58*0a6a1f1dSLionel Sambuc llvm::BumpPtrAllocator *Allocator;
59*0a6a1f1dSLionel Sambuc };
60*0a6a1f1dSLionel Sambuc
61*0a6a1f1dSLionel Sambuc
62*0a6a1f1dSLionel Sambuc } // end namespace til
63*0a6a1f1dSLionel Sambuc } // end namespace threadSafety
64*0a6a1f1dSLionel Sambuc } // end namespace clang
65*0a6a1f1dSLionel Sambuc
66*0a6a1f1dSLionel Sambuc
new(size_t Sz,clang::threadSafety::til::MemRegionRef & R)67*0a6a1f1dSLionel Sambuc inline void *operator new(size_t Sz,
68*0a6a1f1dSLionel Sambuc clang::threadSafety::til::MemRegionRef &R) {
69*0a6a1f1dSLionel Sambuc return R.allocate(Sz);
70*0a6a1f1dSLionel Sambuc }
71*0a6a1f1dSLionel Sambuc
72*0a6a1f1dSLionel Sambuc
73*0a6a1f1dSLionel Sambuc namespace clang {
74*0a6a1f1dSLionel Sambuc namespace threadSafety {
75*0a6a1f1dSLionel Sambuc
76*0a6a1f1dSLionel Sambuc std::string getSourceLiteralString(const clang::Expr *CE);
77*0a6a1f1dSLionel Sambuc
78*0a6a1f1dSLionel Sambuc using llvm::StringRef;
79*0a6a1f1dSLionel Sambuc using clang::SourceLocation;
80*0a6a1f1dSLionel Sambuc
81*0a6a1f1dSLionel Sambuc namespace til {
82*0a6a1f1dSLionel Sambuc
83*0a6a1f1dSLionel Sambuc
84*0a6a1f1dSLionel Sambuc // A simple fixed size array class that does not manage its own memory,
85*0a6a1f1dSLionel Sambuc // suitable for use with bump pointer allocation.
86*0a6a1f1dSLionel Sambuc template <class T> class SimpleArray {
87*0a6a1f1dSLionel Sambuc public:
SimpleArray()88*0a6a1f1dSLionel Sambuc SimpleArray() : Data(nullptr), Size(0), Capacity(0) {}
89*0a6a1f1dSLionel Sambuc SimpleArray(T *Dat, size_t Cp, size_t Sz = 0)
Data(Dat)90*0a6a1f1dSLionel Sambuc : Data(Dat), Size(Sz), Capacity(Cp) {}
SimpleArray(MemRegionRef A,size_t Cp)91*0a6a1f1dSLionel Sambuc SimpleArray(MemRegionRef A, size_t Cp)
92*0a6a1f1dSLionel Sambuc : Data(Cp == 0 ? nullptr : A.allocateT<T>(Cp)), Size(0), Capacity(Cp) {}
SimpleArray(SimpleArray<T> && A)93*0a6a1f1dSLionel Sambuc SimpleArray(SimpleArray<T> &&A)
94*0a6a1f1dSLionel Sambuc : Data(A.Data), Size(A.Size), Capacity(A.Capacity) {
95*0a6a1f1dSLionel Sambuc A.Data = nullptr;
96*0a6a1f1dSLionel Sambuc A.Size = 0;
97*0a6a1f1dSLionel Sambuc A.Capacity = 0;
98*0a6a1f1dSLionel Sambuc }
99*0a6a1f1dSLionel Sambuc
100*0a6a1f1dSLionel Sambuc SimpleArray &operator=(SimpleArray &&RHS) {
101*0a6a1f1dSLionel Sambuc if (this != &RHS) {
102*0a6a1f1dSLionel Sambuc Data = RHS.Data;
103*0a6a1f1dSLionel Sambuc Size = RHS.Size;
104*0a6a1f1dSLionel Sambuc Capacity = RHS.Capacity;
105*0a6a1f1dSLionel Sambuc
106*0a6a1f1dSLionel Sambuc RHS.Data = nullptr;
107*0a6a1f1dSLionel Sambuc RHS.Size = RHS.Capacity = 0;
108*0a6a1f1dSLionel Sambuc }
109*0a6a1f1dSLionel Sambuc return *this;
110*0a6a1f1dSLionel Sambuc }
111*0a6a1f1dSLionel Sambuc
112*0a6a1f1dSLionel Sambuc // Reserve space for at least Ncp items, reallocating if necessary.
reserve(size_t Ncp,MemRegionRef A)113*0a6a1f1dSLionel Sambuc void reserve(size_t Ncp, MemRegionRef A) {
114*0a6a1f1dSLionel Sambuc if (Ncp <= Capacity)
115*0a6a1f1dSLionel Sambuc return;
116*0a6a1f1dSLionel Sambuc T *Odata = Data;
117*0a6a1f1dSLionel Sambuc Data = A.allocateT<T>(Ncp);
118*0a6a1f1dSLionel Sambuc Capacity = Ncp;
119*0a6a1f1dSLionel Sambuc memcpy(Data, Odata, sizeof(T) * Size);
120*0a6a1f1dSLionel Sambuc return;
121*0a6a1f1dSLionel Sambuc }
122*0a6a1f1dSLionel Sambuc
123*0a6a1f1dSLionel Sambuc // Reserve space for at least N more items.
reserveCheck(size_t N,MemRegionRef A)124*0a6a1f1dSLionel Sambuc void reserveCheck(size_t N, MemRegionRef A) {
125*0a6a1f1dSLionel Sambuc if (Capacity == 0)
126*0a6a1f1dSLionel Sambuc reserve(u_max(InitialCapacity, N), A);
127*0a6a1f1dSLionel Sambuc else if (Size + N < Capacity)
128*0a6a1f1dSLionel Sambuc reserve(u_max(Size + N, Capacity * 2), A);
129*0a6a1f1dSLionel Sambuc }
130*0a6a1f1dSLionel Sambuc
131*0a6a1f1dSLionel Sambuc typedef T *iterator;
132*0a6a1f1dSLionel Sambuc typedef const T *const_iterator;
133*0a6a1f1dSLionel Sambuc
size()134*0a6a1f1dSLionel Sambuc size_t size() const { return Size; }
capacity()135*0a6a1f1dSLionel Sambuc size_t capacity() const { return Capacity; }
136*0a6a1f1dSLionel Sambuc
137*0a6a1f1dSLionel Sambuc T &operator[](unsigned i) {
138*0a6a1f1dSLionel Sambuc assert(i < Size && "Array index out of bounds.");
139*0a6a1f1dSLionel Sambuc return Data[i];
140*0a6a1f1dSLionel Sambuc }
141*0a6a1f1dSLionel Sambuc const T &operator[](unsigned i) const {
142*0a6a1f1dSLionel Sambuc assert(i < Size && "Array index out of bounds.");
143*0a6a1f1dSLionel Sambuc return Data[i];
144*0a6a1f1dSLionel Sambuc }
back()145*0a6a1f1dSLionel Sambuc T &back() {
146*0a6a1f1dSLionel Sambuc assert(Size && "No elements in the array.");
147*0a6a1f1dSLionel Sambuc return Data[Size - 1];
148*0a6a1f1dSLionel Sambuc }
back()149*0a6a1f1dSLionel Sambuc const T &back() const {
150*0a6a1f1dSLionel Sambuc assert(Size && "No elements in the array.");
151*0a6a1f1dSLionel Sambuc return Data[Size - 1];
152*0a6a1f1dSLionel Sambuc }
153*0a6a1f1dSLionel Sambuc
begin()154*0a6a1f1dSLionel Sambuc iterator begin() { return Data; }
end()155*0a6a1f1dSLionel Sambuc iterator end() { return Data + Size; }
156*0a6a1f1dSLionel Sambuc
begin()157*0a6a1f1dSLionel Sambuc const_iterator begin() const { return Data; }
end()158*0a6a1f1dSLionel Sambuc const_iterator end() const { return Data + Size; }
159*0a6a1f1dSLionel Sambuc
cbegin()160*0a6a1f1dSLionel Sambuc const_iterator cbegin() const { return Data; }
cend()161*0a6a1f1dSLionel Sambuc const_iterator cend() const { return Data + Size; }
162*0a6a1f1dSLionel Sambuc
push_back(const T & Elem)163*0a6a1f1dSLionel Sambuc void push_back(const T &Elem) {
164*0a6a1f1dSLionel Sambuc assert(Size < Capacity);
165*0a6a1f1dSLionel Sambuc Data[Size++] = Elem;
166*0a6a1f1dSLionel Sambuc }
167*0a6a1f1dSLionel Sambuc
168*0a6a1f1dSLionel Sambuc // drop last n elements from array
169*0a6a1f1dSLionel Sambuc void drop(unsigned n = 0) {
170*0a6a1f1dSLionel Sambuc assert(Size > n);
171*0a6a1f1dSLionel Sambuc Size -= n;
172*0a6a1f1dSLionel Sambuc }
173*0a6a1f1dSLionel Sambuc
setValues(unsigned Sz,const T & C)174*0a6a1f1dSLionel Sambuc void setValues(unsigned Sz, const T& C) {
175*0a6a1f1dSLionel Sambuc assert(Sz <= Capacity);
176*0a6a1f1dSLionel Sambuc Size = Sz;
177*0a6a1f1dSLionel Sambuc for (unsigned i = 0; i < Sz; ++i) {
178*0a6a1f1dSLionel Sambuc Data[i] = C;
179*0a6a1f1dSLionel Sambuc }
180*0a6a1f1dSLionel Sambuc }
181*0a6a1f1dSLionel Sambuc
append(Iter I,Iter E)182*0a6a1f1dSLionel Sambuc template <class Iter> unsigned append(Iter I, Iter E) {
183*0a6a1f1dSLionel Sambuc size_t Osz = Size;
184*0a6a1f1dSLionel Sambuc size_t J = Osz;
185*0a6a1f1dSLionel Sambuc for (; J < Capacity && I != E; ++J, ++I)
186*0a6a1f1dSLionel Sambuc Data[J] = *I;
187*0a6a1f1dSLionel Sambuc Size = J;
188*0a6a1f1dSLionel Sambuc return J - Osz;
189*0a6a1f1dSLionel Sambuc }
190*0a6a1f1dSLionel Sambuc
191*0a6a1f1dSLionel Sambuc // An adaptor to reverse a simple array
192*0a6a1f1dSLionel Sambuc class ReverseAdaptor {
193*0a6a1f1dSLionel Sambuc public:
ReverseAdaptor(SimpleArray & Array)194*0a6a1f1dSLionel Sambuc ReverseAdaptor(SimpleArray &Array) : Array(Array) {}
195*0a6a1f1dSLionel Sambuc // A reverse iterator used by the reverse adaptor
196*0a6a1f1dSLionel Sambuc class Iterator {
197*0a6a1f1dSLionel Sambuc public:
Iterator(T * Data)198*0a6a1f1dSLionel Sambuc Iterator(T *Data) : Data(Data) {}
199*0a6a1f1dSLionel Sambuc T &operator*() { return *Data; }
200*0a6a1f1dSLionel Sambuc const T &operator*() const { return *Data; }
201*0a6a1f1dSLionel Sambuc Iterator &operator++() {
202*0a6a1f1dSLionel Sambuc --Data;
203*0a6a1f1dSLionel Sambuc return *this;
204*0a6a1f1dSLionel Sambuc }
205*0a6a1f1dSLionel Sambuc bool operator!=(Iterator Other) { return Data != Other.Data; }
206*0a6a1f1dSLionel Sambuc
207*0a6a1f1dSLionel Sambuc private:
208*0a6a1f1dSLionel Sambuc T *Data;
209*0a6a1f1dSLionel Sambuc };
begin()210*0a6a1f1dSLionel Sambuc Iterator begin() { return Array.end() - 1; }
end()211*0a6a1f1dSLionel Sambuc Iterator end() { return Array.begin() - 1; }
begin()212*0a6a1f1dSLionel Sambuc const Iterator begin() const { return Array.end() - 1; }
end()213*0a6a1f1dSLionel Sambuc const Iterator end() const { return Array.begin() - 1; }
214*0a6a1f1dSLionel Sambuc
215*0a6a1f1dSLionel Sambuc private:
216*0a6a1f1dSLionel Sambuc SimpleArray &Array;
217*0a6a1f1dSLionel Sambuc };
218*0a6a1f1dSLionel Sambuc
reverse()219*0a6a1f1dSLionel Sambuc const ReverseAdaptor reverse() const { return ReverseAdaptor(*this); }
reverse()220*0a6a1f1dSLionel Sambuc ReverseAdaptor reverse() { return ReverseAdaptor(*this); }
221*0a6a1f1dSLionel Sambuc
222*0a6a1f1dSLionel Sambuc private:
223*0a6a1f1dSLionel Sambuc // std::max is annoying here, because it requires a reference,
224*0a6a1f1dSLionel Sambuc // thus forcing InitialCapacity to be initialized outside the .h file.
u_max(size_t i,size_t j)225*0a6a1f1dSLionel Sambuc size_t u_max(size_t i, size_t j) { return (i < j) ? j : i; }
226*0a6a1f1dSLionel Sambuc
227*0a6a1f1dSLionel Sambuc static const size_t InitialCapacity = 4;
228*0a6a1f1dSLionel Sambuc
229*0a6a1f1dSLionel Sambuc SimpleArray(const SimpleArray<T> &A) LLVM_DELETED_FUNCTION;
230*0a6a1f1dSLionel Sambuc
231*0a6a1f1dSLionel Sambuc T *Data;
232*0a6a1f1dSLionel Sambuc size_t Size;
233*0a6a1f1dSLionel Sambuc size_t Capacity;
234*0a6a1f1dSLionel Sambuc };
235*0a6a1f1dSLionel Sambuc
236*0a6a1f1dSLionel Sambuc
237*0a6a1f1dSLionel Sambuc } // end namespace til
238*0a6a1f1dSLionel Sambuc
239*0a6a1f1dSLionel Sambuc
240*0a6a1f1dSLionel Sambuc // A copy on write vector.
241*0a6a1f1dSLionel Sambuc // The vector can be in one of three states:
242*0a6a1f1dSLionel Sambuc // * invalid -- no operations are permitted.
243*0a6a1f1dSLionel Sambuc // * read-only -- read operations are permitted.
244*0a6a1f1dSLionel Sambuc // * writable -- read and write operations are permitted.
245*0a6a1f1dSLionel Sambuc // The init(), destroy(), and makeWritable() methods will change state.
246*0a6a1f1dSLionel Sambuc template<typename T>
247*0a6a1f1dSLionel Sambuc class CopyOnWriteVector {
248*0a6a1f1dSLionel Sambuc class VectorData {
249*0a6a1f1dSLionel Sambuc public:
VectorData()250*0a6a1f1dSLionel Sambuc VectorData() : NumRefs(1) { }
VectorData(const VectorData & VD)251*0a6a1f1dSLionel Sambuc VectorData(const VectorData &VD) : NumRefs(1), Vect(VD.Vect) { }
252*0a6a1f1dSLionel Sambuc
253*0a6a1f1dSLionel Sambuc unsigned NumRefs;
254*0a6a1f1dSLionel Sambuc std::vector<T> Vect;
255*0a6a1f1dSLionel Sambuc };
256*0a6a1f1dSLionel Sambuc
257*0a6a1f1dSLionel Sambuc // No copy constructor or copy assignment. Use clone() with move assignment.
258*0a6a1f1dSLionel Sambuc CopyOnWriteVector(const CopyOnWriteVector &V) LLVM_DELETED_FUNCTION;
259*0a6a1f1dSLionel Sambuc void operator=(const CopyOnWriteVector &V) LLVM_DELETED_FUNCTION;
260*0a6a1f1dSLionel Sambuc
261*0a6a1f1dSLionel Sambuc public:
CopyOnWriteVector()262*0a6a1f1dSLionel Sambuc CopyOnWriteVector() : Data(nullptr) {}
CopyOnWriteVector(CopyOnWriteVector && V)263*0a6a1f1dSLionel Sambuc CopyOnWriteVector(CopyOnWriteVector &&V) : Data(V.Data) { V.Data = nullptr; }
~CopyOnWriteVector()264*0a6a1f1dSLionel Sambuc ~CopyOnWriteVector() { destroy(); }
265*0a6a1f1dSLionel Sambuc
266*0a6a1f1dSLionel Sambuc // Returns true if this holds a valid vector.
valid()267*0a6a1f1dSLionel Sambuc bool valid() const { return Data; }
268*0a6a1f1dSLionel Sambuc
269*0a6a1f1dSLionel Sambuc // Returns true if this vector is writable.
writable()270*0a6a1f1dSLionel Sambuc bool writable() const { return Data && Data->NumRefs == 1; }
271*0a6a1f1dSLionel Sambuc
272*0a6a1f1dSLionel Sambuc // If this vector is not valid, initialize it to a valid vector.
init()273*0a6a1f1dSLionel Sambuc void init() {
274*0a6a1f1dSLionel Sambuc if (!Data) {
275*0a6a1f1dSLionel Sambuc Data = new VectorData();
276*0a6a1f1dSLionel Sambuc }
277*0a6a1f1dSLionel Sambuc }
278*0a6a1f1dSLionel Sambuc
279*0a6a1f1dSLionel Sambuc // Destroy this vector; thus making it invalid.
destroy()280*0a6a1f1dSLionel Sambuc void destroy() {
281*0a6a1f1dSLionel Sambuc if (!Data)
282*0a6a1f1dSLionel Sambuc return;
283*0a6a1f1dSLionel Sambuc if (Data->NumRefs <= 1)
284*0a6a1f1dSLionel Sambuc delete Data;
285*0a6a1f1dSLionel Sambuc else
286*0a6a1f1dSLionel Sambuc --Data->NumRefs;
287*0a6a1f1dSLionel Sambuc Data = nullptr;
288*0a6a1f1dSLionel Sambuc }
289*0a6a1f1dSLionel Sambuc
290*0a6a1f1dSLionel Sambuc // Make this vector writable, creating a copy if needed.
makeWritable()291*0a6a1f1dSLionel Sambuc void makeWritable() {
292*0a6a1f1dSLionel Sambuc if (!Data) {
293*0a6a1f1dSLionel Sambuc Data = new VectorData();
294*0a6a1f1dSLionel Sambuc return;
295*0a6a1f1dSLionel Sambuc }
296*0a6a1f1dSLionel Sambuc if (Data->NumRefs == 1)
297*0a6a1f1dSLionel Sambuc return; // already writeable.
298*0a6a1f1dSLionel Sambuc --Data->NumRefs;
299*0a6a1f1dSLionel Sambuc Data = new VectorData(*Data);
300*0a6a1f1dSLionel Sambuc }
301*0a6a1f1dSLionel Sambuc
302*0a6a1f1dSLionel Sambuc // Create a lazy copy of this vector.
clone()303*0a6a1f1dSLionel Sambuc CopyOnWriteVector clone() { return CopyOnWriteVector(Data); }
304*0a6a1f1dSLionel Sambuc
305*0a6a1f1dSLionel Sambuc CopyOnWriteVector &operator=(CopyOnWriteVector &&V) {
306*0a6a1f1dSLionel Sambuc destroy();
307*0a6a1f1dSLionel Sambuc Data = V.Data;
308*0a6a1f1dSLionel Sambuc V.Data = nullptr;
309*0a6a1f1dSLionel Sambuc return *this;
310*0a6a1f1dSLionel Sambuc }
311*0a6a1f1dSLionel Sambuc
312*0a6a1f1dSLionel Sambuc typedef typename std::vector<T>::const_iterator const_iterator;
313*0a6a1f1dSLionel Sambuc
elements()314*0a6a1f1dSLionel Sambuc const std::vector<T> &elements() const { return Data->Vect; }
315*0a6a1f1dSLionel Sambuc
begin()316*0a6a1f1dSLionel Sambuc const_iterator begin() const { return elements().cbegin(); }
end()317*0a6a1f1dSLionel Sambuc const_iterator end() const { return elements().cend(); }
318*0a6a1f1dSLionel Sambuc
319*0a6a1f1dSLionel Sambuc const T& operator[](unsigned i) const { return elements()[i]; }
320*0a6a1f1dSLionel Sambuc
size()321*0a6a1f1dSLionel Sambuc unsigned size() const { return Data ? elements().size() : 0; }
322*0a6a1f1dSLionel Sambuc
323*0a6a1f1dSLionel Sambuc // Return true if V and this vector refer to the same data.
sameAs(const CopyOnWriteVector & V)324*0a6a1f1dSLionel Sambuc bool sameAs(const CopyOnWriteVector &V) const { return Data == V.Data; }
325*0a6a1f1dSLionel Sambuc
326*0a6a1f1dSLionel Sambuc // Clear vector. The vector must be writable.
clear()327*0a6a1f1dSLionel Sambuc void clear() {
328*0a6a1f1dSLionel Sambuc assert(writable() && "Vector is not writable!");
329*0a6a1f1dSLionel Sambuc Data->Vect.clear();
330*0a6a1f1dSLionel Sambuc }
331*0a6a1f1dSLionel Sambuc
332*0a6a1f1dSLionel Sambuc // Push a new element onto the end. The vector must be writable.
push_back(const T & Elem)333*0a6a1f1dSLionel Sambuc void push_back(const T &Elem) {
334*0a6a1f1dSLionel Sambuc assert(writable() && "Vector is not writable!");
335*0a6a1f1dSLionel Sambuc Data->Vect.push_back(Elem);
336*0a6a1f1dSLionel Sambuc }
337*0a6a1f1dSLionel Sambuc
338*0a6a1f1dSLionel Sambuc // Gets a mutable reference to the element at index(i).
339*0a6a1f1dSLionel Sambuc // The vector must be writable.
elem(unsigned i)340*0a6a1f1dSLionel Sambuc T& elem(unsigned i) {
341*0a6a1f1dSLionel Sambuc assert(writable() && "Vector is not writable!");
342*0a6a1f1dSLionel Sambuc return Data->Vect[i];
343*0a6a1f1dSLionel Sambuc }
344*0a6a1f1dSLionel Sambuc
345*0a6a1f1dSLionel Sambuc // Drops elements from the back until the vector has size i.
downsize(unsigned i)346*0a6a1f1dSLionel Sambuc void downsize(unsigned i) {
347*0a6a1f1dSLionel Sambuc assert(writable() && "Vector is not writable!");
348*0a6a1f1dSLionel Sambuc Data->Vect.erase(Data->Vect.begin() + i, Data->Vect.end());
349*0a6a1f1dSLionel Sambuc }
350*0a6a1f1dSLionel Sambuc
351*0a6a1f1dSLionel Sambuc private:
CopyOnWriteVector(VectorData * D)352*0a6a1f1dSLionel Sambuc CopyOnWriteVector(VectorData *D) : Data(D) {
353*0a6a1f1dSLionel Sambuc if (!Data)
354*0a6a1f1dSLionel Sambuc return;
355*0a6a1f1dSLionel Sambuc ++Data->NumRefs;
356*0a6a1f1dSLionel Sambuc }
357*0a6a1f1dSLionel Sambuc
358*0a6a1f1dSLionel Sambuc VectorData *Data;
359*0a6a1f1dSLionel Sambuc };
360*0a6a1f1dSLionel Sambuc
361*0a6a1f1dSLionel Sambuc
362*0a6a1f1dSLionel Sambuc inline std::ostream& operator<<(std::ostream& ss, const StringRef str) {
363*0a6a1f1dSLionel Sambuc return ss.write(str.data(), str.size());
364*0a6a1f1dSLionel Sambuc }
365*0a6a1f1dSLionel Sambuc
366*0a6a1f1dSLionel Sambuc
367*0a6a1f1dSLionel Sambuc } // end namespace threadSafety
368*0a6a1f1dSLionel Sambuc } // end namespace clang
369*0a6a1f1dSLionel Sambuc
370*0a6a1f1dSLionel Sambuc #endif // LLVM_CLANG_THREAD_SAFETY_UTIL_H
371