Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
qglobal.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// Copyright (C) 2017 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qplatformdefs.h"
6#include "qstring.h"
7#include "qbytearrayview.h"
8#include "qlist.h"
9#include "qdir.h"
10#include "qdatetime.h"
11#include <private/qlocale_tools_p.h>
12#include "qnativeinterface.h"
13#include "qnativeinterface_p.h"
14
15#include <stdlib.h>
16#include <stdarg.h>
17#include <string.h>
18
19#include <errno.h>
20#if defined(Q_CC_MSVC)
21# include <crtdbg.h>
22#endif
23
24#ifdef Q_OS_WIN
25# include <qt_windows.h>
26#endif
27
28#if defined(Q_OS_VXWORKS) && defined(_WRS_KERNEL)
29# include <envLib.h>
30#endif
31
32#if defined(Q_OS_INTEGRITY)
33extern "C" {
34 // Function mmap resides in libshm_client.a. To be able to link with it one needs
35 // to define symbols 'shm_area_password' and 'shm_area_name', because the library
36 // is meant to allow the application that links to it to use POSIX shared memory
37 // without full system POSIX.
38# pragma weak shm_area_password
39# pragma weak shm_area_name
40 char shm_area_password[] = "dummy";
41 char shm_area_name[] = "dummy";
42}
43#endif
44
45#ifdef qFatal
46// the qFatal in this file are just redirections from elsewhere, so
47// don't capture any context again
48# undef qFatal
49#endif
50
52
53using namespace Qt::StringLiterals;
54
108/*
109 Dijkstra's bisection algorithm to find the square root of an integer.
110 Deliberately not exported as part of the Qt API, but used in both
111 qsimplerichtext.cpp and qgfxraster_qws.cpp
112*/
113Q_CORE_EXPORT Q_DECL_CONST_FUNCTION unsigned int qt_int_sqrt(unsigned int n)
114{
115 // n must be in the range 0...UINT_MAX/2-1
116 if (n >= (UINT_MAX >> 2)) {
117 unsigned int r = 2 * qt_int_sqrt(n / 4);
118 unsigned int r2 = r + 1;
119 return (n >= r2 * r2) ? r2 : r;
120 }
121 uint h, p = 0, q = 1, r = n;
122 while (q <= n)
123 q <<= 2;
124 while (q != 1) {
125 q >>= 2;
126 h = p + q;
127 p >>= 1;
128 if (r >= h) {
129 p += q;
130 r -= h;
131 }
132 }
133 return p;
134}
135
136void qAbort()
137{
138#ifdef Q_OS_WIN
139 // std::abort() in the MSVC runtime will call _exit(3) if the abort
140 // behavior is _WRITE_ABORT_MSG - see also _set_abort_behavior(). This is
141 // the default for a debug-mode build of the runtime. Worse, MinGW's
142 // std::abort() implementation (in msvcrt.dll) is basically a call to
143 // _exit(3) too. Unfortunately, _exit() and _Exit() *do* run the static
144 // destructors of objects in DLLs, a violation of the C++ standard (see
145 // [support.start.term]). So we bypass std::abort() and directly
146 // terminate the application.
147
148# if defined(Q_CC_MSVC)
149 if (IsProcessorFeaturePresent(PF_FASTFAIL_AVAILABLE))
150 __fastfail(FAST_FAIL_FATAL_APP_EXIT);
151# else
152 RaiseFailFastException(nullptr, nullptr, 0);
153# endif
154
155 // Fallback
156 TerminateProcess(GetCurrentProcess(), STATUS_FATAL_APP_EXIT);
157
158 // Tell the compiler the application has stopped.
160#else // !Q_OS_WIN
161 std::abort();
162#endif
163}
164
165// Also specified to behave as if they call tzset():
166// localtime() -- but not localtime_r(), which we use when threaded
167// strftime() -- not used (except in tests)
168
170{
172};
173
174Q_GLOBAL_STATIC(QInternal_CallBackTable, global_callback_table)
175
176bool QInternal::registerCallback(Callback cb, qInternalCallback callback)
177{
178 if (unsigned(cb) < unsigned(QInternal::LastCallback)) {
179 QInternal_CallBackTable *cbt = global_callback_table();
180 cbt->callbacks.resize(cb + 1);
181 cbt->callbacks[cb].append(callback);
182 return true;
183 }
184 return false;
185}
186
188{
189 if (unsigned(cb) < unsigned(QInternal::LastCallback)) {
190 if (global_callback_table.exists()) {
191 QInternal_CallBackTable *cbt = global_callback_table();
192 return cbt->callbacks[cb].removeAll(callback) > 0;
193 }
194 }
195 return false;
196}
197
199{
200 Q_ASSERT_X(cb >= 0, "QInternal::activateCallback()", "Callback id must be a valid id");
201
202 if (!global_callback_table.exists())
203 return false;
204
205 QInternal_CallBackTable *cbt = &(*global_callback_table);
206 if (cbt && cb < cbt->callbacks.size()) {
208 bool ret = false;
209 for (int i = 0; i < callbacks.size(); ++i)
210 ret |= (callbacks.at(i))(parameters);
211 return ret;
212 }
213 return false;
214}
215
387namespace QtPrivate {
388Q_LOGGING_CATEGORY(lcNativeInterface, "qt.nativeinterface")
389}
390
static bool unregisterCallback(Callback, qInternalCallback)
Definition qglobal.cpp:187
static bool activateCallbacks(Callback, void **)
Definition qglobal.cpp:198
Definition qlist.h:74
qsizetype removeAll(const AT &t)
Definition qlist.h:575
void resize(qsizetype size)
Definition qlist.h:392
void append(parameter_type t)
Definition qlist.h:441
Combined button and popup list for selecting options.
\macro QT_NAMESPACE
#define Q_UNREACHABLE_IMPL()
#define Q_DECL_CONST_FUNCTION
void qAbort()
Definition qglobal.cpp:136
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION unsigned int qt_int_sqrt(unsigned int n)
\inmodule QtCore \title Global Qt Declarations
Definition qglobal.cpp:113
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
#define Q_LOGGING_CATEGORY(name,...)
return ret
bool(* qInternalCallback)(void **)
GLboolean r
[2]
GLfloat n
GLfloat GLfloat GLfloat GLfloat h
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLfloat GLfloat p
[1]
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
PromiseCallbacks callbacks
Definition qstdweb.cpp:270
unsigned int uint
Definition qtypes.h:29
QRect r2(QPoint(100, 200), QSize(11, 16))
QList< QList< qInternalCallback > > callbacks
Definition qglobal.cpp:171