From fc06a3f822d77a81d236a461d5d9e1180e8a01c1 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Fri, 22 Aug 2008 01:10:24 +0000 Subject: [PATCH] Added methods that allow asynchronous resizing of the hash table. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27121 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/kernel/util/OpenHashTable.h | 52 ++++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/headers/private/kernel/util/OpenHashTable.h b/headers/private/kernel/util/OpenHashTable.h index da4c87be8d..6cf2fcb8ae 100644 --- a/headers/private/kernel/util/OpenHashTable.h +++ b/headers/private/kernel/util/OpenHashTable.h @@ -167,6 +167,49 @@ public: return true; } + /*! If the table needs resizing, the number of bytes for the required + allocation is returned. If no resizing is needed, 0 is returned. + */ + size_t ResizeNeeded() const + { + size_t size = fTableSize; + if (size == 0 || fItemCount >= (size * 200 / 256)) { + // grow table + if (size == 0) + size = kMinimumSize; + while (fItemCount >= size * 200 / 256) + size <<= 1; + } else if (size > kMinimumSize && fItemCount < size * 50 / 256) { + // shrink table + while (fItemCount < size * 50 / 256) + size >>= 1; + if (size < kMinimumSize) + size = kMinimumSize; + } + + if (size == fTableSize) + return 0; + + return size * sizeof(ValueType*); + } + + /*! Resizes the table using the given allocation. The allocation must not + be \c NULL. It must be of size \a size, which must a value returned + earlier by ResizeNeeded(). If the size requirements have changed in the + meantime, the method free()s the given allocation and returns \c false. + Otherwise \c true is returned. + */ + bool Resize(void* allocation, size_t size) + { + if (size != ResizeNeeded()) { + free(allocation); + return false; + } + + _Resize((ValueType**)allocation, size / sizeof(ValueType*)); + return true; + } + class Iterator { public: Iterator(const HashTable *table) @@ -228,11 +271,17 @@ protected: bool _Resize(size_t newSize) { - ValueType **newTable + ValueType** newTable = (ValueType**)malloc(sizeof(ValueType*) * newSize); if (newTable == NULL) return false; + _Resize(newTable, newSize); + return true; + } + + void _Resize(ValueType** newTable, size_t newSize) + { for (size_t i = 0; i < newSize; i++) newTable[i] = NULL; @@ -251,7 +300,6 @@ protected: fTableSize = newSize; fTable = newTable; - return true; } HashTableLink *_Link(ValueType *bucket) const