Skip to content

Commit

Permalink
fix: mcache Malloc should repect capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Jun 7, 2024
1 parent 21ff3be commit 65caa4f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lang/mcache/mcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func Malloc(size int, capacity ...int) []byte {

sizeIdx := calcIndex(c)
var ret = caches[sizeIdx].Get().([]byte)
ret = ret[:size]
ret = ret[:size:c]
return ret
}

Expand Down
11 changes: 8 additions & 3 deletions lang/mcache/mcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ import (

func TestMalloc(t *testing.T) {
// cached by mcache
size := 4096 - 1
buf := Malloc(size)
size := 128
buf := Malloc(size, size+1)
assert.Equal(t, len(buf), size)
assert.Equal(t, cap(buf), size+1)
_ = buf[:size+1] // resize

// cached by mcache
size = 4096 - 1
buf = Malloc(size)
assert.Equal(t, len(buf), size)
assert.Equal(t, cap(buf), size)
Free(buf)

// cached by mcache
Expand Down

0 comments on commit 65caa4f

Please sign in to comment.