1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox HDD container test utility - scripting engine, internal stack implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-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 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef VBOX_INCLUDED_SRC_testcase_VDScriptStack_h
|
---|
29 | #define VBOX_INCLUDED_SRC_testcase_VDScriptStack_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <iprt/list.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 |
|
---|
37 | #include "VDScript.h"
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Stack structure.
|
---|
41 | */
|
---|
42 | typedef struct VDSCRIPTSTACK
|
---|
43 | {
|
---|
44 | /** Size of one stack element. */
|
---|
45 | size_t cbStackEntry;
|
---|
46 | /** Stack memory. */
|
---|
47 | void *pvStack;
|
---|
48 | /** Number of elements on the stack. */
|
---|
49 | unsigned cOnStack;
|
---|
50 | /** Maximum number of elements the stack can hold. */
|
---|
51 | unsigned cOnStackMax;
|
---|
52 | } VDSCRIPTSTACK;
|
---|
53 | /** Pointer to a stack. */
|
---|
54 | typedef VDSCRIPTSTACK *PVDSCRIPTSTACK;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Init the stack structure.
|
---|
58 | *
|
---|
59 | * @returns nothing.
|
---|
60 | * @param pStack The stack to initialize.
|
---|
61 | * @param cbStackEntry The size of one stack entry.
|
---|
62 | */
|
---|
63 | DECLINLINE(void) vdScriptStackInit(PVDSCRIPTSTACK pStack, size_t cbStackEntry)
|
---|
64 | {
|
---|
65 | pStack->cbStackEntry = cbStackEntry;
|
---|
66 | pStack->pvStack = NULL;
|
---|
67 | pStack->cOnStack = 0;
|
---|
68 | pStack->cOnStackMax = 0;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Destroys the given stack freeing all memory.
|
---|
73 | *
|
---|
74 | * @returns nothing.
|
---|
75 | * @param pStack The stack to destroy.
|
---|
76 | */
|
---|
77 | DECLINLINE(void) vdScriptStackDestroy(PVDSCRIPTSTACK pStack)
|
---|
78 | {
|
---|
79 | if (pStack->pvStack)
|
---|
80 | RTMemFree(pStack->pvStack);
|
---|
81 | pStack->cbStackEntry = 0;
|
---|
82 | pStack->pvStack = NULL;
|
---|
83 | pStack->cOnStack = 0;
|
---|
84 | pStack->cOnStackMax = 0;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Gets the topmost unused stack entry.
|
---|
89 | *
|
---|
90 | * @returns Pointer to the first unused entry.
|
---|
91 | * NULL if there is no room left and increasing the stack failed.
|
---|
92 | * @param pStack The stack.
|
---|
93 | */
|
---|
94 | DECLINLINE(void *)vdScriptStackGetUnused(PVDSCRIPTSTACK pStack)
|
---|
95 | {
|
---|
96 | void *pvElem = NULL;
|
---|
97 |
|
---|
98 | if (pStack->cOnStack >= pStack->cOnStackMax)
|
---|
99 | {
|
---|
100 | unsigned cOnStackMaxNew = pStack->cOnStackMax + 10;
|
---|
101 | void *pvStackNew = NULL;
|
---|
102 |
|
---|
103 | /* Try to increase stack space. */
|
---|
104 | pvStackNew = RTMemRealloc(pStack->pvStack, cOnStackMaxNew * pStack->cbStackEntry);
|
---|
105 | if (pvStackNew)
|
---|
106 | {
|
---|
107 | pStack->pvStack = pvStackNew;
|
---|
108 | pStack->cOnStackMax = cOnStackMaxNew;
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (pStack->cOnStack < pStack->cOnStackMax)
|
---|
114 | pvElem = (char *)pStack->pvStack + pStack->cOnStack * pStack->cbStackEntry;
|
---|
115 |
|
---|
116 | return pvElem;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Gets the topmost used entry on the stack.
|
---|
121 | *
|
---|
122 | * @returns Pointer to the first used entry
|
---|
123 | * or NULL if the stack is empty.
|
---|
124 | * @param pStack The stack.
|
---|
125 | */
|
---|
126 | DECLINLINE(void *)vdScriptStackGetUsed(PVDSCRIPTSTACK pStack)
|
---|
127 | {
|
---|
128 | if (!pStack->cOnStack)
|
---|
129 | return NULL;
|
---|
130 | else
|
---|
131 | return (char *)pStack->pvStack + (pStack->cOnStack - 1) * pStack->cbStackEntry;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Increases the used element count for the given stack.
|
---|
136 | *
|
---|
137 | * @returns nothing.
|
---|
138 | * @param pStack The stack.
|
---|
139 | */
|
---|
140 | DECLINLINE(void) vdScriptStackPush(PVDSCRIPTSTACK pStack)
|
---|
141 | {
|
---|
142 | pStack->cOnStack++;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Decreases the used element count for the given stack.
|
---|
147 | *
|
---|
148 | * @returns nothing.
|
---|
149 | * @param pStack The stack.
|
---|
150 | */
|
---|
151 | DECLINLINE(void) vdScriptStackPop(PVDSCRIPTSTACK pStack)
|
---|
152 | {
|
---|
153 | pStack->cOnStack--;
|
---|
154 | }
|
---|
155 |
|
---|
156 | #endif /* !VBOX_INCLUDED_SRC_testcase_VDScriptStack_h */
|
---|