1 //===-- iterate.cpp ---------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "gwp_asan/tests/harness.h"
15 TEST_F(CustomGuardedPoolAllocator
, Iterate
) {
17 std::vector
<std::pair
<void *, size_t>> Allocated
;
18 auto alloc
= [&](size_t size
) {
19 Allocated
.push_back({GPA
.allocate(size
), size
});
22 void *Ptr
= GPA
.allocate(5);
28 std::sort(Allocated
.begin(), Allocated
.end());
31 void *Base
= Allocated
[0].first
;
32 size_t Size
= reinterpret_cast<size_t>(Allocated
.back().first
) -
33 reinterpret_cast<size_t>(Base
) + 1;
34 std::vector
<std::pair
<void *, size_t>> Found
;
37 [](uintptr_t Addr
, size_t Size
, void *Arg
) {
38 reinterpret_cast<std::vector
<std::pair
<void *, size_t>> *>(Arg
)
39 ->push_back({(void *)Addr
, Size
});
41 reinterpret_cast<void *>(&Found
));
44 std::sort(Found
.begin(), Found
.end());
45 EXPECT_EQ(Allocated
, Found
);
47 // Now without the last allocation.
49 Size
= reinterpret_cast<size_t>(Allocated
.back().first
) -
50 reinterpret_cast<size_t>(Base
); // Allocated.back() is out of range.
54 [](uintptr_t Addr
, size_t Size
, void *Arg
) {
55 reinterpret_cast<std::vector
<std::pair
<void *, size_t>> *>(Arg
)
56 ->push_back({(void *)Addr
, Size
});
58 reinterpret_cast<void *>(&Found
));
61 // We should have found every allocation but the last.
62 // Remove it and compare the rest.
63 std::sort(Found
.begin(), Found
.end());
64 GPA
.deallocate(Allocated
.back().first
);
66 EXPECT_EQ(Allocated
, Found
);
68 for (auto PS
: Allocated
)
69 GPA
.deallocate(PS
.first
);