changeset 148:72c25cc27d8b

For your convenience pools now have a function called purgeLoadedResources exposed to python which will delete all loaded resources that have a ref count of zero. You can use this after closing a map or something alike. It may however impact performance, as the next map may very well reload the same resources. Just be aware of that. :-)
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 11 Oct 2008 06:05:11 +0000
parents fb6ccb367dd1
children 823544621b5b
files engine/core/util/resource/pool.cpp engine/core/util/resource/pool.h engine/core/util/resource/resource.i tests/core_tests/test_imagepool.cpp
diffstat 4 files changed, 47 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/engine/core/util/resource/pool.cpp	Thu Oct 09 13:36:13 2008 +0000
+++ b/engine/core/util/resource/pool.cpp	Sat Oct 11 06:05:11 2008 +0000
@@ -49,14 +49,14 @@
 		FL_LOG(_log, LMsg("Pool destroyed: ") << m_name);
 		printStatistics();
 		sanityCheck();
-		clear();
+		reset();
 		std::vector<ResourceLoader*>::iterator loader;
 		for (loader = m_loaders.begin(); loader != m_loaders.end(); loader++) {
 			delete (*loader);
 		}
 	}
 	
-	void Pool::clear() {
+	void Pool::reset() {
 		std::vector<IPoolListener*>::iterator listener;
 		for (listener = m_listeners.begin(); listener != m_listeners.end(); listener++) {
 			(*listener)->poolCleared();
@@ -75,6 +75,24 @@
 		m_location_to_entry.clear();
 	}
 
+	int Pool::purgeLoadedResources() {
+		std::vector<IPoolListener*>::iterator listener;
+		for (listener = m_listeners.begin(); listener != m_listeners.end(); listener++) {
+			(*listener)->poolCleared();
+		}
+		int count = 0;
+		std::vector<PoolEntry*>::iterator it;
+		for (it = m_entries.begin(); it != m_entries.end(); it++) {
+			PoolEntry* entry = *it;
+			if( entry->resource && entry->resource->getRefCount() == 0 ) {
+				delete entry->resource;
+				entry->resource = 0;
+				++count;
+			}
+		}
+		return count;
+	}
+
 	void Pool::addResourceLoader(ResourceLoader* loader) {
 		m_loaders.push_back(loader);
 	}
--- a/engine/core/util/resource/pool.h	Thu Oct 09 13:36:13 2008 +0000
+++ b/engine/core/util/resource/pool.h	Sat Oct 11 06:05:11 2008 +0000
@@ -66,6 +66,7 @@
 		static const int INVALID_ID = -1;
 
 		/** Default constructor.
+		 *  @param name The name used in debug output.
 		 */
 		Pool(const std::string& name);
 
@@ -109,14 +110,17 @@
 		 */
 		virtual void release(unsigned int index, bool dec = false);
 
+		/** Purge all loaded resources.
+		 *  This will purge all loaded resources with a ref count of zero.
+		 *  Indices remain valid, though.
+		 *  @return Number of resources deleted.
+		 */
+		virtual int purgeLoadedResources();
+
 		/** Gets amount of resources in the pool with given status
 		 */
 		virtual int getResourceCount(int status);
 
-		/** Clears pool from resources. Frees associated memory 
-		 */
-		virtual void clear();
-
 		/** Adds pool listener.
 		 * Pool listeners get indications e.g. when ownerships of pooled
 		 * resources change.
@@ -133,7 +137,14 @@
 
 		/** Performs a sanity check for the location map.
 		 */
-		virtual void sanityCheck();
+		void sanityCheck();
+
+		/** Resets the pool.
+		 *  This will purge all loaded resources with a ref count of zero.
+		 *  The location and loader information for the locations is lost.
+		 *  Only the resource loaders are retained.
+		 */
+		virtual void reset();
 
 	protected:
 	private:
@@ -162,6 +173,10 @@
 		std::vector<ResourceLoader*> m_loaders;
 		int m_curind;
 		std::string m_name;
+
+		/** Clears pool from ALL resources. Frees associated memory 
+		 */
+		void cleanUp();
 	};
 
 } // FIFE
--- a/engine/core/util/resource/resource.i	Thu Oct 09 13:36:13 2008 +0000
+++ b/engine/core/util/resource/resource.i	Sat Oct 11 06:05:11 2008 +0000
@@ -86,6 +86,7 @@
 		virtual int addResourceFromFile(const std::string& filename);
 		virtual int addResourceFromLocation(const ResourceLocation& loc);
 		virtual int getResourceCount(int status);
+		virtual int purgeLoadedResources();
 		virtual void addResourceLoader(ResourceLoader* loader);
 		virtual void release(unsigned int index, bool dec = false);
 		virtual unsigned int getIndex(const std::string& filename);
--- a/tests/core_tests/test_imagepool.cpp	Thu Oct 09 13:36:13 2008 +0000
+++ b/tests/core_tests/test_imagepool.cpp	Sat Oct 11 06:05:11 2008 +0000
@@ -125,7 +125,12 @@
 	}
 	CHECK_EQUAL(3, pool.getResourceCount(RES_LOADED));
 	CHECK_EQUAL(0, pool.getResourceCount(RES_NON_LOADED));
-	pool.clear();
+
+	CHECK_EQUAL(3, pool.purgeLoadedResources() );
+
+	CHECK_EQUAL(0, pool.getResourceCount(RES_LOADED));
+	CHECK_EQUAL(3, pool.getResourceCount(RES_NON_LOADED));
+	pool.reset();
 	CHECK_EQUAL(0, pool.getResourceCount(RES_LOADED));
 	CHECK_EQUAL(0, pool.getResourceCount(RES_NON_LOADED));
 }