aboutsummaryrefslogtreecommitdiff
path: root/src/armnnUtils/LeakChecking.cpp
blob: 13d4dc35ee6b6fee01f767ab135bff7b3331f931 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#ifdef ARMNN_LEAK_CHECKING_ENABLED

#include "LeakChecking.hpp"
#include "gperftools/heap-checker.h"

namespace armnnUtils
{

struct ScopedLeakChecker::Impl
{
    HeapLeakChecker m_LeakChecker;

    Impl(const std::string & name)
    : m_LeakChecker(name.c_str())
    {
    }
};

ScopedLeakChecker::ScopedLeakChecker(const std::string & name)
: m_Impl(new Impl(name))
{
}

ScopedLeakChecker::~ScopedLeakChecker() {}

bool ScopedLeakChecker::IsActive()
{
    return HeapLeakChecker::IsActive();
}

bool ScopedLeakChecker::NoLeaks()
{
    return (IsActive() ? m_Impl->m_LeakChecker.NoLeaks() : true);
}

ssize_t ScopedLeakChecker::BytesLeaked() const
{
    return (IsActive() ? m_Impl->m_LeakChecker.BytesLeaked(): 0);
}

ssize_t ScopedLeakChecker::ObjectsLeaked() const
{
    return (IsActive() ? m_Impl->m_LeakChecker.ObjectsLeaked(): 0 );
}

struct ScopedDisableLeakChecking::Impl
{
    HeapLeakChecker::Disabler m_Disabler;
};

ScopedDisableLeakChecking::ScopedDisableLeakChecking()
: m_Impl(new Impl)
{
}

ScopedDisableLeakChecking::~ScopedDisableLeakChecking()
{
}

void LocalLeakCheckingOnly()
{
    auto * globalChecker = HeapLeakChecker::GlobalChecker();
    if (globalChecker)
    {
        // Don't care about global leaks and make sure we won't report any.
        // This is because leak checking supposed to run in well defined
        // contexts through the ScopedLeakChecker, otherwise we risk false
        // positives because of external factors.
        globalChecker->NoGlobalLeaks();
        globalChecker->CancelGlobalCheck();
    }
}

} // namespace armnnUtils

#endif // ARMNN_LEAK_CHECKING_ENABLED