aboutsummaryrefslogtreecommitdiff
path: root/common/hashmap.h
diff options
context:
space:
mode:
authorBertrand Augereau2008-03-30 06:02:34 +0000
committerBertrand Augereau2008-03-30 06:02:34 +0000
commita84d1ea78b5590357c2fb62cdb5d4ccfc3008e8a (patch)
tree37ce385ec0ccfa5fd49d6697b8b82bfe999664ba /common/hashmap.h
parent411a588850604d67e8a606c0496999f5065d35d5 (diff)
downloadscummvm-rg350-a84d1ea78b5590357c2fb62cdb5d4ccfc3008e8a.tar.gz
scummvm-rg350-a84d1ea78b5590357c2fb62cdb5d4ccfc3008e8a.tar.bz2
scummvm-rg350-a84d1ea78b5590357c2fb62cdb5d4ccfc3008e8a.zip
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) svn-id: r31321
Diffstat (limited to 'common/hashmap.h')
-rw-r--r--common/hashmap.h36
1 files changed, 32 insertions, 4 deletions
diff --git a/common/hashmap.h b/common/hashmap.h
index 89b06bd5de..f794fbe405 100644
--- a/common/hashmap.h
+++ b/common/hashmap.h
@@ -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 @@ uint nextTableSize(uint x);
// 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 @@ public:
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 @@ public:
* 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 @@ HashMap<Key, Val, HashFunc, EqualFunc>::HashMap()
* 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);
}