1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
#include "dstack.hpp"
#include <algorithm>
namespace SpasmImpl
{
Dstack::Dstack() : bottom(NULL), tos(NULL), s_size(0) {}
Dstack::Dstack(const Dstack& ds) : s_size(ds.s_size)
{
copy_dstack(ds);
}
Dstack& Dstack::operator=(const Dstack& ds)
{
if (this != &ds)
{
if (bottom)
delete[] bottom;
s_size = ds.s_size;
copy_dstack(ds);
}
return *this;
}
Dstack::~Dstack()
{
if (bottom)
delete[] bottom;
}
/**
* Makes the stack occupy new_size bytes
* \param new_size new size for the stack
*/
void Dstack::reserve(size_t new_size)
{
size_t offset = tos - bottom;
byte* new_bottom = new byte[new_size];
if (bottom)
{
std::copy(bottom, bottom + std::min(s_size, new_size), new_bottom);
delete[] bottom;
}
bottom = new_bottom;
tos = new_bottom + offset;
s_size = new_size;
}
/*!
** Pushes x in the stack.
** If the stack is full its size is doubled.
** \param x data_t object to be pushed on the stack
*/
void Dstack::push(data_t x)
{
if (bottom + s_size < tos + sizeof(data_t))
reserve((s_size ? s_size : sizeof(data_t)) * 2);
*((data_t*)tos) = x;
tos += sizeof(data_t);
}
/*!
** Pushes the object of size x_size bytes pointed by x in the stack.
** If the stack is full its size is doubled (or more is x_size is greater
** than the size of the stack).
** \param x pointer to the element to be pushed on the stack
** \param x_size size of the element
*/
void Dstack::push(byte* x, size_t x_size)
{
if (bottom + s_size < tos + x_size)
reserve(s_size + ((s_size > x_size) ? s_size : x_size));
std::copy(x, x + x_size, tos);
tos += x_size;
}
/*!
** Pops an object of size x_size from the stack.
** The object is written to the place pointed by x.
**
** \param x pointer to the place for popping
** \param x_size size of the object to be popped
*/
void Dstack::pop(void* x, size_t x_size)
{
if (tos - x_size >= bottom)
{
std::copy(tos - x_size, tos, (byte*)x);
tos -= x_size;
}
}
/*!
** \return a single data_t object, popping it from the stack.
*/
data_t Dstack::pop()
{
data_t x{};
if (tos - sizeof(data_t) >= bottom)
{
tos -= sizeof(data_t);
x = *((data_t*)tos);
}
return x;
}
/*!
** \return copy of the data_t object that is on top of the stack
*/
data_t Dstack::top() const
{
data_t x{};
if (tos - sizeof(data_t) >= bottom)
{
x = *((data_t*)(tos - sizeof(data_t)));
}
return x;
}
/*!
** \return the size of the stack in bytes
*/
size_t Dstack::size() const
{
return tos - bottom;
}
/*!
** \return the space reserved for the stack in bytes
*/
size_t Dstack::reserved() const
{
return s_size;
}
/*!
** Frees unused memory. Note that it is not always possible because memory
** management is done with new / delete
*/
void Dstack::strip_memory()
{
reserve(size());
}
/*!
** \return true if the stack is empty
*/
bool Dstack::empty() const
{
return tos - bottom == 0;
}
/*!
** Creates a copy of ds
**
** \param ds data stack to be copied
*/
void Dstack::copy_dstack(const Dstack& ds)
{
if (ds.bottom)
{
bottom = new byte[ds.s_size];
tos = bottom + (ds.tos - ds.bottom);
std::copy(ds.bottom, ds.tos, bottom);
}
else
bottom = tos = NULL;
}
} // namespace SpasmImpl
|