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.
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) {
42 defer runtime
.GOMAXPROCS(runtime
.GOMAXPROCS(1))
44 fmt
.Printf("size=%d count=%d ...\n", size
, count
)
46 runtime
.ReadMemStats(stats
)
48 for i
:= 0; i
< count
; i
++ {
49 b
[i
] = runtime
.Alloc(uintptr(size
))
50 base
, n
:= runtime
.Lookup(b
[i
])
51 if base
!= b
[i
] ||
!OkAmount(uintptr(size
), n
) {
52 println("lookup failed: got", base
, n
, "for", b
[i
])
55 runtime
.ReadMemStats(stats
)
57 println("too much memory allocated")
61 runtime
.ReadMemStats(stats
)
64 fmt
.Printf("size=%d count=%d stats=%+v\n", size
, count
, *stats
)
67 for j
:= 0; j
< count
; j
++ {
72 alloc
:= uintptr(stats
.Alloc
)
73 base
, n
:= runtime
.Lookup(b
[i
])
74 if base
!= b
[i
] ||
!OkAmount(uintptr(size
), n
) {
75 println("lookup failed: got", base
, n
, "for", b
[i
])
79 runtime
.ReadMemStats(stats
)
80 if stats
.Alloc
!= uint64(alloc
-n
) {
81 println("free alloc got", stats
.Alloc
, "expected", alloc
-n
, "after free of", n
)
85 println("too much memory allocated")
89 runtime
.ReadMemStats(stats
)
93 fmt
.Printf("size=%d count=%d stats=%+v\n", size
, count
, *stats
)
96 println("wrong alloc count: ", n2
-n1
, n3
-n4
)
101 func atoi(s
string) int {
102 i
, _
:= strconv
.Atoi(s
)
107 runtime
.MemProfileRate
= 0 // disable profiler
109 b
= make([]*byte, 10000)
111 AllocAndFree(atoi(flag
.Arg(0)), atoi(flag
.Arg(1)))
118 for j
:= 1; j
<= maxb
; j
<<= 1 {
120 max
:= uintptr(1 << 28)
124 if uintptr(j
)*uintptr(n
) > max
{
125 n
= int(max
/ uintptr(j
))
130 for m
:= 1; m
<= n
; {