Akonadi

braveheart.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 Daniel Vr├ítil <[email protected]>
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 
19 using namespace std::chrono_literals;
20 
21 namespace Akonadi
22 {
23 class Braveheart
24 {
25 private:
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 
45 public:
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 
58 namespace
59 {
60 Braveheart Wallace; // clazy:exclude=non-pod-global-static
61 
62 }
63 
64 } // namespace Akonadi
65 
66 #endif // HAVE_MALLOC_TRIM
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void timeout()
QThread * currentThread()
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jun 30 2022 03:51:45 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.