From 4d005a7dbbbe21df83cef4838ba5907bc9b742d8 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 11 Aug 2010 16:18:38 +0000 Subject: [PATCH] Implement a naive garbage collector. --- src/asset.cpp | 4 ++-- src/asset.h | 3 ++- src/editor.cpp | 14 +++++++------- src/gtkvideo.cpp | 4 ++-- src/map.cpp | 1 - src/test-map.cpp | 6 +++--- src/ticker.cpp | 45 ++++++++++++++++++++++++++++++--------------- src/tiler.cpp | 7 +++---- src/tileset.cpp | 4 ++-- 9 files changed, 51 insertions(+), 37 deletions(-) diff --git a/src/asset.cpp b/src/asset.cpp index 54ce5bbc..3bee580c 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -1,5 +1,5 @@ -#include +#include #include "asset.h" #include "ticker.h" @@ -9,7 +9,7 @@ */ Asset::Asset() : - index(0), + next(0), ref(0), destroy(0) { diff --git a/src/asset.h b/src/asset.h index 4616603a..329b93bd 100644 --- a/src/asset.h +++ b/src/asset.h @@ -23,7 +23,8 @@ public: virtual int Unref(); protected: - int index, ref, destroy; + Asset *next; + int ref, destroy; }; #endif // __DH_ASSET_H__ diff --git a/src/editor.cpp b/src/editor.cpp index 1f99f450..b3115be6 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -1,17 +1,17 @@ -#include -#include -#include +#include +#include -#include "gtkvideo.h" -#include "ticker.h" -#include "game.h" +#include -#include #include #include #include +#include "gtkvideo.h" +#include "ticker.h" +#include "game.h" + volatile int quit = 0; static gint main_quit(GtkWidget *widget, GdkEventExpose *event) diff --git a/src/gtkvideo.cpp b/src/gtkvideo.cpp index 06a10bef..47783e31 100644 --- a/src/gtkvideo.cpp +++ b/src/gtkvideo.cpp @@ -1,4 +1,6 @@ +#include + #include #include @@ -13,8 +15,6 @@ # include #endif -#include - #include "gtkvideo.h" /* diff --git a/src/map.cpp b/src/map.cpp index 31c5c871..f40c8142 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include "map.h" diff --git a/src/test-map.cpp b/src/test-map.cpp index 24ef9208..0458b5b7 100644 --- a/src/test-map.cpp +++ b/src/test-map.cpp @@ -1,9 +1,9 @@ // Test stuff -#include +#include +#include -#include -#include +#include #include "sdlvideo.h" #include "game.h" diff --git a/src/ticker.cpp b/src/ticker.cpp index 3f606ae7..97dd8094 100644 --- a/src/ticker.cpp +++ b/src/ticker.cpp @@ -3,8 +3,9 @@ * The ticker */ +#include +#include #include -#include #include "ticker.h" #include "asset.h" @@ -19,19 +20,19 @@ static class TickerData public: TickerData() : - assets(0), + list(0), nassets(0) { - /* Nothing to do */ } ~TickerData() { - free(assets); + if (nassets) + fprintf(stderr, "ERROR: still %i assets in ticker\n", nassets); } private: - Asset **assets; + Asset *list; int nassets; } tickerdata; @@ -44,23 +45,37 @@ static TickerData * const data = &tickerdata; void Ticker::Register(Asset *asset) { - int tmp = data->nassets++; - data->assets = (Asset **)realloc(data->assets, - data->nassets * sizeof(Asset *)); - data->assets[tmp] = asset; - - asset->index = tmp; + asset->next = data->list; + data->list = asset; + data->nassets++; } void Ticker::TickGame(float delta_time) { - for (int i = 0; i < data->nassets; i++) - data->assets[i]->TickGame(delta_time); + /* Garbage collect objects that can be destroyed */ + for (Asset *asset = data->list, *prev = NULL; + asset; + prev = asset, asset = asset->next) + if (asset->destroy) + { + if (prev) + prev->next = asset->next; + else + data->list = asset->next; + + data->nassets--; + delete asset; + } + + /* Tick objects for the game loop */ + for (Asset *asset = data->list; asset; asset = asset->next) + asset->TickGame(delta_time); } void Ticker::TickRender(float delta_time) { - for (int i = 0; i < data->nassets; i++) - data->assets[i]->TickRender(delta_time); + /* Tick objects for the render loop */ + for (Asset *asset = data->list; asset; asset = asset->next) + asset->TickRender(delta_time); } diff --git a/src/tiler.cpp b/src/tiler.cpp index 5c50e7a6..496067d1 100644 --- a/src/tiler.cpp +++ b/src/tiler.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include #include "tiler.h" #include "tileset.h" @@ -28,6 +28,8 @@ public: ~TilerData() { + if (ntilesets) + fprintf(stderr, "ERROR: still %i tilesets in tiler\n", ntilesets); free(tilesets); } @@ -81,10 +83,7 @@ void Tiler::Deregister(int id) --id; /* ID 0 is for the empty tileset */ if (data->tilesets[id]->Unref() == 0) - { - delete data->tilesets[id]; data->tilesets[id] = NULL; - } } void Tiler::Render(uint32_t code, int x, int y) diff --git a/src/tileset.cpp b/src/tileset.cpp index c5cc1147..12550cb2 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -1,4 +1,6 @@ +#include + #ifdef WIN32 # define WIN32_LEAN_AND_MEAN # include @@ -13,8 +15,6 @@ #include #include -#include - #include "tileset.h" /*