Chromium Code Reviews| 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 #include "SkTArray.h" | 23 #include "SkTArray.h" |
| 24 #include "SkThread_platform.h" | 24 #include "SkThread_platform.h" |
| 25 | 25 |
| 26 extern bool gPrintInstCount; | 26 extern bool gPrintInstCount; |
| 27 | 27 |
| 28 SkBaseMutex& SkGetInstCntAddInstChildMutex(); | |
| 29 | |
| 28 // The non-root classes just register themselves with their parent | 30 // The non-root classes just register themselves with their parent |
| 29 #define SK_DECLARE_INST_COUNT(className) \ | 31 #define SK_DECLARE_INST_COUNT(className) \ |
| 30 SK_DECLARE_INST_COUNT_INTERNAL(className, \ | 32 SK_DECLARE_INST_COUNT_INTERNAL(className, \ |
| 31 INHERITED::AddInstChild(CheckInstanceCount);) | 33 INHERITED::AddInstChild(CheckInstanceCount);) |
| 32 | 34 |
| 33 // The root classes registers a function to print out the memory stats when | 35 // The root classes registers a function to print out the memory stats when |
| 34 // the app ends | 36 // the app ends |
| 35 #define SK_DECLARE_INST_COUNT_ROOT(className) \ | 37 #define SK_DECLARE_INST_COUNT_ROOT(className) \ |
| 36 SK_DECLARE_INST_COUNT_INTERNAL(className, atexit(exitPrint);) | 38 SK_DECLARE_INST_COUNT_INTERNAL(className, atexit(exitPrint);) |
| 37 | 39 |
| 38 #define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep) \ | 40 #define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep) \ |
| 39 class SkInstanceCountHelper { \ | 41 class SkInstanceCountHelper { \ |
| 40 public: \ | 42 public: \ |
| 41 typedef int (*PFCheckInstCnt)(int level, bool cleanUp); \ | 43 typedef int (*PFCheckInstCnt)(int level, bool cleanUp); \ |
| 42 SkInstanceCountHelper() { \ | 44 SkInstanceCountHelper() { \ |
| 43 static bool gInited; \ | 45 if (0 == sk_atomic_inc(GetInstanceCountPtr())) { \ |
| 44 if (!gInited) { \ | 46 sk_membar_aquire__after_atomic_dec(); \ |
| 45 initStep \ | 47 static bool gInited; \ |
|
bsalomon
2013/12/04 15:04:19
Perhaps we should use SK_DECLARE_STATIC_ONCE()/SkO
bungeman-skia
2013/12/04 15:24:45
Seconded.
Kimmo Kinnunen
2013/12/05 08:03:23
Done.
| |
| 46 GetChildren() = new SkTArray<PFCheckInstCnt>; \ | 48 if (!gInited) { \ |
| 47 gInited = true; \ | 49 initStep \ |
| 50 gInited = true; \ | |
| 51 } \ | |
| 48 } \ | 52 } \ |
| 49 sk_atomic_inc(GetInstanceCountPtr()); \ | |
| 50 } \ | 53 } \ |
| 51 \ | 54 \ |
| 52 SkInstanceCountHelper(const SkInstanceCountHelper&) { \ | 55 SkInstanceCountHelper(const SkInstanceCountHelper&) { \ |
| 53 sk_atomic_inc(GetInstanceCountPtr()); \ | 56 sk_atomic_inc(GetInstanceCountPtr()); \ |
| 54 } \ | 57 } \ |
| 55 \ | 58 \ |
| 56 ~SkInstanceCountHelper() { \ | 59 ~SkInstanceCountHelper() { \ |
| 57 sk_atomic_dec(GetInstanceCountPtr()); \ | 60 sk_atomic_dec(GetInstanceCountPtr()); \ |
| 58 } \ | 61 } \ |
| 59 \ | 62 \ |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 SkDebugf("%*c Leaked ???: %d\n", 4*(level + 1), ' ', count); \ | 101 SkDebugf("%*c Leaked ???: %d\n", 4*(level + 1), ' ', count); \ |
| 99 } \ | 102 } \ |
| 100 if (cleanUp) { \ | 103 if (cleanUp) { \ |
| 101 delete children; \ | 104 delete children; \ |
| 102 SkInstanceCountHelper::GetChildren() = NULL; \ | 105 SkInstanceCountHelper::GetChildren() = NULL; \ |
| 103 } \ | 106 } \ |
| 104 return GetInstanceCount(); \ | 107 return GetInstanceCount(); \ |
| 105 } \ | 108 } \ |
| 106 \ | 109 \ |
| 107 static void AddInstChild(int (*childCheckInstCnt)(int, bool)) { \ | 110 static void AddInstChild(int (*childCheckInstCnt)(int, bool)) { \ |
| 108 if (CheckInstanceCount != childCheckInstCnt && \ | 111 if (CheckInstanceCount != childCheckInstCnt) { \ |
| 109 NULL != SkInstanceCountHelper::GetChildren()) { \ | 112 SkAutoMutexAcquire ama(SkGetInstCntAddInstChildMutex()); \ |
| 113 if (NULL == SkInstanceCountHelper::GetChildren()) { \ | |
|
robertphillips
2013/12/04 14:01:22
Why not use PFCheckInstCnt here?
Kimmo Kinnunen
2013/12/05 08:03:23
I was confused with the previous need for separate
| |
| 114 SkInstanceCountHelper::GetChildren() = new SkTArray<int (*)(int, bool)>; \ | |
| 115 } \ | |
| 110 SkInstanceCountHelper::GetChildren()->push_back(childCheckInstCnt); \ | 116 SkInstanceCountHelper::GetChildren()->push_back(childCheckInstCnt); \ |
| 111 } \ | 117 } \ |
| 112 } | 118 } |
| 113 | 119 |
| 114 #else | 120 #else |
| 115 // Typically SK_ENABLE_INST_COUNT=0. Make sure the class declares public typedef INHERITED by | 121 // 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 | 122 // causing a compile-time error if the typedef is missing. This way SK_ENABLE_IN ST_COUNT=1 stays |
| 117 // compiling. | 123 // compiling. |
| 118 #define SK_DECLARE_INST_COUNT(className) static void AddInstChild() { INHERITED: :AddInstChild(); } | 124 #define SK_DECLARE_INST_COUNT(className) static void AddInstChild() { INHERITED: :AddInstChild(); } |
| 119 #define SK_DECLARE_INST_COUNT_ROOT(className) static void AddInstChild() { } | 125 #define SK_DECLARE_INST_COUNT_ROOT(className) static void AddInstChild() { } |
| 120 #endif | 126 #endif |
| 121 | 127 |
| 122 // Following are deprecated. They are defined only for backwards API compatibili ty. | 128 // 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) | 129 #define SK_DECLARE_INST_COUNT_TEMPLATE(className) SK_DECLARE_INST_COUNT(classNam e) |
| 124 #define SK_DEFINE_INST_COUNT(className) | 130 #define SK_DEFINE_INST_COUNT(className) |
| 125 #define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className) | 131 #define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className) |
| 126 | 132 |
| 127 #endif // SkInstCnt_DEFINED | 133 #endif // SkInstCnt_DEFINED |
| OLD | NEW |