michael@0: // Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_DEBUG_LEAK_ANNOTATIONS_H_ michael@0: #define BASE_DEBUG_LEAK_ANNOTATIONS_H_ michael@0: michael@0: #include "build/build_config.h" michael@0: michael@0: // This file defines macros which can be used to annotate intentional memory michael@0: // leaks. Support for annotations is implemented in HeapChecker and michael@0: // LeakSanitizer. Annotated objects will be treated as a source of live michael@0: // pointers, i.e. any heap objects reachable by following pointers from an michael@0: // annotated object will not be reported as leaks. michael@0: // michael@0: // ANNOTATE_SCOPED_MEMORY_LEAK: all allocations made in the current scope michael@0: // will be annotated as leaks. michael@0: // ANNOTATE_LEAKING_OBJECT_PTR(X): the heap object referenced by pointer X will michael@0: // be annotated as a leak. michael@0: // michael@0: // Note that HeapChecker will report a fatal error if an object which has been michael@0: // annotated with ANNOTATE_LEAKING_OBJECT_PTR is later deleted (but michael@0: // LeakSanitizer won't). michael@0: michael@0: #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_NACL) && \ michael@0: defined(USE_HEAPCHECKER) michael@0: michael@0: #include "third_party/tcmalloc/chromium/src/gperftools/heap-checker.h" michael@0: michael@0: #define ANNOTATE_SCOPED_MEMORY_LEAK \ michael@0: HeapLeakChecker::Disabler heap_leak_checker_disabler; static_cast(0) michael@0: michael@0: #define ANNOTATE_LEAKING_OBJECT_PTR(X) \ michael@0: HeapLeakChecker::IgnoreObject(X) michael@0: michael@0: #elif defined(LEAK_SANITIZER) && !defined(OS_NACL) michael@0: michael@0: extern "C" { michael@0: void __lsan_disable(); michael@0: void __lsan_enable(); michael@0: void __lsan_ignore_object(const void *p); michael@0: } // extern "C" michael@0: michael@0: class ScopedLeakSanitizerDisabler { michael@0: public: michael@0: ScopedLeakSanitizerDisabler() { __lsan_disable(); } michael@0: ~ScopedLeakSanitizerDisabler() { __lsan_enable(); } michael@0: private: michael@0: DISALLOW_COPY_AND_ASSIGN(ScopedLeakSanitizerDisabler); michael@0: }; michael@0: michael@0: #define ANNOTATE_SCOPED_MEMORY_LEAK \ michael@0: ScopedLeakSanitizerDisabler leak_sanitizer_disabler; static_cast(0) michael@0: michael@0: #define ANNOTATE_LEAKING_OBJECT_PTR(X) __lsan_ignore_object(X); michael@0: michael@0: #else michael@0: michael@0: // If neither HeapChecker nor LSan are used, the annotations should be no-ops. michael@0: #define ANNOTATE_SCOPED_MEMORY_LEAK ((void)0) michael@0: #define ANNOTATE_LEAKING_OBJECT_PTR(X) ((void)0) michael@0: michael@0: #endif michael@0: michael@0: #endif // BASE_DEBUG_LEAK_ANNOTATIONS_H_