| 62 | }; |
| 63 | |
| 64 | TEST(TrackingAllocatorTest, SimpleNoTracking) { |
| 65 | Allocator* a = cpu_allocator(); |
| 66 | |
| 67 | EXPECT_FALSE(a->TracksAllocationSizes()); |
| 68 | |
| 69 | // Don't enable the tracking inside the tracking allocator. Since |
| 70 | // the cpu_allocator doesn't track allocations itself the tracking |
| 71 | // will be partial |
| 72 | TrackingAllocator* ta = new TrackingAllocator(a, false); |
| 73 | |
| 74 | void* p1 = ta->AllocateRaw(4, 4); |
| 75 | ta->DeallocateRaw(p1); |
| 76 | void* p2 = ta->AllocateRaw(4, 12); |
| 77 | |
| 78 | std::tuple<size_t, size_t, size_t> sizes = ta->GetSizes(); |
| 79 | |
| 80 | EXPECT_EQ(16, std::get<0>(sizes)); |
| 81 | EXPECT_EQ(0, std::get<1>(sizes)); |
| 82 | EXPECT_EQ(0, std::get<2>(sizes)); |
| 83 | |
| 84 | ta->DeallocateRaw(p2); |
| 85 | auto records = ta->GetRecordsAndUnRef(); |
| 86 | EXPECT_EQ(4, records[0].alloc_bytes); |
| 87 | EXPECT_EQ(12, records[1].alloc_bytes); |
| 88 | |
| 89 | // This time enable the tracking inside the tracking allocator |
| 90 | ta = new TrackingAllocator(a, true); |
| 91 | p1 = ta->AllocateRaw(4, 4); |
| 92 | EXPECT_EQ(4, ta->RequestedSize(p1)); |
| 93 | EXPECT_LE(4, ta->AllocatedSize(p1)); |
| 94 | EXPECT_EQ(1, ta->AllocationId(p1)); |
| 95 | |
| 96 | ta->DeallocateRaw(p1); |
| 97 | p2 = ta->AllocateRaw(4, 12); |
| 98 | EXPECT_EQ(12, ta->RequestedSize(p2)); |
| 99 | EXPECT_LE(12, ta->AllocatedSize(p2)); |
| 100 | EXPECT_EQ(2, ta->AllocationId(p2)); |
| 101 | |
| 102 | sizes = ta->GetSizes(); |
| 103 | |
| 104 | EXPECT_LE(16, std::get<0>(sizes)); |
| 105 | EXPECT_LE(12, std::get<1>(sizes)); |
| 106 | EXPECT_LE(12, std::get<2>(sizes)); |
| 107 | |
| 108 | ta->DeallocateRaw(p2); |
| 109 | records = ta->GetRecordsAndUnRef(); |
| 110 | EXPECT_LE(4, records[0].alloc_bytes); |
| 111 | EXPECT_GE(-4, records[1].alloc_bytes); |
| 112 | EXPECT_LE(12, records[2].alloc_bytes); |
| 113 | EXPECT_GE(-12, records[3].alloc_bytes); |
| 114 | } |
| 115 | |
| 116 | TEST(TrackingAllocatorTest, SimpleTracking) { |
| 117 | TestableSizeTrackingAllocator a = TestableSizeTrackingAllocator(); |
nothing calls this directly
no test coverage detected