X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fsnapshot.c;h=43a42c34d50920813717c73314337aba168a6ad8;hb=eb72f4adc74084da40e39484b2545e3d49d39eaa;hp=f57cb5fa6dd757f8de167541aecaaf77006d54be;hpb=f6bcc10ce7b517f04eb5cddf01c9ec29ee729b7e;p=rocksndiamonds.git diff --git a/src/libgame/snapshot.c b/src/libgame/snapshot.c index f57cb5fa..43a42c34 100644 --- a/src/libgame/snapshot.c +++ b/src/libgame/snapshot.c @@ -18,11 +18,15 @@ static ListNode *snapshot_current = NULL; static int num_snapshots_in_list = 0; - #ifdef DEBUG #define DEBUG_SNAPSHOTS 0 #endif +#if DEBUG_SNAPSHOTS +static int num_snapshot_buffers = 0; +static int num_snapshot_bytes = 0; +#endif + // ----------------------------------------------------------------------------- // functions for handling buffers for a single snapshot @@ -40,6 +44,11 @@ void SaveSnapshotBuffer(ListNode **snapshot_buffers, void *buffer, int size) memcpy(bi->buffer_copy, buffer, size); addNodeToList(snapshot_buffers, NULL, bi); + +#if DEBUG_SNAPSHOTS + num_snapshot_buffers++; + num_snapshot_bytes += size; +#endif } static void LoadSnapshotBuffer(struct SnapshotNodeInfo *bi) @@ -57,17 +66,30 @@ void LoadSnapshotBuffers(ListNode *snapshot_buffers) } } +static void FreeSnapshotBuffer(void *bi_raw) +{ + struct SnapshotNodeInfo *bi = (struct SnapshotNodeInfo *)bi_raw; + +#if DEBUG_SNAPSHOTS + num_snapshot_buffers--; + num_snapshot_bytes -= bi->size; +#endif + + checked_free(bi->buffer_copy); + checked_free(bi); +} + void FreeSnapshotBuffers(ListNode *snapshot_buffers) { while (snapshot_buffers != NULL) - deleteNodeFromList(&snapshot_buffers, snapshot_buffers->key, checked_free); + deleteNodeFromList(&snapshot_buffers, NULL, FreeSnapshotBuffer); } // ----------------------------------------------------------------------------- -// functions for handling one of several snapshots +// functions for handling single shapshot or list of snapshots // ----------------------------------------------------------------------------- -static void FreeSnapshotExt(void *snapshot_buffers_ptr) +static void FreeSnapshot(void *snapshot_buffers_ptr) { FreeSnapshotBuffers(snapshot_buffers_ptr); } @@ -79,11 +101,16 @@ void FreeSnapshotSingle() snapshot_single = NULL; } -void FreeSnapshotList_UpToNode(ListNode *node) +static void FreeSnapshotList_UpToNode(ListNode *node) { while (snapshot_list != node) { - deleteNodeFromList(&snapshot_list, snapshot_list->key, FreeSnapshotExt); +#if DEBUG_SNAPSHOTS + printf("::: FreeSnapshotList_*() [%s, %d, %d]\n", + snapshot_list->key, num_snapshot_buffers, num_snapshot_bytes); +#endif + + deleteNodeFromList(&snapshot_list, snapshot_list->key, FreeSnapshot); num_snapshots_in_list--; } @@ -91,6 +118,10 @@ void FreeSnapshotList_UpToNode(ListNode *node) void FreeSnapshotList() { +#if DEBUG_SNAPSHOTS + printf("::: FreeSnapshotList()\n"); +#endif + FreeSnapshotList_UpToNode(NULL); snapshot_current = NULL; @@ -117,7 +148,8 @@ void SaveSnapshotToList(ListNode *snapshot_buffers) num_snapshots_in_list++; #if DEBUG_SNAPSHOTS - printf("::: SaveSnapshotToList() [%s]\n", snapshot_current->key); + printf("::: SaveSnapshotToList() [%s, %d, %d]\n", + snapshot_current->key, num_snapshot_buffers, num_snapshot_bytes); #endif } @@ -133,11 +165,12 @@ boolean LoadSnapshotSingle() return FALSE; } -boolean LoadSnapshotFromList_Older() +boolean LoadSnapshotFromList_Older(int steps) { - if (snapshot_current->next) + if (snapshot_current && snapshot_current->next) { - snapshot_current = snapshot_current->next; + while (snapshot_current->next && steps--) + snapshot_current = snapshot_current->next; LoadSnapshotBuffers(snapshot_current->content); @@ -151,11 +184,12 @@ boolean LoadSnapshotFromList_Older() return FALSE; } -boolean LoadSnapshotFromList_Newer() +boolean LoadSnapshotFromList_Newer(int steps) { - if (snapshot_current->prev) + if (snapshot_current && snapshot_current->prev) { - snapshot_current = snapshot_current->prev; + while (snapshot_current->prev && steps--) + snapshot_current = snapshot_current->prev; LoadSnapshotBuffers(snapshot_current->content); @@ -168,3 +202,8 @@ boolean LoadSnapshotFromList_Newer() return FALSE; } + +boolean CheckSnapshotList() +{ + return (snapshot_list ? TRUE : FALSE); +}