OSDN Git Service

PR go/50656
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / go.test / test / mallocrand.go
1 // $G $D/$F.go && $L $F.$A && ./$A.out
2
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.
6
7 // Random malloc test.
8
9 package main
10
11 import (
12         "flag"
13         "math/rand"
14         "runtime"
15         "unsafe"
16 )
17
18 var chatty = flag.Bool("v", false, "chatty")
19
20 var footprint uint64
21 var allocated uint64
22
23 func bigger() {
24         runtime.UpdateMemStats()
25         if f := runtime.MemStats.Sys; footprint < f {
26                 footprint = f
27                 if *chatty {
28                         println("Footprint", footprint, " for ", allocated)
29                 }
30                 if footprint > 1e9 {
31                         println("too big")
32                         panic("fail")
33                 }
34         }
35 }
36
37 // Prime the data structures by allocating one of
38 // each block in order.  After this, there should be
39 // little reason to ask for more memory from the OS.
40 func prime() {
41         for i := 0; i < 16; i++ {
42                 b := runtime.Alloc(1 << uint(i))
43                 runtime.Free(b)
44         }
45         for i := uintptr(0); i < 256; i++ {
46                 b := runtime.Alloc(i << 12)
47                 runtime.Free(b)
48         }
49 }
50
51 func memset(b *byte, c byte, n uintptr) {
52         np := uintptr(n)
53         for i := uintptr(0); i < np; i++ {
54                 *(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(b)) + i)) = c
55         }
56 }
57
58 func main() {
59         flag.Parse()
60         //      prime()
61         var blocks [1]struct {
62                 base *byte
63                 siz  uintptr
64         }
65         for i := 0; i < 1<<10; i++ {
66                 if i%(1<<10) == 0 && *chatty {
67                         println(i)
68                 }
69                 b := rand.Int() % len(blocks)
70                 if blocks[b].base != nil {
71                         //      println("Free", blocks[b].siz, blocks[b].base)
72                         runtime.Free(blocks[b].base)
73                         blocks[b].base = nil
74                         allocated -= uint64(blocks[b].siz)
75                         continue
76                 }
77                 siz := uintptr(rand.Int() >> (11 + rand.Uint32()%20))
78                 base := runtime.Alloc(siz)
79                 //      ptr := uintptr(syscall.BytePtr(base))+uintptr(siz/2)
80                 //      obj, size, ref, ok := allocator.find(ptr)
81                 //      if obj != base || *ref != 0 || !ok {
82                 //              println("find", siz, obj, ref, ok)
83                 //              panic("fail")
84                 //      }
85                 blocks[b].base = base
86                 blocks[b].siz = siz
87                 allocated += uint64(siz)
88                 //      println("Alloc", siz, base)
89                 memset(base, 0xbb, siz)
90                 bigger()
91         }
92 }