VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/rand/rand.cpp

Last change on this file was 99758, checked in by vboxsync, 12 months ago

IPRT: Make doxygen 1.9.6 happy. Mostly removing duplicate docs (iprt is documented in the header files). bugref:10442

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.5 KB
Line 
1/* $Id: rand.cpp 99758 2023-05-11 21:37:59Z vboxsync $ */
2/** @file
3 * IPRT - Random Numbers.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/rand.h>
42#include "internal/iprt.h"
43
44#include <iprt/time.h>
45#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
46# include <iprt/asm-amd64-x86.h>
47#endif
48#include <iprt/errcore.h>
49#include <iprt/assert.h>
50#include <iprt/thread.h>
51#include <iprt/once.h>
52#include "internal/rand.h"
53
54
55/*********************************************************************************************************************************
56* Global Variables *
57*********************************************************************************************************************************/
58/** For lazily initializing of the random generator. */
59static RTONCE g_rtRandOnce = RTONCE_INITIALIZER;
60/** The default random generator. */
61static RTRAND g_hRand = NIL_RTRAND;
62
63
64/**
65 * Perform lazy initialization.
66 *
67 * @returns IPRT status code.
68 * @param pvUser Ignored.
69 */
70static DECLCALLBACK(int) rtRandInitOnce(void *pvUser)
71{
72 RTRAND hRand;
73 int rc = RTRandAdvCreateSystemFaster(&hRand);
74 if (RT_FAILURE(rc))
75 rc = RTRandAdvCreateParkMiller(&hRand);
76 if (RT_SUCCESS(rc))
77 {
78#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
79 RTRandAdvSeed(hRand, ASMReadTSC() >> 8);
80#else
81 RTRandAdvSeed(hRand, RTTimeNanoTS() >> 8);
82#endif
83 g_hRand = hRand;
84 }
85 else
86 AssertRC(rc);
87
88 NOREF(pvUser);
89 return rc;
90}
91
92
93/**
94 * Termination counterpart to rtRandInitOnce.
95 *
96 * @param pvUser Ignored.
97 * @param fLazyCleanUpOk Set if we're terminating the process.
98 */
99static DECLCALLBACK(void) rtRandTermOnce(void *pvUser, bool fLazyCleanUpOk)
100{
101 if (!fLazyCleanUpOk)
102 {
103 RTRAND hRand = g_hRand;
104 g_hRand = NIL_RTRAND;
105 if (hRand != NIL_RTRAND)
106 {
107 int rc = RTRandAdvDestroy(hRand);
108 AssertRC(rc);
109 }
110 }
111 NOREF(pvUser);
112}
113
114
115RTDECL(void) RTRandBytes(void *pv, size_t cb) RT_NO_THROW_DEF
116{
117 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
118 RTRandAdvBytes(g_hRand, pv, cb);
119}
120RT_EXPORT_SYMBOL(RTRandBytes);
121
122
123RTDECL(uint32_t) RTRandU32Ex(uint32_t u32First, uint32_t u32Last) RT_NO_THROW_DEF
124{
125 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
126 return RTRandAdvU32Ex(g_hRand, u32First, u32Last);
127}
128RT_EXPORT_SYMBOL(RTRandU32Ex);
129
130
131RTDECL(uint32_t) RTRandU32(void) RT_NO_THROW_DEF
132{
133 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
134 return RTRandAdvU32(g_hRand);
135}
136RT_EXPORT_SYMBOL(RTRandU32);
137
138
139RTDECL(int32_t) RTRandS32Ex(int32_t i32First, int32_t i32Last) RT_NO_THROW_DEF
140{
141 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
142 return RTRandAdvS32Ex(g_hRand, i32First, i32Last);
143}
144RT_EXPORT_SYMBOL(RTRandS32Ex);
145
146
147RTDECL(int32_t) RTRandS32(void) RT_NO_THROW_DEF
148{
149 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
150 return RTRandAdvS32(g_hRand);
151}
152RT_EXPORT_SYMBOL(RTRandS32);
153
154
155RTDECL(uint64_t) RTRandU64Ex(uint64_t u64First, uint64_t u64Last) RT_NO_THROW_DEF
156{
157 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
158 return RTRandAdvU64Ex(g_hRand, u64First, u64Last);
159}
160RT_EXPORT_SYMBOL(RTRandU64Ex);
161
162
163RTDECL(uint64_t) RTRandU64(void) RT_NO_THROW_DEF
164{
165 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
166 return RTRandAdvU64(g_hRand);
167}
168RT_EXPORT_SYMBOL(RTRandU64);
169
170
171RTDECL(int64_t) RTRandS64Ex(int64_t i64First, int64_t i64Last) RT_NO_THROW_DEF
172{
173 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
174 return RTRandAdvS64Ex(g_hRand, i64First, i64Last);
175}
176RT_EXPORT_SYMBOL(RTRandS64Ex);
177
178
179RTDECL(int64_t) RTRandS64(void) RT_NO_THROW_DEF
180{
181 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
182 return RTRandAdvS32(g_hRand);
183}
184RT_EXPORT_SYMBOL(RTRandS64);
185
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use