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.

llvm-svn: 347140
This commit is contained in:
David Blaikie 2018-11-17 18:03:47 +00:00
parent 0e1a9d5ee6
commit ef543381ed
3 changed files with 62 additions and 0 deletions

View File

@ -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

View File

@ -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;
}
}

View File

@ -59,6 +59,7 @@ add_llvm_library(LLVMSupport
BinaryStreamWriter.cpp
BlockFrequency.cpp
BranchProbability.cpp
BuryPointer.cpp
CachePruning.cpp
circular_raw_ostream.cpp
Chrono.cpp