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

George Karpenkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 6 16:08:24 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL341599: Provide a method for generating deterministic IDs for pointers allocated in… (authored by george.karpenkov, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D51393?vs=164294&id=164309#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D51393

Files:
  llvm/trunk/include/llvm/Support/Allocator.h


Index: llvm/trunk/include/llvm/Support/Allocator.h
===================================================================
--- llvm/trunk/include/llvm/Support/Allocator.h
+++ llvm/trunk/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,33 @@
 
   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 = static_cast<const char *>(Ptr);
+    int64_t InSlabIdx = 0;
+    for (size_t Idx = 0, E = Slabs.size(); Idx < E; Idx++) {
+      const char *S = static_cast<const char *>(Slabs[Idx]);
+      if (P >= S && P < S + computeSlabSize(Idx))
+        return InSlabIdx + static_cast<int64_t>(P - S);
+      InSlabIdx += static_cast<int64_t>(computeSlabSize(Idx));
+    }
+
+    // Use negative index to denote custom sized slabs.
+    int64_t InCustomSizedSlabIdx = -1;
+    for (size_t Idx = 0, E = CustomSizedSlabs.size(); Idx < E; Idx++) {
+      const char *S = static_cast<const char *>(CustomSizedSlabs[Idx].first);
+      size_t Size = CustomSizedSlabs[Idx].second;
+      if (P >= S && P < S + Size)
+        return InCustomSizedSlabIdx - static_cast<int64_t>(P - S);
+      InCustomSizedSlabIdx -= static_cast<int64_t>(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.164309.patch
Type: text/x-patch
Size: 1908 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180906/676f258a/attachment.bin>


More information about the llvm-commits mailing list