1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
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);