| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 | 8 |
| 9 #ifndef SkInstCnt_DEFINED | 9 #ifndef SkInstCnt_DEFINED |
| 10 #define SkInstCnt_DEFINED | 10 #define SkInstCnt_DEFINED |
| 11 | 11 |
| 12 /* | 12 /* |
| 13 * The instance counting system consists of three macros that create the | 13 * The instance counting system consists of three macros that create the |
| 14 * instance counting machinery. A class is added to the system by adding: | 14 * instance counting machinery. A class is added to the system by adding: |
| 15 * SK_DECLARE_INST_COUNT at the top of its declaration for derived classes | 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 | 16 * SK_DECLARE_INST_COUNT_ROOT at the top of its declaration for a root class |
| 17 * At the end of an application a call to all the "root" objects' | 17 * At the end of an application a call to all the "root" objects' |
| 18 * CheckInstanceCount methods should be made | 18 * CheckInstanceCount methods should be made |
| 19 */ | 19 */ |
| 20 #include "SkTypes.h" | 20 #include "SkTypes.h" |
| 21 | 21 |
| 22 #if SK_ENABLE_INST_COUNT | 22 #if SK_ENABLE_INST_COUNT |
| 23 // Static variables inside member functions below may be defined multiple times |
| 24 // if Skia is being used as a dynamic library. Instance counting should be on |
| 25 // only for static builds. See bug skia:2058. |
| 26 #if defined(SKIA_DLL) |
| 27 #error Instance counting works only when Skia is built as a static library. |
| 28 #endif |
| 29 |
| 30 #include "SkOnce.h" |
| 23 #include "SkTArray.h" | 31 #include "SkTArray.h" |
| 24 #include "SkThread.h" | 32 #include "SkThread.h" |
| 25 | |
| 26 extern bool gPrintInstCount; | 33 extern bool gPrintInstCount; |
| 27 | 34 |
| 28 // The non-root classes just register themselves with their parent | 35 // The non-root classes just register themselves with their parent |
| 29 #define SK_DECLARE_INST_COUNT(className) \ | 36 #define SK_DECLARE_INST_COUNT(className) \ |
| 30 SK_DECLARE_INST_COUNT_INTERNAL(className, \ | 37 SK_DECLARE_INST_COUNT_INTERNAL(className, \ |
| 31 INHERITED::AddInstChild(CheckInstanceCount);) | 38 INHERITED::AddInstChild(CheckInstanceCount);) |
| 32 | 39 |
| 33 // The root classes registers a function to print out the memory stats when | 40 // The root classes registers a function to print out the memory stats when |
| 34 // the app ends | 41 // the app ends |
| 35 #define SK_DECLARE_INST_COUNT_ROOT(className) \ | 42 #define SK_DECLARE_INST_COUNT_ROOT(className) \ |
| 36 SK_DECLARE_INST_COUNT_INTERNAL(className, atexit(exitPrint);) | 43 SK_DECLARE_INST_COUNT_INTERNAL(className, atexit(exitPrint);) |
| 37 | 44 |
| 38 #define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep) \ | 45 #define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep) \ |
| 39 class SkInstanceCountHelper { \ | 46 class SkInstanceCountHelper { \ |
| 40 public: \ | 47 public: \ |
| 41 typedef int (*PFCheckInstCnt)(int level, bool cleanUp); \ | |
| 42 SkInstanceCountHelper() { \ | 48 SkInstanceCountHelper() { \ |
| 43 static bool gInited; \ | 49 SK_DECLARE_STATIC_ONCE(once); \ |
| 44 if (!gInited) { \ | 50 SkOnce(&once, init, 0); \ |
| 45 initStep \ | |
| 46 GetChildren() = new SkTArray<PFCheckInstCnt>; \ | |
| 47 gInited = true; \ | |
| 48 } \ | |
| 49 sk_atomic_inc(GetInstanceCountPtr()); \ | 51 sk_atomic_inc(GetInstanceCountPtr()); \ |
| 50 } \ | 52 } \ |
| 51 \ | 53 \ |
| 54 static void init(int) { \ |
| 55 initStep \ |
| 56 } \ |
| 57 \ |
| 52 SkInstanceCountHelper(const SkInstanceCountHelper&) { \ | 58 SkInstanceCountHelper(const SkInstanceCountHelper&) { \ |
| 53 sk_atomic_inc(GetInstanceCountPtr()); \ | 59 sk_atomic_inc(GetInstanceCountPtr()); \ |
| 54 } \ | 60 } \ |
| 55 \ | 61 \ |
| 56 ~SkInstanceCountHelper() { \ | 62 ~SkInstanceCountHelper() { \ |
| 57 sk_atomic_dec(GetInstanceCountPtr()); \ | 63 sk_atomic_dec(GetInstanceCountPtr()); \ |
| 58 } \ | 64 } \ |
| 59 \ | 65 \ |
| 60 static int32_t* GetInstanceCountPtr() { \ | 66 static int32_t* GetInstanceCountPtr() { \ |
| 61 static int32_t gInstanceCount; \ | 67 static int32_t gInstanceCount; \ |
| 62 return &gInstanceCount; \ | 68 return &gInstanceCount; \ |
| 63 } \ | 69 } \ |
| 64 \ | 70 \ |
| 65 static SkTArray<PFCheckInstCnt>*& GetChildren() { \ | 71 static SkTArray<int (*)(int, bool)>*& GetChildren() { \ |
| 66 static SkTArray<PFCheckInstCnt>* gChildren; \ | 72 static SkTArray<int (*)(int, bool)>* gChildren; \ |
| 67 return gChildren; \ | 73 return gChildren; \ |
| 68 } \ | 74 } \ |
| 69 \ | 75 \ |
| 76 static SkBaseMutex& GetChildrenMutex() { \ |
| 77 SK_DECLARE_STATIC_MUTEX(childrenMutex); \ |
| 78 return childrenMutex; \ |
| 79 } \ |
| 80 \ |
| 70 } fInstanceCountHelper; \ | 81 } fInstanceCountHelper; \ |
| 71 \ | 82 \ |
| 72 static int32_t GetInstanceCount() { \ | 83 static int32_t GetInstanceCount() { \ |
| 73 return *SkInstanceCountHelper::GetInstanceCountPtr(); \ | 84 return *SkInstanceCountHelper::GetInstanceCountPtr(); \ |
| 74 } \ | 85 } \ |
| 75 \ | 86 \ |
| 76 static void exitPrint() { \ | 87 static void exitPrint() { \ |
| 77 CheckInstanceCount(0, true); \ | 88 CheckInstanceCount(0, true); \ |
| 78 } \ | 89 } \ |
| 79 \ | 90 \ |
| 80 static int CheckInstanceCount(int level = 0, bool cleanUp = false) { \ | 91 static int CheckInstanceCount(int level = 0, bool cleanUp = false) { \ |
| 81 if (gPrintInstCount && 0 != GetInstanceCount()) { \ | 92 if (gPrintInstCount && 0 != GetInstanceCount()) { \ |
| 82 SkDebugf("%*c Leaked %s: %d\n", \ | 93 SkDebugf("%*c Leaked %s: %d\n", \ |
| 83 4*level, ' ', #className, \ | 94 4*level, ' ', #className, \ |
| 84 GetInstanceCount()); \ | 95 GetInstanceCount()); \ |
| 85 } \ | 96 } \ |
| 86 if (NULL == SkInstanceCountHelper::GetChildren()) { \ | 97 if (NULL == SkInstanceCountHelper::GetChildren()) { \ |
| 87 return GetInstanceCount(); \ | 98 return GetInstanceCount(); \ |
| 88 } \ | 99 } \ |
| 89 SkTArray<int (*)(int, bool)>* children = \ | 100 SkTArray<int (*)(int, bool)>* children = \ |
| 90 SkInstanceCountHelper::GetChildren(); \ | 101 SkInstanceCountHelper::GetChildren(); \ |
| 91 int childCount = children->count(); \ | 102 int childCount = children->count(); \ |
| 92 int count = GetInstanceCount(); \ | 103 int count = GetInstanceCount(); \ |
| 93 for (int i = 0; i < childCount; ++i) { \ | 104 for (int i = 0; i < childCount; ++i) { \ |
| 94 count -= (*(*children)[i])(level+1, cleanUp); \ | 105 count -= (*(*children)[i])(level+1, cleanUp); \ |
| 95 } \ | 106 } \ |
| 96 SkASSERT(count >= 0); \ | 107 SkASSERT(count >= 0); \ |
| 97 if (gPrintInstCount && childCount > 0 && count > 0) { \ | 108 if (gPrintInstCount && childCount > 0 && count > 0) { \ |
| 98 SkDebugf("%*c Leaked ???: %d\n", 4*(level + 1), ' ', count); \ | 109 SkDebugf("%*c Leaked ???: %d\n", 4*(level + 1), ' ', count); \ |
| 99 } \ | 110 } \ |
| 100 if (cleanUp) { \ | 111 if (cleanUp) { \ |
| 101 delete children; \ | 112 delete children; \ |
| 102 SkInstanceCountHelper::GetChildren() = NULL; \ | 113 SkInstanceCountHelper::GetChildren() = NULL; \ |
| 103 } \ | 114 } \ |
| 104 return GetInstanceCount(); \ | 115 return GetInstanceCount(); \ |
| 105 } \ | 116 } \ |
| 106 \ | 117 \ |
| 107 static void AddInstChild(int (*childCheckInstCnt)(int, bool)) { \ | 118 static void AddInstChild(int (*childCheckInstCnt)(int, bool)) { \ |
| 108 if (CheckInstanceCount != childCheckInstCnt && \ | 119 if (CheckInstanceCount != childCheckInstCnt) { \ |
| 109 NULL != SkInstanceCountHelper::GetChildren()) { \ | 120 SkAutoMutexAcquire ama(SkInstanceCountHelper::GetChildrenMutex()); \ |
| 121 if (NULL == SkInstanceCountHelper::GetChildren()) { \ |
| 122 SkInstanceCountHelper::GetChildren() = \ |
| 123 new SkTArray<int (*)(int, bool)>; \ |
| 124 } \ |
| 110 SkInstanceCountHelper::GetChildren()->push_back(childCheckInstCnt);
\ | 125 SkInstanceCountHelper::GetChildren()->push_back(childCheckInstCnt);
\ |
| 111 } \ | 126 } \ |
| 112 } | 127 } |
| 113 | 128 |
| 114 #else | 129 #else |
| 115 // Typically SK_ENABLE_INST_COUNT=0. Make sure the class declares public typedef
INHERITED by | 130 // 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_IN
ST_COUNT=1 stays | 131 // causing a compile-time error if the typedef is missing. This way SK_ENABLE_IN
ST_COUNT=1 stays |
| 117 // compiling. | 132 // compiling. |
| 118 #define SK_DECLARE_INST_COUNT(className) static void AddInstChild() { INHERITED:
:AddInstChild(); } | 133 #define SK_DECLARE_INST_COUNT(className) static void AddInstChild() { INHERITED:
:AddInstChild(); } |
| 119 #define SK_DECLARE_INST_COUNT_ROOT(className) static void AddInstChild() { } | 134 #define SK_DECLARE_INST_COUNT_ROOT(className) static void AddInstChild() { } |
| 120 #endif | 135 #endif |
| 121 | 136 |
| 122 // Following are deprecated. They are defined only for backwards API compatibili
ty. | 137 // Following are deprecated. They are defined only for backwards API compatibili
ty. |
| 123 #define SK_DECLARE_INST_COUNT_TEMPLATE(className) SK_DECLARE_INST_COUNT(classNam
e) | 138 #define SK_DECLARE_INST_COUNT_TEMPLATE(className) SK_DECLARE_INST_COUNT(classNam
e) |
| 124 #define SK_DEFINE_INST_COUNT(className) | 139 #define SK_DEFINE_INST_COUNT(className) |
| 125 #define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className) | 140 #define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className) |
| 126 | 141 |
| 127 #endif // SkInstCnt_DEFINED | 142 #endif // SkInstCnt_DEFINED |
| OLD | NEW |