SkInstCnt.h revision f6747b0b90b3a270ec7b7bdfdc211cf5c19f28c2
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.
6 */
7
8
9#ifndef SkInstCnt_DEFINED
10#define SkInstCnt_DEFINED
11
12/*
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:
15 *   DECLARE_INST_COUNT at the top of its declaration
16 *   DEFINE_INST_COUNT at the top of its .cpp file
17 *   and a PRINT_INST_COUNT line at the application's end point
18 */
19#ifdef SK_DEBUG
20#define DECLARE_INST_COUNT                  \
21    class SkInstanceCountHelper {           \
22    public:                                 \
23        SkInstanceCountHelper() {           \
24            gInstanceCount++;               \
25        }                                   \
26                                            \
27        ~SkInstanceCountHelper() {          \
28            gInstanceCount--;               \
29        }                                   \
30                                            \
31        static int32_t gInstanceCount;      \
32    } fInstanceCountHelper;                 \
33                                            \
34    static int32_t GetInstanceCount() {     \
35        return SkInstanceCountHelper::gInstanceCount;   \
36    }
37
38#define DEFINE_INST_COUNT(className)        \
39    int32_t className::SkInstanceCountHelper::gInstanceCount = 0;
40
41#define PRINT_INST_COUNT(className)         \
42    SkDebugf("Leaked %s objects: %d\n",     \
43                  #className,               \
44                  className::GetInstanceCount());
45#else
46#define DECLARE_INST_COUNT
47#define DEFINE_INST_COUNT(className)
48#define PRINT_INST_COUNT(className)
49#endif
50
51#endif // SkInstCnt_DEFINED
52