[llvm] r347140 - Move BuryPointer from Clang to LLVM for use in other LLVM tools

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 17 10:03:47 PST 2018


Author: dblaikie
Date: Sat Nov 17 10:03:47 2018
New Revision: 347140

URL: http://llvm.org/viewvc/llvm-project?rev=347140&view=rev
Log:
Move BuryPointer from Clang to LLVM for use in other LLVM tools

Specifically planning to use this in llvm-symbolizer to remove the cost
of cleanup there.

Added:
    llvm/trunk/include/llvm/Support/BuryPointer.h
    llvm/trunk/lib/Support/BuryPointer.cpp
Modified:
    llvm/trunk/lib/Support/CMakeLists.txt

Added: llvm/trunk/include/llvm/Support/BuryPointer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/BuryPointer.h?rev=347140&view=auto
==============================================================================
--- llvm/trunk/include/llvm/Support/BuryPointer.h (added)
+++ llvm/trunk/include/llvm/Support/BuryPointer.h Sat Nov 17 10:03:47 2018
@@ -0,0 +1,30 @@
+//===- llvm/Support/BuryPointer.h - Memory Manipulation/Leak ----*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_BURYPOINTER_H
+#define LLVM_SUPPORT_BURYPOINTER_H
+
+#include <memory>
+
+namespace llvm {
+
+// In tools that will exit soon anyway, going through the process of explicitly
+// deallocating resources can be unnecessary - better to leak the resources and
+// let the OS clean them up when the process ends. Use this function to ensure
+// the memory is not misdiagnosed as an unintentional leak by leak detection
+// tools (this is achieved by preserving pointers to the object in a globally
+// visible array).
+void BuryPointer(const void *Ptr);
+template <typename T> void BuryPointer(std::unique_ptr<T> Ptr) {
+  BuryPointer(Ptr.release());
+}
+
+} // namespace llvm
+
+#endif

Added: llvm/trunk/lib/Support/BuryPointer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/BuryPointer.cpp?rev=347140&view=auto
==============================================================================
--- llvm/trunk/lib/Support/BuryPointer.cpp (added)
+++ llvm/trunk/lib/Support/BuryPointer.cpp Sat Nov 17 10:03:47 2018
@@ -0,0 +1,31 @@
+//===- BuryPointer.cpp - Memory Manipulation/Leak ---------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/BuryPointer.h"
+#include "llvm/Support/Compiler.h"
+#include <atomic>
+
+namespace llvm {
+
+void BuryPointer(const void *Ptr) {
+  // This function may be called only a small fixed amount of times per each
+  // invocation, otherwise we do actually have a leak which we want to report.
+  // If this function is called more than kGraveYardMaxSize times, the pointers
+  // will not be properly buried and a leak detector will report a leak, which
+  // is what we want in such case.
+  static const size_t kGraveYardMaxSize = 16;
+  LLVM_ATTRIBUTE_UNUSED static const void *GraveYard[kGraveYardMaxSize];
+  static std::atomic<unsigned> GraveYardSize;
+  unsigned Idx = GraveYardSize++;
+  if (Idx >= kGraveYardMaxSize)
+    return;
+  GraveYard[Idx] = Ptr;
+}
+
+}

Modified: llvm/trunk/lib/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CMakeLists.txt?rev=347140&r1=347139&r2=347140&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CMakeLists.txt (original)
+++ llvm/trunk/lib/Support/CMakeLists.txt Sat Nov 17 10:03:47 2018
@@ -59,6 +59,7 @@ add_llvm_library(LLVMSupport
   BinaryStreamWriter.cpp
   BlockFrequency.cpp
   BranchProbability.cpp
+  BuryPointer.cpp
   CachePruning.cpp
   circular_raw_ostream.cpp
   Chrono.cpp




More information about the llvm-commits mailing list