diff options
| author | chrislu <chris.lu@gmail.com> | 2022-01-22 08:05:04 -0800 |
|---|---|---|
| committer | chrislu <chris.lu@gmail.com> | 2022-01-22 08:05:04 -0800 |
| commit | b9ae16fbc564c29a42606215d3dc4ec3b99a54eb (patch) | |
| tree | 5600ec515b23611b4ced288cc269aa1ae9ad4425 | |
| parent | 1ee828b768100e5f242bbf0146fe5ba50c496914 (diff) | |
| download | seaweedfs-b9ae16fbc564c29a42606215d3dc4ec3b99a54eb.tar.xz seaweedfs-b9ae16fbc564c29a42606215d3dc4ec3b99a54eb.zip | |
fix memory allocation
| -rw-r--r-- | weed/util/mem/slot_pool.go | 2 | ||||
| -rw-r--r-- | weed/util/mem/slot_pool_test.go | 11 |
2 files changed, 12 insertions, 1 deletions
diff --git a/weed/util/mem/slot_pool.go b/weed/util/mem/slot_pool.go index d5a55f23f..5bd759ab7 100644 --- a/weed/util/mem/slot_pool.go +++ b/weed/util/mem/slot_pool.go @@ -14,7 +14,7 @@ const ( func bitCount(size int) (count int) { for ; size > min_size; count++ { - size = size >> 1 + size = (size + 1) >> 1 } return } diff --git a/weed/util/mem/slot_pool_test.go b/weed/util/mem/slot_pool_test.go index 114951522..44f9ec004 100644 --- a/weed/util/mem/slot_pool_test.go +++ b/weed/util/mem/slot_pool_test.go @@ -1,6 +1,7 @@ package mem import ( + "github.com/stretchr/testify/assert" "testing" ) @@ -25,6 +26,16 @@ func TestAllocateFree(t *testing.T) { } +func TestAllocateFreeEdgeCases(t *testing.T) { + assert.Equal(t, 1, bitCount(2048)) + assert.Equal(t, 2, bitCount(2049)) + + buf := Allocate(2048) + Free(buf) + buf = Allocate(2049) + Free(buf) +} + func TestBitCount(t *testing.T) { count := bitCount(12) if count != 0 { |
