summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qrandom.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/global/qrandom.cpp')
-rw-r--r--src/corelib/global/qrandom.cpp85
1 files changed, 54 insertions, 31 deletions
diff --git a/src/corelib/global/qrandom.cpp b/src/corelib/global/qrandom.cpp
index 7da86188bb..f5c01aeaa6 100644
--- a/src/corelib/global/qrandom.cpp
+++ b/src/corelib/global/qrandom.cpp
@@ -93,7 +93,7 @@ DECLSPEC_IMPORT BOOLEAN WINAPI SystemFunction036(PVOID RandomBuffer, ULONG Rando
QT_BEGIN_NAMESPACE
#if defined(Q_PROCESSOR_X86) && QT_COMPILER_SUPPORTS_HERE(RDRND)
-static qssize_t qt_random_cpu(void *buffer, qssize_t count) Q_DECL_NOTHROW;
+static qsizetype qt_random_cpu(void *buffer, qsizetype count) Q_DECL_NOTHROW;
# ifdef Q_PROCESSOR_X86_64
# define _rdrandXX_step _rdrand64_step
@@ -101,7 +101,7 @@ static qssize_t qt_random_cpu(void *buffer, qssize_t count) Q_DECL_NOTHROW;
# define _rdrandXX_step _rdrand32_step
# endif
-static QT_FUNCTION_TARGET(RDRND) qssize_t qt_random_cpu(void *buffer, qssize_t count) Q_DECL_NOTHROW
+static QT_FUNCTION_TARGET(RDRND) qsizetype qt_random_cpu(void *buffer, qsizetype count) Q_DECL_NOTHROW
{
unsigned *ptr = reinterpret_cast<unsigned *>(buffer);
unsigned *end = ptr + count;
@@ -122,7 +122,7 @@ out:
return ptr - reinterpret_cast<unsigned *>(buffer);
}
#else
-static qssize_t qt_random_cpu(void *, qssize_t)
+static qsizetype qt_random_cpu(void *, qsizetype)
{
return 0;
}
@@ -136,10 +136,10 @@ enum {
struct QRandomGenerator::SystemGenerator
{
#if QT_CONFIG(getentropy)
- static qssize_t fillBuffer(void *buffer, qssize_t count) Q_DECL_NOTHROW
+ static qsizetype fillBuffer(void *buffer, qsizetype count) Q_DECL_NOTHROW
{
// getentropy can read at most 256 bytes, so break the reading
- qssize_t read = 0;
+ qsizetype read = 0;
while (count - read > 256) {
// getentropy can't fail under normal circumstances
int ret = getentropy(reinterpret_cast<uchar *>(buffer) + read, 256);
@@ -195,24 +195,24 @@ struct QRandomGenerator::SystemGenerator
Q_DECL_CONSTEXPR SystemGenerator() : fdp1 Q_BASIC_ATOMIC_INITIALIZER(0) {}
- qssize_t fillBuffer(void *buffer, qssize_t count)
+ qsizetype fillBuffer(void *buffer, qsizetype count)
{
int fd = openDevice();
if (Q_UNLIKELY(fd < 0))
return 0;
qint64 n = qt_safe_read(fd, buffer, count);
- return qMax<qssize_t>(n, 0); // ignore any errors
+ return qMax<qsizetype>(n, 0); // ignore any errors
}
#elif defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
- qssize_t fillBuffer(void *buffer, qssize_t count) Q_DECL_NOTHROW
+ qsizetype fillBuffer(void *buffer, qsizetype count) Q_DECL_NOTHROW
{
auto RtlGenRandom = SystemFunction036;
return RtlGenRandom(buffer, ULONG(count)) ? count: 0;
}
#elif defined(Q_OS_WINRT)
- qssize_t fillBuffer(void *, qssize_t) Q_DECL_NOTHROW
+ qsizetype fillBuffer(void *, qsizetype) Q_DECL_NOTHROW
{
// always use the fallback
return 0;
@@ -243,7 +243,7 @@ struct QRandomGenerator::SystemGenerator
#if defined(Q_OS_WIN)
static void fallback_update_seed(unsigned) {}
-static void fallback_fill(quint32 *ptr, qssize_t left) Q_DECL_NOTHROW
+static void fallback_fill(quint32 *ptr, qsizetype left) Q_DECL_NOTHROW
{
// on Windows, rand_s is a high-quality random number generator
// and it requires no seeding
@@ -255,14 +255,14 @@ static void fallback_fill(quint32 *ptr, qssize_t left) Q_DECL_NOTHROW
}
#elif QT_CONFIG(getentropy)
static void fallback_update_seed(unsigned) {}
-static void fallback_fill(quint32 *, qssize_t) Q_DECL_NOTHROW
+static void fallback_fill(quint32 *, qsizetype) Q_DECL_NOTHROW
{
// no fallback necessary, getentropy cannot fail under normal circumstances
Q_UNREACHABLE();
}
#elif defined(Q_OS_BSD4)
static void fallback_update_seed(unsigned) {}
-static void fallback_fill(quint32 *ptr, qssize_t left) Q_DECL_NOTHROW
+static void fallback_fill(quint32 *ptr, qsizetype left) Q_DECL_NOTHROW
{
// BSDs have arc4random(4) and these work even in chroot(2)
arc4random_buf(ptr, left * sizeof(*ptr));
@@ -281,7 +281,7 @@ Q_NEVER_INLINE
#ifdef Q_CC_GNU
__attribute__((cold)) // this function is pretty big, so optimize for size
#endif
-static void fallback_fill(quint32 *ptr, qssize_t left) Q_DECL_NOTHROW
+static void fallback_fill(quint32 *ptr, qsizetype left) Q_DECL_NOTHROW
{
quint32 scratch[12]; // see element count below
quint32 *end = scratch;
@@ -358,7 +358,7 @@ Q_NEVER_INLINE void QRandomGenerator::SystemGenerator::generate(quint32 *begin,
Q_DECL_NOEXCEPT_EXPR(FillBufferNoexcept)
{
quint32 *buffer = begin;
- qssize_t count = end - begin;
+ qsizetype count = end - begin;
if (Q_UNLIKELY(uint(qt_randomdevice_control) & SetRandomData)) {
uint value = uint(qt_randomdevice_control) & RandomDataMask;
@@ -366,14 +366,14 @@ Q_NEVER_INLINE void QRandomGenerator::SystemGenerator::generate(quint32 *begin,
return;
}
- qssize_t filled = 0;
+ qsizetype filled = 0;
if (qt_has_hwrng() && (uint(qt_randomdevice_control) & SkipHWRNG) == 0)
filled += qt_random_cpu(buffer, count);
if (filled != count && (uint(qt_randomdevice_control) & SkipSystemRNG) == 0) {
- qssize_t bytesFilled =
- fillBuffer(buffer + filled, (count - filled) * qssize_t(sizeof(*buffer)));
- filled += bytesFilled / qssize_t(sizeof(*buffer));
+ qsizetype bytesFilled =
+ fillBuffer(buffer + filled, (count - filled) * qsizetype(sizeof(*buffer)));
+ filled += bytesFilled / qsizetype(sizeof(*buffer));
}
if (filled)
fallback_update_seed(*buffer);
@@ -656,6 +656,11 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
*/
/*!
+ \enum QRandomGenerator::System
+ \internal
+*/
+
+/*!
\fn QRandomGenerator::QRandomGenerator(quint32 seedValue)
Initializes this QRandomGenerator object with the value \a seedValue as
@@ -677,7 +682,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
*/
/*!
- \fn QRandomGenerator::QRandomGenerator(const quint32 *seedBuffer, qssize_t len)
+ \fn QRandomGenerator::QRandomGenerator(const quint32 *seedBuffer, qsizetype len)
\overload
Initializes this QRandomGenerator object with \a len values found in
@@ -694,7 +699,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
*/
/*!
- \fn QRandomGenerator::QRandomGenerator(const quint32 *begin, const quin32 *end)
+ \fn QRandomGenerator::QRandomGenerator(const quint32 *begin, const quint32 *end)
\overload
Initializes this QRandomGenerator object with the values found in the range
@@ -762,7 +767,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
A typedef to the type that operator()() returns. That is, quint32.
- \sa operator()()
+ \sa {QRandomGenerator::operator()}{operator()()}
*/
/*!
@@ -774,6 +779,22 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
*/
/*!
+ \fn quint32 QRandomGenerator::generate()
+
+ Generates a 32-bit random quantity and returns it.
+
+ \sa {QRandomGenerator::operator()}{operator()()}, generate64()
+ */
+
+/*!
+ \fn quint64 QRandomGenerator::generate64()
+
+ Generates a 64-bit random quantity and returns it.
+
+ \sa {QRandomGenerator::operator()}{operator()()}, generate()
+ */
+
+/*!
\fn result_type QRandomGenerator::min()
Returns the minimum value that QRandomGenerator may ever generate. That is, 0.
@@ -800,7 +821,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
\fn void QRandomGenerator::seed(std::seed_seq &seed)
\overload
- Reseeds this object using the seed sequence \a sseq as the seed.
+ Reseeds this object using the seed sequence \a seed as the seed.
*/
/*!
@@ -826,7 +847,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
\endcode
This function complies with the requirements for the function
- \c{\l{http://en.cppreference.com/w/cpp/numeric/random/seed_seq/generate}{std::seed_seq::generate}},
+ \l{http://en.cppreference.com/w/cpp/numeric/random/seed_seq/generate}{\c std::seed_seq::generate},
which requires unsigned 32-bit integer values.
Note that if the [begin, end) range refers to an area that can store more
@@ -853,7 +874,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
*/
/*!
- \fn void QRandomGenerator::fillRange(UInt *buffer, qssize_t count)
+ \fn void QRandomGenerator::fillRange(UInt *buffer, qsizetype count)
Generates \a count 32- or 64-bit quantities (depending on the type \c UInt)
and stores them in the buffer pointed by \a buffer. This is the most
@@ -906,16 +927,16 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
\endcode
The same may also be obtained by using
- \c{\l{http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution}{std::uniform_real_distribution}}
+ \l{http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution}{\c std::uniform_real_distribution}
with parameters 0 and 1.
\sa generate(), generate64(), bounded()
*/
/*!
- \fn qreal QRandomGenerator::bounded(qreal highest)
+ \fn double QRandomGenerator::bounded(double highest)
- Generates one random qreal in the range between 0 (inclusive) and \a
+ Generates one random double in the range between 0 (inclusive) and \a
highest (exclusive). This function is equivalent to and is implemented as:
\code
@@ -931,7 +952,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
Generates one random 32-bit quantity in the range between 0 (inclusive) and
\a highest (exclusive). The same result may also be obtained by using
- \c{\l{http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution}{std::uniform_int_distribution}}
+ \l{http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution}{\c std::uniform_int_distribution}
with parameters 0 and \c{highest - 1}. That class can also be used to obtain
quantities larger than 32 bits.
@@ -969,7 +990,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
Generates one random 32-bit quantity in the range between \a lowest (inclusive)
and \a highest (exclusive). The same result may also be obtained by using
- \c{\l{http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution}{std::uniform_int_distribution}}
+ \l{http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution}{\c std::uniform_int_distribution}
with parameters \a lowest and \c{\a highest - 1}. That class can also be used to
obtain quantities larger than 32 bits.
@@ -1098,7 +1119,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
A typedef to the type that operator()() returns. That is, quint64.
- \sa operator()()
+ \sa {QRandomGenerator64::operator()}{operator()()}
*/
/*!
@@ -1169,7 +1190,9 @@ QRandomGenerator64 QRandomGenerator64::securelySeeded()
return result;
}
-/// \internal
+/*!
+ \internal
+*/
inline QRandomGenerator::QRandomGenerator(System)
: type(SystemRNG)
{