Debian Codegraph @

terps/git/saveundo.c

raw symbols SCIP
1 // $Id: saveundo.c,v 1.15 2003/10/20 16:05:06 iain Exp $
2
3 #include "git.h"
4 #include <stdlib.h>
5 #include <string.h>
6 #include <assert.h>
7
8 typedef const git_uint8 * MemoryPage;
9 typedef MemoryPage * MemoryMap;
10
11 typedef struct UndoRecord UndoRecord;
12
13 struct UndoRecord
14 {
15 git_uint32 endMem;
16 MemoryMap memoryMap;
17 git_sint32 stackSize;
18 git_sint32 * stack;
19 glui32 heapSize;
20 glui32 * heap;
21 UndoRecord * prev;
22 UndoRecord * next;
23 };
24
25 static UndoRecord * gUndo = NULL;
26 static git_uint32 gUndoSize = 0;
27 static git_uint32 gMaxUndoSize = 256 * 1024;
28
29 static void reserveSpace (git_uint32);
30 static void deleteRecord (UndoRecord * u);
31
32 void initUndo (git_uint32 size)
33 {
34 gMaxUndoSize = size;
35 gUndoSize = 0;
36 gUndo = NULL;
37 }
38
39 int hasUndo ()
40 {
41 if (gUndo == NULL)
42 return 1;
43 else
44 return 0;
45 }
46
47 int saveUndo (git_sint32 * base, git_sint32 * sp)
48 {
49 git_uint32 undoSize = sizeof(UndoRecord);
50 git_uint32 mapSize = sizeof(MemoryPage*) * (gEndMem - gRamStart) / 256;
51 git_uint32 stackSize = sizeof(git_sint32) * (sp - base);
52 git_uint32 totalSize = undoSize + mapSize + stackSize;
53
54 git_uint32 addr = gRamStart; // Address in glulx memory.
55 git_uint32 slot = 0; // Slot in our memory map.
56
57 UndoRecord * undo = malloc (undoSize);
58 if (undo == NULL)
59 fatalError ("Couldn't allocate undo record");
60
61 undo->endMem = gEndMem;
62 undo->memoryMap = malloc (mapSize);
63 undo->stackSize = stackSize;
64 undo->stack = malloc (stackSize);
65 undo->prev = NULL;
66 undo->next = NULL;
67
68 if (undo->memoryMap == NULL || undo->stack == NULL)
69 fatalError ("Couldn't allocate memory for undo");
70
71 // Save the stack.
72 memcpy (undo->stack, base, undo->stackSize);
73
74 // Are we diffing against the previous undo record,
75 // or against the initial gamefile state?
76 if (gUndo == NULL)
77 {
78 // We're diffing against the gamefile.
79 for ( ; addr < gExtStart ; addr += 256, ++slot)
80 {
81 if (memcmp (gInitMem + addr, gMem + addr, 256) != 0)
82 {
83 // We need to save this page.
84 git_uint8 * page = malloc(256);
85 if (page == NULL)
86 fatalError ("Couldn't allocate memory for undo");
87
88 memcpy (page, gMem + addr, 256);
89 undo->memoryMap[slot] = page;
90 totalSize += 256;
91 }
92 else
93 {
94 // We don't need to save this page.
95 // Just make it point into ROM.
96 undo->memoryMap[slot] = gInitMem + addr;
97 }
98 }
99
100 // If the memory map has been extended, save the exended area
101 for (addr = gExtStart ; addr < gEndMem ; addr += 256, ++slot)
102 {
103 git_uint8 * page = malloc(256);
104 if (page == NULL)
105 fatalError ("Couldn't allocate memory for undo");
106
107 memcpy (page, gMem + addr, 256);
108 undo->memoryMap[slot] = page;
109 totalSize += 256;
110 }
111 }
112 else
113 {
114 // We're diffing against the most recent undo record.
115 git_uint32 endMem = (gUndo->endMem < gEndMem) ? gUndo->endMem : gEndMem;
116 for ( ; addr < endMem ; addr += 256, ++slot)
117 {
118 if (memcmp (gUndo->memoryMap [slot], gMem + addr, 256) != 0)
119 {
120 // We need to save this page.
121 git_uint8 * page = malloc(256);
122 memcpy (page, gMem + addr, 256);
123 undo->memoryMap[slot] = page;
124 totalSize += 256;
125 }
126 else
127 {
128 // We don't need to save this page. Just copy
129 // the pointer from the previous undo record.
130 undo->memoryMap[slot] = gUndo->memoryMap[slot];
131 }
132 }
133
134 // If the memory map has been extended, save the exended area
135 for (addr = endMem ; addr < gEndMem ; addr += 256, ++slot)
136 {
137 git_uint8 * page = malloc(256);
138 if (page == NULL)
139 fatalError ("Couldn't allocate memory for undo");
140
141 memcpy (page, gMem + addr, 256);
142 undo->memoryMap[slot] = page;
143 totalSize += 256;
144 }
145 }
146
147 // Save the heap.
148 if (heap_get_summary (&(undo->heapSize), &(undo->heap)))
149 fatalError ("Couldn't get heap summary");
150 totalSize += undo->heapSize * 4;
151
152 // Link this record into the undo list.
153
154 undo->prev = gUndo;
155 if (gUndo)
156 gUndo->next = undo;
157
158 gUndo = undo;
159 gUndoSize += totalSize;
160
161 // Delete old records until we have enough free space.
162 reserveSpace (0);
163
164 // And we're done.
165 return 0;
166 }
167
168 int restoreUndo (git_sint32* base, git_uint32 protectPos, git_uint32 protectSize)
169 {
170 if (gUndo == NULL)
171 {
172 // Nothing to undo!
173 return 1;
174 }
175 else
176 {
177 UndoRecord * undo = gUndo;
178
179 git_uint32 addr = gRamStart; // Address in glulx memory.
180 MemoryMap map = undo->memoryMap; // Glulx memory map.
181
182 // Restore the size of the memory map
183 heap_clear ();
184 resizeMemory (undo->endMem, 1);
185
186 // Restore the stack.
187 memcpy (base, undo->stack, undo->stackSize);
188 gStackPointer = base + (undo->stackSize / sizeof(git_sint32));
189
190 // Restore the contents of RAM.
191
192 if (protectSize > 0 && protectPos < gEndMem)
193 {
194 for ( ; addr < (protectPos & ~0xff) ; addr += 256, ++map)
195 memcpy (gMem + addr, *map, 256);
196
197 memcpy (gMem + addr, *map, protectPos & 0xff);
198 protectSize += protectPos & 0xff;
199
200 while (protectSize > 256)
201 addr += 256, ++map, protectSize -= 256;
202
203 if (addr < gEndMem)
204 {
205 memcpy (gMem + addr + protectSize,
206 *map + protectSize,
207 256 - protectSize);
208 }
209 addr += 256, ++map;
210 }
211
212 for ( ; addr < gEndMem ; addr += 256, ++map)
213 memcpy (gMem + addr, *map, 256);
214
215 // Restore the heap.
216 if (heap_apply_summary (undo->heapSize, undo->heap))
217 fatalError ("Couldn't apply heap summary");
218
219 // Delete the undo record.
220
221 gUndo = undo->prev;
222 deleteRecord (undo);
223
224 if (gUndo)
225 gUndo->next = NULL;
226 else
227 assert (gUndoSize == 0);
228
229 // And we're done.
230 return 0;
231 }
232 }
233
234 void discardUndo ()
235 {
236 if (gUndo == NULL)
237 {
238 // Nothing to undo!
239 return;
240 }
241 else
242 {
243 UndoRecord * undo = gUndo;
244 // Delete the undo record.
245 gUndo = undo->prev;
246 deleteRecord (undo);
247
248 if (gUndo)
249 gUndo->next = NULL;
250 else
251 assert (gUndoSize == 0);
252
253 // And we're done.
254 return;
255 }
256 }
257
258 void resetUndo ()
259 {
260 reserveSpace (gMaxUndoSize);
261 assert (gUndo == NULL);
262 assert (gUndoSize == 0);
263 }
264
265 void shutdownUndo ()
266 {
267 resetUndo();
268 }
269
270 static void reserveSpace (git_uint32 n)
271 {
272 UndoRecord * u = gUndo;
273 if (u == NULL)
274 return;
275
276 // Find the oldest undo record.
277
278 while (u->prev)
279 u = u->prev;
280
281 // Delete records until we've freed up the required amount of space.
282
283 while (gUndoSize + n > gMaxUndoSize)
284 {
285 if (u->next)
286 {
287 assert (u->next->prev == u);
288 u = u->next;
289
290 deleteRecord (u->prev);
291 u->prev = NULL;
292 }
293 else
294 {
295 assert (u == gUndo);
296 if (n > 0)
297 {
298 gUndo = NULL;
299
300 deleteRecord (u);
301 assert (gUndoSize == 0);
302 }
303 break;
304 }
305 }
306 }
307
308 static void deleteRecord (UndoRecord * u)
309 {
310 git_uint32 addr = gRamStart; // Address in glulx memory.
311 git_uint32 slot = 0; // Slot in our memory map.
312
313 // Zero out all the slots which are duplicates
314 // of pages held in older undo records.
315
316 if (u->prev)
317 {
318 // We're diffing against the previous undo record.
319 while (addr < u->endMem && addr < u->prev->endMem)
320 {
321 if (u->memoryMap [slot] == u->prev->memoryMap [slot])
322 u->memoryMap [slot] = NULL;
323 addr += 256, ++slot;
324 }
325 }
326 else
327 {
328 // We're diffing against the gamefile.
329 while (addr < u->endMem && addr < gExtStart)
330 {
331 if (u->memoryMap [slot] == (gInitMem + addr))
332 u->memoryMap [slot] = NULL;
333 addr += 256, ++slot;
334 }
335 }
336
337 // Zero out all the slots which are duplicates
338 // of newer undo records.
339
340 if (u->next)
341 {
342 addr = gRamStart;
343 slot = 0;
344
345 while (addr < u->endMem && addr < u->next->endMem)
346 {
347 if (u->memoryMap [slot] == u->next->memoryMap [slot])
348 u->memoryMap [slot] = NULL;
349 addr += 256, ++slot;
350 }
351 }
352
353 // Free all the slots which are owned by this record only.
354
355 addr = gRamStart;
356 slot = 0;
357 while (addr < u->endMem)
358 {
359 if (u->memoryMap [slot])
360 {
361 free ((void*) u->memoryMap [slot]);
362 gUndoSize -= 256;
363 }
364 addr += 256, ++slot;
365 }
366
367 // Free the memory map itself.
368 free ((void*) u->memoryMap);
369 gUndoSize -= sizeof(MemoryPage*) * (u->endMem - gRamStart) / 256;
370
371 // Free the stack.
372 free (u->stack);
373 gUndoSize -= u->stackSize;
374
375 // Free the heap.
376 free (u->heap);
377 gUndoSize -= u->heapSize * 4;
378
379 // Finally, free the record.
380 free (u);
381 gUndoSize -= sizeof(UndoRecord);
382 }