[PATCH] D51393: Provide a method for generating deterministic IDs for pointers allocated in BumpPtrAllocator

George Karpenkov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 30 11:33:39 PDT 2018


george.karpenkov updated this revision to Diff 163383.

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,35 @@
 
   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<int64_t> identifyObject(const void *Ptr) {
+    const char *P = reinterpret_cast<const char *>(Ptr);
+    int64_t TotalSlabOffset = 0;
+    for (size_t Idx = 0; Idx < Slabs.size(); Idx++) {
+      const char *S = reinterpret_cast<const char *>(Slabs[Idx]);
+      size_t IdxSlabSize = computeSlabSize(Idx);
+      if (P >= S && P < S + IdxSlabSize)
+        return TotalSlabOffset + (P - S);
+      TotalSlabOffset += IdxSlabSize;
+    }
+
+    // Use negative index to denote custom sized slabs.
+    int64_t CustomSlabOffset = 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 CustomSlabOffset - (P - S);
+      CustomSlabOffset -= Size;
+    }
+    return None;
+  }
+
   size_t getTotalMemory() const {
     size_t TotalMemory = 0;
     for (auto I = Slabs.begin(), E = Slabs.end(); I != E; ++I)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51393.163383.patch
Type: text/x-patch
Size: 1855 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180830/fb693fcd/attachment.bin>


More information about the cfe-commits mailing list