summaryrefslogtreecommitdiff
path: root/gcc/testsuite/go.test/test/mallocrand.go
blob: e6b422e2244afaa5607d569e4e1e509a1e0e88df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// $G $D/$F.go && $L $F.$A && ./$A.out

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Random malloc test.

package main

import (
	"flag"
	"rand"
	"runtime"
	"unsafe"
)

var chatty = flag.Bool("v", false, "chatty")

var footprint uint64
var allocated uint64

func bigger() {
	if f := runtime.MemStats.Sys; footprint < f {
		footprint = f
		if *chatty {
			println("Footprint", footprint, " for ", allocated)
		}
		if footprint > 1e9 {
			println("too big")
			panic("fail")
		}
	}
}

// Prime the data structures by allocating one of
// each block in order.  After this, there should be
// little reason to ask for more memory from the OS.
func prime() {
	for i := 0; i < 16; i++ {
		b := runtime.Alloc(1 << uint(i))
		runtime.Free(b)
	}
	for i := uintptr(0); i < 256; i++ {
		b := runtime.Alloc(i << 12)
		runtime.Free(b)
	}
}

func memset(b *byte, c byte, n uintptr) {
	np := uintptr(n)
	for i := uintptr(0); i < np; i++ {
		*(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(b)) + i)) = c
	}
}

func main() {
	flag.Parse()
	//	prime()
	var blocks [1]struct {
		base *byte
		siz  uintptr
	}
	for i := 0; i < 1<<10; i++ {
		if i%(1<<10) == 0 && *chatty {
			println(i)
		}
		b := rand.Int() % len(blocks)
		if blocks[b].base != nil {
			//	println("Free", blocks[b].siz, blocks[b].base)
			runtime.Free(blocks[b].base)
			blocks[b].base = nil
			allocated -= uint64(blocks[b].siz)
			continue
		}
		siz := uintptr(rand.Int() >> (11 + rand.Uint32()%20))
		base := runtime.Alloc(siz)
		//	ptr := uintptr(syscall.BytePtr(base))+uintptr(siz/2)
		//	obj, size, ref, ok := allocator.find(ptr)
		//	if obj != base || *ref != 0 || !ok {
		//		println("find", siz, obj, ref, ok)
		//		panic("fail")
		//	}
		blocks[b].base = base
		blocks[b].siz = siz
		allocated += uint64(siz)
		//	println("Alloc", siz, base)
		memset(base, 0xbb, siz)
		bigger()
	}
}