]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: make gostringnocopy update maxstring
authorKeith Randall <khr@golang.org>
Thu, 11 Sep 2014 23:53:34 +0000 (16:53 -0700)
committerKeith Randall <khr@golang.org>
Thu, 11 Sep 2014 23:53:34 +0000 (16:53 -0700)
Fixes #8706

LGTM=josharian
R=josharian
CC=golang-codereviews
https://golang.org/cl/143880043

src/runtime/export_test.go
src/runtime/string.c
src/runtime/string_test.go

index 1f1b5fc794e38fa59e7523eb7fb150fa489f6033..be352557fbd17b531622b05e748c5c8692493988 100644 (file)
@@ -160,3 +160,6 @@ func GostringW(w []uint16) (s string) {
        })
        return
 }
+
+var Gostringnocopy = gostringnocopy
+var Maxstring = &maxstring
index 811a289060930ddc10503cc900d799c29b82f264..ed5debc33e00fb023fc520f47670fbd6264b9331 100644 (file)
@@ -42,10 +42,15 @@ String
 runtime·gostringnocopy(byte *str)
 {
        String s;
+       uintptr ms;
        
        s.str = str;
        s.len = runtime·findnull(str);
-       return s;
+       while(true) {
+               ms = runtime·maxstring;
+               if(s.len <= ms || runtime·casp((void**)&runtime·maxstring, (void*)ms, (void*)s.len))
+                       return s;
+       }
 }
 
 // TODO: move this elsewhere
index e7ac51a5f0ba6978a6dc7dd255b8340857977afa..1551ecc82b68a7a30be2b017fa480092a644c25b 100644 (file)
@@ -145,3 +145,16 @@ func main() {
        panic(s)
 }
 `
+
+func TestGostringnocopy(t *testing.T) {
+       max := *runtime.Maxstring
+       b := make([]byte, max+10)
+       for i := uintptr(0); i < max+9; i++ {
+               b[i] = 'a'
+       }
+       _ = runtime.Gostringnocopy(&b[0])
+       newmax := *runtime.Maxstring
+       if newmax != max+9 {
+               t.Errorf("want %d, got %d", max+9, newmax)
+       }
+}