gfx/skia/trunk/include/core/SkInstCnt.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial