1 | /* $Id: VDIoRnd.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox HDD container test utility - I/O data generator.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2024 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 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #define LOGGROUP LOGGROUP_DEFAULT
|
---|
29 | #include <iprt/log.h>
|
---|
30 | #include <iprt/errcore.h>
|
---|
31 | #include <iprt/mem.h>
|
---|
32 | #include <iprt/rand.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 |
|
---|
35 | #include "VDIoRnd.h"
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * I/O random data generator instance data.
|
---|
39 | */
|
---|
40 | typedef struct VDIORND
|
---|
41 | {
|
---|
42 | /** Pointer to the buffer holding the random data. */
|
---|
43 | uint8_t *pbPattern;
|
---|
44 | /** Size of the buffer. */
|
---|
45 | size_t cbPattern;
|
---|
46 | /** RNG */
|
---|
47 | RTRAND hRand;
|
---|
48 | } VDIORND;
|
---|
49 |
|
---|
50 | int VDIoRndCreate(PPVDIORND ppIoRnd, size_t cbPattern, uint64_t uSeed)
|
---|
51 | {
|
---|
52 | PVDIORND pIoRnd = NULL;
|
---|
53 | int rc = VINF_SUCCESS;
|
---|
54 |
|
---|
55 | AssertPtrReturn(ppIoRnd, VERR_INVALID_POINTER);
|
---|
56 |
|
---|
57 | pIoRnd = (PVDIORND)RTMemAllocZ(sizeof(VDIORND));
|
---|
58 | if (pIoRnd)
|
---|
59 | pIoRnd->pbPattern = (uint8_t *)RTMemPageAllocZ(cbPattern);
|
---|
60 |
|
---|
61 | if ( pIoRnd
|
---|
62 | && pIoRnd->pbPattern)
|
---|
63 | {
|
---|
64 | pIoRnd->cbPattern = cbPattern;
|
---|
65 |
|
---|
66 | rc = RTRandAdvCreateParkMiller(&pIoRnd->hRand);
|
---|
67 | if (RT_SUCCESS(rc))
|
---|
68 | {
|
---|
69 | RTRandAdvSeed(pIoRnd->hRand, uSeed);
|
---|
70 | RTRandAdvBytes(pIoRnd->hRand, pIoRnd->pbPattern, cbPattern);
|
---|
71 | }
|
---|
72 | else
|
---|
73 | {
|
---|
74 | RTMemPageFree(pIoRnd->pbPattern, cbPattern);
|
---|
75 | RTMemFree(pIoRnd);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | else
|
---|
79 | {
|
---|
80 | if (pIoRnd)
|
---|
81 | RTMemFree(pIoRnd);
|
---|
82 | rc = VERR_NO_MEMORY;
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (RT_SUCCESS(rc))
|
---|
86 | *ppIoRnd = pIoRnd;
|
---|
87 |
|
---|
88 | return rc;
|
---|
89 | }
|
---|
90 |
|
---|
91 | void VDIoRndDestroy(PVDIORND pIoRnd)
|
---|
92 | {
|
---|
93 | AssertPtrReturnVoid(pIoRnd);
|
---|
94 |
|
---|
95 | RTRandAdvDestroy(pIoRnd->hRand);
|
---|
96 | RTMemPageFree(pIoRnd->pbPattern, pIoRnd->cbPattern);
|
---|
97 | RTMemFree(pIoRnd);
|
---|
98 | }
|
---|
99 |
|
---|
100 | int VDIoRndGetBuffer(PVDIORND pIoRnd, void **ppv, size_t cb)
|
---|
101 | {
|
---|
102 | AssertPtrReturn(pIoRnd, VERR_INVALID_POINTER);
|
---|
103 | AssertPtrReturn(ppv, VERR_INVALID_POINTER);
|
---|
104 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
105 |
|
---|
106 | if (cb > pIoRnd->cbPattern - 512)
|
---|
107 | return VERR_INVALID_PARAMETER;
|
---|
108 |
|
---|
109 | *ppv = pIoRnd->pbPattern + RT_ALIGN_64(RTRandAdvU64Ex(pIoRnd->hRand, 0, pIoRnd->cbPattern - cb - 512), 512);
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | uint32_t VDIoRndGetU32Ex(PVDIORND pIoRnd, uint32_t uMin, uint32_t uMax)
|
---|
115 | {
|
---|
116 | return RTRandAdvU32Ex(pIoRnd->hRand, uMin, uMax);
|
---|
117 | }
|
---|
118 |
|
---|