ArmNN
 23.11
LeakChecking.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #ifdef ARMNN_LEAK_CHECKING_ENABLED
7 
8 #include "LeakChecking.hpp"
9 #include "gperftools/heap-checker.h"
10 
11 namespace armnnUtils
12 {
13 
14 struct ScopedLeakChecker::Impl
15 {
16  HeapLeakChecker m_LeakChecker;
17 
18  Impl(const std::string & name)
19  : m_LeakChecker(name.c_str())
20  {
21  }
22 };
23 
24 ScopedLeakChecker::ScopedLeakChecker(const std::string & name)
25 : m_Impl(new Impl(name))
26 {
27 }
28 
29 ScopedLeakChecker::~ScopedLeakChecker() {}
30 
31 bool ScopedLeakChecker::IsActive()
32 {
33  return HeapLeakChecker::IsActive();
34 }
35 
36 bool ScopedLeakChecker::NoLeaks()
37 {
38  return (IsActive() ? m_Impl->m_LeakChecker.NoLeaks() : true);
39 }
40 
41 ssize_t ScopedLeakChecker::BytesLeaked() const
42 {
43  return (IsActive() ? m_Impl->m_LeakChecker.BytesLeaked(): 0);
44 }
45 
46 ssize_t ScopedLeakChecker::ObjectsLeaked() const
47 {
48  return (IsActive() ? m_Impl->m_LeakChecker.ObjectsLeaked(): 0 );
49 }
50 
51 struct ScopedDisableLeakChecking::Impl
52 {
53  HeapLeakChecker::Disabler m_Disabler;
54 };
55 
56 ScopedDisableLeakChecking::ScopedDisableLeakChecking()
57 : m_Impl(new Impl)
58 {
59 }
60 
61 ScopedDisableLeakChecking::~ScopedDisableLeakChecking()
62 {
63 }
64 
65 void LocalLeakCheckingOnly()
66 {
67  auto * globalChecker = HeapLeakChecker::GlobalChecker();
68  if (globalChecker)
69  {
70  // Don't care about global leaks and make sure we won't report any.
71  // This is because leak checking supposed to run in well defined
72  // contexts through the ScopedLeakChecker, otherwise we risk false
73  // positives because of external factors.
74  globalChecker->NoGlobalLeaks();
75  globalChecker->CancelGlobalCheck();
76  }
77 }
78 
79 } // namespace armnnUtils
80 
81 #endif // ARMNN_LEAK_CHECKING_ENABLED
LeakChecking.hpp
armnnUtils
Definition: CompatibleTypes.hpp:10