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 |
| 17 | * SK_DEFINE_INST_COUNT at the top of its .cpp file (for both kinds). |
| 18 | * At the end of an application a call to all the "root" objects' |
| 19 | * CheckInstanceCount methods should be made |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 20 | */ |
robertphillips@google.com | b74af87 | 2012-06-27 19:41:42 +0000 | [diff] [blame] | 21 | #ifdef SK_ENABLE_INST_COUNT |
robertphillips@google.com | 97fafe1 | 2012-06-27 19:52:20 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 23 | #include "SkTArray.h" |
| 24 | |
robertphillips@google.com | b74af87 | 2012-06-27 19:41:42 +0000 | [diff] [blame] | 25 | extern bool gPrintInstCount; |
| 26 | |
robertphillips@google.com | 97fafe1 | 2012-06-27 19:52:20 +0000 | [diff] [blame] | 27 | // The non-root classes just register themselves with their parent |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 28 | #define SK_DECLARE_INST_COUNT(className) \ |
| 29 | SK_DECLARE_INST_COUNT_INTERNAL(className, \ |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 30 | INHERITED::AddInstChild(CheckInstanceCount);,\ |
| 31 | /**/) |
| 32 | |
| 33 | #define SK_DECLARE_INST_COUNT_TEMPLATE(className) \ |
| 34 | SK_DECLARE_INST_COUNT_INTERNAL(className, \ |
| 35 | INHERITED::AddInstChild(CheckInstanceCount);, \ |
| 36 | typename) |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 37 | |
robertphillips@google.com | 97fafe1 | 2012-06-27 19:52:20 +0000 | [diff] [blame] | 38 | // The root classes registers a function to print out the memory stats when |
| 39 | // the app ends |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 40 | #define SK_DECLARE_INST_COUNT_ROOT(className) \ |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 41 | SK_DECLARE_INST_COUNT_INTERNAL(className, atexit(exitPrint);, /**/) |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 42 | |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 43 | #define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep, templateType) \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 44 | class SkInstanceCountHelper { \ |
| 45 | public: \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 46 | typedef int (*PFCheckInstCnt)(int level, bool cleanUp); \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 47 | SkInstanceCountHelper() { \ |
| 48 | if (!gInited) { \ |
| 49 | initStep \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 50 | gChildren = new SkTArray<PFCheckInstCnt>; \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 51 | gInited = true; \ |
| 52 | } \ |
| 53 | gInstanceCount++; \ |
| 54 | } \ |
| 55 | \ |
| 56 | SkInstanceCountHelper(const SkInstanceCountHelper& other) { \ |
| 57 | gInstanceCount++; \ |
| 58 | } \ |
| 59 | \ |
| 60 | ~SkInstanceCountHelper() { \ |
| 61 | gInstanceCount--; \ |
| 62 | } \ |
| 63 | \ |
| 64 | static int32_t gInstanceCount; \ |
| 65 | static bool gInited; \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 66 | static SkTArray<PFCheckInstCnt>* gChildren; \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 67 | } fInstanceCountHelper; \ |
| 68 | \ |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 69 | static int32_t GetInstanceCount() { \ |
| 70 | return SkInstanceCountHelper::gInstanceCount; \ |
| 71 | } \ |
| 72 | \ |
robertphillips@google.com | 97fafe1 | 2012-06-27 19:52:20 +0000 | [diff] [blame] | 73 | static void exitPrint() { \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 74 | CheckInstanceCount(0, true); \ |
robertphillips@google.com | 97fafe1 | 2012-06-27 19:52:20 +0000 | [diff] [blame] | 75 | } \ |
| 76 | \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 77 | static int CheckInstanceCount(int level = 0, bool cleanUp = false) { \ |
robertphillips@google.com | b74af87 | 2012-06-27 19:41:42 +0000 | [diff] [blame] | 78 | if (gPrintInstCount && 0 != SkInstanceCountHelper::gInstanceCount) {\ |
reed@google.com | 73e19fb | 2012-06-26 18:58:03 +0000 | [diff] [blame] | 79 | SkDebugf("%*c Leaked %s: %d\n", \ |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 80 | 4*level, ' ', #className, \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 81 | SkInstanceCountHelper::gInstanceCount); \ |
| 82 | } \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 83 | if (NULL == SkInstanceCountHelper::gChildren) { \ |
| 84 | return SkInstanceCountHelper::gInstanceCount; \ |
| 85 | } \ |
| 86 | int childCount = SkInstanceCountHelper::gChildren->count(); \ |
reed@google.com | 73e19fb | 2012-06-26 18:58:03 +0000 | [diff] [blame] | 87 | int count = SkInstanceCountHelper::gInstanceCount; \ |
| 88 | for (int i = 0; i < childCount; ++i) { \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 89 | count -= (*(*SkInstanceCountHelper::gChildren)[i])(level+1, cleanUp); \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 90 | } \ |
reed@google.com | 73e19fb | 2012-06-26 18:58:03 +0000 | [diff] [blame] | 91 | SkASSERT(count >= 0); \ |
robertphillips@google.com | b74af87 | 2012-06-27 19:41:42 +0000 | [diff] [blame] | 92 | if (gPrintInstCount && childCount > 0 && count > 0) { \ |
reed@google.com | 73e19fb | 2012-06-26 18:58:03 +0000 | [diff] [blame] | 93 | SkDebugf("%*c Leaked ???: %d\n", 4*(level + 1), ' ', count); \ |
| 94 | } \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 95 | if (cleanUp) { \ |
| 96 | delete SkInstanceCountHelper::gChildren; \ |
| 97 | SkInstanceCountHelper::gChildren = NULL; \ |
| 98 | } \ |
reed@google.com | 73e19fb | 2012-06-26 18:58:03 +0000 | [diff] [blame] | 99 | return SkInstanceCountHelper::gInstanceCount; \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 100 | } \ |
| 101 | \ |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 102 | static void AddInstChild(templateType SkInstanceCountHelper::PFCheckInstCnt \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 103 | childCheckInstCnt) { \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 104 | if (CheckInstanceCount != childCheckInstCnt && \ |
| 105 | NULL != SkInstanceCountHelper::gChildren) { \ |
| 106 | SkInstanceCountHelper::gChildren->push_back(childCheckInstCnt); \ |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 107 | } \ |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 108 | } |
| 109 | |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 110 | #define SK_DEFINE_INST_COUNT(className) \ |
| 111 | int32_t className::SkInstanceCountHelper::gInstanceCount = 0; \ |
| 112 | bool className::SkInstanceCountHelper::gInited = false; \ |
robertphillips@google.com | 2357927 | 2012-06-28 21:30:45 +0000 | [diff] [blame] | 113 | SkTArray<className::SkInstanceCountHelper::PFCheckInstCnt>* \ |
| 114 | className::SkInstanceCountHelper::gChildren = NULL; |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 115 | |
robertphillips@google.com | 4fa9c9f | 2012-08-22 17:31:22 +0000 | [diff] [blame] | 116 | #define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className) \ |
| 117 | templateInfo int32_t className::SkInstanceCountHelper::gInstanceCount = 0;\ |
| 118 | templateInfo bool className::SkInstanceCountHelper::gInited = false; \ |
| 119 | templateInfo \ |
| 120 | SkTArray<typename className::SkInstanceCountHelper::PFCheckInstCnt>*\ |
| 121 | className::SkInstanceCountHelper::gChildren = NULL; |
| 122 | |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 123 | #else |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 124 | #define SK_DECLARE_INST_COUNT(className) |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 125 | #define SK_DECLARE_INST_COUNT_TEMPLATE(className) |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 126 | #define SK_DECLARE_INST_COUNT_ROOT(className) |
| 127 | #define SK_DEFINE_INST_COUNT(className) |
robertphillips@google.com | 4fa9c9f | 2012-08-22 17:31:22 +0000 | [diff] [blame] | 128 | #define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className) |
robertphillips@google.com | 977b9c8 | 2012-06-05 19:35:09 +0000 | [diff] [blame] | 129 | #endif |
| 130 | |
| 131 | #endif // SkInstCnt_DEFINED |