From c37e160a4201c88dffb72ba7aa937aef77d78791 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Sun, 15 Dec 2013 22:24:45 +0000 Subject: [PATCH] simple memory profiling on Windows using the CRT debug heap refs #81 --- src/blackmisc/blackmiscfreefunctions.cpp | 70 +++++++++++++++++++++++- src/blackmisc/blackmiscfreefunctions.h | 14 +++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/src/blackmisc/blackmiscfreefunctions.cpp b/src/blackmisc/blackmiscfreefunctions.cpp index 74c211315..3b8013f04 100644 --- a/src/blackmisc/blackmiscfreefunctions.cpp +++ b/src/blackmisc/blackmiscfreefunctions.cpp @@ -62,7 +62,6 @@ void BlackMisc::Math::registerMetadata() CVector3D::registerMetadata(); } - /* * Metadata for Geo */ @@ -327,6 +326,75 @@ QVariant BlackMisc::complexQtTypeFromDbusArgument(const QDBusArgument &argument, return QVariant(); // suppress compiler warning } +#ifdef Q_CC_MSVC +#include + +// surpress some GCC warnings, if someone finds +// a better solution for this, feel free +#if defined(__GCC__) || defined(__MINGW32__) || defined(__MINGW64__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-variable" +#pragma GCC diagnostic ignored "-Wunused-value" +#pragma GCC diagnostic ignored "-Wunused-but-set-variable" +#endif + +/* + * Heap size of an object + */ +size_t BlackMisc::heapSizeOf(const QMetaType &metaType) +{ + + metaType.destroy(metaType.create()); // ignore one-off allocations of a class being instantiated for the first time + _CrtMemState oldState, newState, diff; + oldState.lTotalCount = newState.lTotalCount = diff.lTotalCount = 0; // avoid compiler warning + diff.lSizes[_NORMAL_BLOCK] = 0; + _CrtMemCheckpoint(&oldState); + void *p = metaType.create(); + _CrtMemCheckpoint(&newState); + metaType.destroy(p); + _CrtMemDifference(&diff, &oldState, &newState); + return diff.lSizes[_NORMAL_BLOCK]; +} + +/* + * Heap size of an object + */ +size_t BlackMisc::heapSizeOf(const QMetaObject &metaObject) +{ + delete metaObject.newInstance(); //ignore one-off allocations of a class being instantiated for the first time + _CrtMemState oldState, newState, diff; + oldState.lTotalCount = newState.lTotalCount = diff.lTotalCount = 0; // avoid compiler warning + diff.lSizes[_NORMAL_BLOCK] = 0; + _CrtMemCheckpoint(&oldState); + QObject *obj = metaObject.newInstance(); + _CrtMemCheckpoint(&newState); + delete obj; + _CrtMemDifference(&diff, &oldState, &newState); + return diff.lSizes[_NORMAL_BLOCK]; +} +#if defined(__GCC__) || defined(__MINGW32__) || defined(__MINGW64__) +#pragma GCC diagnostic pop +#endif + +#else //!Q_OS_WIN32 +/* + * Heap size of an object + */ +size_t BlackMisc::heapSizeOf(const QMetaType &) +{ + qDebug() << "heapSizeOf not supported on this OS"; + return 0; +} +/* + * Heap size of an object + */ +size_t BlackMisc::heapSizeOf(const QMetaObject &) +{ + qDebug() << "heapSizeOf not supported on this OS"; + return 0; +} +#endif //!Q_OS_WIN32 + /* * Dump all user types */ diff --git a/src/blackmisc/blackmiscfreefunctions.h b/src/blackmisc/blackmiscfreefunctions.h index f1b2d87f8..9d34fd059 100644 --- a/src/blackmisc/blackmiscfreefunctions.h +++ b/src/blackmisc/blackmiscfreefunctions.h @@ -169,6 +169,20 @@ namespace BlackMisc */ uint calculateHash(const QList &values, const char *className); + /*! + * \brief Real heap size of an object + * \param type + * \return + */ + size_t heapSizeOf(const QMetaType &type); + + /*! + * \brief Real heap size of an object + * \param objectType + * \return + */ + size_t heapSizeOf(const QMetaObject &objectType); + } // BlackMisc #endif // guard