moved the storage requirements (i.e. ParentType *) to OpenHashTable's Definition which we now instantiate per OpenHashTable.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20824 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Hugo Santos
2007-04-25 19:21:06 +00:00
parent 226bb7a9d4
commit 77e70865e1
3 changed files with 58 additions and 46 deletions
+26 -17
View File
@@ -12,7 +12,7 @@
#include <KernelExport.h> #include <KernelExport.h>
// the Definition template must have three methods: `HashKey', `Hash', // the Definition template must have four methods: `HashKey', `Hash',
// `Compare' and `GetLink;. It must also define several types as shown in the // `Compare' and `GetLink;. It must also define several types as shown in the
// following example: // following example:
// //
@@ -23,16 +23,16 @@
// }; // };
// //
// struct HashTableDefinition { // struct HashTableDefinition {
// typedef void * ParentType; // typedef void ParentType;
// typedef int KeyType; // typedef int KeyType;
// typedef Foo ValueType; // typedef Foo ValueType;
// //
// static size_t HashKey(void *parent, int key) { return key >> 1; } // HashTableDefinition(void *parent) {}
// static size_t Hash(void *parent, Foo *value) { return HashKey(value->bar); } //
// static bool Compare(void *parent, int key, Foo *value) // size_t HashKey(int key) { return key >> 1; }
// { return value->bar == key; } // size_t Hash(Foo *value) { return HashKey(value->bar); }
// static HashTableLink<Foo> *GetLink(void *parent, Foo *value) // bool Compare(int key, Foo *value) { return value->bar == key; }
// { return value; } // HashTableLink<Foo> *GetLink(Foo *value) { return value; }
// }; // };
template<typename Type> template<typename Type>
@@ -44,7 +44,6 @@ template<typename Definition, bool AutoExpand = true,
bool CheckDuplicates = false> bool CheckDuplicates = false>
class OpenHashTable { class OpenHashTable {
public: public:
typedef typename Definition::ParentType ParentType;
typedef typename Definition::KeyType KeyType; typedef typename Definition::KeyType KeyType;
typedef typename Definition::ValueType ValueType; typedef typename Definition::ValueType ValueType;
@@ -57,8 +56,18 @@ public:
// regrowth factor: 200 / 256 = 78.125% // regrowth factor: 200 / 256 = 78.125%
// 50 / 256 = 19.53125% // 50 / 256 = 19.53125%
OpenHashTable(const ParentType &parent, size_t initialSize = kMinimumSize) OpenHashTable(size_t initialSize = kMinimumSize)
: fParent(parent), fItemCount(0), fTable(NULL) : fItemCount(0), fTable(NULL)
{
if (initialSize < kMinimumSize)
initialSize = kMinimumSize;
_Resize(initialSize);
}
OpenHashTable(typename Definition::ParentType *parent,
size_t initialSize = kMinimumSize)
: fDefinition(parent), fItemCount(0), fTable(NULL)
{ {
if (initialSize < kMinimumSize) if (initialSize < kMinimumSize)
initialSize = kMinimumSize; initialSize = kMinimumSize;
@@ -75,11 +84,11 @@ public:
ValueType *Lookup(const KeyType &key) const ValueType *Lookup(const KeyType &key) const
{ {
size_t index = Definition::HashKey(fParent, key) & (fTableSize - 1); size_t index = fDefinition.HashKey(key) & (fTableSize - 1);
ValueType *slot = fTable[index]; ValueType *slot = fTable[index];
while (slot) { while (slot) {
if (Definition::Compare(fParent, key, slot)) if (fDefinition.Compare(key, slot))
break; break;
slot = _Link(slot)->fNext; slot = _Link(slot)->fNext;
} }
@@ -123,7 +132,7 @@ public:
void RemoveUnchecked(ValueType *value) void RemoveUnchecked(ValueType *value)
{ {
size_t index = Definition::Hash(fParent, value) & (fTableSize - 1); size_t index = fDefinition.Hash(value) & (fTableSize - 1);
ValueType *previous = NULL, *slot = fTable[index]; ValueType *previous = NULL, *slot = fTable[index];
while (slot) { while (slot) {
@@ -158,7 +167,7 @@ public:
private: private:
void _Insert(ValueType **table, size_t tableSize, ValueType *value) void _Insert(ValueType **table, size_t tableSize, ValueType *value)
{ {
size_t index = Definition::Hash(fParent, value) & (tableSize - 1); size_t index = fDefinition.Hash(value) & (tableSize - 1);
_Link(value)->fNext = table[index]; _Link(value)->fNext = table[index];
table[index] = value; table[index] = value;
@@ -193,10 +202,10 @@ private:
HashTableLink<ValueType> *_Link(ValueType *bucket) const HashTableLink<ValueType> *_Link(ValueType *bucket) const
{ {
return Definition::GetLink(fParent, bucket); return fDefinition.GetLink(bucket);
} }
ParentType fParent; Definition fDefinition;
size_t fTableSize, fItemCount; size_t fTableSize, fItemCount;
ValueType **fTable; ValueType **fTable;
}; };
@@ -29,24 +29,27 @@ static const uint16 kLastReservedPort = 1023;
static const uint16 kFirstEphemeralPort = 40000; static const uint16 kFirstEphemeralPort = 40000;
ConnectionHashDefinition::ConnectionHashDefinition(EndpointManager *manager)
: fManager(manager) {}
size_t size_t
ConnectionHashDefinition::HashKey(EndpointManager *manager, const KeyType &key) ConnectionHashDefinition::HashKey(const KeyType &key) const
{ {
return ConstSocketAddress(manager->AddressModule(), return ConstSocketAddress(fManager->AddressModule(),
key.first).HashPair(key.second); key.first).HashPair(key.second);
} }
size_t size_t
ConnectionHashDefinition::Hash(EndpointManager *manager, TCPEndpoint *endpoint) ConnectionHashDefinition::Hash(TCPEndpoint *endpoint) const
{ {
return endpoint->LocalAddress().HashPair(*endpoint->PeerAddress()); return endpoint->LocalAddress().HashPair(*endpoint->PeerAddress());
} }
bool bool
ConnectionHashDefinition::Compare(EndpointManager *manager, const KeyType &key, ConnectionHashDefinition::Compare(const KeyType &key,
TCPEndpoint *endpoint) TCPEndpoint *endpoint) const
{ {
return endpoint->LocalAddress().EqualTo(key.first, true) return endpoint->LocalAddress().EqualTo(key.first, true)
&& endpoint->PeerAddress().EqualTo(key.second, true); && endpoint->PeerAddress().EqualTo(key.second, true);
@@ -54,45 +57,42 @@ ConnectionHashDefinition::Compare(EndpointManager *manager, const KeyType &key,
HashTableLink<TCPEndpoint> * HashTableLink<TCPEndpoint> *
ConnectionHashDefinition::GetLink(EndpointManager *manager, ConnectionHashDefinition::GetLink(TCPEndpoint *endpoint) const
TCPEndpoint *endpoint)
{ {
return &endpoint->fConnectionHashLink; return &endpoint->fConnectionHashLink;
} }
size_t size_t
EndpointHashDefinition::HashKey(EndpointManager *manager, uint16 port) EndpointHashDefinition::HashKey(uint16 port) const
{ {
return port; return port;
} }
size_t size_t
EndpointHashDefinition::Hash(EndpointManager *manager, TCPEndpoint *endpoint) EndpointHashDefinition::Hash(TCPEndpoint *endpoint) const
{ {
return endpoint->LocalAddress().GetPort(); return endpoint->LocalAddress().GetPort();
} }
bool bool
EndpointHashDefinition::Compare(EndpointManager *manager, uint16 port, EndpointHashDefinition::Compare(uint16 port, TCPEndpoint *endpoint) const
TCPEndpoint *endpoint)
{ {
return endpoint->LocalAddress().GetPort() == port; return endpoint->LocalAddress().GetPort() == port;
} }
HashTableLink<TCPEndpoint> * HashTableLink<TCPEndpoint> *
EndpointHashDefinition::GetLink(EndpointManager *manager, EndpointHashDefinition::GetLink(TCPEndpoint *endpoint) const
TCPEndpoint *endpoint)
{ {
return &endpoint->fEndpointHashLink; return &endpoint->fEndpointHashLink;
} }
EndpointManager::EndpointManager(net_domain *domain) EndpointManager::EndpointManager(net_domain *domain)
: fDomain(domain), fConnectionHash(this), fEndpointHash(this) : fDomain(domain), fConnectionHash(this)
{ {
benaphore_init(&fLock, "endpoint manager"); benaphore_init(&fLock, "endpoint manager");
} }
@@ -25,30 +25,33 @@ class EndpointManager;
class TCPEndpoint; class TCPEndpoint;
struct ConnectionHashDefinition { struct ConnectionHashDefinition {
typedef EndpointManager *ParentType; public:
typedef EndpointManager ParentType;
typedef std::pair<const sockaddr *, const sockaddr *> KeyType; typedef std::pair<const sockaddr *, const sockaddr *> KeyType;
typedef TCPEndpoint ValueType; typedef TCPEndpoint ValueType;
static size_t HashKey(EndpointManager *manager, const KeyType &key); ConnectionHashDefinition(EndpointManager *manager);
static size_t Hash(EndpointManager *manager, TCPEndpoint *endpoint);
static bool Compare(EndpointManager *manager, const KeyType &key, size_t HashKey(const KeyType &key) const;
TCPEndpoint *endpoint); size_t Hash(TCPEndpoint *endpoint) const;
static HashTableLink<TCPEndpoint> *GetLink(EndpointManager *manager, bool Compare(const KeyType &key, TCPEndpoint *endpoint) const;
TCPEndpoint *endpoint); HashTableLink<TCPEndpoint> *GetLink(TCPEndpoint *endpoint) const;
private:
EndpointManager *fManager;
}; };
struct EndpointHashDefinition { class EndpointHashDefinition {
typedef EndpointManager *ParentType; public:
typedef EndpointManager ParentType;
typedef uint16 KeyType; typedef uint16 KeyType;
typedef TCPEndpoint ValueType; typedef TCPEndpoint ValueType;
static size_t HashKey(EndpointManager *manager, uint16 port); size_t HashKey(uint16 port) const;
static size_t Hash(EndpointManager *manager, TCPEndpoint *endpoint); size_t Hash(TCPEndpoint *endpoint) const;
static bool Compare(EndpointManager *manager, uint16 port, bool Compare(uint16 port, TCPEndpoint *endpoint) const;
TCPEndpoint *endpoint); HashTableLink<TCPEndpoint> *GetLink(TCPEndpoint *endpoint) const;
static HashTableLink<TCPEndpoint> *GetLink(EndpointManager *manager,
TCPEndpoint *endpoint);
}; };