diff options
| author | Matthew Dempsky <mdempsky@google.com> | 2015-10-20 00:35:12 -0700 |
|---|---|---|
| committer | Matthew Dempsky <mdempsky@google.com> | 2015-10-20 23:13:27 +0000 |
| commit | d4a7ea1b71523115f9baead2d0f98fd8cf517577 (patch) | |
| tree | e2c06b78fd0c18d670c95a2b0163108046581fd0 /src/runtime/string.go | |
| parent | ef986fa3fc9b8035151e10658de66873bb25bba5 (diff) | |
| download | go-d4a7ea1b71523115f9baead2d0f98fd8cf517577.tar.xz | |
runtime: add stringStructOf helper function
Instead of open-coding conversions from *string to unsafe.Pointer then
to *stringStruct, add a helper function to add some type safety.
Bonus: This caught two **string values being converted to
*stringStruct in heapdump.go.
While here, get rid of the redundant _string type, but add in a
stringStructDWARF type used for generating DWARF debug info.
Change-Id: I8882f8cca66ac45190270f82019a5d85db023bd2
Reviewed-on: https://go-review.googlesource.com/16131
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/runtime/string.go')
| -rw-r--r-- | src/runtime/string.go | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/runtime/string.go b/src/runtime/string.go index 0b31173b30..680001d8df 100644 --- a/src/runtime/string.go +++ b/src/runtime/string.go @@ -94,7 +94,7 @@ func slicebytetostring(buf *tmpBuf, b []byte) string { // stringDataOnStack reports whether the string's data is // stored on the current goroutine's stack. func stringDataOnStack(s string) bool { - ptr := uintptr((*stringStruct)(unsafe.Pointer(&s)).str) + ptr := uintptr(stringStructOf(&s).str) stk := getg().stack return stk.lo <= ptr && ptr < stk.hi } @@ -147,7 +147,7 @@ func stringtoslicebytetmp(s string) []byte { // The only such case today is: // for i, c := range []byte(str) - str := (*stringStruct)(unsafe.Pointer(&s)) + str := stringStructOf(&s) ret := slice{array: unsafe.Pointer(str.str), len: str.len, cap: str.len} return *(*[]byte)(unsafe.Pointer(&ret)) } @@ -207,6 +207,16 @@ type stringStruct struct { len int } +// Variant with *byte pointer type for DWARF debugging. +type stringStructDWARF struct { + str *byte + len int +} + +func stringStructOf(sp *string) *stringStruct { + return (*stringStruct)(unsafe.Pointer(sp)) +} + func intstring(buf *[4]byte, v int64) string { var s string var b []byte @@ -263,8 +273,8 @@ func stringiter2(s string, k int) (int, rune) { func rawstring(size int) (s string, b []byte) { p := mallocgc(uintptr(size), nil, flagNoScan|flagNoZero) - (*stringStruct)(unsafe.Pointer(&s)).str = p - (*stringStruct)(unsafe.Pointer(&s)).len = size + stringStructOf(&s).str = p + stringStructOf(&s).len = size *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size} |
