[Scummvm-cvs-logs] SF.net SVN: scummvm: [31321] scummvm/trunk/common

tramboi at users.sourceforge.net tramboi at users.sourceforge.net
Sun Mar 30 08:02:34 CEST 2008


Revision: 31321
          http://scummvm.svn.sourceforge.net/scummvm/?rev=31321&view=rev
Author:   tramboi
Date:     2008-03-29 23:02:34 -0700 (Sat, 29 Mar 2008)

Log Message:
-----------
The hashmap uses the memorypool for allocating/deallocating its Nodes
(It is faster and it saves approximately 70kB the DS or other small devices will appreciate having)

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

Modified: scummvm/trunk/common/hashmap.h
===================================================================
--- scummvm/trunk/common/hashmap.h	2008-03-30 05:42:39 UTC (rev 31320)
+++ scummvm/trunk/common/hashmap.h	2008-03-30 06:02:34 UTC (rev 31321)
@@ -58,6 +58,12 @@
 #include "common/str.h"
 #include "common/util.h"
 
+#define USE_HASHMAP_MEMORY_POOL
+#ifdef USE_HASHMAP_MEMORY_POOL
+#include "common/memorypool.h"
+#include <new>
+#endif
+
 namespace Common {
 
 // The table sizes ideally are primes. We use a helper function to find
@@ -70,6 +76,7 @@
 // hash table that is too small).
 //#define DEBUG_HASH_COLLISIONS
 
+
 /**
  * HashMap<Key,Val> maps objects of type Key to objects of type Val.
  * For each used Key type, we need an "uint hashit(Key,uint)" function
@@ -98,13 +105,28 @@
 		Node(const Key &key) : _key(key), _value() {}
 	};
 
+
+#ifdef USE_HASHMAP_MEMORY_POOL
+        MemoryPool _nodePool;
+
 	Node *allocNode(const Key& key) {
+	  void* mem = _nodePool.malloc();
+	  return new (mem) Node(key);
+	} 
+
+	void freeNode(Node* node) {
+	  node->~Node();
+	  _nodePool.free(node);
+	}
+#else
+        Node* allocNode(const Key& key) {
 		return new Node(key);
 	} 
 
 	void freeNode(Node *node) {
 		delete node;
 	}
+#endif
 
 	Node **_arr;	// hashtable of size arrsize.
 	uint _arrsize, _nele;
@@ -328,8 +350,11 @@
  * Base constructor, creates an empty hashmap.
  */
 template <class Key, class Val, class HashFunc, class EqualFunc>
-HashMap<Key, Val, HashFunc, EqualFunc>::HashMap()
-	: _defaultVal() {
+HashMap<Key, Val, HashFunc, EqualFunc>::HashMap() :
+#ifdef USE_HASHMAP_MEMORY_POOL
+ _nodePool(sizeof(Node)),
+#endif
+ _defaultVal() {
 	_arrsize = nextTableSize(0);
 	_arr = new Node *[_arrsize];
 	assert(_arr != NULL);
@@ -349,8 +374,11 @@
  * to heap buffers for the internal storage.
  */
 template <class Key, class Val, class HashFunc, class EqualFunc>
-HashMap<Key, Val, HashFunc, EqualFunc>::HashMap(const HM_t& map)
-	: _defaultVal()  {
+HashMap<Key, Val, HashFunc, EqualFunc>::HashMap(const HM_t& map) : 
+#ifdef USE_HASHMAP_MEMORY_POOL
+ _nodePool(sizeof(Node)),
+#endif
+         _defaultVal()  {
 	assign(map);
 }
 

Modified: scummvm/trunk/common/memorypool.cpp
===================================================================
--- scummvm/trunk/common/memorypool.cpp	2008-03-30 05:42:39 UTC (rev 31320)
+++ scummvm/trunk/common/memorypool.cpp	2008-03-30 06:02:34 UTC (rev 31321)
@@ -33,7 +33,7 @@
 
 MemoryPool::~MemoryPool() {
   for(size_t i=0; i<_pages.size(); ++i)
-    free(_pages[i]);
+    ::free(_pages[i]);
 }
 
 void* MemoryPool::malloc() {
@@ -83,12 +83,16 @@
     iterator = *(void**)iterator;
   }
 
+  size_t freedPagesCount = 0;
   for(size_t i=0; i<_pages.size(); ++i) {
     if(numberOfFreeChunksPerPage[i] == CHUNK_PAGE_SIZE) {
-      free(_pages[i]);
+      ::free(_pages[i]);
       _pages[i] = NULL; // TODO : Remove NULL values
+      ++freedPagesCount;
     }
   }
+
+  printf("%d freed pages\n", freedPagesCount); 
 }
 
 }


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