xref: /aosp_15_r20/external/armnn/src/armnnUtils/LeakChecking.hpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #ifdef ARMNN_LEAK_CHECKING_ENABLED
8 
9 #include <string>
10 #include <cstddef>
11 #include <memory>
12 
13 namespace armnnUtils
14 {
15 
16 class ScopedLeakChecker final
17 {
18 public:
19     ScopedLeakChecker(const std::string & name);
20     ~ScopedLeakChecker();
21 
22     // Forwarding these to Google Performance Tools.
23     static bool IsActive();
24     bool NoLeaks();
25     // Note that the following two functions only work after
26     // NoLeaks() has been called. See explanations in
27     // heap-checker.h
28     ssize_t BytesLeaked() const;
29     ssize_t ObjectsLeaked() const;
30 
31 private:
32     // Hides imlementation so we don't litter other's namespaces
33     // with heap checker related stuff.
34     struct Impl;
35     std::unique_ptr<Impl> m_Impl;
36 
37     // No default construction and copying.
38     ScopedLeakChecker() = delete;
39     ScopedLeakChecker(const ScopedLeakChecker &) = delete;
40     ScopedLeakChecker & operator=(const ScopedLeakChecker &) = delete;
41 };
42 
43 class ScopedDisableLeakChecking final
44 {
45 public:
46     ScopedDisableLeakChecking();
47     ~ScopedDisableLeakChecking();
48 
49 private:
50     // Hides imlementation so we don't litter other's namespaces
51     // with heap checker related stuff.
52     struct Impl;
53     std::unique_ptr<Impl> m_Impl;
54 
55     // No copying.
56     ScopedDisableLeakChecking(const ScopedDisableLeakChecking &) = delete;
57     ScopedDisableLeakChecking & operator=(const ScopedDisableLeakChecking &) = delete;
58 };
59 
60 // disable global leak checks starting from 'main'
61 void LocalLeakCheckingOnly();
62 
63 } // namespace armnnUtils
64 
65 #define ARMNN_SCOPED_LEAK_CHECKER(TAG) \
66     armnnUtils::ScopedLeakChecker __scoped_armnn_leak_checker__(TAG)
67 
68 #define ARMNN_LEAK_CHECKER_IS_ACTIVE() \
69     armnnUtils::ScopedLeakChecker::IsActive()
70 
71 #define ARMNN_NO_LEAKS_IN_SCOPE() \
72     __scoped_armnn_leak_checker__.NoLeaks()
73 
74 #define ARMNN_BYTES_LEAKED_IN_SCOPE() \
75     __scoped_armnn_leak_checker__.BytesLeaked()
76 
77 #define ARMNN_OBJECTS_LEAKED_IN_SCOPE() \
78     __scoped_armnn_leak_checker__.ObjectsLeaked()
79 
80 #define ARMNN_DISABLE_LEAK_CHECKING_IN_SCOPE() \
81     armnnUtils::ScopedDisableLeakChecking __disable_leak_checking_in_scope__
82 
83 #define ARMNN_LOCAL_LEAK_CHECKING_ONLY() \
84     armnnUtils::LocalLeakCheckingOnly()
85 
86 #else // ARMNN_LEAK_CHECKING_ENABLED
87 
88 #define ARMNN_SCOPED_LEAK_CHECKER(TAG)
89 #define ARMNN_LEAK_CHECKER_IS_ACTIVE() false
90 #define ARMNN_NO_LEAKS_IN_SCOPE() true
91 #define ARMNN_BYTES_LEAKED_IN_SCOPE() 0
92 #define ARMNN_OBJECTS_LEAKED_IN_SCOPE() 0
93 #define ARMNN_DISABLE_LEAK_CHECKING_IN_SCOPE()
94 #define ARMNN_LOCAL_LEAK_CHECKING_ONLY()
95 
96 #endif // ARMNN_LEAK_CHECKING_ENABLED
97