File indexing completed on 2024-11-10 04:40:37
0001 /* 0002 SPDX-FileCopyrightText: 2016 Daniel Vrátil <dvratil@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <config-akonadi.h> 0008 0009 #if HAVE_MALLOC_TRIM 0010 0011 #include <QCoreApplication> 0012 #include <QThread> 0013 #include <QTimer> 0014 #include <QVariant> 0015 0016 #include <chrono> 0017 #include <malloc.h> 0018 0019 using namespace std::chrono_literals; 0020 0021 namespace Akonadi 0022 { 0023 class Braveheart 0024 { 0025 private: 0026 static void sonOfScotland() 0027 { 0028 Q_ASSERT(qApp->thread() == QThread::currentThread()); 0029 0030 if (!qApp->property("__Akonadi__Braveheart").isNull()) { 0031 // One Scottish warrior is enough.... 0032 return; 0033 } 0034 auto freedom = new QTimer(qApp); 0035 QObject::connect(freedom, &QTimer::timeout, freedom, []() { 0036 // They may take our lives, but they will never 0037 // take our memory! 0038 malloc_trim(50 * 1024 * 1024); 0039 }); 0040 // Fight for freedom every 15 minutes 0041 freedom->start(15min); 0042 qApp->setProperty("__Akonadi__Braveheart", true); 0043 } 0044 0045 public: 0046 explicit Braveheart() 0047 { 0048 qAddPreRoutine([]() { 0049 if (qApp->thread() != QThread::currentThread()) { 0050 QTimer::singleShot(0, qApp, sonOfScotland); 0051 } else { 0052 sonOfScotland(); 0053 } 0054 }); 0055 } 0056 }; 0057 0058 namespace 0059 { 0060 Braveheart Wallace; // clazy:exclude=non-pod-global-static 0061 0062 } 0063 0064 } // namespace Akonadi 0065 0066 #endif // HAVE_MALLOC_TRIM