BuryPointer.cpp 1.1 KB

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