@@ -14,6 +14,10 @@ using namespace llvm;
1414
1515namespace {
1616
17+ struct Object1 {
18+ char Data[1 ];
19+ };
20+
1721struct Object8 {
1822 char Data[8 ];
1923};
@@ -22,12 +26,32 @@ class DecoratedMallocAllocator : public MallocAllocator {
2226public:
2327 int DeallocCount = 0 ;
2428
29+ void Deallocate (const void *Ptr, size_t Size, size_t Alignment) {
30+ DeallocCount++;
31+ MallocAllocator::Deallocate (Ptr, Size, Alignment);
32+ }
33+
2534 template <typename T> void Deallocate (T *Ptr) {
2635 DeallocCount++;
2736 MallocAllocator::Deallocate (Ptr);
2837 }
2938};
3039
40+ TEST (RecyclerTest, RecycleAllocation) {
41+ DecoratedMallocAllocator Allocator;
42+ // Recycler needs size to be atleast 8 bytes.
43+ Recycler<Object1, 8 , 8 > R;
44+ Object1 *A1 = R.Allocate (Allocator);
45+ Object1 *A2 = R.Allocate (Allocator);
46+ R.Deallocate (Allocator, A2);
47+ Object1 *A3 = R.Allocate (Allocator);
48+ EXPECT_EQ (A2, A3); // reuse the deallocated object.
49+ R.Deallocate (Allocator, A1);
50+ R.Deallocate (Allocator, A3);
51+ R.clear (Allocator); // Should deallocate A1 and A3.
52+ EXPECT_EQ (Allocator.DeallocCount , 2 );
53+ }
54+
3155TEST (RecyclerTest, MoveConstructor) {
3256 DecoratedMallocAllocator Allocator;
3357 Recycler<Object8> R;
0 commit comments