george.karpenkov updated this revision to Diff 163224. george.karpenkov edited the summary of this revision. george.karpenkov added a comment.
In fact, generating a single "long" seems even better. https://reviews.llvm.org/D51393 Files: llvm/include/llvm/Support/Allocator.h 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,34 @@ size_t GetNumSlabs() const { return Slabs.size() + CustomSizedSlabs.size(); } + /// \return An index uniquely and reproducibly identifying + /// an input pointer \p Ptr in the given allocator. + /// The returned value is negative iff the object is inside a custom-size + /// slab. + /// Returns an empty optional if the pointer is not found in the allocator. + llvm::Optional<long> identifyObject(const void *Ptr) { + const char *P = reinterpret_cast<const char *>(Ptr); + long InSlabIdx = 0; + for (size_t Idx = 0; Idx < Slabs.size(); Idx++) { + const char *S = reinterpret_cast<const char *>(Slabs[Idx]); + if (P >= S && P < S + computeSlabSize(Idx)) + return InSlabIdx + (P - S); + InSlabIdx += computeSlabSize(Idx); + } + + // Use negative index to denote custom sized slabs. + long InCustomSizedSlabIdx = 0; + for (size_t Idx = 0; Idx < CustomSizedSlabs.size(); Idx++) { + const char *S = + reinterpret_cast<const char *>(CustomSizedSlabs[Idx].first); + size_t Size = CustomSizedSlabs[Idx].second; + if (P >= S && P < S + Size) + return InCustomSizedSlabIdx - (P - S); + InCustomSizedSlabIdx -= Size; + } + return None; + } + size_t getTotalMemory() const { size_t TotalMemory = 0; for (auto I = Slabs.begin(), E = Slabs.end(); I != E; ++I)
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,34 @@ size_t GetNumSlabs() const { return Slabs.size() + CustomSizedSlabs.size(); } + /// \return An index uniquely and reproducibly identifying + /// an input pointer \p Ptr in the given allocator. + /// The returned value is negative iff the object is inside a custom-size + /// slab. + /// Returns an empty optional if the pointer is not found in the allocator. + llvm::Optional<long> identifyObject(const void *Ptr) { + const char *P = reinterpret_cast<const char *>(Ptr); + long InSlabIdx = 0; + for (size_t Idx = 0; Idx < Slabs.size(); Idx++) { + const char *S = reinterpret_cast<const char *>(Slabs[Idx]); + if (P >= S && P < S + computeSlabSize(Idx)) + return InSlabIdx + (P - S); + InSlabIdx += computeSlabSize(Idx); + } + + // Use negative index to denote custom sized slabs. + long InCustomSizedSlabIdx = 0; + for (size_t Idx = 0; Idx < CustomSizedSlabs.size(); Idx++) { + const char *S = + reinterpret_cast<const char *>(CustomSizedSlabs[Idx].first); + size_t Size = CustomSizedSlabs[Idx].second; + if (P >= S && P < S + Size) + return InCustomSizedSlabIdx - (P - S); + InCustomSizedSlabIdx -= Size; + } + return None; + } + size_t getTotalMemory() const { size_t TotalMemory = 0; for (auto I = Slabs.begin(), E = Slabs.end(); I != E; ++I)
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits