leak_annotations.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_DEBUG_LEAK_ANNOTATIONS_H_
  5. #define BASE_DEBUG_LEAK_ANNOTATIONS_H_
  6. #include "build/build_config.h"
  7. // This file defines macros which can be used to annotate intentional memory
  8. // leaks. Support for annotations is implemented in LeakSanitizer. Annotated
  9. // objects will be treated as a source of live pointers, i.e. any heap objects
  10. // reachable by following pointers from an annotated object will not be
  11. // reported as leaks.
  12. //
  13. // ANNOTATE_SCOPED_MEMORY_LEAK: all allocations made in the current scope
  14. // will be annotated as leaks.
  15. // ANNOTATE_LEAKING_OBJECT_PTR(X): the heap object referenced by pointer X will
  16. // be annotated as a leak.
  17. #if defined(LEAK_SANITIZER) && !BUILDFLAG(IS_NACL)
  18. #include <sanitizer/lsan_interface.h>
  19. class ScopedLeakSanitizerDisabler {
  20. public:
  21. ScopedLeakSanitizerDisabler() { __lsan_disable(); }
  22. ScopedLeakSanitizerDisabler(const ScopedLeakSanitizerDisabler&) = delete;
  23. ScopedLeakSanitizerDisabler& operator=(const ScopedLeakSanitizerDisabler&) =
  24. delete;
  25. ~ScopedLeakSanitizerDisabler() { __lsan_enable(); }
  26. };
  27. #define ANNOTATE_SCOPED_MEMORY_LEAK \
  28. ScopedLeakSanitizerDisabler leak_sanitizer_disabler; static_cast<void>(0)
  29. #define ANNOTATE_LEAKING_OBJECT_PTR(X) __lsan_ignore_object(X);
  30. #else
  31. #define ANNOTATE_SCOPED_MEMORY_LEAK ((void)0)
  32. #define ANNOTATE_LEAKING_OBJECT_PTR(X) ((void)0)
  33. #endif
  34. #endif // BASE_DEBUG_LEAK_ANNOTATIONS_H_