summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-07-11 13:22:58 +0200
committerAndreas Kling <kling@serenityos.org>2021-07-11 14:14:51 +0200
commit3aabace9f5e887ccba61e47fa0133a3d47b6d97b (patch)
tree8d793fbf827bd026b30cf6f1c4d1c56b33b90649
parent05a00c397815c412cc42f1605051587ba091e7a5 (diff)
downloadserenity-3aabace9f5e887ccba61e47fa0133a3d47b6d97b.zip
AK: Use kfree_sized() in AK::HashTable
-rw-r--r--AK/HashTable.h22
1 files changed, 16 insertions, 6 deletions
diff --git a/AK/HashTable.h b/AK/HashTable.h
index b832232439..9a9921f7d7 100644
--- a/AK/HashTable.h
+++ b/AK/HashTable.h
@@ -129,7 +129,7 @@ public:
m_buckets[i].slot()->~T();
}
- kfree(m_buckets);
+ kfree_sized(m_buckets, size_in_bytes(m_capacity));
}
HashTable(const HashTable& other)
@@ -360,22 +360,32 @@ private:
}
}
+ static size_t size_in_bytes(size_t capacity)
+ {
+ if constexpr (IsOrdered) {
+ return sizeof(BucketType) * capacity;
+ } else {
+ return sizeof(BucketType) * (capacity + 1);
+ }
+ }
+
void rehash(size_t new_capacity)
{
new_capacity = max(new_capacity, static_cast<size_t>(4));
new_capacity = kmalloc_good_size(new_capacity * sizeof(BucketType)) / sizeof(BucketType);
auto* old_buckets = m_buckets;
+ auto old_capacity = m_capacity;
Iterator old_iter = begin();
if constexpr (IsOrdered) {
- m_buckets = (BucketType*)kmalloc(sizeof(BucketType) * (new_capacity));
- __builtin_memset(m_buckets, 0, sizeof(BucketType) * (new_capacity));
+ m_buckets = (BucketType*)kmalloc(size_in_bytes(new_capacity));
+ __builtin_memset(m_buckets, 0, size_in_bytes(new_capacity));
m_collection_data = { nullptr, nullptr };
} else {
- m_buckets = (BucketType*)kmalloc(sizeof(BucketType) * (new_capacity + 1));
- __builtin_memset(m_buckets, 0, sizeof(BucketType) * (new_capacity + 1));
+ m_buckets = (BucketType*)kmalloc(size_in_bytes(new_capacity));
+ __builtin_memset(m_buckets, 0, size_in_bytes(new_capacity));
}
m_capacity = new_capacity;
@@ -392,7 +402,7 @@ private:
it->~T();
}
- kfree(old_buckets);
+ kfree_sized(old_buckets, size_in_bytes(old_capacity));
}
template<typename Finder>