Debian Codegraph @

terps/scare/scmemos.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o ...
24 */
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "scare.h"
31 #include "scprotos.h"
32
33
34 /* Assorted definitions and constants. */
35 static const sc_uint MEMENTO_MAGIC = 0x9fd33d1d;
36 enum { MEMO_ALLOCATION_BLOCK = 32 };
37
38 /*
39 * Game memo structure, saves a serialized game. Allocation is preserved so
40 * that structures can be reused without requiring reallocation.
41 */
42 typedef struct sc_memo_s
43 {
44 sc_byte *serialized_game;
45 sc_int allocation;
46 sc_int length;
47 } sc_memo_t;
48 typedef sc_memo_t *sc_memoref_t;
49
50 /*
51 * Game command history structure, similar to a memo. Saves a player input
52 * command to create a history, reusing allocation where possible.
53 */
54 typedef struct sc_history_s
55 {
56 sc_char *command;
57 sc_int sequence;
58 sc_int timestamp;
59 sc_int turns;
60 sc_int allocation;
61 sc_int length;
62 } sc_history_t;
63 typedef sc_history_t *sc_historyref_t;
64
65 /*
66 * Memo set structure. This reserves space for a predetermined number of
67 * serialized games, and an indicator cursor showing where additions are
68 * placed. The structure is a ring, with old elements being overwritten by
69 * newer arrivals. Also tacked onto this structure is a set of strings
70 * used to hold a command history that operates in a somewhat csh-like way,
71 * also a ring with limited capacity.
72 */
73 enum { MEMO_UNDO_TABLE_SIZE = 16, MEMO_HISTORY_TABLE_SIZE = 64 };
74 typedef struct sc_memo_set_s
75 {
76 sc_uint magic;
77 sc_memo_t memo[MEMO_UNDO_TABLE_SIZE];
78 sc_int memo_cursor;
79
80 sc_history_t history[MEMO_HISTORY_TABLE_SIZE];
81 sc_int history_count;
82 sc_int current_history;
83 sc_bool is_at_start;
84 } sc_memo_set_t;
85
86
87 /*
88 * memo_is_valid()
89 *
90 * Return TRUE if pointer is a valid memo set, FALSE otherwise.
91 */
92 static sc_bool
93 memo_is_valid (sc_memo_setref_t memento)
94 {
95 return memento && memento->magic == MEMENTO_MAGIC;
96 }
97
98
99 /*
100 * memo_round_up()
101 *
102 * Round up an allocation in bytes to the next allocation block.
103 */
104 static sc_int
105 memo_round_up (sc_int allocation)
106 {
107 sc_int extended;
108
109 extended = allocation + MEMO_ALLOCATION_BLOCK - 1;
110 return (extended / MEMO_ALLOCATION_BLOCK) * MEMO_ALLOCATION_BLOCK;
111 }
112
113
114 /*
115 * memo_create()
116 *
117 * Create and return a new set of memos.
118 */
119 sc_memo_setref_t
120 memo_create (void)
121 {
122 sc_memo_setref_t memento;
123
124 /* Create and initialize a clean set of memos. */
125 memento = sc_malloc (sizeof (*memento));
126 memento->magic = MEMENTO_MAGIC;
127
128 memset (memento->memo, 0, sizeof (memento->memo));
129 memento->memo_cursor = 0;
130
131 memset (memento->history, 0, sizeof (memento->history));
132 memento->history_count = 0;
133 memento->current_history = 0;
134 memento->is_at_start = FALSE;
135
136 return memento;
137 }
138
139
140 /*
141 * memo_destroy()
142 *
143 * Destroy a memo set, and free its heap memory.
144 */
145 void
146 memo_destroy (sc_memo_setref_t memento)
147 {
148 sc_int index_;
149 assert (memo_is_valid (memento));
150
151 /* Free the content of any used memo and any used history. */
152 for (index_ = 0; index_ < MEMO_UNDO_TABLE_SIZE; index_++)
153 {
154 sc_memoref_t memo;
155
156 memo = memento->memo + index_;
157 sc_free (memo->serialized_game);
158 }
159 for (index_ = 0; index_ < MEMO_HISTORY_TABLE_SIZE; index_++)
160 {
161 sc_historyref_t history;
162
163 history = memento->history + index_;
164 sc_free (history->command);
165 }
166
167 /* Poison and free the memo set itself. */
168 memset (memento, 0xaa, sizeof (*memento));
169 sc_free (memento);
170 }
171
172
173 /*
174 * memo_save_game_callback()
175 *
176 * Callback function for game serialization. Appends the data passed in to
177 * that already stored in the memo.
178 */
179 static void
180 memo_save_game_callback (void *opaque, const sc_byte *buffer, sc_int length)
181 {
182 sc_memoref_t memo = opaque;
183 sc_int required;
184 assert (opaque && buffer && length > 0);
185
186 /*
187 * If necessary, increase the allocation for this memo. Serialized games
188 * tend to grow slightly as the game progresses, so we add a bit of extra
189 * to the actual allocation.
190 */
191 required = memo->length + length;
192 if (required > memo->allocation)
193 {
194 required = memo_round_up (required + 2 * MEMO_ALLOCATION_BLOCK);
195 memo->serialized_game = sc_realloc (memo->serialized_game, required);
196 memo->allocation = required;
197 }
198
199 /* Add this block of data to the buffer. */
200 memcpy (memo->serialized_game + memo->length, buffer, length);
201 memo->length += length;
202 }
203
204
205 /*
206 * memo_save_game()
207 *
208 * Store a game in the next memo slot.
209 */
210 void
211 memo_save_game (sc_memo_setref_t memento, sc_gameref_t game)
212 {
213 sc_memoref_t memo;
214 assert (memo_is_valid (memento));
215
216 /*
217 * If the current slot is in use, we can re-use its allocation. Saved
218 * games will tend to be of roughly equal sizes, so it's worth doing.
219 */
220 memo = memento->memo + memento->memo_cursor;
221 memo->length = 0;
222
223 /* Serialize the given game into this memo. */
224 ser_save_game (game, memo_save_game_callback, memo);
225
226 /*
227 * If serialization worked (failure would be a surprise), advance the
228 * current memo cursor.
229 */
230 if (memo->length > 0)
231 {
232 memento->memo_cursor++;
233 memento->memo_cursor %= MEMO_UNDO_TABLE_SIZE;
234 }
235 else
236 sc_error ("memo_save_game: warning: game save failed\n");
237 }
238
239
240 /*
241 * memo_load_game_callback()
242 *
243 * Callback function for game deserialization. Returns data from the memo
244 * until it's drained.
245 */
246 static sc_int
247 memo_load_game_callback (void *opaque, sc_byte *buffer, sc_int length)
248 {
249 sc_memoref_t memo = opaque;
250 sc_int bytes;
251 assert (opaque && buffer && length > 0);
252
253 /* Send back either all the bytes, or as many as the buffer allows. */
254 bytes = (memo->length < length) ? memo->length : length;
255
256 /* Read and remove the first block of data (or all if less than length). */
257 memcpy (buffer, memo->serialized_game, bytes);
258 memmove (memo->serialized_game,
259 memo->serialized_game + bytes, memo->length - bytes);
260 memo->length -= bytes;
261
262 /* Return the count of bytes placed in the buffer. */
263 return bytes;
264 }
265
266
267 /*
268 * memo_load_game()
269 *
270 * Restore a game from the last memo slot used, if possible.
271 */
272 sc_bool
273 memo_load_game (sc_memo_setref_t memento, sc_gameref_t game)
274 {
275 sc_int cursor;
276 sc_memoref_t memo;
277 assert (memo_is_valid (memento));
278
279 /* Look back one from the current memo cursor. */
280 cursor = (memento->memo_cursor == 0)
281 ? MEMO_UNDO_TABLE_SIZE - 1 : memento->memo_cursor - 1;
282 memo = memento->memo + cursor;
283
284 /* If this slot is not empty, restore the serialized game held in it. */
285 if (memo->length > 0)
286 {
287 sc_bool status;
288
289 /*
290 * Deserialize the given game from this memo; failure would be somewhat
291 * of a surprise here.
292 */
293 status = ser_load_game (game, memo_load_game_callback, memo);
294 if (!status)
295 sc_error ("memo_load_game: warning: game load failed\n");
296
297 /*
298 * This should have drained the memo of all data, but to be sure that
299 * there's no chance of trying to restore from this slot again, we'll
300 * force it anyway.
301 */
302 if (memo->length > 0)
303 {
304 sc_error ("memo_load_game: warning: data remains after loading\n");
305 memo->length = 0;
306 }
307
308 /* Regress current memo, and return TRUE if we restored a memo. */
309 memento->memo_cursor = cursor;
310 return status;
311 }
312
313 /* There are no more memos to restore. */
314 return FALSE;
315 }
316
317
318 /*
319 * memo_is_load_available()
320 *
321 * Returns TRUE if a memo restore is likely to succeed if called, FALSE
322 * otherwise.
323 */
324 sc_bool
325 memo_is_load_available (sc_memo_setref_t memento)
326 {
327 sc_int cursor;
328 sc_memoref_t memo;
329 assert (memo_is_valid (memento));
330
331 /*
332 * Look back one from the current memo cursor. Return TRUE if this slot
333 * contains a serialized game.
334 */
335 cursor = (memento->memo_cursor == 0)
336 ? MEMO_UNDO_TABLE_SIZE - 1 : memento->memo_cursor - 1;
337 memo = memento->memo + cursor;
338 return memo->length > 0;
339 }
340
341
342 /*
343 * memo_clear_games()
344 *
345 * Forget the memos of saved games.
346 */
347 void
348 memo_clear_games (sc_memo_setref_t memento)
349 {
350 sc_int index_;
351 assert (memo_is_valid (memento));
352
353 /* Deallocate every entry. */
354 for (index_ = 0; index_ < MEMO_UNDO_TABLE_SIZE; index_++)
355 {
356 sc_memoref_t memo;
357
358 memo = memento->memo + index_;
359 sc_free (memo->serialized_game);
360 }
361
362 /* Reset all entries and the cursor. */
363 memset (memento->memo, 0, sizeof (memento->memo));
364 memento->memo_cursor = 0;
365 }
366
367
368 /*
369 * memo_save_command()
370 *
371 * Store a player command in the command history, evicting any least recently
372 * used item if necessary.
373 */
374 void
375 memo_save_command (sc_memo_setref_t memento,
376 const sc_char *command, sc_int timestamp, sc_int turns)
377 {
378 sc_historyref_t history;
379 sc_int length;
380 assert (memo_is_valid (memento));
381
382 /* As with memos, reuse the allocation of the next slot if it has one. */
383 history = memento->history
384 + memento->history_count % MEMO_HISTORY_TABLE_SIZE;
385
386 /*
387 * Resize the allocation for this slot if required. Strings tend to be
388 * short, so round up to a block to avoid too many reallocs.
389 */
390 length = strlen (command) + 1;
391 if (history->allocation < length)
392 {
393 sc_int required;
394
395 required = memo_round_up (length);
396 history->command = sc_realloc (history->command, required);
397 history->allocation = required;
398 }
399
400 /* Save the string into this slot, and normalize it for neatness. */
401 strcpy (history->command, command);
402 sc_normalize_string (history->command);
403 history->sequence = memento->history_count + 1;
404 history->timestamp = timestamp;
405 history->turns = turns;
406 history->length = length;
407
408 /* Increment the count of histories handled. */
409 memento->history_count++;
410 }
411
412
413 /*
414 * memo_unsave_command()
415 *
416 * Remove the last saved command. This is special functionality for the
417 * history lister. To keep synchronized with the runner main loop, it needs
418 * to "invent" a history item at the end of the list before listing, then
419 * remove it again as the main runner loop will add the real thing.
420 */
421 void
422 memo_unsave_command (sc_memo_setref_t memento)
423 {
424 assert (memo_is_valid (memento));
425
426 /* Do nothing if for some reason there's no history to unsave. */
427 if (memento->history_count > 0)
428 {
429 sc_historyref_t history;
430
431 /* Decrement the count of histories handled, erase the prior entry. */
432 memento->history_count--;
433 history = memento->history
434 + memento->history_count % MEMO_HISTORY_TABLE_SIZE;
435 history->sequence = 0;
436 history->timestamp = 0;
437 history->turns = 0;
438 history->length = 0;
439 }
440 }
441
442
443 /*
444 * memo_get_command_count()
445 *
446 * Return a count of available saved commands.
447 */
448 sc_int
449 memo_get_command_count (sc_memo_setref_t memento)
450 {
451 assert (memo_is_valid (memento));
452
453 /* Return the lesser of the history count and the history table size. */
454 if (memento->history_count < MEMO_HISTORY_TABLE_SIZE)
455 return memento->history_count;
456 else
457 return MEMO_HISTORY_TABLE_SIZE;
458 }
459
460
461 /*
462 * memo_first_command()
463 *
464 * Iterator rewind function, reset current location to the first command.
465 */
466 void
467 memo_first_command (sc_memo_setref_t memento)
468 {
469 sc_int cursor;
470 sc_historyref_t history;
471 assert (memo_is_valid (memento));
472
473 /*
474 * If the buffer has cycled, we have the full complement of saved commands,
475 * so start iterating at the current cursor. Otherwise, start from index 0.
476 * Detect cycling by looking at the current slot; if it's filled, we've
477 * been here before. Set at_start flag to indicate the special case for
478 * circular buffers.
479 */
480 cursor = memento->history_count % MEMO_HISTORY_TABLE_SIZE;
481 history = memento->history + cursor;
482 memento->current_history = (history->length > 0) ? cursor : 0;
483 memento->is_at_start = TRUE;
484 }
485
486
487 /*
488 * memo_next_command()
489 *
490 * Iterator function, return the next saved command and its sequence id
491 * starting at 1, and the timestamp and turns when the command was saved.
492 */
493 void
494 memo_next_command (sc_memo_setref_t memento,
495 const sc_char **command, sc_int *sequence,
496 sc_int *timestamp, sc_int *turns)
497 {
498 assert (memo_is_valid (memento));
499
500 /* If valid, return the current command and advance. */
501 if (memo_more_commands (memento))
502 {
503 sc_historyref_t history;
504
505 /* Note the current history, and advance its index. */
506 history = memento->history + memento->current_history;
507 memento->current_history++;
508 memento->current_history %= MEMO_HISTORY_TABLE_SIZE;
509 memento->is_at_start = FALSE;
510
511 /* Return details from the history noted above. */
512 *command = history->command;
513 *sequence = history->sequence;
514 *timestamp = history->timestamp;
515 *turns = history->turns;
516 }
517 else
518 {
519 /* Return NULL and zeroes if no more commands available. */
520 *command = NULL;
521 *sequence = 0;
522 *timestamp = 0;
523 *turns = 0;
524 }
525 }
526
527
528 /*
529 * memo_more_commands()
530 *
531 * Iterator end function, returns TRUE if more commands are readable.
532 */
533 sc_bool
534 memo_more_commands (sc_memo_setref_t memento)
535 {
536 sc_int cursor;
537 sc_historyref_t history;
538 assert (memo_is_valid (memento));
539
540 /* Get the current effective write position, and the current history. */
541 cursor = memento->history_count % MEMO_HISTORY_TABLE_SIZE;
542 history = memento->history + memento->current_history;
543
544 /*
545 * More data if the current history is behind the write position and is
546 * occupied, or if it matches and is occupied and we're at the start of
547 * iteration (circular buffer special case).
548 */
549 if (memento->current_history == cursor)
550 return (memento->is_at_start) ? history->length > 0 : FALSE;
551 else
552 return history->length > 0;
553 }
554
555
556 /*
557 * memo_find_command()
558 *
559 * Find and return the command string for the given sequence number (-ve
560 * indicates an offset from the last defined), or NULL if not found.
561 */
562 const sc_char *
563 memo_find_command (sc_memo_setref_t memento, sc_int sequence)
564 {
565 sc_int target, index_;
566 sc_historyref_t matched;
567 assert (memo_is_valid (memento));
568
569 /* Decide on a search target, depending on the sign of sequence. */
570 target = (sequence < 0) ? memento->history_count + sequence + 1: sequence;
571
572 /*
573 * A backwards search starting at the write position would probably be more
574 * efficient here, but this is a rarely called function so we'll do it the
575 * simpler way.
576 */
577 matched = NULL;
578 for (index_ = 0; index_ < MEMO_HISTORY_TABLE_SIZE; index_++)
579 {
580 sc_historyref_t history;
581
582 history = memento->history + index_;
583 if (history->sequence == target)
584 {
585 matched = history;
586 break;
587 }
588 }
589
590 /*
591 * Return the command or NULL. If sequence passed in was zero, and the
592 * history was not full, this will still return NULL as it should, since
593 * this unused history's command found by the search above will be NULL.
594 */
595 return matched ? matched->command : NULL;
596 }
597
598
599 /*
600 * memo_clear_commands()
601 *
602 * Forget all saved commands.
603 */
604 void
605 memo_clear_commands (sc_memo_setref_t memento)
606 {
607 sc_int index_;
608 assert (memo_is_valid (memento));
609
610 /* Deallocate every entry. */
611 for (index_ = 0; index_ < MEMO_HISTORY_TABLE_SIZE; index_++)
612 {
613 sc_historyref_t history;
614
615 history = memento->history + index_;
616 sc_free (history->command);
617 }
618
619 /* Reset all entries, the count, and the iteration variables. */
620 memset (memento->history, 0, sizeof (memento->history));
621 memento->history_count = 0;
622 memento->current_history = 0;
623 memento->is_at_start = FALSE;
624 }