1 // NodeTable.cpp 2 3 #include "Debug.h" 4 #include "NodeTable.h" 5 6 // constructor 7 NodeTable::NodeTable() 8 : fElementArray(1000), 9 fNodes(1000, &fElementArray) 10 { 11 } 12 13 // destructor 14 NodeTable::~NodeTable() 15 { 16 } 17 18 // InitCheck 19 status_t 20 NodeTable::InitCheck() const 21 { 22 RETURN_ERROR(fNodes.InitCheck() && fElementArray.InitCheck() 23 ? B_OK : B_NO_MEMORY); 24 } 25 26 // AddNode 27 status_t 28 NodeTable::AddNode(Node *node) 29 { 30 status_t error = (node ? B_OK : B_BAD_VALUE); 31 if (error == B_OK) { 32 NodeHashElement *element 33 = fNodes.Add(NodeHashElement::HashForID(node)); 34 if (element) 35 element->fNode = node; 36 else 37 SET_ERROR(error, B_NO_MEMORY); 38 } 39 return error; 40 } 41 42 // RemoveNode 43 status_t 44 NodeTable::RemoveNode(Node *node) 45 { 46 status_t error = (node ? B_OK : B_BAD_VALUE); 47 if (error == B_OK) 48 error = RemoveNode(node->GetID()); 49 return error; 50 } 51 52 // RemoveNode 53 status_t 54 NodeTable::RemoveNode(ino_t id) 55 { 56 status_t error = B_OK; 57 if (NodeHashElement *element = _FindElement(id)) 58 fNodes.Remove(element); 59 else 60 error = B_ERROR; 61 return error; 62 } 63 64 // GetNode 65 Node * 66 NodeTable::GetNode(ino_t id) 67 { 68 Node *node = NULL; 69 if (NodeHashElement *element = _FindElement(id)) 70 node = element->fNode; 71 return node; 72 } 73 74 // GetAllocationInfo 75 void 76 NodeTable::GetAllocationInfo(AllocationInfo &info) 77 { 78 info.AddNodeTableAllocation(fNodes.ArraySize(), fNodes.VectorSize(), 79 sizeof(NodeHashElement), 80 fNodes.CountElements()); 81 } 82 83 // _FindElement 84 NodeHashElement * 85 NodeTable::_FindElement(ino_t id) const 86 { 87 NodeHashElement *element 88 = fNodes.FindFirst(NodeHashElement::HashForID(id)); 89 while (element && element->fNode->GetID() != id) { 90 if (element->fNext >= 0) 91 element = fNodes.ElementAt(element->fNext); 92 else 93 element = NULL; 94 } 95 return element; 96 } 97 98