-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsaveundo.c
382 lines (317 loc) · 9.57 KB
/
saveundo.c
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// $Id: saveundo.c,v 1.15 2003/10/20 16:05:06 iain Exp $
#include "git.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
typedef const git_uint8 * MemoryPage;
typedef MemoryPage * MemoryMap;
typedef struct UndoRecord UndoRecord;
struct UndoRecord
{
git_uint32 endMem;
MemoryMap memoryMap;
git_sint32 stackSize;
git_sint32 * stack;
glui32 heapSize;
glui32 * heap;
UndoRecord * prev;
UndoRecord * next;
};
static UndoRecord * gUndo = NULL;
static git_uint32 gUndoSize = 0;
static git_uint32 gMaxUndoSize = 256 * 1024;
static void reserveSpace (git_uint32);
static void deleteRecord (UndoRecord * u);
void initUndo (git_uint32 size)
{
gMaxUndoSize = size;
gUndoSize = 0;
gUndo = NULL;
}
int hasUndo ()
{
if (gUndo == NULL)
return 1;
else
return 0;
}
int saveUndo (git_sint32 * base, git_sint32 * sp)
{
git_uint32 undoSize = sizeof(UndoRecord);
git_uint32 mapSize = sizeof(MemoryPage*) * (gEndMem - gRamStart) / 256;
git_uint32 stackSize = sizeof(git_sint32) * (sp - base);
git_uint32 totalSize = undoSize + mapSize + stackSize;
git_uint32 addr = gRamStart; // Address in glulx memory.
git_uint32 slot = 0; // Slot in our memory map.
UndoRecord * undo = malloc (undoSize);
if (undo == NULL)
fatalError ("Couldn't allocate undo record");
undo->endMem = gEndMem;
undo->memoryMap = malloc (mapSize);
undo->stackSize = stackSize;
undo->stack = malloc (stackSize);
undo->prev = NULL;
undo->next = NULL;
if (undo->memoryMap == NULL || undo->stack == NULL)
fatalError ("Couldn't allocate memory for undo");
// Save the stack.
memcpy (undo->stack, base, undo->stackSize);
// Are we diffing against the previous undo record,
// or against the initial gamefile state?
if (gUndo == NULL)
{
// We're diffing against the gamefile.
for ( ; addr < gExtStart ; addr += 256, ++slot)
{
if (memcmp (gInitMem + addr, gMem + addr, 256) != 0)
{
// We need to save this page.
git_uint8 * page = malloc(256);
if (page == NULL)
fatalError ("Couldn't allocate memory for undo");
memcpy (page, gMem + addr, 256);
undo->memoryMap[slot] = page;
totalSize += 256;
}
else
{
// We don't need to save this page.
// Just make it point into ROM.
undo->memoryMap[slot] = gInitMem + addr;
}
}
// If the memory map has been extended, save the exended area
for (addr = gExtStart ; addr < gEndMem ; addr += 256, ++slot)
{
git_uint8 * page = malloc(256);
if (page == NULL)
fatalError ("Couldn't allocate memory for undo");
memcpy (page, gMem + addr, 256);
undo->memoryMap[slot] = page;
totalSize += 256;
}
}
else
{
// We're diffing against the most recent undo record.
git_uint32 endMem = (gUndo->endMem < gEndMem) ? gUndo->endMem : gEndMem;
for ( ; addr < endMem ; addr += 256, ++slot)
{
if (memcmp (gUndo->memoryMap [slot], gMem + addr, 256) != 0)
{
// We need to save this page.
git_uint8 * page = malloc(256);
memcpy (page, gMem + addr, 256);
undo->memoryMap[slot] = page;
totalSize += 256;
}
else
{
// We don't need to save this page. Just copy
// the pointer from the previous undo record.
undo->memoryMap[slot] = gUndo->memoryMap[slot];
}
}
// If the memory map has been extended, save the exended area
for (addr = endMem ; addr < gEndMem ; addr += 256, ++slot)
{
git_uint8 * page = malloc(256);
if (page == NULL)
fatalError ("Couldn't allocate memory for undo");
memcpy (page, gMem + addr, 256);
undo->memoryMap[slot] = page;
totalSize += 256;
}
}
// Save the heap.
if (heap_get_summary (&(undo->heapSize), &(undo->heap)))
fatalError ("Couldn't get heap summary");
totalSize += undo->heapSize * 4;
// Link this record into the undo list.
undo->prev = gUndo;
if (gUndo)
gUndo->next = undo;
gUndo = undo;
gUndoSize += totalSize;
// Delete old records until we have enough free space.
reserveSpace (0);
// And we're done.
return 0;
}
int restoreUndo (git_sint32* base, git_uint32 protectPos, git_uint32 protectSize)
{
if (gUndo == NULL)
{
// Nothing to undo!
return 1;
}
else
{
UndoRecord * undo = gUndo;
git_uint32 addr = gRamStart; // Address in glulx memory.
MemoryMap map = undo->memoryMap; // Glulx memory map.
// Restore the size of the memory map
heap_clear ();
resizeMemory (undo->endMem, 1);
// Restore the stack.
memcpy (base, undo->stack, undo->stackSize);
gStackPointer = base + (undo->stackSize / sizeof(git_sint32));
// Restore the contents of RAM.
if (protectSize > 0 && protectPos < gEndMem)
{
for ( ; addr < (protectPos & ~0xff) ; addr += 256, ++map)
memcpy (gMem + addr, *map, 256);
memcpy (gMem + addr, *map, protectPos & 0xff);
protectSize += protectPos & 0xff;
while (protectSize > 256)
addr += 256, ++map, protectSize -= 256;
if (addr < gEndMem)
{
memcpy (gMem + addr + protectSize,
*map + protectSize,
256 - protectSize);
}
addr += 256, ++map;
}
for ( ; addr < gEndMem ; addr += 256, ++map)
memcpy (gMem + addr, *map, 256);
// Restore the heap.
if (heap_apply_summary (undo->heapSize, undo->heap))
fatalError ("Couldn't apply heap summary");
// Delete the undo record.
gUndo = undo->prev;
deleteRecord (undo);
if (gUndo)
gUndo->next = NULL;
else
assert (gUndoSize == 0);
// And we're done.
return 0;
}
}
void discardUndo ()
{
if (gUndo == NULL)
{
// Nothing to undo!
return;
}
else
{
UndoRecord * undo = gUndo;
// Delete the undo record.
gUndo = undo->prev;
deleteRecord (undo);
if (gUndo)
gUndo->next = NULL;
else
assert (gUndoSize == 0);
// And we're done.
return;
}
}
void resetUndo ()
{
reserveSpace (gMaxUndoSize);
assert (gUndo == NULL);
assert (gUndoSize == 0);
}
void shutdownUndo ()
{
resetUndo();
}
static void reserveSpace (git_uint32 n)
{
UndoRecord * u = gUndo;
if (u == NULL)
return;
// Find the oldest undo record.
while (u->prev)
u = u->prev;
// Delete records until we've freed up the required amount of space.
while (gUndoSize + n > gMaxUndoSize)
{
if (u->next)
{
assert (u->next->prev == u);
u = u->next;
deleteRecord (u->prev);
u->prev = NULL;
}
else
{
assert (u == gUndo);
if (n > 0)
{
gUndo = NULL;
deleteRecord (u);
assert (gUndoSize == 0);
}
break;
}
}
}
static void deleteRecord (UndoRecord * u)
{
git_uint32 addr = gRamStart; // Address in glulx memory.
git_uint32 slot = 0; // Slot in our memory map.
// Zero out all the slots which are duplicates
// of pages held in older undo records.
if (u->prev)
{
// We're diffing against the previous undo record.
while (addr < u->endMem && addr < u->prev->endMem)
{
if (u->memoryMap [slot] == u->prev->memoryMap [slot])
u->memoryMap [slot] = NULL;
addr += 256, ++slot;
}
}
else
{
// We're diffing against the gamefile.
while (addr < u->endMem && addr < gExtStart)
{
if (u->memoryMap [slot] == (gInitMem + addr))
u->memoryMap [slot] = NULL;
addr += 256, ++slot;
}
}
// Zero out all the slots which are duplicates
// of newer undo records.
if (u->next)
{
addr = gRamStart;
slot = 0;
while (addr < u->endMem && addr < u->next->endMem)
{
if (u->memoryMap [slot] == u->next->memoryMap [slot])
u->memoryMap [slot] = NULL;
addr += 256, ++slot;
}
}
// Free all the slots which are owned by this record only.
addr = gRamStart;
slot = 0;
while (addr < u->endMem)
{
if (u->memoryMap [slot])
{
free ((void*) u->memoryMap [slot]);
gUndoSize -= 256;
}
addr += 256, ++slot;
}
// Free the memory map itself.
free ((void*) u->memoryMap);
gUndoSize -= sizeof(MemoryPage*) * (u->endMem - gRamStart) / 256;
// Free the stack.
free (u->stack);
gUndoSize -= u->stackSize;
// Free the heap.
free (u->heap);
gUndoSize -= u->heapSize * 4;
// Finally, free the record.
free (u);
gUndoSize -= sizeof(UndoRecord);
}