blob: 4d42a8a734613b6359a10879b27c164f31885647 [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#pragma once
6
7#ifdef ARMNN_LEAK_CHECKING_ENABLED
8
9#include <string>
10#include <cstddef>
11#include <memory>
12
13namespace armnnUtils
14{
15
16class ScopedLeakChecker final
17{
18public:
19 ScopedLeakChecker(const std::string & name);
20 ~ScopedLeakChecker();
21
telsoa01c577f2c2018-08-31 09:22:23 +010022 // Forwarding these to Google Performance Tools.
surmeh013537c2c2018-05-18 16:31:43 +010023 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
31private:
telsoa01c577f2c2018-08-31 09:22:23 +010032 // Hides imlementation so we don't litter other's namespaces
33 // with heap checker related stuff.
surmeh013537c2c2018-05-18 16:31:43 +010034 struct Impl;
35 std::unique_ptr<Impl> m_Impl;
36
telsoa01c577f2c2018-08-31 09:22:23 +010037 // No default construction and copying.
surmeh013537c2c2018-05-18 16:31:43 +010038 ScopedLeakChecker() = delete;
39 ScopedLeakChecker(const ScopedLeakChecker &) = delete;
40 ScopedLeakChecker & operator=(const ScopedLeakChecker &) = delete;
41};
42
43class ScopedDisableLeakChecking final
44{
45public:
46 ScopedDisableLeakChecking();
47 ~ScopedDisableLeakChecking();
48
49private:
telsoa01c577f2c2018-08-31 09:22:23 +010050 // Hides imlementation so we don't litter other's namespaces
51 // with heap checker related stuff.
surmeh013537c2c2018-05-18 16:31:43 +010052 struct Impl;
53 std::unique_ptr<Impl> m_Impl;
54
telsoa01c577f2c2018-08-31 09:22:23 +010055 // No copying.
surmeh013537c2c2018-05-18 16:31:43 +010056 ScopedDisableLeakChecking(const ScopedDisableLeakChecking &) = delete;
57 ScopedDisableLeakChecking & operator=(const ScopedDisableLeakChecking &) = delete;
58};
59
telsoa01c577f2c2018-08-31 09:22:23 +010060// disable global leak checks starting from 'main'
61void LocalLeakCheckingOnly();
62
surmeh013537c2c2018-05-18 16:31:43 +010063} // 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
telsoa01c577f2c2018-08-31 09:22:23 +010083#define ARMNN_LOCAL_LEAK_CHECKING_ONLY() \
84 armnnUtils::LocalLeakCheckingOnly()
85
surmeh013537c2c2018-05-18 16:31:43 +010086#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()
telsoa01c577f2c2018-08-31 09:22:23 +010094#define ARMNN_LOCAL_LEAK_CHECKING_ONLY()
surmeh013537c2c2018-05-18 16:31:43 +010095
96#endif // ARMNN_LEAK_CHECKING_ENABLED