Add missing QT_{BEGIN,END}_NAMESPACE
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlmemoryprofiler.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qqmlmemoryprofiler_p.h"
43 #include <QUrl>
44
45 QT_BEGIN_NAMESPACE
46
47 enum LibraryState
48 {
49     Unloaded,
50     Failed,
51     Loaded
52 };
53
54 static LibraryState state = Unloaded;
55
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)();
64
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;
73
74 extern QFunctionPointer qt_linux_find_symbol_sys(const char *symbol);
75
76 static bool openLibrary()
77 {
78 #ifdef Q_OS_LINUX
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");
88
89         if (memprofile_stats && memprofile_clear && memprofile_enable && memprofile_disable &&
90             memprofile_push_location && memprofile_pop_location && memprofile_save && memprofile_is_enabled)
91             state = Loaded;
92         else
93             state = Failed;
94     }
95 #endif  // Q_OS_LINUX
96
97     return state == Loaded;
98 }
99
100 QQmlMemoryScope::QQmlMemoryScope(const QUrl &url) : pushed(false)
101 {
102     if (openLibrary() && memprofile_is_enabled()) {
103         const char *location = url.path().toUtf8().constData();
104         memprofile_push_location(location, 0);
105         pushed = true;
106     }
107 }
108
109 QQmlMemoryScope::QQmlMemoryScope(const char *string) : pushed(false)
110 {
111     if (openLibrary() && memprofile_is_enabled()) {
112         memprofile_push_location(string, 0);
113         pushed = true;
114     }
115 }
116
117 QQmlMemoryScope::~QQmlMemoryScope()
118 {
119     if (pushed)
120         memprofile_pop_location();
121 }
122
123 bool QQmlMemoryProfiler::isEnabled()
124 {
125     if (openLibrary())
126         return memprofile_is_enabled();
127
128     return false;
129 }
130
131 void QQmlMemoryProfiler::enable()
132 {
133     if (openLibrary())
134         memprofile_enable();
135 }
136
137 void QQmlMemoryProfiler::disable()
138 {
139     if (openLibrary())
140         memprofile_disable();
141 }
142
143 void QQmlMemoryProfiler::clear()
144 {
145     if (openLibrary())
146         memprofile_clear();
147 }
148
149 void QQmlMemoryProfiler::stats(int *allocCount, int *bytesAllocated)
150 {
151     if (openLibrary())
152         memprofile_stats(allocCount, bytesAllocated);
153 }
154
155 void QQmlMemoryProfiler::save(const char *filename)
156 {
157     if (openLibrary())
158         memprofile_save(filename);
159 }
160
161 QT_END_NAMESPACE