Index: llvm/include/llvm/Support/Allocator.h =================================================================== --- llvm/include/llvm/Support/Allocator.h +++ llvm/include/llvm/Support/Allocator.h @@ -21,6 +21,7 @@ #ifndef LLVM_SUPPORT_ALLOCATOR_H #define LLVM_SUPPORT_ALLOCATOR_H +#include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/ErrorHandling.h" @@ -283,6 +284,28 @@ size_t GetNumSlabs() const { return Slabs.size() + CustomSizedSlabs.size(); } + /// \return A reproducible pair of objects, uniquely identifying + /// an input pointer \p Ptr in the given allocator. + /// Returns an empty optional if the pointer is not found in the allocator. + llvm::Optional> identifyObject(const void *Ptr) { + const char *P = reinterpret_cast(Ptr); + for (size_t Idx=0; Idx < Slabs.size(); Idx++) { + const char *S = reinterpret_cast(Slabs[Idx]); + if (P >= S && P < S + computeSlabSize(Idx)) + return std::make_pair(Idx, P - S); + } + for (size_t Idx=0; Idx < CustomSizedSlabs.size(); Idx++) { + const char *S = + reinterpret_cast(CustomSizedSlabs[Idx].first); + size_t Size = CustomSizedSlabs[Idx].second; + + // Use negative index to denote custom sized slabs. + if (P >= S && P < S + Size) + return std::make_pair(-Idx - 1, P - S); + } + return None; + } + size_t getTotalMemory() const { size_t TotalMemory = 0; for (auto I = Slabs.begin(), E = Slabs.end(); I != E; ++I)