ramfs: Adapt NodeTable to use BOpenHashTable.

This commit is contained in:
Augustin Cavalier
2019-08-31 12:28:09 -04:00
parent b4e2eff2f6
commit 565c58a527
3 changed files with 29 additions and 66 deletions
@@ -38,6 +38,8 @@ public:
virtual status_t InitCheck() const;
Node*& HashLink() { return fHashLink; }
inline void SetVolume(Volume *volume) { fVolume = volume; }
inline Volume *GetVolume() const { return fVolume; }
@@ -109,6 +111,7 @@ public:
virtual void GetAllocationInfo(AllocationInfo &info);
private:
Node *fHashLink;
Volume *fVolume;
ino_t fID;
int32 fRefCount;
@@ -8,9 +8,8 @@
// constructor
NodeTable::NodeTable()
: fElementArray(1000),
fNodes(1000, &fElementArray)
{
fInitStatus = fNodes.Init(1000);
}
// destructor
@@ -22,8 +21,7 @@ NodeTable::~NodeTable()
status_t
NodeTable::InitCheck() const
{
RETURN_ERROR(fNodes.InitCheck() && fElementArray.InitCheck()
? B_OK : B_NO_MEMORY);
RETURN_ERROR(fInitStatus);
}
// AddNode
@@ -32,12 +30,9 @@ NodeTable::AddNode(Node *node)
{
status_t error = (node ? B_OK : B_BAD_VALUE);
if (error == B_OK) {
NodeHashElement *element
= fNodes.Add(NodeHashElement::HashForID(node));
if (element)
element->fNode = node;
else
SET_ERROR(error, B_NO_MEMORY);
if (fNodes.Lookup(node->GetID()) != nullptr)
fNodes.Remove(node);
SET_ERROR(error, fNodes.Insert(node));
}
return error;
}
@@ -57,7 +52,7 @@ status_t
NodeTable::RemoveNode(ino_t id)
{
status_t error = B_OK;
if (NodeHashElement *element = _FindElement(id))
if (Node *element = fNodes.Lookup(id))
fNodes.Remove(element);
else
error = B_ERROR;
@@ -68,9 +63,7 @@ NodeTable::RemoveNode(ino_t id)
Node *
NodeTable::GetNode(ino_t id)
{
Node *node = NULL;
if (NodeHashElement *element = _FindElement(id))
node = element->fNode;
Node *node = fNodes.Lookup(id);
return node;
}
@@ -78,23 +71,6 @@ NodeTable::GetNode(ino_t id)
void
NodeTable::GetAllocationInfo(AllocationInfo &info)
{
info.AddNodeTableAllocation(fNodes.ArraySize(), fNodes.VectorSize(),
sizeof(NodeHashElement),
fNodes.CountElements());
info.AddNodeTableAllocation(0, fNodes.TableSize(),
sizeof(Node*), fNodes.CountElements());
}
// _FindElement
NodeHashElement *
NodeTable::_FindElement(ino_t id) const
{
NodeHashElement *element
= fNodes.FindFirst(NodeHashElement::HashForID(id));
while (element && element->fNode->GetID() != id) {
if (element->fNext >= 0)
element = fNodes.ElementAt(element->fNext);
else
element = NULL;
}
return element;
}
@@ -5,44 +5,35 @@
#ifndef NODE_TABLE_H
#define NODE_TABLE_H
#include <util/OpenHashTable.h>
#include "AllocationInfo.h"
#include "Node.h"
#include "OpenHashTable.h"
// NodeHashElement
class NodeHashElement : public OpenHashElement {
public:
NodeHashElement() : OpenHashElement(), fNode(NULL)
// NodeHash
struct NodeHash {
typedef ino_t KeyType;
typedef Node ValueType;
size_t HashKey(KeyType key) const
{
fNext = -1;
return uint32(key & 0xffffffff);
}
static inline uint32 HashForID(ino_t id)
size_t Hash(ValueType* value) const
{
return uint32(id & 0xffffffff);
return HashKey(value->GetID());
}
static inline uint32 HashForID(Node *node)
bool Compare(KeyType key, ValueType* value) const
{
return HashForID(node->GetID());
return value->GetID() == key;
}
inline uint32 Hash() const
ValueType*& GetLink(ValueType* value) const
{
return HashForID(fNode);
return value->HashLink();
}
inline bool operator==(const OpenHashElement &element) const
{
return (static_cast<const NodeHashElement&>(element).fNode == fNode);
}
inline void Adopt(NodeHashElement &element)
{
fNode = element.fNode;
}
Node *fNode;
};
// NodeTable
@@ -62,15 +53,8 @@ public:
void GetAllocationInfo(AllocationInfo &info);
private:
NodeHashElement *_FindElement(ino_t id) const;
private:
OpenHashElementArray<NodeHashElement> fElementArray;
OpenHashTable<NodeHashElement, OpenHashElementArray<NodeHashElement> >
fNodes;
BOpenHashTable<NodeHash> fNodes;
status_t fInitStatus;
};
// undefine the PRINT from <Debug.h>
//#undef PRINT
#endif // NODE_TABLE_H