xref: /freebsd/contrib/llvm-project/llvm/lib/Support/BuryPointer.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===- BuryPointer.cpp - Memory Manipulation/Leak ---------------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "llvm/Support/BuryPointer.h"
10*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
11*0b57cec5SDimitry Andric #include <atomic>
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric namespace llvm {
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric void BuryPointer(const void *Ptr) {
16*0b57cec5SDimitry Andric   // This function may be called only a small fixed amount of times per each
17*0b57cec5SDimitry Andric   // invocation, otherwise we do actually have a leak which we want to report.
18*0b57cec5SDimitry Andric   // If this function is called more than kGraveYardMaxSize times, the pointers
19*0b57cec5SDimitry Andric   // will not be properly buried and a leak detector will report a leak, which
20*0b57cec5SDimitry Andric   // is what we want in such case.
21*0b57cec5SDimitry Andric   static const size_t kGraveYardMaxSize = 16;
22*0b57cec5SDimitry Andric   LLVM_ATTRIBUTE_UNUSED static const void *GraveYard[kGraveYardMaxSize];
23*0b57cec5SDimitry Andric   static std::atomic<unsigned> GraveYardSize;
24*0b57cec5SDimitry Andric   unsigned Idx = GraveYardSize++;
25*0b57cec5SDimitry Andric   if (Idx >= kGraveYardMaxSize)
26*0b57cec5SDimitry Andric     return;
27*0b57cec5SDimitry Andric   GraveYard[Idx] = Ptr;
28*0b57cec5SDimitry Andric }
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric }
31