diff options
| -rw-r--r-- | llvm/include/llvm/Support/BuryPointer.h | 30 | ||||
| -rw-r--r-- | llvm/lib/Support/BuryPointer.cpp | 31 | ||||
| -rw-r--r-- | llvm/lib/Support/CMakeLists.txt | 1 |
3 files changed, 62 insertions, 0 deletions
diff --git a/llvm/include/llvm/Support/BuryPointer.h b/llvm/include/llvm/Support/BuryPointer.h new file mode 100644 index 00000000000..53f1f395b92 --- /dev/null +++ b/llvm/include/llvm/Support/BuryPointer.h @@ -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 diff --git a/llvm/lib/Support/BuryPointer.cpp b/llvm/lib/Support/BuryPointer.cpp new file mode 100644 index 00000000000..6c988b4a0ab --- /dev/null +++ b/llvm/lib/Support/BuryPointer.cpp @@ -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; +} + +} diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt index 1446359f0cf..96d7f7c54b0 100644 --- a/llvm/lib/Support/CMakeLists.txt +++ b/llvm/lib/Support/CMakeLists.txt @@ -59,6 +59,7 @@ add_llvm_library(LLVMSupport BinaryStreamWriter.cpp BlockFrequency.cpp BranchProbability.cpp + BuryPointer.cpp CachePruning.cpp circular_raw_ostream.cpp Chrono.cpp |

