1 // $G $D/$F.go && $L $F.$A && ./$A.out
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Repeated malloc test.
18 var chatty = flag.Bool("v", false, "chatty")
19 var reverse = flag.Bool("r", false, "reverse")
20 var longtest = flag.Bool("l", false, "long test")
23 var stats = new(runtime.MemStats)
25 func OkAmount(size, n uintptr) bool {
41 func AllocAndFree(size, count int) {
43 fmt.Printf("size=%d count=%d ...\n", size, count)
45 runtime.ReadMemStats(stats)
47 for i := 0; i < count; i++ {
48 b[i] = runtime.Alloc(uintptr(size))
49 base, n := runtime.Lookup(b[i])
50 if base != b[i] || !OkAmount(uintptr(size), n) {
51 println("lookup failed: got", base, n, "for", b[i])
54 runtime.ReadMemStats(stats)
56 println("too much memory allocated")
60 runtime.ReadMemStats(stats)
63 fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
66 for j := 0; j < count; j++ {
71 alloc := uintptr(stats.Alloc)
72 base, n := runtime.Lookup(b[i])
73 if base != b[i] || !OkAmount(uintptr(size), n) {
74 println("lookup failed: got", base, n, "for", b[i])
78 runtime.ReadMemStats(stats)
79 if stats.Alloc != uint64(alloc-n) {
80 println("free alloc got", stats.Alloc, "expected", alloc-n, "after free of", n)
84 println("too much memory allocated")
88 runtime.ReadMemStats(stats)
92 fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
95 println("wrong alloc count: ", n2-n1, n3-n4)
100 func atoi(s string) int {
101 i, _ := strconv.Atoi(s)
106 runtime.MemProfileRate = 0 // disable profiler
108 b = make([]*byte, 10000)
110 AllocAndFree(atoi(flag.Arg(0)), atoi(flag.Arg(1)))
117 for j := 1; j <= maxb; j <<= 1 {
119 max := uintptr(1 << 28)
123 if uintptr(j)*uintptr(n) > max {
124 n = int(max / uintptr(j))
129 for m := 1; m <= n; {