// Prime the data structures by allocating one of // each block in order. After this, there should be // little reason to ask for more memory from the OS. func prime() { for i := 0; i < 16; i++ { b := malloc.Alloc(1 << uint(i)) malloc.Free(b) } for i := uintptr(0); i < 256; i++ { b := malloc.Alloc(i << 12) malloc.Free(b) } }
func main() { flag.Parse() malloc.GetStats().Alloc = 0 // ignore stacks for i := 0; i < 1<<7; i++ { for j := 1; j <= 1<<22; j <<= 1 { if i == 0 && *chatty { println("First alloc:", j) } if a := malloc.GetStats().Alloc; a != 0 { panicln("no allocations but stats report", a, "bytes allocated") } b := malloc.Alloc(uintptr(j)) during := malloc.GetStats().Alloc malloc.Free(b) if a := malloc.GetStats().Alloc; a != 0 { panic("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)") } bigger() } if i%(1<<10) == 0 && *chatty { println(i) } if i == 0 { if *chatty { println("Primed", i) } // malloc.frozen = true; } } }
func main() { flag.Parse() // prime(); var blocks [1]struct { base *byte siz uintptr } for i := 0; i < 1<<10; i++ { if i%(1<<10) == 0 && *chatty { println(i) } b := rand.Int() % len(blocks) if blocks[b].base != nil { // println("Free", blocks[b].siz, blocks[b].base); malloc.Free(blocks[b].base) blocks[b].base = nil allocated -= uint64(blocks[b].siz) continue } siz := uintptr(rand.Int() >> (11 + rand.Uint32()%20)) base := malloc.Alloc(siz) // ptr := uintptr(syscall.BytePtr(base))+uintptr(siz/2); // obj, size, ref, ok := allocator.find(ptr); // if obj != base || *ref != 0 || !ok { // panicln("find", siz, obj, ref, ok); // } blocks[b].base = base blocks[b].siz = siz allocated += uint64(siz) // println("Alloc", siz, base); memset(base, 0xbb, siz) bigger() } }
func AllocAndFree(size, count int) { if *chatty { fmt.Printf("size=%d count=%d ...\n", size, count) } n1 := stats.Alloc for i := 0; i < count; i++ { b[i] = malloc.Alloc(uintptr(size)) base, n := malloc.Lookup(b[i]) if base != b[i] || !OkAmount(uintptr(size), n) { panicln("lookup failed: got", base, n, "for", b[i]) } if malloc.GetStats().Sys > 1e9 { panicln("too much memory allocated") } } n2 := stats.Alloc if *chatty { fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats) } n3 := stats.Alloc for j := 0; j < count; j++ { i := j if *reverse { i = count - 1 - j } alloc := uintptr(stats.Alloc) base, n := malloc.Lookup(b[i]) if base != b[i] || !OkAmount(uintptr(size), n) { panicln("lookup failed: got", base, n, "for", b[i]) } malloc.Free(b[i]) if stats.Alloc != uint64(alloc-n) { panicln("free alloc got", stats.Alloc, "expected", alloc-n, "after free of", n) } if malloc.GetStats().Sys > 1e9 { panicln("too much memory allocated") } } n4 := stats.Alloc if *chatty { fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats) } if n2-n1 != n3-n4 { panicln("wrong alloc count: ", n2-n1, n3-n4) } }
func main() { malloc.Free(malloc.Alloc(1)) if *chatty { fmt.Printf("%+v %v\n", *malloc.GetStats(), uint64(0)) } }