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