From 48452a276d63639f54d3ce1a8d36663e26949526 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Sat, 23 Aug 2014 23:01:59 -0400 Subject: [PATCH] runtime: adjust errorCString definition to avoid allocation The low-level implementation of divide on ARM assumes that it can panic with an error created by newErrorCString without allocating. If we make interface data words require pointer values, the current definition would require an allocation when stored in an interface. Changing the definition to use unsafe.Pointer instead of uintptr avoids the allocation. This change is okay because the field really is a pointer (to a C string in rodata). Update #8405. This should make CL 133830043 safe to try again. LGTM=bradfitz R=golang-codereviews, bradfitz CC=dave, golang-codereviews, r https://golang.org/cl/133820043 --- src/pkg/runtime/error.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/pkg/runtime/error.go b/src/pkg/runtime/error.go index 12fd09eaf9..f379fc443a 100644 --- a/src/pkg/runtime/error.go +++ b/src/pkg/runtime/error.go @@ -4,6 +4,8 @@ package runtime +import "unsafe" + // The Error interface identifies a run time error. type Error interface { error @@ -75,17 +77,19 @@ func newErrorString(s string, ret *interface{}) { } // An errorCString represents a runtime error described by a single C string. -// Not "type errorCString uintptr" because of http://golang.org/issue/7084. -type errorCString struct{ cstr uintptr } +// Not "type errorCString unsafe.Pointer" because of http://golang.org/issue/7084. +// Not uintptr because we want to avoid an allocation if interfaces can't hold +// uintptrs directly (and cstr _is_ a pointer). +type errorCString struct{ cstr unsafe.Pointer } func (e errorCString) RuntimeError() {} func (e errorCString) Error() string { - return "runtime error: " + cstringToGo(e.cstr) + return "runtime error: " + cstringToGo(uintptr(e.cstr)) } // For calling from C. -func newErrorCString(s uintptr, ret *interface{}) { +func newErrorCString(s unsafe.Pointer, ret *interface{}) { *ret = errorCString{s} } -- 2.48.1