LLVM: include/llvm/ADT/IntrusiveRefCntPtr.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60#ifndef LLVM_ADT_INTRUSIVEREFCNTPTR_H

61#define LLVM_ADT_INTRUSIVEREFCNTPTR_H

62

63#include

64#include

65#include

66#include

67

68namespace llvm {

69

70

71

72

73

74

75

77 mutable unsigned RefCount = 0;

78

79protected:

83

84#ifndef NDEBUG

86 assert(RefCount == 0 &&

87 "Destruction occurred when there are still references to this.");

88 }

89#else

90

91

93#endif

94

95public:

96 unsigned UseCount() const { return RefCount; }

97

98 void Retain() const { ++RefCount; }

99

101 assert(RefCount > 0 && "Reference count is already zero.");

102 if (--RefCount == 0)

103 delete static_cast<const Derived *>(this);

104 }

105};

106

107

109 mutable std::atomic RefCount{0};

110

111protected:

116

117#ifndef NDEBUG

119 assert(RefCount == 0 &&

120 "Destruction occurred when there are still references to this.");

121 }

122#else

123

124

126#endif

127

128public:

129 unsigned UseCount() const { return RefCount.load(std::memory_order_relaxed); }

130

131 void Retain() const { RefCount.fetch_add(1, std::memory_order_relaxed); }

132

134 int NewRefCount = RefCount.fetch_sub(1, std::memory_order_acq_rel) - 1;

135 assert(NewRefCount >= 0 && "Reference count was already zero.");

136 if (NewRefCount == 0)

137 delete static_cast<const Derived *>(this);

138 }

139};

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

162 static unsigned useCount(const T *obj) { return obj->UseCount(); }

163 static void retain(T *obj) { obj->Retain(); }

164 static void release(T *obj) { obj->Release(); }

165};

166

167

168

169

170

171

172

174 T *Obj = nullptr;

175

176public:

178

183

184 template <class X,

185 std::enable_if_t<std::is_convertible<X *, T *>::value, bool> = true>

187 S.Obj = nullptr;

188 }

189

190 template <class X,

191 std::enable_if_t<std::is_convertible<X *, T *>::value, bool> = true>

195

197

202

205 T *get() const { return Obj; }

206 explicit operator bool() const { return Obj; }

207

209 T *tmp = other.Obj;

210 other.Obj = Obj;

211 Obj = tmp;

212 }

213

218

220

224

225private:

226 void retain() {

227 if (Obj)

229 }

230

232 if (Obj)

234 }

235

237};

238

239template <class T, class U>

242 return A.get() == B.get();

243}

244

245template <class T, class U>

248 return A.get() != B.get();

249}

250

251template <class T, class U>

253 return A.get() == B;

254}

255

256template <class T, class U>

258 return A.get() != B;

259}

260

261template <class T, class U>

263 return A == B.get();

264}

265

266template <class T, class U>

268 return A != B.get();

269}

270

271template

275

276template

280

281template

283 return !(A == B);

284}

285

286template

288 return !(A == B);

289}

290

291

292

293template struct simplify_type;

294

297

299 return Val.get();

300 }

301};

302

305

307 return Val.get();

308 }

309};

310

311

312template <typename T, typename... Args>

316

317}

318

319#endif

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")

static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")

static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")

A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...

Definition IntrusiveRefCntPtr.h:173

~IntrusiveRefCntPtr()

Definition IntrusiveRefCntPtr.h:196

void reset()

Definition IntrusiveRefCntPtr.h:214

IntrusiveRefCntPtr(T *obj)

Definition IntrusiveRefCntPtr.h:180

IntrusiveRefCntPtr & operator=(IntrusiveRefCntPtr S)

Definition IntrusiveRefCntPtr.h:198

unsigned useCount() const

Definition IntrusiveRefCntPtr.h:221

IntrusiveRefCntPtr(IntrusiveRefCntPtr &&S)

Definition IntrusiveRefCntPtr.h:182

T element_type

Definition IntrusiveRefCntPtr.h:177

friend class IntrusiveRefCntPtr

Definition IntrusiveRefCntPtr.h:236

T & operator*() const

Definition IntrusiveRefCntPtr.h:203

IntrusiveRefCntPtr(const IntrusiveRefCntPtr &S)

Definition IntrusiveRefCntPtr.h:181

void swap(IntrusiveRefCntPtr &other)

Definition IntrusiveRefCntPtr.h:208

ResourceTracker * get() const

Definition IntrusiveRefCntPtr.h:205

IntrusiveRefCntPtr(std::unique_ptr< X > S)

Definition IntrusiveRefCntPtr.h:192

IntrusiveRefCntPtr(IntrusiveRefCntPtr< X > S)

Definition IntrusiveRefCntPtr.h:186

void resetWithoutRelease()

Definition IntrusiveRefCntPtr.h:219

T * operator->() const

Definition IntrusiveRefCntPtr.h:204

IntrusiveRefCntPtr()=default

RefCountedBase(const RefCountedBase &)

Definition IntrusiveRefCntPtr.h:81

void Retain() const

Definition IntrusiveRefCntPtr.h:98

RefCountedBase & operator=(const RefCountedBase &)=delete

unsigned UseCount() const

Definition IntrusiveRefCntPtr.h:96

~RefCountedBase()

Definition IntrusiveRefCntPtr.h:85

void Release() const

Definition IntrusiveRefCntPtr.h:100

A thread-safe version of RefCountedBase.

Definition IntrusiveRefCntPtr.h:108

ThreadSafeRefCountedBase(const ThreadSafeRefCountedBase &)

Definition IntrusiveRefCntPtr.h:113

~ThreadSafeRefCountedBase()

Definition IntrusiveRefCntPtr.h:118

ThreadSafeRefCountedBase()=default

ThreadSafeRefCountedBase & operator=(const ThreadSafeRefCountedBase &)=delete

void Retain() const

Definition IntrusiveRefCntPtr.h:131

void Release() const

Definition IntrusiveRefCntPtr.h:133

unsigned UseCount() const

Definition IntrusiveRefCntPtr.h:129

This is an optimization pass for GlobalISel generic memory operations.

bool operator!=(uint64_t V1, const APInt &V2)

IntrusiveRefCntPtr< T > makeIntrusiveRefCnt(Args &&...A)

Factory function for creating intrusive ref counted pointers.

Definition IntrusiveRefCntPtr.h:313

bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)

Class you can specialize to provide custom retain/release functionality for a type.

Definition IntrusiveRefCntPtr.h:161

static void retain(T *obj)

Definition IntrusiveRefCntPtr.h:163

static unsigned useCount(const T *obj)

Definition IntrusiveRefCntPtr.h:162

static void release(T *obj)

Definition IntrusiveRefCntPtr.h:164

T * SimpleType

Definition IntrusiveRefCntPtr.h:296

static SimpleType getSimplifiedValue(IntrusiveRefCntPtr< T > &Val)

Definition IntrusiveRefCntPtr.h:298

T * SimpleType

Definition IntrusiveRefCntPtr.h:304

static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr< T > &Val)

Definition IntrusiveRefCntPtr.h:306

Define a template that can be specialized by smart pointers to reflect the fact that they are automat...