Skip to content

[Support] Add SpecificBumpPtrAllocator::identifyObject #100475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions llvm/include/llvm/Support/Allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,13 @@ template <typename T> class SpecificBumpPtrAllocator {

/// Allocate space for an array of objects without constructing them.
T *Allocate(size_t num = 1) { return Allocator.Allocate<T>(num); }

/// \return An index uniquely and reproducibly identifying
/// an input pointer \p Ptr in the given allocator.
/// Returns an empty optional if the pointer is not found in the allocator.
std::optional<int64_t> identifyObject(const void *Ptr) {
return Allocator.identifyObject(Ptr);
}
};

} // end namespace llvm
Expand Down
21 changes: 21 additions & 0 deletions llvm/unittests/Support/AllocatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,27 @@ TEST(AllocatorTest, TestSlowerSlabGrowthDelay) {
EXPECT_EQ(SlabSize * GrowthDelay + SlabSize * 2, Alloc.getTotalMemory());
}

TEST(AllocatorTest, TestIdentifyObject) {
BumpPtrAllocator Alloc;

uint64_t *a = (uint64_t *)Alloc.Allocate(sizeof(uint64_t), alignof(uint64_t));
std::optional<int64_t> maybe_a_belongs = Alloc.identifyObject(a);
EXPECT_TRUE(maybe_a_belongs.has_value());
EXPECT_TRUE(*maybe_a_belongs >= 0);

uint64_t *b = nullptr;
std::optional<int64_t> maybe_b_belongs = Alloc.identifyObject(b);
EXPECT_FALSE(maybe_b_belongs);

// The default slab size is 4096 (or 512 uint64_t values). Custom slabs are
// allocated when the requested size is larger than the slab size.
uint64_t *c =
(uint64_t *)Alloc.Allocate(sizeof(uint64_t) * 1024, alignof(uint64_t));
std::optional<int64_t> maybe_c_belongs = Alloc.identifyObject(c);
EXPECT_TRUE(maybe_c_belongs.has_value());
EXPECT_TRUE(*maybe_c_belongs < 0);
}

// Mock slab allocator that returns slabs aligned on 4096 bytes. There is no
// easy portable way to do this, so this is kind of a hack.
class MockSlabAllocator {
Expand Down
Loading