1 | /* $Id: VDIoRnd.cpp 36529 2011-04-04 13:54:13Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox HDD container test utility - I/O data generator.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #define LOGGROUP LOGGROUP_DEFAULT
|
---|
19 | #include <iprt/log.h>
|
---|
20 | #include <iprt/err.h>
|
---|
21 | #include <iprt/mem.h>
|
---|
22 | #include <iprt/rand.h>
|
---|
23 | #include <iprt/assert.h>
|
---|
24 |
|
---|
25 | #include "VDIoRnd.h"
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * I/O random data generator instance data.
|
---|
29 | */
|
---|
30 | typedef struct VDIORND
|
---|
31 | {
|
---|
32 | /** Pointer to the buffer holding the random data. */
|
---|
33 | uint8_t *pbPattern;
|
---|
34 | /** Size of the buffer. */
|
---|
35 | size_t cbPattern;
|
---|
36 | /** RNG */
|
---|
37 | RTRAND hRand;
|
---|
38 | } VDIORND;
|
---|
39 |
|
---|
40 | int VDIoRndCreate(PPVDIORND ppIoRnd, size_t cbPattern, uint64_t uSeed)
|
---|
41 | {
|
---|
42 | PVDIORND pIoRnd = NULL;
|
---|
43 | int rc = VINF_SUCCESS;
|
---|
44 |
|
---|
45 | AssertPtrReturn(ppIoRnd, VERR_INVALID_POINTER);
|
---|
46 |
|
---|
47 | pIoRnd = (PVDIORND)RTMemAllocZ(sizeof(VDIORND));
|
---|
48 | if (pIoRnd)
|
---|
49 | pIoRnd->pbPattern = (uint8_t *)RTMemPageAllocZ(cbPattern);
|
---|
50 |
|
---|
51 | if ( pIoRnd
|
---|
52 | && pIoRnd->pbPattern)
|
---|
53 | {
|
---|
54 | pIoRnd->cbPattern = cbPattern;
|
---|
55 |
|
---|
56 | rc = RTRandAdvCreateParkMiller(&pIoRnd->hRand);
|
---|
57 | if (RT_SUCCESS(rc))
|
---|
58 | {
|
---|
59 | RTRandAdvSeed(pIoRnd->hRand, uSeed);
|
---|
60 | RTRandAdvBytes(pIoRnd->hRand, pIoRnd->pbPattern, cbPattern);
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | RTMemPageFree(pIoRnd->pbPattern, cbPattern);
|
---|
65 | RTMemFree(pIoRnd);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | else
|
---|
69 | {
|
---|
70 | if (pIoRnd)
|
---|
71 | RTMemFree(pIoRnd);
|
---|
72 | rc = VERR_NO_MEMORY;
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (RT_SUCCESS(rc))
|
---|
76 | *ppIoRnd = pIoRnd;
|
---|
77 |
|
---|
78 | return rc;
|
---|
79 | }
|
---|
80 |
|
---|
81 | void VDIoRndDestroy(PVDIORND pIoRnd)
|
---|
82 | {
|
---|
83 | AssertPtrReturnVoid(pIoRnd);
|
---|
84 |
|
---|
85 | RTRandAdvDestroy(pIoRnd->hRand);
|
---|
86 | RTMemPageFree(pIoRnd->pbPattern, pIoRnd->cbPattern);
|
---|
87 | RTMemFree(pIoRnd);
|
---|
88 | }
|
---|
89 |
|
---|
90 | int VDIoRndGetBuffer(PVDIORND pIoRnd, void **ppv, size_t cb)
|
---|
91 | {
|
---|
92 | AssertPtrReturn(pIoRnd, VERR_INVALID_POINTER);
|
---|
93 | AssertPtrReturn(ppv, VERR_INVALID_POINTER);
|
---|
94 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
95 |
|
---|
96 | if (cb > pIoRnd->cbPattern - 512)
|
---|
97 | return VERR_INVALID_PARAMETER;
|
---|
98 |
|
---|
99 | *ppv = pIoRnd->pbPattern + RT_ALIGN_64(RTRandAdvU64Ex(pIoRnd->hRand, 0, pIoRnd->cbPattern - cb - 512), 512);
|
---|
100 | return VINF_SUCCESS;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | uint32_t VDIoRndGetU32Ex(PVDIORND pIoRnd, uint32_t uMin, uint32_t uMax)
|
---|
105 | {
|
---|
106 | return RTRandAdvU32Ex(pIoRnd->hRand, uMin, uMax);
|
---|
107 | }
|
---|
108 |
|
---|