From 77e70865e17399a31813cca47bcaca80f271b341 Mon Sep 17 00:00:00 2001 From: Hugo Santos Date: Wed, 25 Apr 2007 19:21:06 +0000 Subject: [PATCH] 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 --- headers/private/kernel/util/OpenHashTable.h | 43 +++++++++++-------- .../network/protocols/tcp/EndpointManager.cpp | 28 ++++++------ .../network/protocols/tcp/EndpointManager.h | 33 +++++++------- 3 files changed, 58 insertions(+), 46 deletions(-) diff --git a/headers/private/kernel/util/OpenHashTable.h b/headers/private/kernel/util/OpenHashTable.h index 75b4df74c0..ab3cd2c997 100644 --- a/headers/private/kernel/util/OpenHashTable.h +++ b/headers/private/kernel/util/OpenHashTable.h @@ -12,7 +12,7 @@ #include -// 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 // following example: // @@ -23,16 +23,16 @@ // }; // // struct HashTableDefinition { -// typedef void * ParentType; +// typedef void ParentType; // typedef int KeyType; // typedef Foo ValueType; // -// static size_t HashKey(void *parent, int key) { return key >> 1; } -// static size_t Hash(void *parent, Foo *value) { return HashKey(value->bar); } -// static bool Compare(void *parent, int key, Foo *value) -// { return value->bar == key; } -// static HashTableLink *GetLink(void *parent, Foo *value) -// { return value; } +// HashTableDefinition(void *parent) {} +// +// size_t HashKey(int key) { return key >> 1; } +// size_t Hash(Foo *value) { return HashKey(value->bar); } +// bool Compare(int key, Foo *value) { return value->bar == key; } +// HashTableLink *GetLink(Foo *value) { return value; } // }; template @@ -44,7 +44,6 @@ template class OpenHashTable { public: - typedef typename Definition::ParentType ParentType; typedef typename Definition::KeyType KeyType; typedef typename Definition::ValueType ValueType; @@ -57,8 +56,18 @@ public: // regrowth factor: 200 / 256 = 78.125% // 50 / 256 = 19.53125% - OpenHashTable(const ParentType &parent, size_t initialSize = kMinimumSize) - : fParent(parent), fItemCount(0), fTable(NULL) + OpenHashTable(size_t initialSize = kMinimumSize) + : 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) initialSize = kMinimumSize; @@ -75,11 +84,11 @@ public: 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]; while (slot) { - if (Definition::Compare(fParent, key, slot)) + if (fDefinition.Compare(key, slot)) break; slot = _Link(slot)->fNext; } @@ -123,7 +132,7 @@ public: 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]; while (slot) { @@ -158,7 +167,7 @@ public: private: 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]; table[index] = value; @@ -193,10 +202,10 @@ private: HashTableLink *_Link(ValueType *bucket) const { - return Definition::GetLink(fParent, bucket); + return fDefinition.GetLink(bucket); } - ParentType fParent; + Definition fDefinition; size_t fTableSize, fItemCount; ValueType **fTable; }; diff --git a/src/add-ons/kernel/network/protocols/tcp/EndpointManager.cpp b/src/add-ons/kernel/network/protocols/tcp/EndpointManager.cpp index 394ad05483..f58b14733a 100644 --- a/src/add-ons/kernel/network/protocols/tcp/EndpointManager.cpp +++ b/src/add-ons/kernel/network/protocols/tcp/EndpointManager.cpp @@ -29,24 +29,27 @@ static const uint16 kLastReservedPort = 1023; static const uint16 kFirstEphemeralPort = 40000; +ConnectionHashDefinition::ConnectionHashDefinition(EndpointManager *manager) + : fManager(manager) {} + 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); } size_t -ConnectionHashDefinition::Hash(EndpointManager *manager, TCPEndpoint *endpoint) +ConnectionHashDefinition::Hash(TCPEndpoint *endpoint) const { return endpoint->LocalAddress().HashPair(*endpoint->PeerAddress()); } bool -ConnectionHashDefinition::Compare(EndpointManager *manager, const KeyType &key, - TCPEndpoint *endpoint) +ConnectionHashDefinition::Compare(const KeyType &key, + TCPEndpoint *endpoint) const { return endpoint->LocalAddress().EqualTo(key.first, true) && endpoint->PeerAddress().EqualTo(key.second, true); @@ -54,45 +57,42 @@ ConnectionHashDefinition::Compare(EndpointManager *manager, const KeyType &key, HashTableLink * -ConnectionHashDefinition::GetLink(EndpointManager *manager, - TCPEndpoint *endpoint) +ConnectionHashDefinition::GetLink(TCPEndpoint *endpoint) const { return &endpoint->fConnectionHashLink; } size_t -EndpointHashDefinition::HashKey(EndpointManager *manager, uint16 port) +EndpointHashDefinition::HashKey(uint16 port) const { return port; } size_t -EndpointHashDefinition::Hash(EndpointManager *manager, TCPEndpoint *endpoint) +EndpointHashDefinition::Hash(TCPEndpoint *endpoint) const { return endpoint->LocalAddress().GetPort(); } bool -EndpointHashDefinition::Compare(EndpointManager *manager, uint16 port, - TCPEndpoint *endpoint) +EndpointHashDefinition::Compare(uint16 port, TCPEndpoint *endpoint) const { return endpoint->LocalAddress().GetPort() == port; } HashTableLink * -EndpointHashDefinition::GetLink(EndpointManager *manager, - TCPEndpoint *endpoint) +EndpointHashDefinition::GetLink(TCPEndpoint *endpoint) const { return &endpoint->fEndpointHashLink; } EndpointManager::EndpointManager(net_domain *domain) - : fDomain(domain), fConnectionHash(this), fEndpointHash(this) + : fDomain(domain), fConnectionHash(this) { benaphore_init(&fLock, "endpoint manager"); } diff --git a/src/add-ons/kernel/network/protocols/tcp/EndpointManager.h b/src/add-ons/kernel/network/protocols/tcp/EndpointManager.h index e11b379e85..0aa1f9cd27 100644 --- a/src/add-ons/kernel/network/protocols/tcp/EndpointManager.h +++ b/src/add-ons/kernel/network/protocols/tcp/EndpointManager.h @@ -25,30 +25,33 @@ class EndpointManager; class TCPEndpoint; struct ConnectionHashDefinition { - typedef EndpointManager *ParentType; +public: + typedef EndpointManager ParentType; typedef std::pair KeyType; typedef TCPEndpoint ValueType; - static size_t HashKey(EndpointManager *manager, const KeyType &key); - static size_t Hash(EndpointManager *manager, TCPEndpoint *endpoint); - static bool Compare(EndpointManager *manager, const KeyType &key, - TCPEndpoint *endpoint); - static HashTableLink *GetLink(EndpointManager *manager, - TCPEndpoint *endpoint); + ConnectionHashDefinition(EndpointManager *manager); + + size_t HashKey(const KeyType &key) const; + size_t Hash(TCPEndpoint *endpoint) const; + bool Compare(const KeyType &key, TCPEndpoint *endpoint) const; + HashTableLink *GetLink(TCPEndpoint *endpoint) const; + +private: + EndpointManager *fManager; }; -struct EndpointHashDefinition { - typedef EndpointManager *ParentType; +class EndpointHashDefinition { +public: + typedef EndpointManager ParentType; typedef uint16 KeyType; typedef TCPEndpoint ValueType; - static size_t HashKey(EndpointManager *manager, uint16 port); - static size_t Hash(EndpointManager *manager, TCPEndpoint *endpoint); - static bool Compare(EndpointManager *manager, uint16 port, - TCPEndpoint *endpoint); - static HashTableLink *GetLink(EndpointManager *manager, - TCPEndpoint *endpoint); + size_t HashKey(uint16 port) const; + size_t Hash(TCPEndpoint *endpoint) const; + bool Compare(uint16 port, TCPEndpoint *endpoint) const; + HashTableLink *GetLink(TCPEndpoint *endpoint) const; };