RefCntTest.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright 2012 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkRefCnt.h"
  8. #include "include/core/SkTypes.h"
  9. #include "include/private/SkWeakRefCnt.h"
  10. #include "tests/Test.h"
  11. #include <thread>
  12. static void bounce_ref(void* data) {
  13. SkRefCnt* ref = static_cast<SkRefCnt*>(data);
  14. for (int i = 0; i < 100000; ++i) {
  15. ref->ref();
  16. ref->unref();
  17. }
  18. }
  19. static void test_refCnt(skiatest::Reporter* reporter) {
  20. SkRefCnt* ref = new SkRefCnt();
  21. std::thread thing1(bounce_ref, ref);
  22. std::thread thing2(bounce_ref, ref);
  23. thing1.join();
  24. thing2.join();
  25. REPORTER_ASSERT(reporter, ref->unique());
  26. ref->unref();
  27. }
  28. static void bounce_weak_ref(void* data) {
  29. SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data);
  30. for (int i = 0; i < 100000; ++i) {
  31. if (ref->try_ref()) {
  32. ref->unref();
  33. }
  34. }
  35. }
  36. static void bounce_weak_weak_ref(void* data) {
  37. SkWeakRefCnt* ref = static_cast<SkWeakRefCnt*>(data);
  38. for (int i = 0; i < 100000; ++i) {
  39. ref->weak_ref();
  40. ref->weak_unref();
  41. }
  42. }
  43. static void test_weakRefCnt(skiatest::Reporter* reporter) {
  44. SkWeakRefCnt* ref = new SkWeakRefCnt();
  45. std::thread thing1(bounce_ref, ref);
  46. std::thread thing2(bounce_ref, ref);
  47. std::thread thing3(bounce_weak_ref, ref);
  48. std::thread thing4(bounce_weak_weak_ref, ref);
  49. thing1.join();
  50. thing2.join();
  51. thing3.join();
  52. thing4.join();
  53. REPORTER_ASSERT(reporter, ref->unique());
  54. SkDEBUGCODE(REPORTER_ASSERT(reporter, ref->getWeakCnt() == 1));
  55. ref->unref();
  56. }
  57. DEF_TEST(RefCnt, reporter) {
  58. test_refCnt(reporter);
  59. test_weakRefCnt(reporter);
  60. }
  61. ///////////////////////////////////////////////////////////////////////////////////////////////////
  62. static int gRefCounter;
  63. static int gUnrefCounter;
  64. static int gNewCounter;
  65. static int gDeleteCounter;
  66. #define check(reporter, ref, unref, make, kill) \
  67. REPORTER_ASSERT(reporter, gRefCounter == ref); \
  68. REPORTER_ASSERT(reporter, gUnrefCounter == unref); \
  69. REPORTER_ASSERT(reporter, gNewCounter == make); \
  70. REPORTER_ASSERT(reporter, gDeleteCounter == kill)
  71. class Effect {
  72. public:
  73. Effect() : fRefCnt(1) {
  74. gNewCounter += 1;
  75. }
  76. virtual ~Effect() {}
  77. int fRefCnt;
  78. void ref() {
  79. gRefCounter += 1;
  80. fRefCnt += 1;
  81. }
  82. void unref() {
  83. gUnrefCounter += 1;
  84. SkASSERT(fRefCnt > 0);
  85. if (0 == --fRefCnt) {
  86. gDeleteCounter += 1;
  87. delete this;
  88. }
  89. }
  90. int* method() const { return new int; }
  91. };
  92. static sk_sp<Effect> Create() {
  93. return sk_make_sp<Effect>();
  94. }
  95. class Paint {
  96. public:
  97. sk_sp<Effect> fEffect;
  98. const sk_sp<Effect>& get() const { return fEffect; }
  99. void set(sk_sp<Effect> value) {
  100. fEffect = std::move(value);
  101. }
  102. };
  103. struct EffectImpl : public Effect {
  104. ~EffectImpl() override {}
  105. static sk_sp<EffectImpl> Create() {
  106. return sk_sp<EffectImpl>(new EffectImpl);
  107. }
  108. int fValue;
  109. };
  110. static sk_sp<Effect> make_effect() {
  111. auto foo = EffectImpl::Create();
  112. foo->fValue = 42;
  113. return std::move(foo);
  114. }
  115. static void reset_counters() {
  116. gRefCounter = 0;
  117. gUnrefCounter = 0;
  118. gNewCounter = 0;
  119. gDeleteCounter = 0;
  120. }
  121. DEF_TEST(sk_sp, reporter) {
  122. reset_counters();
  123. Paint paint;
  124. REPORTER_ASSERT(reporter, paint.fEffect.get() == nullptr);
  125. REPORTER_ASSERT(reporter, !paint.get());
  126. check(reporter, 0, 0, 0, 0);
  127. paint.set(Create());
  128. check(reporter, 0, 0, 1, 0);
  129. REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 1);
  130. if (paint.get()) {
  131. REPORTER_ASSERT(reporter, true);
  132. } else {
  133. REPORTER_ASSERT(reporter, false);
  134. }
  135. if (!paint.get()) {
  136. REPORTER_ASSERT(reporter, false);
  137. } else {
  138. REPORTER_ASSERT(reporter, true);
  139. }
  140. paint.set(nullptr);
  141. check(reporter, 0, 1, 1, 1);
  142. if (paint.get()) {
  143. REPORTER_ASSERT(reporter, false);
  144. } else {
  145. REPORTER_ASSERT(reporter, true);
  146. }
  147. if (!paint.get()) {
  148. REPORTER_ASSERT(reporter, true);
  149. } else {
  150. REPORTER_ASSERT(reporter, false);
  151. }
  152. auto e = Create();
  153. REPORTER_ASSERT(reporter, sizeof(e) == sizeof(void*));
  154. check(reporter, 0, 1, 2, 1);
  155. paint.set(e);
  156. check(reporter, 1, 1, 2, 1);
  157. REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 2);
  158. Paint paint2;
  159. paint2.set(paint.get());
  160. check(reporter, 2, 1, 2, 1);
  161. REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 3);
  162. // Test sk_sp::operator->
  163. delete paint.get()->method();
  164. check(reporter, 2, 1, 2, 1);
  165. // Test sk_sp::operator*
  166. delete (*paint.get()).method();
  167. check(reporter, 2, 1, 2, 1);
  168. paint.set(nullptr);
  169. e = nullptr;
  170. paint2.set(nullptr);
  171. check(reporter, 2, 4, 2, 2);
  172. reset_counters();
  173. {
  174. // Test convertible sk_sp assignment.
  175. check(reporter, 0, 0, 0, 0);
  176. sk_sp<Effect> foo(nullptr);
  177. REPORTER_ASSERT(reporter, !foo);
  178. foo = make_effect();
  179. REPORTER_ASSERT(reporter, foo);
  180. check(reporter, 0, 0, 1, 0);
  181. }
  182. check(reporter, 0, 1, 1, 1);
  183. // Test passing convertible rvalue into funtion.
  184. reset_counters();
  185. paint.set(EffectImpl::Create());
  186. check(reporter, 0, 0, 1, 0);
  187. paint.set(nullptr);
  188. check(reporter, 0, 1, 1, 1);
  189. reset_counters();
  190. auto baz = EffectImpl::Create();
  191. check(reporter, 0, 0, 1, 0);
  192. paint.set(std::move(baz));
  193. check(reporter, 0, 0, 1, 0);
  194. REPORTER_ASSERT(reporter, !baz); // NOLINT(bugprone-use-after-move)
  195. paint.set(nullptr);
  196. check(reporter, 0, 1, 1, 1);
  197. reset_counters();
  198. {
  199. // test comparison operator with convertible type.
  200. sk_sp<EffectImpl> bar1 = EffectImpl::Create();
  201. sk_sp<Effect> bar2(bar1); // convertible copy constructor
  202. check(reporter, 1, 0, 1, 0);
  203. REPORTER_ASSERT(reporter, bar1);
  204. REPORTER_ASSERT(reporter, bar2);
  205. REPORTER_ASSERT(reporter, bar1 == bar2);
  206. REPORTER_ASSERT(reporter, bar2 == bar1);
  207. REPORTER_ASSERT(reporter, !(bar1 != bar2));
  208. REPORTER_ASSERT(reporter, !(bar2 != bar1));
  209. sk_sp<Effect> bar3(nullptr);
  210. bar3 = bar1; // convertible copy assignment
  211. check(reporter, 2, 0, 1, 0);
  212. }
  213. check(reporter, 2, 3, 1, 1);
  214. // test passing convertible copy into funtion.
  215. reset_counters();
  216. baz = EffectImpl::Create();
  217. check(reporter, 0, 0, 1, 0);
  218. paint.set(baz);
  219. check(reporter, 1, 0, 1, 0);
  220. baz = nullptr;
  221. check(reporter, 1, 1, 1, 0);
  222. paint.set(nullptr);
  223. check(reporter, 1, 2, 1, 1);
  224. {
  225. sk_sp<SkRefCnt> empty;
  226. sk_sp<SkRefCnt> notEmpty = sk_make_sp<SkRefCnt>();
  227. REPORTER_ASSERT(reporter, empty == sk_sp<SkRefCnt>());
  228. REPORTER_ASSERT(reporter, notEmpty != empty);
  229. REPORTER_ASSERT(reporter, empty != notEmpty);
  230. REPORTER_ASSERT(reporter, nullptr == empty);
  231. REPORTER_ASSERT(reporter, empty == nullptr);
  232. REPORTER_ASSERT(reporter, empty == empty);
  233. REPORTER_ASSERT(reporter, nullptr <= empty);
  234. REPORTER_ASSERT(reporter, empty <= nullptr);
  235. REPORTER_ASSERT(reporter, empty <= empty);
  236. REPORTER_ASSERT(reporter, nullptr >= empty);
  237. REPORTER_ASSERT(reporter, empty >= nullptr);
  238. REPORTER_ASSERT(reporter, empty >= empty);
  239. }
  240. {
  241. sk_sp<SkRefCnt> a = sk_make_sp<SkRefCnt>();
  242. sk_sp<SkRefCnt> b = sk_make_sp<SkRefCnt>();
  243. REPORTER_ASSERT(reporter, a != b);
  244. REPORTER_ASSERT(reporter, (a < b) != (b < a));
  245. REPORTER_ASSERT(reporter, (b > a) != (a > b));
  246. REPORTER_ASSERT(reporter, (a <= b) != (b <= a));
  247. REPORTER_ASSERT(reporter, (b >= a) != (a >= b));
  248. REPORTER_ASSERT(reporter, a == a);
  249. REPORTER_ASSERT(reporter, a <= a);
  250. REPORTER_ASSERT(reporter, a >= a);
  251. }
  252. // http://wg21.cmeerw.net/lwg/issue998
  253. {
  254. class foo : public SkRefCnt {
  255. public:
  256. foo() : bar(this) {}
  257. void reset() { bar.reset(); }
  258. private:
  259. sk_sp<foo> bar;
  260. };
  261. // The following should properly delete the object and not cause undefined behavior.
  262. // This is an ugly example, but the same issue can arise in more subtle ways.
  263. (new foo)->reset();
  264. }
  265. // https://crrev.com/0d4ef2583a6f19c3e61be04d36eb1a60b133832c
  266. {
  267. struct StructB;
  268. struct StructA : public SkRefCnt {
  269. sk_sp<StructB> b;
  270. };
  271. struct StructB : public SkRefCnt {
  272. sk_sp<StructA> a;
  273. ~StructB() override {} // Some clang versions don't emit this implicitly.
  274. };
  275. // Create a reference cycle.
  276. StructA* a = new StructA;
  277. a->b.reset(new StructB);
  278. a->b->a.reset(a);
  279. // Break the cycle by calling reset(). This will cause |a| (and hence, |a.b|)
  280. // to be deleted before the call to reset() returns. This tests that the
  281. // implementation of sk_sp::reset() doesn't access |this| after it
  282. // deletes the underlying pointer. This behaviour is consistent with the
  283. // definition of unique_ptr::reset in C++11.
  284. a->b.reset();
  285. }
  286. }
  287. namespace {
  288. struct FooAbstract : public SkRefCnt {
  289. virtual void f() = 0;
  290. };
  291. struct FooConcrete : public FooAbstract {
  292. void f() override {}
  293. };
  294. }
  295. static sk_sp<FooAbstract> make_foo() {
  296. // can not cast FooConcrete to FooAbstract.
  297. // can cast FooConcrete* to FooAbstract*.
  298. return sk_make_sp<FooConcrete>();
  299. }
  300. DEF_TEST(sk_make_sp, r) {
  301. auto x = make_foo();
  302. }
  303. // Test that reset() "adopts" ownership from the caller, even if we are given the same ptr twice
  304. //
  305. DEF_TEST(sk_sp_reset, r) {
  306. SkRefCnt* rc = new SkRefCnt;
  307. REPORTER_ASSERT(r, rc->unique());
  308. sk_sp<SkRefCnt> sp;
  309. sp.reset(rc);
  310. // We have transfered our ownership over to sp
  311. REPORTER_ASSERT(r, rc->unique());
  312. rc->ref(); // now "rc" is also an owner
  313. REPORTER_ASSERT(r, !rc->unique());
  314. sp.reset(rc); // this should transfer our ownership over to sp
  315. REPORTER_ASSERT(r, rc->unique());
  316. }
  317. DEF_TEST(sk_sp_ref, r) {
  318. SkRefCnt* rc = new SkRefCnt;
  319. REPORTER_ASSERT(r, rc->unique());
  320. {
  321. sk_sp<SkRefCnt> sp = sk_ref_sp(rc);
  322. REPORTER_ASSERT(r, !rc->unique());
  323. }
  324. REPORTER_ASSERT(r, rc->unique());
  325. rc->unref();
  326. }