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