1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qqmlmemoryprofiler_p.h"
54 static LibraryState state = Unloaded;
56 typedef void (qmlmemprofile_stats)(int *allocCount, int *bytesAllocated);
57 typedef void (qmlmemprofile_clear)();
58 typedef void (qmlmemprofile_enable)();
59 typedef void (qmlmemprofile_disable)();
60 typedef void (qmlmemprofile_push_location)(const char *filename, int lineNumber);
61 typedef void (qmlmemprofile_pop_location)();
62 typedef void (qmlmemprofile_save)(const char *filename);
63 typedef int (qmlmemprofile_is_enabled)();
65 static qmlmemprofile_stats *memprofile_stats;
66 static qmlmemprofile_clear *memprofile_clear;
67 static qmlmemprofile_enable *memprofile_enable;
68 static qmlmemprofile_disable *memprofile_disable;
69 static qmlmemprofile_push_location *memprofile_push_location;
70 static qmlmemprofile_pop_location *memprofile_pop_location;
71 static qmlmemprofile_save *memprofile_save;
72 static qmlmemprofile_is_enabled *memprofile_is_enabled;
74 extern QFunctionPointer qt_linux_find_symbol_sys(const char *symbol);
76 static bool openLibrary()
79 if (state == Unloaded) {
80 memprofile_stats = (qmlmemprofile_stats *) qt_linux_find_symbol_sys("qmlmemprofile_stats");
81 memprofile_clear = (qmlmemprofile_clear *) qt_linux_find_symbol_sys("qmlmemprofile_clear");
82 memprofile_enable = (qmlmemprofile_enable *) qt_linux_find_symbol_sys("qmlmemprofile_enable");
83 memprofile_disable = (qmlmemprofile_disable *) qt_linux_find_symbol_sys("qmlmemprofile_disable");
84 memprofile_push_location = (qmlmemprofile_push_location *) qt_linux_find_symbol_sys("qmlmemprofile_push_location");
85 memprofile_pop_location = (qmlmemprofile_pop_location *) qt_linux_find_symbol_sys("qmlmemprofile_pop_location");
86 memprofile_save = (qmlmemprofile_save *) qt_linux_find_symbol_sys("qmlmemprofile_save");
87 memprofile_is_enabled = (qmlmemprofile_is_enabled *) qt_linux_find_symbol_sys("qmlmemprofile_is_enabled");
89 if (memprofile_stats && memprofile_clear && memprofile_enable && memprofile_disable &&
90 memprofile_push_location && memprofile_pop_location && memprofile_save && memprofile_is_enabled)
97 return state == Loaded;
100 QQmlMemoryScope::QQmlMemoryScope(const QUrl &url) : pushed(false)
102 if (openLibrary() && memprofile_is_enabled()) {
103 const char *location = url.path().toUtf8().constData();
104 memprofile_push_location(location, 0);
109 QQmlMemoryScope::QQmlMemoryScope(const char *string) : pushed(false)
111 if (openLibrary() && memprofile_is_enabled()) {
112 memprofile_push_location(string, 0);
117 QQmlMemoryScope::~QQmlMemoryScope()
120 memprofile_pop_location();
123 bool QQmlMemoryProfiler::isEnabled()
126 return memprofile_is_enabled();
131 void QQmlMemoryProfiler::enable()
137 void QQmlMemoryProfiler::disable()
140 memprofile_disable();
143 void QQmlMemoryProfiler::clear()
149 void QQmlMemoryProfiler::stats(int *allocCount, int *bytesAllocated)
152 memprofile_stats(allocCount, bytesAllocated);
155 void QQmlMemoryProfiler::save(const char *filename)
158 memprofile_save(filename);