From d4a7ea1b71523115f9baead2d0f98fd8cf517577 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Tue, 20 Oct 2015 00:35:12 -0700 Subject: 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 TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick Reviewed-by: Ian Lance Taylor --- src/runtime/string.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src/runtime/string.go') 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} -- cgit v1.3-5-g9baa