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:
@@ -12,7 +12,7 @@
|
||||
|
||||
#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
|
||||
// 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<Foo> *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<Foo> *GetLink(Foo *value) { return value; }
|
||||
// };
|
||||
|
||||
template<typename Type>
|
||||
@@ -44,7 +44,6 @@ template<typename Definition, bool AutoExpand = true,
|
||||
bool CheckDuplicates = false>
|
||||
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<ValueType> *_Link(ValueType *bucket) const
|
||||
{
|
||||
return Definition::GetLink(fParent, bucket);
|
||||
return fDefinition.GetLink(bucket);
|
||||
}
|
||||
|
||||
ParentType fParent;
|
||||
Definition fDefinition;
|
||||
size_t fTableSize, fItemCount;
|
||||
ValueType **fTable;
|
||||
};
|
||||
|
||||
@@ -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<TCPEndpoint> *
|
||||
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<TCPEndpoint> *
|
||||
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");
|
||||
}
|
||||
|
||||
@@ -25,30 +25,33 @@ class EndpointManager;
|
||||
class TCPEndpoint;
|
||||
|
||||
struct ConnectionHashDefinition {
|
||||
typedef EndpointManager *ParentType;
|
||||
public:
|
||||
typedef EndpointManager ParentType;
|
||||
typedef std::pair<const sockaddr *, const sockaddr *> 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<TCPEndpoint> *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<TCPEndpoint> *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<TCPEndpoint> *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<TCPEndpoint> *GetLink(TCPEndpoint *endpoint) const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user