2008-12-18 16:42:28 -07:00
|
|
|
// $G $D/$F.go && $L $F.$A && ./$A.out
|
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Random malloc test.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag";
|
|
|
|
"malloc";
|
|
|
|
"rand";
|
|
|
|
"unsafe";
|
|
|
|
)
|
|
|
|
|
2009-01-09 14:42:46 -07:00
|
|
|
var chatty = flag.Bool("v", false, "chatty");
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
|
|
var footprint uint64;
|
|
|
|
var allocated uint64;
|
|
|
|
func bigger() {
|
2009-01-16 17:12:14 -07:00
|
|
|
if f := malloc.GetStats().Sys; footprint < f {
|
2008-12-18 16:42:28 -07:00
|
|
|
footprint = f;
|
2009-01-09 14:42:46 -07:00
|
|
|
if *chatty {
|
2008-12-18 16:42:28 -07:00
|
|
|
println("Footprint", footprint, " for ", allocated);
|
|
|
|
}
|
|
|
|
if footprint > 1e9 {
|
|
|
|
panicln("too big");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 := uint64(0); i < 256; i++ {
|
|
|
|
b := malloc.Alloc(i<<12);
|
|
|
|
malloc.Free(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func memset(b *byte, c byte, n uint64) {
|
|
|
|
np := uintptr(n);
|
|
|
|
for i := uintptr(0); i < np; i++ {
|
2009-01-20 15:40:40 -07:00
|
|
|
*(b.(unsafe.Pointer).(uintptr)+i).(unsafe.Pointer).(*byte) = c;
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse();
|
|
|
|
// prime();
|
|
|
|
var blocks [1] struct { base *byte; siz uint64; };
|
|
|
|
for i := 0; i < 1<<12; i++ {
|
2009-01-09 14:42:46 -07:00
|
|
|
if i%(1<<10) == 0 && *chatty {
|
2008-12-18 16:42:28 -07:00
|
|
|
println(i);
|
|
|
|
}
|
2009-01-16 17:12:14 -07:00
|
|
|
b := rand.Int() % len(blocks);
|
2008-12-18 16:42:28 -07:00
|
|
|
if blocks[b].base != nil {
|
|
|
|
// println("Free", blocks[b].siz, blocks[b].base);
|
|
|
|
malloc.Free(blocks[b].base);
|
|
|
|
blocks[b].base = nil;
|
|
|
|
allocated -= blocks[b].siz;
|
|
|
|
continue
|
|
|
|
}
|
2009-01-16 17:12:14 -07:00
|
|
|
siz := uint64(rand.Int() >> (11 + rand.Uint32() % 20));
|
2008-12-18 16:42:28 -07:00
|
|
|
base := malloc.Alloc(siz);
|
|
|
|
// ptr := uint64(syscall.BytePtr(base))+uint64(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 += siz;
|
|
|
|
// println("Alloc", siz, base);
|
|
|
|
memset(base, 0xbb, siz);
|
|
|
|
bigger();
|
|
|
|
}
|
|
|
|
}
|