[Scummvm-cvs-logs] SF.net SVN: scummvm:[34868] scummvm/trunk/common/memorypool.cpp

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Thu Oct 30 18:45:35 CET 2008


Revision: 34868
          http://scummvm.svn.sourceforge.net/scummvm/?rev=34868&view=rev
Author:   fingolfin
Date:     2008-10-30 17:45:35 +0000 (Thu, 30 Oct 2008)

Log Message:
-----------
Reset _chunksPerPage after MemoryPool::freeUnusedPages, to avoid enormous memory consumption in various easy to trigger situations; out of paranoia, prohibit for now memory chunks bigger than 16MB

Modified Paths:
--------------
    scummvm/trunk/common/memorypool.cpp

Modified: scummvm/trunk/common/memorypool.cpp
===================================================================
--- scummvm/trunk/common/memorypool.cpp	2008-10-29 20:45:28 UTC (rev 34867)
+++ scummvm/trunk/common/memorypool.cpp	2008-10-30 17:45:35 UTC (rev 34868)
@@ -28,6 +28,11 @@
 
 namespace Common {
 
+enum {
+	INITIAL_CHUNKS_PER_PAGE = 8
+};
+
+
 MemoryPool::MemoryPool(size_t chunkSize) {
 	// You must at least fit the pointer in the node (technically unneeded considering the next rounding statement)
 	_chunkSize = MAX(chunkSize, sizeof(void*));
@@ -37,7 +42,7 @@
 
 	_next = NULL;
 	
-	_chunksPerPage = 8;
+	_chunksPerPage = INITIAL_CHUNKS_PER_PAGE;
 }
 
 MemoryPool::~MemoryPool() {
@@ -50,9 +55,12 @@
 	
 	// Allocate a new page
 	page.numChunks = _chunksPerPage;
+	assert(page.numChunks * _chunkSize < 16*1024*1024);	// Refuse to allocate pages bigger than 16 MB
+
 	page.start = ::malloc(page.numChunks * _chunkSize);
 	assert(page.start);
 	_pages.push_back(page);
+
 	
 	// Next time, we'll alocate a page twice as big as this one.
 	_chunksPerPage *= 2;
@@ -122,8 +130,6 @@
 	}
 
 	// Free all pages which are not in use.
-	// TODO: Might want to reset _chunksPerPage here (e.g. to the largest
-	//      _pages[i].numChunks value still in use).
 	size_t freedPagesCount = 0;
 	for (size_t i = 0; i < _pages.size(); ++i)  {
 		if (numberOfFreeChunksPerPage[i] == _pages[i].numChunks) {
@@ -141,6 +147,8 @@
 		}
 	}
 
+//	printf("freed %d pages out of %d\n", (int)freedPagesCount, (int)_pages.size());
+
 	for (size_t i = 0; i < _pages.size(); )  {
 		if (_pages[i].start == NULL) {
 			_pages.remove_at(i);
@@ -150,7 +158,12 @@
 		}
 	}
 
-	//printf("%d freed pages\n", freedPagesCount); 
+	// Reset _chunksPerPage
+	_chunksPerPage = INITIAL_CHUNKS_PER_PAGE;
+	for (size_t i = 0; i < _pages.size(); ++i) {
+		if (_chunksPerPage < _pages[i].numChunks)
+			_chunksPerPage = _pages[i].numChunks;
+	}
 }
 
 }	// End of namespace Common


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list