robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | |
| 9 | #ifndef SkInstCnt_DEFINED |
| 10 | #define SkInstCnt_DEFINED |
| 11 | |
| 12 | /* |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 13 | * The instance counting system consists of three macros that create the |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 14 | * instance counting machinery. A class is added to the system by adding: |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 15 | * SK_DECLARE_INST_COUNT at the top of its declaration for derived classes |
| 16 | * SK_DECLARE_INST_COUNT_ROOT at the top of its declaration for a root class |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 17 | * At the end of an application a call to all the "root" objects' |
| 18 | * CheckInstanceCount methods should be made |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 19 | */ |
bsalomon@google.com | 4e23068 | 2013-01-15 20:37:04 +0000 | [diff] [blame] | 20 | #include "SkTypes.h" |
caryclark@google.com | 594dd3c | 2012-09-24 19:33:57 +0000 | [diff] [blame] | 21 | |
bsalomon@google.com | 4e23068 | 2013-01-15 20:37:04 +0000 | [diff] [blame] | 22 | #if SK_ENABLE_INST_COUNT |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 23 | #include "SkTArray.h" |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 24 | #include "SkThread_platform.h" |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 25 | |
robertphillips@google.com | b74af87 | 2012-06-27 19:41:42 +0000 | [diff] [blame] | 26 | extern bool gPrintInstCount; |
| 27 | |
robertphillips@google.com | 97fafe1 | 2012-06-27 19:52:20 +0000 | [diff] [blame] | 28 | // The non-root classes just register themselves with their parent |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 29 | #define SK_DECLARE_INST_COUNT(className) \ |
| 30 | SK_DECLARE_INST_COUNT_INTERNAL(className, \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 31 | INHERITED::AddInstChild(CheckInstanceCount);) |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 32 | |
robertphillips@google.com | 97fafe1 | 2012-06-27 19:52:20 +0000 | [diff] [blame] | 33 | // The root classes registers a function to print out the memory stats when |
| 34 | // the app ends |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 35 | #define SK_DECLARE_INST_COUNT_ROOT(className) \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 36 | SK_DECLARE_INST_COUNT_INTERNAL(className, atexit(exitPrint);) |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 37 | |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 38 | #define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep) \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 39 | class SkInstanceCountHelper { \ |
| 40 | public: \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 41 | typedef int (*PFCheckInstCnt)(int level, bool cleanUp); \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 42 | SkInstanceCountHelper() { \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 43 | static bool gInited; \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 44 | if (!gInited) { \ |
| 45 | initStep \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 46 | GetChildren() = new SkTArray<PFCheckInstCnt>; \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 47 | gInited = true; \ |
| 48 | } \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 49 | sk_atomic_inc(GetInstanceCountPtr()); \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 50 | } \ |
| 51 | \ |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 52 | SkInstanceCountHelper(const SkInstanceCountHelper&) { \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 53 | sk_atomic_inc(GetInstanceCountPtr()); \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 54 | } \ |
| 55 | \ |
| 56 | ~SkInstanceCountHelper() { \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 57 | sk_atomic_dec(GetInstanceCountPtr()); \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 58 | } \ |
| 59 | \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 60 | static int32_t* GetInstanceCountPtr() { \ |
| 61 | static int32_t gInstanceCount; \ |
| 62 | return &gInstanceCount; \ |
| 63 | } \ |
| 64 | \ |
| 65 | static SkTArray<PFCheckInstCnt>*& GetChildren() { \ |
| 66 | static SkTArray<PFCheckInstCnt>* gChildren; \ |
| 67 | return gChildren; \ |
| 68 | } \ |
| 69 | \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 70 | } fInstanceCountHelper; \ |
| 71 | \ |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 72 | static int32_t GetInstanceCount() { \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 73 | return *SkInstanceCountHelper::GetInstanceCountPtr(); \ |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 74 | } \ |
| 75 | \ |
robertphillips@google.com | 97fafe1 | 2012-06-27 19:52:20 +0000 | [diff] [blame] | 76 | static void exitPrint() { \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 77 | CheckInstanceCount(0, true); \ |
robertphillips@google.com | 97fafe1 | 2012-06-27 19:52:20 +0000 | [diff] [blame] | 78 | } \ |
| 79 | \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 80 | static int CheckInstanceCount(int level = 0, bool cleanUp = false) { \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 81 | if (gPrintInstCount && 0 != GetInstanceCount()) { \ |
reed@google.com | 73e19fb | 2012-06-26 18:58:03 +0000 | [diff] [blame] | 82 | SkDebugf("%*c Leaked %s: %d\n", \ |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 83 | 4*level, ' ', #className, \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 84 | GetInstanceCount()); \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 85 | } \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 86 | if (NULL == SkInstanceCountHelper::GetChildren()) { \ |
| 87 | return GetInstanceCount(); \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 88 | } \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 89 | SkTArray<int (*)(int, bool)>* children = \ |
| 90 | SkInstanceCountHelper::GetChildren(); \ |
| 91 | int childCount = children->count(); \ |
| 92 | int count = GetInstanceCount(); \ |
reed@google.com | 73e19fb | 2012-06-26 18:58:03 +0000 | [diff] [blame] | 93 | for (int i = 0; i < childCount; ++i) { \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 94 | count -= (*(*children)[i])(level+1, cleanUp); \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 95 | } \ |
reed@google.com | 73e19fb | 2012-06-26 18:58:03 +0000 | [diff] [blame] | 96 | SkASSERT(count >= 0); \ |
robertphillips@google.com | b74af87 | 2012-06-27 19:41:42 +0000 | [diff] [blame] | 97 | if (gPrintInstCount && childCount > 0 && count > 0) { \ |
reed@google.com | 73e19fb | 2012-06-26 18:58:03 +0000 | [diff] [blame] | 98 | SkDebugf("%*c Leaked ???: %d\n", 4*(level + 1), ' ', count); \ |
| 99 | } \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 100 | if (cleanUp) { \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 101 | delete children; \ |
| 102 | SkInstanceCountHelper::GetChildren() = NULL; \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 103 | } \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 104 | return GetInstanceCount(); \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 105 | } \ |
| 106 | \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 107 | static void AddInstChild(int (*childCheckInstCnt)(int, bool)) { \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 108 | if (CheckInstanceCount != childCheckInstCnt && \ |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 109 | NULL != SkInstanceCountHelper::GetChildren()) { \ |
| 110 | SkInstanceCountHelper::GetChildren()->push_back(childCheckInstCnt); \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 111 | } \ |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 112 | } |
| 113 | |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 114 | #else |
commit-bot@chromium.org | ab1c138 | 2013-12-05 12:08:12 +0000 | [diff] [blame^] | 115 | // Typically SK_ENABLE_INST_COUNT=0. Make sure the class declares public typedef INHERITED by |
| 116 | // causing a compile-time error if the typedef is missing. This way SK_ENABLE_INST_COUNT=1 stays |
| 117 | // compiling. |
| 118 | #define SK_DECLARE_INST_COUNT(className) static void AddInstChild() { INHERITED::AddInstChild(); } |
| 119 | #define SK_DECLARE_INST_COUNT_ROOT(className) static void AddInstChild() { } |
| 120 | #endif |
| 121 | |
| 122 | // Following are deprecated. They are defined only for backwards API compatibility. |
| 123 | #define SK_DECLARE_INST_COUNT_TEMPLATE(className) SK_DECLARE_INST_COUNT(className) |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 124 | #define SK_DEFINE_INST_COUNT(className) |
robertphillips@google.com | 4fa9c9f | 2012-08-22 17:31:22 +0000 | [diff] [blame] | 125 | #define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className) |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 126 | |
| 127 | #endif // SkInstCnt_DEFINED |