blob: 13d4dc35ee6b6fee01f767ab135bff7b3331f931 [file] [log] [blame]
surmeh013537c2c2018-05-18 16:31:43 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
surmeh013537c2c2018-05-18 16:31:43 +01004//
5
6#ifdef ARMNN_LEAK_CHECKING_ENABLED
7
8#include "LeakChecking.hpp"
9#include "gperftools/heap-checker.h"
10
telsoa01c577f2c2018-08-31 09:22:23 +010011namespace armnnUtils
12{
13
surmeh013537c2c2018-05-18 16:31:43 +010014struct ScopedLeakChecker::Impl
15{
16 HeapLeakChecker m_LeakChecker;
17
18 Impl(const std::string & name)
19 : m_LeakChecker(name.c_str())
20 {
21 }
22};
23
24ScopedLeakChecker::ScopedLeakChecker(const std::string & name)
25: m_Impl(new Impl(name))
26{
27}
28
29ScopedLeakChecker::~ScopedLeakChecker() {}
30
31bool ScopedLeakChecker::IsActive()
32{
33 return HeapLeakChecker::IsActive();
34}
35
36bool ScopedLeakChecker::NoLeaks()
37{
38 return (IsActive() ? m_Impl->m_LeakChecker.NoLeaks() : true);
39}
40
41ssize_t ScopedLeakChecker::BytesLeaked() const
42{
43 return (IsActive() ? m_Impl->m_LeakChecker.BytesLeaked(): 0);
44}
45
46ssize_t ScopedLeakChecker::ObjectsLeaked() const
47{
48 return (IsActive() ? m_Impl->m_LeakChecker.ObjectsLeaked(): 0 );
49}
50
51struct ScopedDisableLeakChecking::Impl
52{
53 HeapLeakChecker::Disabler m_Disabler;
54};
55
56ScopedDisableLeakChecking::ScopedDisableLeakChecking()
57: m_Impl(new Impl)
58{
59}
60
61ScopedDisableLeakChecking::~ScopedDisableLeakChecking()
62{
63}
64
telsoa01c577f2c2018-08-31 09:22:23 +010065void 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
surmeh013537c2c2018-05-18 16:31:43 +010081#endif // ARMNN_LEAK_CHECKING_ENABLED