VirtualBox

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

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.5 KB
Line 
1/* $Id: rand.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - Random Numbers.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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 * @returns IPRT status code.
97 * @param pvUser Ignored.
98 * @param fLazyCleanUpOk Set if we're terminating the process.
99 */
100static DECLCALLBACK(void) rtRandTermOnce(void *pvUser, bool fLazyCleanUpOk)
101{
102 if (!fLazyCleanUpOk)
103 {
104 RTRAND hRand = g_hRand;
105 g_hRand = NIL_RTRAND;
106 if (hRand != NIL_RTRAND)
107 {
108 int rc = RTRandAdvDestroy(hRand);
109 AssertRC(rc);
110 }
111 }
112 NOREF(pvUser);
113}
114
115
116RTDECL(void) RTRandBytes(void *pv, size_t cb) RT_NO_THROW_DEF
117{
118 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
119 RTRandAdvBytes(g_hRand, pv, cb);
120}
121RT_EXPORT_SYMBOL(RTRandBytes);
122
123
124RTDECL(uint32_t) RTRandU32Ex(uint32_t u32First, uint32_t u32Last) RT_NO_THROW_DEF
125{
126 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
127 return RTRandAdvU32Ex(g_hRand, u32First, u32Last);
128}
129RT_EXPORT_SYMBOL(RTRandU32Ex);
130
131
132RTDECL(uint32_t) RTRandU32(void) RT_NO_THROW_DEF
133{
134 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
135 return RTRandAdvU32(g_hRand);
136}
137RT_EXPORT_SYMBOL(RTRandU32);
138
139
140RTDECL(int32_t) RTRandS32Ex(int32_t i32First, int32_t i32Last) RT_NO_THROW_DEF
141{
142 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
143 return RTRandAdvS32Ex(g_hRand, i32First, i32Last);
144}
145RT_EXPORT_SYMBOL(RTRandS32Ex);
146
147
148RTDECL(int32_t) RTRandS32(void) RT_NO_THROW_DEF
149{
150 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
151 return RTRandAdvS32(g_hRand);
152}
153RT_EXPORT_SYMBOL(RTRandS32);
154
155
156RTDECL(uint64_t) RTRandU64Ex(uint64_t u64First, uint64_t u64Last) RT_NO_THROW_DEF
157{
158 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
159 return RTRandAdvU64Ex(g_hRand, u64First, u64Last);
160}
161RT_EXPORT_SYMBOL(RTRandU64Ex);
162
163
164RTDECL(uint64_t) RTRandU64(void) RT_NO_THROW_DEF
165{
166 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
167 return RTRandAdvU64(g_hRand);
168}
169RT_EXPORT_SYMBOL(RTRandU64);
170
171
172RTDECL(int64_t) RTRandS64Ex(int64_t i64First, int64_t i64Last) RT_NO_THROW_DEF
173{
174 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
175 return RTRandAdvS64Ex(g_hRand, i64First, i64Last);
176}
177RT_EXPORT_SYMBOL(RTRandS64Ex);
178
179
180RTDECL(int64_t) RTRandS64(void) RT_NO_THROW_DEF
181{
182 RTOnceEx(&g_rtRandOnce, rtRandInitOnce, rtRandTermOnce, NULL);
183 return RTRandAdvS32(g_hRand);
184}
185RT_EXPORT_SYMBOL(RTRandS64);
186
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette