forked from uncrustify/uncrustify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChunkStack.cpp
More file actions
174 lines (147 loc) · 2.6 KB
/
ChunkStack.cpp
File metadata and controls
174 lines (147 loc) · 2.6 KB
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
173
174
/**
* @file ChunkStack.cpp
* Manages a chunk stack
*
* @author Ben Gardner
* @license GPL v2+
*/
#include "ChunkStack.h"
#include <cstdio>
#include <cstdlib>
ChunkStack::ChunkStack(const ChunkStack& cs)
{
Set(cs);
}
ChunkStack::~ChunkStack()
{
if (m_cse != NULL)
{
free(m_cse);
m_cse = NULL;
m_size = m_len = 0;
}
}
void ChunkStack::Set(const ChunkStack& cs)
{
Init();
Resize(cs.m_len);
for (int idx = 0; idx < cs.m_len; idx++)
{
Push(cs.m_cse[idx].m_pc, cs.m_cse[idx].m_seqnum);
}
m_seqnum = cs.m_seqnum;
}
const ChunkStack::Entry *ChunkStack::Top() const
{
if (m_len > 0)
{
return(&m_cse[m_len - 1]);
}
return(NULL);
}
const ChunkStack::Entry *ChunkStack::Get(int idx) const
{
if ((idx < m_len) && (idx >= 0))
{
return(&m_cse[idx]);
}
return(NULL);
}
chunk_t *ChunkStack::GetChunk(int idx) const
{
if ((idx < m_len) && (idx >= 0))
{
return(m_cse[idx].m_pc);
}
return(NULL);
}
chunk_t *ChunkStack::Pop()
{
if (m_len > 0)
{
m_len--;
return(m_cse[m_len].m_pc);
}
return(NULL);
}
void ChunkStack::Push(chunk_t *pc, int seqnum)
{
if (m_len >= m_size)
{
Resize(m_len + 64);
}
m_cse[m_len].m_pc = pc;
m_cse[m_len].m_seqnum = seqnum;
m_len++;
if (m_seqnum < seqnum)
{
m_seqnum = seqnum;
}
}
void ChunkStack::Init()
{
m_cse = NULL;
m_size = 0;
m_len = 0;
m_seqnum = 0;
}
void ChunkStack::Resize(int newsize)
{
if (m_size < newsize)
{
m_size = newsize;
m_cse = (Entry *)realloc(m_cse, m_size * sizeof(ChunkStack::Entry));
assert(m_cse != NULL);
/*TODO: check for out-of-memory? */
}
}
/**
* Mark an entry to be removed by Collapse()
*
* @param idx The item to remove
*/
void ChunkStack::Zap(int idx)
{
if ((idx < m_len) && (idx >= 0))
{
assert(m_cse != NULL);
m_cse[idx].m_pc = NULL;
}
}
/**
* Compresses down the stack by removing dead entries
*/
void ChunkStack::Collapse()
{
int oldlen = m_len;
m_len = 0;
for (int idx = 0; idx < oldlen; idx++)
{
assert(m_cse != NULL);
if (m_cse[idx].m_pc != NULL)
{
m_cse[m_len].m_pc = m_cse[idx].m_pc;
m_cse[m_len].m_seqnum = m_cse[idx].m_seqnum;
m_len++;
}
}
}
//
//int main(int argc, char **argv)
//{
// ChunkStack cs;
//
// cs.Push((chunk_t *)1);
// cs.Push((chunk_t *)2);
// cs.Push((chunk_t *)3);
// cs.Push((chunk_t *)4);
//
// while (!cs.Empty())
// {
// chunk_t *pc = cs.Pop();
// printf("pc = %p\n", pc);
// }
//
// return (0);
//}
//