From 63fdcb8e7e09f975bd53e7fc18bc9ff3cfe1294c Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Mon, 12 Jan 2015 18:20:22 +0100 Subject: [PATCH] ext2: migrate to BOpenHashTable. --- .../file_systems/ext2/HashRevokeManager.cpp | 39 ++++++++----------- .../file_systems/ext2/HashRevokeManager.h | 35 +++++++++++++++-- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/add-ons/kernel/file_systems/ext2/HashRevokeManager.cpp b/src/add-ons/kernel/file_systems/ext2/HashRevokeManager.cpp index 10f3981714..9f76ece87b 100644 --- a/src/add-ons/kernel/file_systems/ext2/HashRevokeManager.cpp +++ b/src/add-ons/kernel/file_systems/ext2/HashRevokeManager.cpp @@ -33,16 +33,16 @@ HashRevokeManager::~HashRevokeManager() { if (fHash != NULL) { if (fRevokeCount != 0) { - RevokeElement *element = - (RevokeElement*)hash_remove_first(fHash, NULL); + RevokeElement *element = fHash->Clear(true); while (element != NULL) { + RevokeElement* next = element->next; delete element; - element = (RevokeElement*)hash_remove_first(fHash, NULL); + element = next; } } - hash_uninit(fHash); + delete fHash; } } @@ -50,13 +50,9 @@ HashRevokeManager::~HashRevokeManager() status_t HashRevokeManager::Init() { - RevokeElement dummyElement; - - fHash = hash_init(kInitialHashSize, offset_of_member(dummyElement, next), - &HashRevokeManager::Compare, - &HashRevokeManager::Hash); + fHash = new(std::nothrow) RevokeTable(); - if (fHash == NULL) + if (fHash == NULL || fHash->Init(kInitialHashSize) != B_OK) return B_NO_MEMORY; return B_OK; @@ -66,20 +62,19 @@ HashRevokeManager::Init() status_t HashRevokeManager::Insert(uint32 block, uint32 commitID) { - RevokeElement* element = (RevokeElement*)hash_lookup(fHash, &block); + RevokeElement* element = fHash->Lookup(block); if (element != NULL) { TRACE("HashRevokeManager::Insert(): Already has an element\n"); if (element->commitID < commitID) { TRACE("HashRevokeManager::Insert(): Deleting previous element\n"); - status_t retValue = hash_remove(fHash, element); + bool retValue = fHash->Remove(element); - if (retValue != B_OK) - return retValue; + if (!retValue) + return B_ERROR; delete element; - } - else { + } else { return B_OK; // We already have a newer version of the block } @@ -92,24 +87,24 @@ HashRevokeManager::Insert(uint32 block, uint32 commitID) status_t HashRevokeManager::Remove(uint32 block) { - RevokeElement* element = (RevokeElement*)hash_lookup(fHash, &block); + RevokeElement* element = fHash->Lookup(block); if (element == NULL) return B_ERROR; // TODO: Perhaps we should just ignore? - status_t retValue = hash_remove(fHash, element); + bool retValue = fHash->Remove(element); - if (retValue == B_OK) + if (retValue) delete element; - return retValue; + return B_ERROR; } bool HashRevokeManager::Lookup(uint32 block, uint32 commitID) { - RevokeElement* element = (RevokeElement*)hash_lookup(fHash, &block); + RevokeElement* element = fHash->Lookup(block); if (element == NULL) return false; @@ -157,7 +152,7 @@ HashRevokeManager::_ForceInsert(uint32 block, uint32 commitID) element->block = block; element->commitID = commitID; - status_t retValue = hash_insert_grow(fHash, element); + status_t retValue = fHash->Insert(element); if (retValue == B_OK) { fRevokeCount++; diff --git a/src/add-ons/kernel/file_systems/ext2/HashRevokeManager.h b/src/add-ons/kernel/file_systems/ext2/HashRevokeManager.h index ab1f3c4158..0be41abed5 100644 --- a/src/add-ons/kernel/file_systems/ext2/HashRevokeManager.h +++ b/src/add-ons/kernel/file_systems/ext2/HashRevokeManager.h @@ -8,7 +8,7 @@ #ifndef HASHREVOKEMANAGER_H #define HASHREVOKEMANAGER_H -#include +#include #include "RevokeManager.h" @@ -20,6 +20,35 @@ struct RevokeElement { }; +struct RevokeHash { + typedef uint32 KeyType; + typedef RevokeElement ValueType; + + size_t HashKey(KeyType key) const + { + return key; + } + + size_t Hash(ValueType* value) const + { + return HashKey(value->block); + } + + bool Compare(KeyType key, ValueType* value) const + { + return value->block == key; + } + + ValueType*& GetLink(ValueType* value) const + { + return value->next; + } + +}; + +typedef BOpenHashTable RevokeTable; + + class HashRevokeManager : public RevokeManager { public: HashRevokeManager(); @@ -30,7 +59,7 @@ public: virtual status_t Insert(uint32 block, uint32 commitID); virtual status_t Remove(uint32 block); virtual bool Lookup(uint32 block, uint32 commitID); - + static int Compare(void* element, const void* key); static uint32 Hash(void* element, const void* key, uint32 range); @@ -38,7 +67,7 @@ protected: status_t _ForceInsert(uint32 block, uint32 commitID); private: - hash_table* fHash; + RevokeTable* fHash; const int kInitialHashSize; };