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