github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/cmd/link/internal/ld/util.go (about) 1 // Copyright 2015 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. 4 5 package ld 6 7 import ( 8 "bytes" 9 "encoding/binary" 10 "log" 11 "os" 12 "runtime" 13 "runtime/pprof" 14 "strings" 15 "time" 16 ) 17 18 func cstring(x []byte) string { 19 i := bytes.IndexByte(x, '\x00') 20 if i >= 0 { 21 x = x[:i] 22 } 23 return string(x) 24 } 25 26 func tokenize(s string) []string { 27 var f []string 28 for { 29 s = strings.TrimLeft(s, " \t\r\n") 30 if s == "" { 31 break 32 } 33 quote := false 34 i := 0 35 for ; i < len(s); i++ { 36 if s[i] == '\'' { 37 if quote && i+1 < len(s) && s[i+1] == '\'' { 38 i++ 39 continue 40 } 41 quote = !quote 42 } 43 if !quote && (s[i] == ' ' || s[i] == '\t' || s[i] == '\r' || s[i] == '\n') { 44 break 45 } 46 } 47 next := s[:i] 48 s = s[i:] 49 if strings.Contains(next, "'") { 50 var buf []byte 51 quote := false 52 for i := 0; i < len(next); i++ { 53 if next[i] == '\'' { 54 if quote && i+1 < len(next) && next[i+1] == '\'' { 55 i++ 56 buf = append(buf, '\'') 57 } 58 quote = !quote 59 continue 60 } 61 buf = append(buf, next[i]) 62 } 63 next = string(buf) 64 } 65 f = append(f, next) 66 } 67 return f 68 } 69 70 // strings.Compare, introduced in Go 1.5. 71 func stringsCompare(a, b string) int { 72 if a == b { 73 return 0 74 } 75 if a < b { 76 return -1 77 } 78 return +1 79 } 80 81 var atExitFuncs []func() 82 83 func AtExit(f func()) { 84 atExitFuncs = append(atExitFuncs, f) 85 } 86 87 func Exit(code int) { 88 for i := len(atExitFuncs) - 1; i >= 0; i-- { 89 f := atExitFuncs[i] 90 atExitFuncs = atExitFuncs[:i] 91 f() 92 } 93 os.Exit(code) 94 } 95 96 var ( 97 cpuprofile string 98 memprofile string 99 memprofilerate int64 100 ) 101 102 func startProfile() { 103 if cpuprofile != "" { 104 f, err := os.Create(cpuprofile) 105 if err != nil { 106 log.Fatalf("%v", err) 107 } 108 if err := pprof.StartCPUProfile(f); err != nil { 109 log.Fatalf("%v", err) 110 } 111 AtExit(pprof.StopCPUProfile) 112 } 113 if memprofile != "" { 114 if memprofilerate != 0 { 115 runtime.MemProfileRate = int(memprofilerate) 116 } 117 f, err := os.Create(memprofile) 118 if err != nil { 119 log.Fatalf("%v", err) 120 } 121 AtExit(func() { 122 runtime.GC() // profile all outstanding allocations 123 if err := pprof.WriteHeapProfile(f); err != nil { 124 log.Fatalf("%v", err) 125 } 126 }) 127 } 128 } 129 130 func artrim(x []byte) string { 131 i := 0 132 j := len(x) 133 for i < len(x) && x[i] == ' ' { 134 i++ 135 } 136 for j > i && x[j-1] == ' ' { 137 j-- 138 } 139 return string(x[i:j]) 140 } 141 142 func stringtouint32(x []uint32, s string) { 143 for i := 0; len(s) > 0; i++ { 144 var buf [4]byte 145 s = s[copy(buf[:], s):] 146 x[i] = binary.LittleEndian.Uint32(buf[:]) 147 } 148 } 149 150 var start = time.Now() 151 152 func elapsed() float64 { 153 return time.Since(start).Seconds() 154 }