Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / gcc / testsuite / go.test / test / mallocrep1.go
blobeb67bed86babd26ed32a3eafb011cab54353bcf5
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.
9 package main
11 import (
12 "flag"
13 "fmt"
14 "runtime"
15 "strconv"
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")
22 var b []*byte
23 var stats = &runtime.MemStats
25 func OkAmount(size, n uintptr) bool {
26 if n < size {
27 return false
29 if size < 16*8 {
30 if n > size+16 {
31 return false
33 } else {
34 if n > size*9/8 {
35 return false
38 return true
41 func AllocAndFree(size, count int) {
42 if *chatty {
43 fmt.Printf("size=%d count=%d ...\n", size, count)
45 n1 := stats.Alloc
46 for i := 0; i < count; i++ {
47 b[i] = runtime.Alloc(uintptr(size))
48 base, n := runtime.Lookup(b[i])
49 if base != b[i] || !OkAmount(uintptr(size), n) {
50 println("lookup failed: got", base, n, "for", b[i])
51 panic("fail")
53 if runtime.MemStats.Sys > 1e9 {
54 println("too much memory allocated")
55 panic("fail")
58 n2 := stats.Alloc
59 if *chatty {
60 fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
62 n3 := stats.Alloc
63 for j := 0; j < count; j++ {
64 i := j
65 if *reverse {
66 i = count - 1 - j
68 alloc := uintptr(stats.Alloc)
69 base, n := runtime.Lookup(b[i])
70 if base != b[i] || !OkAmount(uintptr(size), n) {
71 println("lookup failed: got", base, n, "for", b[i])
72 panic("fail")
74 runtime.Free(b[i])
75 if stats.Alloc != uint64(alloc-n) {
76 println("free alloc got", stats.Alloc, "expected", alloc-n, "after free of", n)
77 panic("fail")
79 if runtime.MemStats.Sys > 1e9 {
80 println("too much memory allocated")
81 panic("fail")
84 n4 := stats.Alloc
86 if *chatty {
87 fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
89 if n2-n1 != n3-n4 {
90 println("wrong alloc count: ", n2-n1, n3-n4)
91 panic("fail")
95 func atoi(s string) int {
96 i, _ := strconv.Atoi(s)
97 return i
100 func main() {
101 runtime.MemProfileRate = 0 // disable profiler
102 flag.Parse()
103 b = make([]*byte, 10000)
104 if flag.NArg() > 0 {
105 AllocAndFree(atoi(flag.Arg(0)), atoi(flag.Arg(1)))
106 return
108 maxb := 1 << 22
109 if !*longtest {
110 maxb = 1 << 19
112 for j := 1; j <= maxb; j <<= 1 {
113 n := len(b)
114 max := uintptr(1 << 28)
115 if !*longtest {
116 max = uintptr(maxb)
118 if uintptr(j)*uintptr(n) > max {
119 n = int(max / uintptr(j))
121 if n < 10 {
122 n = 10
124 for m := 1; m <= n; {
125 AllocAndFree(j, m)
126 if m == n {
127 break
129 m = 5 * m / 4
130 if m < 4 {
133 if m > n {
134 m = n