surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 22 | // Forwarding these to Google Performance Tools. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 32 | // Hides imlementation so we don't litter other's namespaces |
| 33 | // with heap checker related stuff. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 34 | struct Impl; |
| 35 | std::unique_ptr<Impl> m_Impl; |
| 36 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 37 | // No default construction and copying. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 50 | // Hides imlementation so we don't litter other's namespaces |
| 51 | // with heap checker related stuff. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 52 | struct Impl; |
| 53 | std::unique_ptr<Impl> m_Impl; |
| 54 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 55 | // No copying. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 56 | ScopedDisableLeakChecking(const ScopedDisableLeakChecking &) = delete; |
| 57 | ScopedDisableLeakChecking & operator=(const ScopedDisableLeakChecking &) = delete; |
| 58 | }; |
| 59 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 60 | // disable global leak checks starting from 'main' |
| 61 | void LocalLeakCheckingOnly(); |
| 62 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 83 | #define ARMNN_LOCAL_LEAK_CHECKING_ONLY() \ |
| 84 | armnnUtils::LocalLeakCheckingOnly() |
| 85 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 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() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 94 | #define ARMNN_LOCAL_LEAK_CHECKING_ONLY() |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 95 | |
| 96 | #endif // ARMNN_LEAK_CHECKING_ENABLED |