allocate memory by slabs

This commit is contained in:
Chris Lu
2021-11-27 12:13:00 -08:00
parent e87f276cf6
commit 3a19eea97c
2 changed files with 81 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
package mem
import (
"testing"
)
func TestAllocateFree(t *testing.T) {
buf := Allocate(12)
Free(buf)
if cap(buf) != min_size {
t.Errorf("min size error allocated capacity=%d", cap(buf))
}
if len(buf) != 12 {
t.Errorf("size error")
}
buf = Allocate(4883)
Free(buf)
if cap(buf) != 1024<<bitCount(4883) {
t.Errorf("min size error allocated capacity=%d", cap(buf))
}
if len(buf) != 4883 {
t.Errorf("size error")
}
}
func TestBitCount(t *testing.T) {
count := bitCount(12)
if count != 0 {
t.Errorf("bitCount error count=%d", count)
}
if count != bitCount(min_size) {
t.Errorf("bitCount error count=%d", count)
}
}