package runtime
-import (
- "runtime/internal/atomic"
- "unsafe"
-)
+import "unsafe"
// The constant is known to the compiler.
// There is no fundamental theory behind this number.
*(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
- for {
- ms := maxstring
- if uintptr(size) <= ms || atomic.Casuintptr((*uintptr)(unsafe.Pointer(&maxstring)), ms, uintptr(size)) {
- return
- }
- }
+ return
}
// rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
return l
}
-var maxstring uintptr = 256 // a hint for print
-
//go:nosplit
func gostringnocopy(str *byte) string {
ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
s := *(*string)(unsafe.Pointer(&ss))
- for {
- ms := maxstring
- if uintptr(len(s)) <= ms || atomic.Casuintptr(&maxstring, ms, uintptr(len(s))) {
- break
- }
- }
return 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)
- }
-}
-
func TestCompareTempString(t *testing.T) {
s := strings.Repeat("x", sizeNoStack)
b := []byte(s)