- add sources.
[platform/framework/web/crosswalk.git] / src / base / debug / leak_annotations.h
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
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 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).
24
25 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_NACL) && \
26     defined(USE_HEAPCHECKER)
27
28 #include "third_party/tcmalloc/chromium/src/gperftools/heap-checker.h"
29
30 #define ANNOTATE_SCOPED_MEMORY_LEAK \
31     HeapLeakChecker::Disabler heap_leak_checker_disabler; static_cast<void>(0)
32
33 #define ANNOTATE_LEAKING_OBJECT_PTR(X) \
34     HeapLeakChecker::IgnoreObject(X)
35
36 #elif defined(LEAK_SANITIZER) && !defined(OS_NACL)
37
38 // Public LSan API from <sanitizer/lsan_interface.h>.
39 extern "C" {
40 void __lsan_disable();
41 void __lsan_enable();
42 void __lsan_ignore_object(const void *p);
43
44 // Invoke leak detection immediately. If leaks are found, the process will exit.
45 void __lsan_do_leak_check();
46 }  // extern "C"
47
48 class ScopedLeakSanitizerDisabler {
49  public:
50   ScopedLeakSanitizerDisabler() { __lsan_disable(); }
51   ~ScopedLeakSanitizerDisabler() { __lsan_enable(); }
52  private:
53   DISALLOW_COPY_AND_ASSIGN(ScopedLeakSanitizerDisabler);
54 };
55
56 #define ANNOTATE_SCOPED_MEMORY_LEAK \
57     ScopedLeakSanitizerDisabler leak_sanitizer_disabler; static_cast<void>(0)
58
59 #define ANNOTATE_LEAKING_OBJECT_PTR(X) __lsan_ignore_object(X);
60
61 #else
62
63 // If neither HeapChecker nor LSan are used, the annotations should be no-ops.
64 #define ANNOTATE_SCOPED_MEMORY_LEAK ((void)0)
65 #define ANNOTATE_LEAKING_OBJECT_PTR(X) ((void)0)
66
67 #endif
68
69 #endif  // BASE_DEBUG_LEAK_ANNOTATIONS_H_