|  | 
|  | 1 | +//===--- unittest/Support/RecyclerTest.cpp --------------------------------===// | 
|  | 2 | +// | 
|  | 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | 
|  | 4 | +// See https://llvm.org/LICENSE.txt for license information. | 
|  | 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
|  | 6 | +// | 
|  | 7 | +//===----------------------------------------------------------------------===// | 
|  | 8 | + | 
|  | 9 | +#include "llvm/Support/Recycler.h" | 
|  | 10 | +#include "llvm/Support/AllocatorBase.h" | 
|  | 11 | +#include "gtest/gtest.h" | 
|  | 12 | + | 
|  | 13 | +using namespace llvm; | 
|  | 14 | + | 
|  | 15 | +namespace { | 
|  | 16 | + | 
|  | 17 | +struct Object8 { | 
|  | 18 | + char Data[8]; | 
|  | 19 | +}; | 
|  | 20 | + | 
|  | 21 | +class DecoratedMallocAllocator : public MallocAllocator { | 
|  | 22 | +public: | 
|  | 23 | + int DeallocCount = 0; | 
|  | 24 | + | 
|  | 25 | + template <typename T> void Deallocate(T *Ptr) { | 
|  | 26 | + DeallocCount++; | 
|  | 27 | + MallocAllocator::Deallocate(Ptr); | 
|  | 28 | + } | 
|  | 29 | +}; | 
|  | 30 | + | 
|  | 31 | +TEST(RecyclerTest, MoveConstructor) { | 
|  | 32 | + DecoratedMallocAllocator Allocator; | 
|  | 33 | + Recycler<Object8> R; | 
|  | 34 | + Object8 *A1 = R.Allocate(Allocator); | 
|  | 35 | + Object8 *A2 = R.Allocate(Allocator); | 
|  | 36 | + R.Deallocate(Allocator, A1); | 
|  | 37 | + R.Deallocate(Allocator, A2); | 
|  | 38 | + Recycler<Object8> R2(std::move(R)); | 
|  | 39 | + Object8 *A3 = R2.Allocate(Allocator); | 
|  | 40 | + R2.Deallocate(Allocator, A3); | 
|  | 41 | + R.clear(Allocator); // Should not deallocate anything as it was moved from. | 
|  | 42 | + EXPECT_EQ(Allocator.DeallocCount, 0); | 
|  | 43 | + R2.clear(Allocator); | 
|  | 44 | + EXPECT_EQ(Allocator.DeallocCount, 2); | 
|  | 45 | +} | 
|  | 46 | + | 
|  | 47 | +} // end anonymous namespace | 
0 commit comments