aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/kernel
diff options
context:
space:
mode:
authorFilippos Karapetis2011-01-28 16:54:55 +0000
committerFilippos Karapetis2011-01-28 16:54:55 +0000
commitc63ff39a7f79df2cdac8b28d8c0e0220b3574d11 (patch)
treede918e8306b4219ed9903238987dda1438a5c31a /engines/sword25/kernel
parenta2f960017931ad871c37bf1d9c197418aa8afca9 (diff)
downloadscummvm-rg350-c63ff39a7f79df2cdac8b28d8c0e0220b3574d11.tar.gz
scummvm-rg350-c63ff39a7f79df2cdac8b28d8c0e0220b3574d11.tar.bz2
scummvm-rg350-c63ff39a7f79df2cdac8b28d8c0e0220b3574d11.zip
SWORD25: Resources are now cleaned up correctly
The original checked the total amount of memory occupied by all resources. This has been changed to a maximum number of simultaneous resources instead, so the game resources are no longer leaked. Also disabled the unused or debug functions getUsedMemory(), setMaxMemoryUsage(), setMaxMemoryUsage(), isLogCacheMiss(), setLogCacheMiss(). Performed some cleanup on code related to the above. svn-id: r55594
Diffstat (limited to 'engines/sword25/kernel')
-rw-r--r--engines/sword25/kernel/kernel.cpp9
-rw-r--r--engines/sword25/kernel/kernel.h4
-rw-r--r--engines/sword25/kernel/kernel_script.cpp16
-rw-r--r--engines/sword25/kernel/resmanager.cpp25
-rw-r--r--engines/sword25/kernel/resmanager.h37
5 files changed, 19 insertions, 72 deletions
diff --git a/engines/sword25/kernel/kernel.cpp b/engines/sword25/kernel/kernel.cpp
index 45ec492bc9..6b99b0a115 100644
--- a/engines/sword25/kernel/kernel.cpp
+++ b/engines/sword25/kernel/kernel.cpp
@@ -152,15 +152,6 @@ uint Kernel::getMilliTicks() {
}
/**
- * Returns how much memory is being used
- */
-size_t Kernel::getUsedMemory() {
- // TODO: Actually monitor how much memory is being used, so that the game
- // doesn't keep allocating resources without ever deleting them.
- return 0;
-}
-
-/**
* Returns a pointer to the active Gfx Service, or NULL if no Gfx service is active.
*/
GraphicEngine *Kernel::getGfx() {
diff --git a/engines/sword25/kernel/kernel.h b/engines/sword25/kernel/kernel.h
index 252de64e80..9e7ee0fdd9 100644
--- a/engines/sword25/kernel/kernel.h
+++ b/engines/sword25/kernel/kernel.h
@@ -93,10 +93,6 @@ public:
return _resourceManager;
}
/**
- * Returns how much memory is being used
- */
- size_t getUsedMemory();
- /**
* Returns a random number
* @param Min The minimum allowed value
* @param Max The maximum allowed value
diff --git a/engines/sword25/kernel/kernel_script.cpp b/engines/sword25/kernel/kernel_script.cpp
index e5586a513b..458f6b2deb 100644
--- a/engines/sword25/kernel/kernel_script.cpp
+++ b/engines/sword25/kernel/kernel_script.cpp
@@ -168,7 +168,9 @@ static int getSubversionRevision(lua_State *L) {
}
static int getUsedMemory(lua_State *L) {
- lua_pushnumber(L, Kernel::getInstance()->getUsedMemory());
+ // It doesn't really matter what this call returns,
+ // as it's used in a debug function.
+ lua_pushnumber(L, 0);
return 1;
}
@@ -404,7 +406,9 @@ static int getMaxMemoryUsage(lua_State *L) {
ResourceManager *pResource = pKernel->getResourceManager();
assert(pResource);
- lua_pushnumber(L, pResource->getMaxMemoryUsage());
+ // This is used for debugging, so it doesn't really matter.
+ // The default value set by the scripts is 256000000 bytes
+ lua_pushnumber(L, 256000000);
return 1;
}
@@ -415,7 +419,8 @@ static int setMaxMemoryUsage(lua_State *L) {
ResourceManager *pResource = pKernel->getResourceManager();
assert(pResource);
- pResource->setMaxMemoryUsage(static_cast<uint>(lua_tonumber(L, 1)));
+ // This call is ignored, we set a limit on the number of
+ // simultaneous resources loaded instead.
return 0;
}
@@ -437,7 +442,8 @@ static int isLogCacheMiss(lua_State *L) {
ResourceManager *pResource = pKernel->getResourceManager();
assert(pResource);
- lua_pushbooleancpp(L, pResource->isLogCacheMiss());
+ // This isn't used in any script
+ lua_pushbooleancpp(L, false);
return 1;
}
@@ -448,7 +454,7 @@ static int setLogCacheMiss(lua_State *L) {
ResourceManager *pResource = pKernel->getResourceManager();
assert(pResource);
- pResource->setLogCacheMiss(lua_tobooleancpp(L, 1));
+ // This isn't used in any script
return 0;
}
diff --git a/engines/sword25/kernel/resmanager.cpp b/engines/sword25/kernel/resmanager.cpp
index 3b0dbcf5eb..19da68d4bf 100644
--- a/engines/sword25/kernel/resmanager.cpp
+++ b/engines/sword25/kernel/resmanager.cpp
@@ -40,6 +40,11 @@
namespace Sword25 {
+// Sets the amount of resources that are simultaneously loaded.
+// This needs to be a relatively high number, as all the animation
+// frames in each scene are loaded as separate resources.
+#define SWORD25_RESOURCECACHE_MAX 100
+
ResourceManager::~ResourceManager() {
// Clear all unlocked resources
emptyCache();
@@ -80,7 +85,7 @@ bool ResourceManager::registerResourceService(ResourceService *pService) {
*/
void ResourceManager::deleteResourcesIfNecessary() {
// If enough memory is available, or no resources are loaded, then the function can immediately end
- if (_kernelPtr->getUsedMemory() < _maxMemoryUsage || _resources.empty())
+ if (_resources.size() < SWORD25_RESOURCECACHE_MAX)
return;
// Keep deleting resources until the memory usage of the process falls below the set maximum limit.
@@ -93,7 +98,7 @@ void ResourceManager::deleteResourcesIfNecessary() {
// The resource may be released only if it isn't locked
if ((*iter)->getLockCount() == 0)
iter = deleteResource(*iter);
- } while (iter != _resources.begin() && _kernelPtr->getUsedMemory() > _maxMemoryUsage);
+ } while (iter != _resources.begin() && _resources.size() >= SWORD25_RESOURCECACHE_MAX);
}
/**
@@ -135,10 +140,6 @@ Resource *ResourceManager::requestResource(const Common::String &fileName) {
}
}
- // The resource was not found, therefore, must not be loaded yet
- if (_logCacheMiss)
- warning("\"%s\" was not precached.", uniqueFileName.c_str());
-
Resource *pResource = loadResource(uniqueFileName);
if (pResource) {
pResource->addReference();
@@ -296,16 +297,4 @@ void ResourceManager::dumpLockedResources() {
}
}
-/**
- * Specifies the maximum amount of memory the engine is allowed to use.
- * If this value is exceeded, resources will be unloaded to make room. This value is meant
- * as a guideline, and not as a fixed boundary. It is not guaranteed not to be exceeded;
- * the whole game engine may still use more memory than any amount specified.
- */
-void ResourceManager::setMaxMemoryUsage(uint maxMemoryUsage) {
- // TODO: Game scripts set this to 256000000. Set it to a more conservative value
- _maxMemoryUsage = maxMemoryUsage;
- deleteResourcesIfNecessary();
-}
-
} // End of namespace Sword25
diff --git a/engines/sword25/kernel/resmanager.h b/engines/sword25/kernel/resmanager.h
index c097ca7024..5b2bfd395f 100644
--- a/engines/sword25/kernel/resmanager.h
+++ b/engines/sword25/kernel/resmanager.h
@@ -82,37 +82,6 @@ public:
void emptyCache();
/**
- * Returns the maximum memory the kernel has used
- */
- int getMaxMemoryUsage() const {
- return _maxMemoryUsage;
- }
-
- /**
- * Specifies the maximum amount of memory the engine is allowed to use.
- * If this value is exceeded, resources will be unloaded to make room. This value is meant
- * as a guideline, and not as a fixed boundary. It is not guaranteed not to be exceeded;
- * the whole game engine may still use more memory than any amount specified.
- */
- void setMaxMemoryUsage(uint maxMemoryUsage);
-
- /**
- * Specifies whether a warning is written to the log when a cache miss occurs.
- * THe default value is "false".
- */
- bool isLogCacheMiss() const {
- return _logCacheMiss;
- }
-
- /**
- * Sets whether warnings are written to the log if a cache miss occurs.
- * @param Flag If "true", then future warnings will be logged
- */
- void setLogCacheMiss(bool flag) {
- _logCacheMiss = flag;
- }
-
- /**
* Writes the names of all currently locked resources to the log file
*/
void dumpLockedResources();
@@ -123,9 +92,7 @@ private:
* Only the BS_Kernel class can generate copies this class. Thus, the constructor is private
*/
ResourceManager(Kernel *pKernel) :
- _kernelPtr(pKernel),
- _maxMemoryUsage(100000000),
- _logCacheMiss(false)
+ _kernelPtr(pKernel)
{}
virtual ~ResourceManager();
@@ -166,12 +133,10 @@ private:
void deleteResourcesIfNecessary();
Kernel *_kernelPtr;
- uint _maxMemoryUsage;
Common::Array<ResourceService *> _resourceServices;
Common::List<Resource *> _resources;
typedef Common::HashMap<Common::String, Resource *> ResMap;
ResMap _resourceHashMap;
- bool _logCacheMiss;
};
} // End of namespace Sword25