From 04baa40fab044a88f8cf121e3221ed06a6fd7781 Mon Sep 17 00:00:00 2001 From: Derek Collison Date: Wed, 7 Nov 2012 14:40:45 -0800 Subject: [PATCH] Put in bounds for growing and shrinking the buckets --- hashmap/hashmap.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hashmap/hashmap.go b/hashmap/hashmap.go index 8d337adf..7b214748 100644 --- a/hashmap/hashmap.go +++ b/hashmap/hashmap.go @@ -154,7 +154,6 @@ func (h *HashMap) Remove(key []byte) { // resize is responsible for reallocating the buckets and // redistributing the hashmap entries. -// FIXME - can only be max_int big func (h *HashMap) resize(nsz uint32) { nmsk := nsz - 1 bkts := make([]*Entry, nsz) @@ -173,13 +172,22 @@ func (h *HashMap) resize(nsz uint32) { h.msk = nmsk } +const maxBktSize = (1<<31)-1 + // grow the HashMap's buckets by 2 func (h *HashMap) grow() { + // Can't grow beyond maxint for now + if len(h.bkts) >= maxBktSize { + return + } h.resize(uint32(2 * len(h.bkts))) } // shrink the HashMap's buckets by 2 func (h *HashMap) shrink() { + if len(h.bkts) <= _BSZ { + return + } h.resize(uint32(len(h.bkts) / 2)) }