10b57cec5SDimitry Andric //===- BuryPointer.cpp - Memory Manipulation/Leak ---------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "llvm/Support/BuryPointer.h" 100b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 110b57cec5SDimitry Andric #include <atomic> 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric namespace llvm { 140b57cec5SDimitry Andric BuryPointer(const void * Ptr)150b57cec5SDimitry Andricvoid BuryPointer(const void *Ptr) { 160b57cec5SDimitry Andric // This function may be called only a small fixed amount of times per each 170b57cec5SDimitry Andric // invocation, otherwise we do actually have a leak which we want to report. 180b57cec5SDimitry Andric // If this function is called more than kGraveYardMaxSize times, the pointers 190b57cec5SDimitry Andric // will not be properly buried and a leak detector will report a leak, which 200b57cec5SDimitry Andric // is what we want in such case. 210b57cec5SDimitry Andric static const size_t kGraveYardMaxSize = 16; 22*fe6060f1SDimitry Andric LLVM_ATTRIBUTE_USED static const void *GraveYard[kGraveYardMaxSize]; 230b57cec5SDimitry Andric static std::atomic<unsigned> GraveYardSize; 240b57cec5SDimitry Andric unsigned Idx = GraveYardSize++; 250b57cec5SDimitry Andric if (Idx >= kGraveYardMaxSize) 260b57cec5SDimitry Andric return; 270b57cec5SDimitry Andric GraveYard[Idx] = Ptr; 280b57cec5SDimitry Andric } 290b57cec5SDimitry Andric 30*fe6060f1SDimitry Andric } // namespace llvm 31