-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryLogItemCollection.cs
243 lines (200 loc) · 6.4 KB
/
MemoryLogItemCollection.cs
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Collections;
namespace MemoryPlayer
{
/// <summary>
/// Collection of all memory log items
/// </summary>
public class MemoryLogItemCollection : IEnumerable
{
// public MemoryLogItemCollection( MemoryViewControl viewControl )
public MemoryLogItemCollection()
{
mMemoryLogItems = new ArrayList();
mCategories = new CategoryManager(); // new CategoryManager( viewControl );
Clear();
}
public void Add(MemoryLogItem newItem)
{
mMemoryLogItems.Add(newItem);
newItem.Category = mCategories.AssignCategory( newItem );
// Attempt to match a Free with it's Alloc
if ( newItem.Type == MemoryLogItem.ItemType.Free )
{
NameFreeBlock();
}
if (mEndTick < newItem.Time)
{
mEndTick = newItem.Time;
}
mNumOperations++;
if (newItem.Type != MemoryLogItem.ItemType.Label &&
newItem.Type != MemoryLogItem.ItemType.Size )
{
if (mMemoryAddressStart > newItem.Location)
{
mMemoryAddressStart = newItem.Location;
}
if (mMemoryAddressEnd < (newItem.Location + newItem.Size))
{
mMemoryAddressEnd = Convert.ToUInt32(newItem.Location + newItem.Size);
}
mMemorySize = mMemoryAddressEnd - mMemoryAddressStart;
}
}
public int FindMatchingFree( int index )
{
MemoryLogItem allocItem = (MemoryLogItem)mMemoryLogItems[ index ];
for ( int i = index + 1; i < mMemoryLogItems.Count; i++ )
{
MemoryLogItem item = (MemoryLogItem)mMemoryLogItems[ i ];
if ( item.Location == allocItem.Location )
{
return i;
}
}
// didn't find it, return -1
return -1;
}
public void NameFreeBlock()
{
int count = mMemoryLogItems.Count;
MemoryLogItem Item = (MemoryLogItem)mMemoryLogItems[count-1];
if ((count >= 2) && (Item.Type == MemoryLogItem.ItemType.Free))
{
for (int index = count-2; index>=0; index--)
{
MemoryLogItem PrevItem = (MemoryLogItem)mMemoryLogItems[index];
if ( (PrevItem.Type == MemoryLogItem.ItemType.Alloc) && (PrevItem.Location == Item.Location))
{
Item.SetName(PrevItem.Name);
Item.Pair = PrevItem;
PrevItem.Pair = Item;
break;
}
}
}
}
public void Clear()
{
mMemoryLogItems.Clear();
mEndTick = 0;
mNumOperations = 0;
mMemoryAddressStart = uint.MaxValue;
mMemoryAddressEnd = 0;
mMemorySize = 0;
}
public int Count
{
get
{
return mMemoryLogItems.Count;
}
}
public MemoryLogItem GetItem(int index)
{
return (MemoryLogItem)mMemoryLogItems[index];
}
public int NumOperations
{
get
{
return mNumOperations;
}
}
public long MemoryStart
{
get
{
return mMemoryAddressStart;
}
}
public long MemoryEnd
{
get
{
return mMemoryAddressEnd;
}
}
public long MemorySize
{
get
{
return mMemorySize;
}
}
public CategoryManager Categories
{
get
{
return mCategories;
}
}
private CategoryManager mCategories;
private ArrayList mMemoryLogItems;
/// <summary>
/// Last Tick for an allocation. This defines the time span for the run.
/// </summary>
private int mEndTick;
/// <summary>
/// Total number of allocations and frees. Does not include labels
/// </summary>
private int mNumOperations;
/// <summary>
/// The lowest address of the allocations
/// </summary>
private uint mMemoryAddressStart;
/// <summary>
/// The highest (address + size). The end of the visible memory.
/// </summary>
private uint mMemoryAddressEnd;
/// <summary>
/// The total size of the logged memory space
/// </summary>
private uint mMemorySize;
public virtual IEnumerator GetEnumerator()
{
return new MemoryLogItemCollection.Enumerator(this);
}
public class Enumerator : IEnumerator
{
MemoryLogItemCollection outer;
int currentIndex = -1;
internal Enumerator(MemoryLogItemCollection outer)
{
this.outer = outer;
}
public object Current
{
get
{
if (currentIndex >= outer.mMemoryLogItems.Count) currentIndex = outer.mMemoryLogItems.Count - 1;
if (currentIndex < 0) currentIndex = 0;
return outer.mMemoryLogItems[currentIndex];
}
}
public bool MoveNext()
{
if (currentIndex >= outer.mMemoryLogItems.Count) currentIndex = outer.mMemoryLogItems.Count - 1;
return ++currentIndex < outer.mMemoryLogItems.Count;
}
public bool MovePrevious()
{
if (currentIndex < 0) currentIndex = 0;
return --currentIndex >= 0;
}
public bool IsEnd()
{
return currentIndex >= outer.mMemoryLogItems.Count - 1;
}
public bool IsStart()
{
return currentIndex <= 0;
}
public void Reset()
{
currentIndex = -1;
}
}
}
}