1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 var chatty
= flag
.Bool("v", false, "chatty")
24 memstats
:= new(runtime
.MemStats
)
25 runtime
.ReadMemStats(memstats
)
26 if f
:= memstats
.Sys
; footprint
< f
{
29 println("Footprint", footprint
, " for ", allocated
)
38 // Prime the data structures by allocating one of
39 // each block in order. After this, there should be
40 // little reason to ask for more memory from the OS.
42 for i
:= 0; i
< 16; i
++ {
43 b
:= runtime
.Alloc(1 << uint(i
))
46 for i
:= uintptr(0); i
< 256; i
++ {
47 b
:= runtime
.Alloc(i
<< 12)
52 func memset(b
*byte, c
byte, n
uintptr) {
54 for i
:= uintptr(0); i
< np
; i
++ {
55 *(*byte)(unsafe
.Pointer(uintptr(unsafe
.Pointer(b
)) + i
)) = c
62 var blocks
[1]struct {
66 for i
:= 0; i
< 1<<10; i
++ {
67 if i
%(1<<10) == 0 && *chatty
{
70 b
:= rand
.Int() % len(blocks
)
71 if blocks
[b
].base
!= nil {
72 // println("Free", blocks[b].siz, blocks[b].base)
73 runtime
.Free(blocks
[b
].base
)
75 allocated
-= uint64(blocks
[b
].siz
)
78 siz
:= uintptr(rand
.Int() >> (11 + rand
.Uint32()%20
))
79 base
:= runtime
.Alloc(siz
)
80 // ptr := uintptr(syscall.BytePtr(base))+uintptr(siz/2)
81 // obj, size, ref, ok := allocator.find(ptr)
82 // if obj != base || *ref != 0 || !ok {
83 // println("find", siz, obj, ref, ok)
88 allocated
+= uint64(siz
)
89 // println("Alloc", siz, base)
90 memset(base
, 0xbb, siz
)