Akonadi

braveheart.cpp
1/*
2 SPDX-FileCopyrightText: 2016 Daniel Vrátil <dvratil@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include <config-akonadi.h>
8
9#if HAVE_MALLOC_TRIM
10
11#include <QCoreApplication>
12#include <QThread>
13#include <QTimer>
14#include <QVariant>
15
16#include <chrono>
17#include <malloc.h>
18
19using namespace std::chrono_literals;
20
21namespace Akonadi
22{
23class Braveheart
24{
25private:
26 static void sonOfScotland()
27 {
28 Q_ASSERT(qApp->thread() == QThread::currentThread());
29
30 if (!qApp->property("__Akonadi__Braveheart").isNull()) {
31 // One Scottish warrior is enough....
32 return;
33 }
34 auto freedom = new QTimer(qApp);
35 QObject::connect(freedom, &QTimer::timeout, freedom, []() {
36 // They may take our lives, but they will never
37 // take our memory!
38 malloc_trim(50 * 1024 * 1024);
39 });
40 // Fight for freedom every 15 minutes
41 freedom->start(15min);
42 qApp->setProperty("__Akonadi__Braveheart", true);
43 }
44
45public:
46 explicit Braveheart()
47 {
48 qAddPreRoutine([]() {
49 if (qApp->thread() != QThread::currentThread()) {
50 QTimer::singleShot(0, qApp, sonOfScotland);
51 } else {
52 sonOfScotland();
53 }
54 });
55 }
56};
57
58namespace
59{
60Braveheart Wallace; // clazy:exclude=non-pod-global-static
61
62}
63
64} // namespace Akonadi
65
66#endif // HAVE_MALLOC_TRIM
Helper integration between Akonadi and Qt.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QThread * currentThread()
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:31:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.