Akonadi

braveheart.cpp
1 /*
2  Copyright (c) 2016 Daniel Vrátil <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include <config-akonadi.h>
21 
22 #ifdef HAVE_MALLOC_TRIM
23 
24 #include <QCoreApplication>
25 #include <QTimer>
26 #include <QVariant>
27 #include <QThread>
28 
29 #include <malloc.h>
30 
31 namespace Akonadi
32 {
33 
34 class Braveheart
35 {
36 private:
37  static void sonOfScotland()
38  {
39  Q_ASSERT(qApp->thread() == QThread::currentThread());
40 
41  if (!qApp->property("__Akonadi__Braveheart").isNull()) {
42  // One Scottish warrior is enough....
43  return;
44  }
45  auto freedom = new QTimer(qApp);
47  freedom, []() {
48  // They may take our lives, but they will never
49  // take our memory!
50  malloc_trim(50 * 1024 * 1024);
51  });
52  // Fight for freedom every 15 minutes
53  freedom->start(15 * 60 * 1000);
54  qApp->setProperty("__Akonadi__Braveheart", true);
55  }
56 
57 public:
58  explicit Braveheart()
59  {
60  qAddPreRoutine([]() {
61  if (qApp->thread() != QThread::currentThread()) {
62  QTimer::singleShot(0, qApp, sonOfScotland);
63  } else {
64  sonOfScotland();
65  }
66  });
67  }
68 };
69 
70 namespace
71 {
72 
73 Braveheart Wallace;
74 
75 }
76 
77 } // namespace Akonadi
78 
79 #endif // HAVE_MALLOC_TRIM
void timeout()
QThread * currentThread()
Helper integration between Akonadi and Qt.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:43:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.