Debian Codegraph @

mfbt/tests/TestUniquePtr.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include <stddef.h>
8
9 #include <memory> // For unique_ptr
10 #include <type_traits>
11 #include <utility>
12
13 #include "mozilla/Assertions.h"
14 #include "mozilla/UniquePtr.h"
15 #include "mozilla/UniquePtrExtensions.h"
16 #include "mozilla/Vector.h"
17
18 using mozilla::DefaultDelete;
19 using mozilla::MakeUnique;
20 using mozilla::UniqueFreePtr;
21 using mozilla::UniquePtr;
22 using mozilla::Vector;
23
24 #define CHECK(c) \
25 do { \
26 bool cond = !!(c); \
27 MOZ_RELEASE_ASSERT(cond, "Test failed: " #c); \
28 } while (false)
29
30 typedef UniquePtr<int> NewInt;
31 static_assert(sizeof(NewInt) == sizeof(int*), "stored most efficiently");
32
33 static size_t gADestructorCalls = 0;
34
35 struct A {
36 public:
37 A() : mX(0) {}
38 virtual ~A() { gADestructorCalls++; }
39
40 int mX;
41 };
42
43 static size_t gBDestructorCalls = 0;
44
45 struct B : public A {
46 public:
47 B() : mY(1) {}
48 ~B() { gBDestructorCalls++; }
49
50 int mY;
51 };
52
53 typedef UniquePtr<A> UniqueA;
54 typedef UniquePtr<B, UniqueA::DeleterType> UniqueB; // permit interconversion
55
56 static_assert(sizeof(UniqueA) == sizeof(A*), "stored most efficiently");
57 static_assert(sizeof(UniqueB) == sizeof(B*), "stored most efficiently");
58
59 struct DeleterSubclass : UniqueA::DeleterType {};
60
61 typedef UniquePtr<B, DeleterSubclass> UniqueC;
62 static_assert(sizeof(UniqueC) == sizeof(B*), "stored most efficiently");
63
64 static UniqueA ReturnUniqueA() { return UniqueA(new B); }
65
66 static UniqueA ReturnLocalA() {
67 UniqueA a(new A);
68 return a;
69 }
70
71 static void TestDeleterType() {
72 // Make sure UniquePtr will use its deleter's pointer type if it defines one.
73 typedef int* Ptr;
74 struct Deleter {
75 typedef Ptr pointer;
76 Deleter() = default;
77 void operator()(int* p) { delete p; }
78 };
79 UniquePtr<Ptr, Deleter> u(new int, Deleter());
80 }
81
82 static bool TestDefaultFreeGuts() {
83 static_assert(std::is_same_v<NewInt::DeleterType, DefaultDelete<int> >,
84 "weird deleter?");
85
86 NewInt n1(new int);
87 CHECK(n1);
88 CHECK(n1.get() != nullptr);
89
90 n1 = nullptr;
91 CHECK(!n1);
92 CHECK(n1.get() == nullptr);
93
94 int* p1 = new int;
95 n1.reset(p1);
96 CHECK(n1);
97 NewInt n2(std::move(n1));
98 CHECK(!n1);
99 CHECK(n1.get() == nullptr);
100 CHECK(n2.get() == p1);
101
102 std::swap(n1, n2);
103 CHECK(n1.get() == p1);
104 CHECK(n2.get() == nullptr);
105
106 n1.swap(n2);
107 CHECK(n1.get() == nullptr);
108 CHECK(n2.get() == p1);
109 delete n2.release();
110
111 CHECK(n1.get() == nullptr);
112 CHECK(n2 == nullptr);
113 CHECK(nullptr == n2);
114
115 int* p2 = new int;
116 int* p3 = new int;
117 n1.reset(p2);
118 n2.reset(p3);
119 CHECK(n1.get() == p2);
120 CHECK(n2.get() == p3);
121
122 n1.swap(n2);
123 CHECK(n2 != nullptr);
124 CHECK(nullptr != n2);
125 CHECK(n2.get() == p2);
126 CHECK(n1.get() == p3);
127
128 UniqueA a1;
129 CHECK(a1 == nullptr);
130 a1.reset(new A);
131 CHECK(gADestructorCalls == 0);
132 CHECK(a1->mX == 0);
133
134 B* bp1 = new B;
135 bp1->mX = 5;
136 CHECK(gBDestructorCalls == 0);
137 a1.reset(bp1);
138 CHECK(gADestructorCalls == 1);
139 CHECK(a1->mX == 5);
140 a1.reset(nullptr);
141 CHECK(gADestructorCalls == 2);
142 CHECK(gBDestructorCalls == 1);
143
144 B* bp2 = new B;
145 UniqueB b1(bp2);
146 UniqueA a2(nullptr);
147 a2 = std::move(b1);
148 CHECK(gADestructorCalls == 2);
149 CHECK(gBDestructorCalls == 1);
150
151 UniqueA a3(std::move(a2));
152 a3 = nullptr;
153 CHECK(gADestructorCalls == 3);
154 CHECK(gBDestructorCalls == 2);
155
156 B* bp3 = new B;
157 bp3->mX = 42;
158 UniqueB b2(bp3);
159 UniqueA a4(std::move(b2));
160 CHECK(b2.get() == nullptr);
161 CHECK((*a4).mX == 42);
162 CHECK(gADestructorCalls == 3);
163 CHECK(gBDestructorCalls == 2);
164
165 UniqueA a5(new A);
166 UniqueB b3(new B);
167 a5 = std::move(b3);
168 CHECK(gADestructorCalls == 4);
169 CHECK(gBDestructorCalls == 2);
170
171 ReturnUniqueA();
172 CHECK(gADestructorCalls == 5);
173 CHECK(gBDestructorCalls == 3);
174
175 ReturnLocalA();
176 CHECK(gADestructorCalls == 6);
177 CHECK(gBDestructorCalls == 3);
178
179 UniqueA a6(ReturnLocalA());
180 a6 = nullptr;
181 CHECK(gADestructorCalls == 7);
182 CHECK(gBDestructorCalls == 3);
183
184 UniqueC c1(new B);
185 UniqueA a7(new B);
186 a7 = std::move(c1);
187 CHECK(gADestructorCalls == 8);
188 CHECK(gBDestructorCalls == 4);
189
190 c1.reset(new B);
191
192 UniqueA a8(std::move(c1));
193 CHECK(gADestructorCalls == 8);
194 CHECK(gBDestructorCalls == 4);
195
196 // These smart pointers still own B resources.
197 CHECK(a4);
198 CHECK(a5);
199 CHECK(a7);
200 CHECK(a8);
201 return true;
202 }
203
204 static bool TestDefaultFree() {
205 CHECK(TestDefaultFreeGuts());
206 CHECK(gADestructorCalls == 12);
207 CHECK(gBDestructorCalls == 8);
208 return true;
209 }
210
211 static size_t FreeClassCounter = 0;
212
213 struct FreeClass {
214 public:
215 FreeClass() = default;
216
217 void operator()(int* aPtr) {
218 FreeClassCounter++;
219 delete aPtr;
220 }
221 };
222
223 typedef UniquePtr<int, FreeClass> NewIntCustom;
224 static_assert(sizeof(NewIntCustom) == sizeof(int*), "stored most efficiently");
225
226 static bool TestFreeClass() {
227 CHECK(FreeClassCounter == 0);
228 {
229 NewIntCustom n1(new int);
230 CHECK(FreeClassCounter == 0);
231 }
232 CHECK(FreeClassCounter == 1);
233
234 NewIntCustom n2;
235 {
236 NewIntCustom n3(new int);
237 CHECK(FreeClassCounter == 1);
238 n2 = std::move(n3);
239 }
240 CHECK(FreeClassCounter == 1);
241 n2 = nullptr;
242 CHECK(FreeClassCounter == 2);
243
244 n2.reset(nullptr);
245 CHECK(FreeClassCounter == 2);
246 n2.reset(new int);
247 n2.reset();
248 CHECK(FreeClassCounter == 3);
249
250 NewIntCustom n4(new int, FreeClass());
251 CHECK(FreeClassCounter == 3);
252 n4.reset(new int);
253 CHECK(FreeClassCounter == 4);
254 n4.reset();
255 CHECK(FreeClassCounter == 5);
256
257 FreeClass f;
258 NewIntCustom n5(new int, f);
259 CHECK(FreeClassCounter == 5);
260 int* p = n5.release();
261 CHECK(FreeClassCounter == 5);
262 delete p;
263
264 return true;
265 }
266
267 typedef UniquePtr<int, DefaultDelete<int>&> IntDeleterRef;
268 typedef UniquePtr<A, DefaultDelete<A>&> ADeleterRef;
269 typedef UniquePtr<B, DefaultDelete<A>&> BDeleterRef;
270
271 static_assert(sizeof(IntDeleterRef) > sizeof(int*),
272 "has to be heavier than an int* to store the reference");
273 static_assert(sizeof(ADeleterRef) > sizeof(A*),
274 "has to be heavier than an A* to store the reference");
275 static_assert(sizeof(BDeleterRef) > sizeof(int*),
276 "has to be heavier than a B* to store the reference");
277
278 static bool TestReferenceDeleterGuts() {
279 DefaultDelete<int> delInt;
280 IntDeleterRef id1(new int, delInt);
281
282 IntDeleterRef id2(std::move(id1));
283 CHECK(id1 == nullptr);
284 CHECK(nullptr != id2);
285 CHECK(&id1.get_deleter() == &id2.get_deleter());
286
287 IntDeleterRef id3(std::move(id2));
288
289 DefaultDelete<A> delA;
290 ADeleterRef a1(new A, delA);
291 a1.reset(nullptr);
292 a1.reset(new B);
293 a1 = nullptr;
294
295 BDeleterRef b1(new B, delA);
296 a1 = std::move(b1);
297
298 BDeleterRef b2(new B, delA);
299
300 ADeleterRef a2(std::move(b2));
301
302 return true;
303 }
304
305 static bool TestReferenceDeleter() {
306 gADestructorCalls = 0;
307 gBDestructorCalls = 0;
308
309 CHECK(TestReferenceDeleterGuts());
310
311 CHECK(gADestructorCalls == 4);
312 CHECK(gBDestructorCalls == 3);
313
314 gADestructorCalls = 0;
315 gBDestructorCalls = 0;
316 return true;
317 }
318
319 typedef void (&FreeSignature)(void*);
320
321 static size_t DeleteIntFunctionCallCount = 0;
322
323 static void DeleteIntFunction(void* aPtr) {
324 DeleteIntFunctionCallCount++;
325 delete[] static_cast<int*>(aPtr);
326 }
327
328 static void SetMallocedInt(UniquePtr<int, FreeSignature>& aPtr, int aI) {
329 int* newPtr = static_cast<int*>(malloc(sizeof(int)));
330 *newPtr = aI;
331 aPtr.reset(newPtr);
332 }
333
334 static UniquePtr<int, FreeSignature> MallocedInt(int aI) {
335 UniquePtr<int, FreeSignature> ptr(static_cast<int*>(malloc(sizeof(int))),
336 free);
337 *ptr = aI;
338 return ptr;
339 }
340 static bool TestFunctionReferenceDeleter() {
341 // Look for allocator mismatches and leaks to verify these bits
342 UniquePtr<int, FreeSignature> i1(MallocedInt(17));
343 CHECK(*i1 == 17);
344
345 SetMallocedInt(i1, 42);
346 CHECK(*i1 == 42);
347
348 // These bits use a custom deleter so we can instrument deletion.
349 {
350 UniquePtr<int, FreeSignature> i2 =
351 UniquePtr<int, FreeSignature>(new int[42], DeleteIntFunction);
352 CHECK(DeleteIntFunctionCallCount == 0);
353
354 i2.reset(new int[76]);
355 CHECK(DeleteIntFunctionCallCount == 1);
356 }
357
358 CHECK(DeleteIntFunctionCallCount == 2);
359
360 return true;
361 }
362
363 template <typename T>
364 struct AppendNullptrTwice {
365 AppendNullptrTwice() = default;
366
367 bool operator()(Vector<T>& vec) {
368 CHECK(vec.append(nullptr));
369 CHECK(vec.append(nullptr));
370 return true;
371 }
372 };
373
374 static size_t AAfter;
375 static size_t BAfter;
376
377 static bool TestVectorGuts() {
378 Vector<UniqueA> vec;
379 CHECK(vec.append(new B));
380 CHECK(vec.append(new A));
381 CHECK(AppendNullptrTwice<UniqueA>()(vec));
382 CHECK(vec.append(new B));
383
384 size_t initialLength = vec.length();
385
386 UniqueA* begin = vec.begin();
387 bool appendA = true;
388 do {
389 CHECK(appendA ? vec.append(new A) : vec.append(new B));
390 appendA = !appendA;
391 } while (begin == vec.begin());
392
393 size_t numAppended = vec.length() - initialLength;
394
395 BAfter = numAppended / 2;
396 AAfter = numAppended - numAppended / 2;
397
398 CHECK(gADestructorCalls == 0);
399 CHECK(gBDestructorCalls == 0);
400 return true;
401 }
402
403 static bool TestVector() {
404 gADestructorCalls = 0;
405 gBDestructorCalls = 0;
406
407 CHECK(TestVectorGuts());
408
409 CHECK(gADestructorCalls == 3 + AAfter + BAfter);
410 CHECK(gBDestructorCalls == 2 + BAfter);
411 return true;
412 }
413
414 typedef UniquePtr<int[]> IntArray;
415 static_assert(sizeof(IntArray) == sizeof(int*), "stored most efficiently");
416
417 static bool TestArray() {
418 static_assert(std::is_same_v<IntArray::DeleterType, DefaultDelete<int[]> >,
419 "weird deleter?");
420
421 IntArray n1(new int[5]);
422 CHECK(n1);
423 CHECK(n1.get() != nullptr);
424
425 n1 = nullptr;
426 CHECK(!n1);
427 CHECK(n1.get() == nullptr);
428
429 int* p1 = new int[42];
430 n1.reset(p1);
431 CHECK(n1);
432 IntArray n2(std::move(n1));
433 CHECK(!n1);
434 CHECK(n1.get() == nullptr);
435 CHECK(n2.get() == p1);
436
437 std::swap(n1, n2);
438 CHECK(n1.get() == p1);
439 CHECK(n2.get() == nullptr);
440
441 n1.swap(n2);
442 CHECK(n1.get() == nullptr);
443 CHECK(n2.get() == p1);
444 delete[] n2.release();
445
446 CHECK(n1.get() == nullptr);
447 CHECK(n2.get() == nullptr);
448
449 int* p2 = new int[7];
450 int* p3 = new int[42];
451 n1.reset(p2);
452 n2.reset(p3);
453 CHECK(n1.get() == p2);
454 CHECK(n2.get() == p3);
455
456 n1.swap(n2);
457 CHECK(n2.get() == p2);
458 CHECK(n1.get() == p3);
459
460 n1 = std::move(n2);
461 CHECK(n1.get() == p2);
462 n1 = std::move(n2);
463 CHECK(n1.get() == nullptr);
464
465 UniquePtr<A[]> a1(new A[17]);
466 static_assert(sizeof(a1) == sizeof(A*), "stored most efficiently");
467
468 UniquePtr<A[]> a2(new A[5], DefaultDelete<A[]>());
469 a2.reset(nullptr);
470 a2.reset(new A[17]);
471 a2 = nullptr;
472
473 UniquePtr<A[]> a3(nullptr);
474 a3.reset(new A[7]);
475
476 return true;
477 }
478
479 struct Q {
480 Q() = default;
481 Q(const Q&) = default;
482
483 Q(Q&, char) {}
484
485 template <typename T>
486 Q(Q, T&&, int) {}
487
488 Q(int, long, double, void*) {}
489 };
490
491 static int randomInt() { return 4; }
492
493 static bool TestMakeUnique() {
494 UniquePtr<int> a1(MakeUnique<int>());
495 UniquePtr<long> a2(MakeUnique<long>(4));
496
497 // no args, easy
498 UniquePtr<Q> q0(MakeUnique<Q>());
499
500 // temporary bound to const lval ref
501 UniquePtr<Q> q1(MakeUnique<Q>(Q()));
502
503 // passing through a non-const lval ref
504 UniquePtr<Q> q2(MakeUnique<Q>(*q1, 'c'));
505
506 // pass by copying, forward a temporary, pass by value
507 UniquePtr<Q> q3(MakeUnique<Q>(Q(), UniquePtr<int>(), randomInt()));
508
509 // various type mismatching to test "fuzzy" forwarding
510 UniquePtr<Q> q4(MakeUnique<Q>('s', 66LL, 3.141592654, &q3));
511
512 UniquePtr<char[]> c1(MakeUnique<char[]>(5));
513
514 return true;
515 }
516
517 static bool TestVoid() {
518 // UniquePtr<void> supports all operations except operator*() and
519 // operator->().
520 UniqueFreePtr<void> p1(malloc(1));
521 UniqueFreePtr<void> p2;
522
523 auto x = p1.get();
524 CHECK(x != nullptr);
525 CHECK((std::is_same_v<decltype(x), void*>));
526
527 p2.reset(p1.release());
528 CHECK(p1.get() == nullptr);
529 CHECK(p2.get() != nullptr);
530
531 p1 = std::move(p2);
532 CHECK(p1);
533 CHECK(!p2);
534
535 p1.swap(p2);
536 CHECK(!p1);
537 CHECK(p2);
538
539 p2 = nullptr;
540 CHECK(!p2);
541
542 return true;
543 }
544
545 static bool TestTempPtrToSetter() {
546 static int sFooRefcount = 0;
547 struct Foo {
548 Foo() { sFooRefcount += 1; }
549
550 ~Foo() { sFooRefcount -= 1; }
551 };
552
553 const auto AllocByOutvar = [](Foo** out) -> bool {
554 *out = new Foo;
555 return true;
556 };
557
558 {
559 UniquePtr<Foo> f;
560 (void)AllocByOutvar(mozilla::TempPtrToSetter(&f));
561 CHECK(sFooRefcount == 1);
562 }
563 CHECK(sFooRefcount == 0);
564
565 {
566 std::unique_ptr<Foo> f;
567 (void)AllocByOutvar(mozilla::TempPtrToSetter(&f));
568 CHECK(sFooRefcount == 1);
569 }
570 CHECK(sFooRefcount == 0);
571
572 return true;
573 }
574
575 int main() {
576 TestDeleterType();
577
578 if (!TestDefaultFree()) {
579 return 1;
580 }
581 if (!TestFreeClass()) {
582 return 1;
583 }
584 if (!TestReferenceDeleter()) {
585 return 1;
586 }
587 if (!TestFunctionReferenceDeleter()) {
588 return 1;
589 }
590 if (!TestVector()) {
591 return 1;
592 }
593 if (!TestArray()) {
594 return 1;
595 }
596 if (!TestMakeUnique()) {
597 return 1;
598 }
599 if (!TestVoid()) {
600 return 1;
601 }
602 if (!TestTempPtrToSetter()) {
603 return 1;
604 }
605 return 0;
606 }