File indexing completed on 2024-05-12 15:56:58

0001 /*
0002  *  SPDX-FileCopyrightText: 2010 Cyrille Berger <cberger@cberger.net>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #ifndef _KIS_MEMORY_LEAK_TRACKER_H_
0008 #define _KIS_MEMORY_LEAK_TRACKER_H_
0009 
0010 #include <QtGlobal>
0011 
0012 #include <kritaglobal_export.h>
0013 
0014 #include <config-memory-leak-tracker.h>
0015 
0016 // Only linux support the memory leak tracker
0017 #ifndef Q_OS_LINUX
0018 #undef HAVE_MEMORY_LEAK_TRACKER
0019 #endif
0020 
0021 // Disable the memory leak tracker on release build
0022 #ifdef NDEBUG
0023 #undef HAVE_MEMORY_LEAK_TRACKER
0024 #endif
0025 
0026 /**
0027  * This class tracks what pointer is reference by who. It is used by
0028  * the smart pointers to detect leaks.
0029  *
0030  * Note that the KisMemoryLeakTracker is currently only available on Linux,
0031  * and translate to NOOP on other platforms. It is also just a debug tool,
0032  * and should not be used in a production build of krita.
0033  */
0034 class KRITAGLOBAL_EXPORT KisMemoryLeakTracker
0035 {
0036 public:
0037     KisMemoryLeakTracker();
0038     ~KisMemoryLeakTracker();
0039     static KisMemoryLeakTracker* instance();
0040     void reference(const void* what, const void* bywho, const char* whatName = 0);
0041     void dereference(const void* what, const void* bywho);
0042     void dumpReferences();
0043     void dumpReferences(const void* what);
0044 public:
0045     template<typename _T_>
0046     void reference(const _T_* what, const void* bywho);
0047     template<typename _T_>
0048     void dereference(const _T_* what, const void* bywho);
0049 private:
0050     struct Private;
0051     Private* const d;
0052 };
0053 
0054 #include <typeinfo>
0055 
0056 template<typename _T_>
0057 void KisMemoryLeakTracker::reference(const _T_* what, const void* bywho)
0058 {
0059     reference((void*)what, bywho, typeid(what).name());
0060 }
0061 
0062 template<typename _T_>
0063 void KisMemoryLeakTracker::dereference(const _T_* what, const void* bywho)
0064 {
0065     dereference((void*)what, bywho);
0066 }
0067 
0068 #endif