OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_DEBUG_LEAK_ANNOTATIONS_H_ | 5 #ifndef BASE_DEBUG_LEAK_ANNOTATIONS_H_ |
6 #define BASE_DEBUG_LEAK_ANNOTATIONS_H_ | 6 #define BASE_DEBUG_LEAK_ANNOTATIONS_H_ |
7 | 7 |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 | 9 |
10 // This file defines macros which can be used to annotate intentional memory | |
11 // leaks. Support for annotations is implemented in HeapChecker and | |
12 // LeakSanitizer. Annotated objects will be treated as a source of live | |
13 // pointers, i.e. any heap objects reachable by following pointers from an | |
14 // annotated object will not be 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 // Note that HeapChecker will report a fatal error if an object which has been | |
22 // annotated with ANNOTATE_LEAKING_OBJECT_PTR is later deleted (but | |
23 // LeakSanitizer won't). | |
Alexander Potapenko
2013/06/13 13:41:27
What about annotating the same object twice?
earthdok
2013/06/13 13:52:02
It's actually not a fatal error in HeapChecker. I
| |
24 | |
25 #define LEAK_SANITIZER 1 | |
Alexander Potapenko
2013/06/13 13:41:27
Looks like you've forgotten to delete this.
earthdok
2013/06/13 13:52:02
Committed accidentally. Fixed.
| |
26 | |
10 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_NACL) && \ | 27 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_NACL) && \ |
11 defined(USE_HEAPCHECKER) | 28 defined(USE_HEAPCHECKER) |
12 | 29 |
13 #include "third_party/tcmalloc/chromium/src/gperftools/heap-checker.h" | 30 #include "third_party/tcmalloc/chromium/src/gperftools/heap-checker.h" |
14 | 31 |
15 // Annotate a program scope as having memory leaks. Tcmalloc's heap leak | |
16 // checker will ignore them. Note that these annotations may mask real bugs | |
17 // and should not be used in the production code. | |
18 #define ANNOTATE_SCOPED_MEMORY_LEAK \ | 32 #define ANNOTATE_SCOPED_MEMORY_LEAK \ |
19 HeapLeakChecker::Disabler heap_leak_checker_disabler | 33 HeapLeakChecker::Disabler heap_leak_checker_disabler |
20 | 34 |
21 // Annotate an object pointer as referencing a leaky object. This object and all | |
22 // the heap objects referenced by it will be ignored by the heap checker. | |
23 // | |
24 // X should be referencing an active allocated object. If it is not, the | |
25 // annotation will be ignored. | |
26 // No object should be annotated with ANNOTATE_SCOPED_MEMORY_LEAK twice. | |
27 // Once an object is annotated with ANNOTATE_SCOPED_MEMORY_LEAK, it cannot be | |
28 // deleted. | |
29 #define ANNOTATE_LEAKING_OBJECT_PTR(X) \ | 35 #define ANNOTATE_LEAKING_OBJECT_PTR(X) \ |
30 HeapLeakChecker::IgnoreObject(X) | 36 HeapLeakChecker::IgnoreObject(X) |
31 | 37 |
38 #elif defined(LEAK_SANITIZER) | |
39 | |
40 extern "C" { | |
41 void __lsan_disable(); | |
42 void __lsan_enable(); | |
43 void __lsan_ignore_object(const void *p); | |
44 } // extern "C" | |
45 | |
46 class ScopedLeakSanitizerDisabler { | |
47 public: | |
48 ScopedLeakSanitizerDisabler() { __lsan_disable(); } | |
49 ~ScopedLeakSanitizerDisabler() { __lsan_enable(); } | |
50 private: | |
51 DISALLOW_COPY_AND_ASSIGN(ScopedLeakSanitizerDisabler); | |
52 }; | |
53 | |
54 #define ANNOTATE_SCOPED_MEMORY_LEAK \ | |
55 ScopedLeakSanitizerDisabler leak_sanitizer_disabler | |
56 | |
57 #define ANNOTATE_LEAKING_OBJECT_PTR(X) __lsan_ignore_object(X); | |
58 | |
32 #else | 59 #else |
33 | 60 |
34 // If tcmalloc is not used, the annotations should be no-ops. | 61 // If neither HeapChecker nor LSan are used, the annotations should be no-ops. |
35 #define ANNOTATE_SCOPED_MEMORY_LEAK ((void)0) | 62 #define ANNOTATE_SCOPED_MEMORY_LEAK ((void)0) |
36 #define ANNOTATE_LEAKING_OBJECT_PTR(X) ((void)0) | 63 #define ANNOTATE_LEAKING_OBJECT_PTR(X) ((void)0) |
37 | 64 |
38 #endif | 65 #endif |
39 | 66 |
40 #endif // BASE_DEBUG_LEAK_ANNOTATIONS_H_ | 67 #endif // BASE_DEBUG_LEAK_ANNOTATIONS_H_ |
OLD | NEW |