Commit ab4df700 authored by Michael Hudson-Doyle's avatar Michael Hudson-Doyle Committed by Ian Lance Taylor

runtime: merge slice and sliceStruct

By removing type slice, renaming type sliceStruct to type slice and
whacking until it compiles.

Has a pleasing net reduction of conversions.

Fixes #10188

Change-Id: I77202b8df637185b632fd7875a1fdd8d52c7a83c
Reviewed-on: https://go-review.googlesource.com/8770Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent f7be77e5
......@@ -281,7 +281,7 @@ func stringHash(s string, seed uintptr) uintptr {
}
func bytesHash(b []byte, seed uintptr) uintptr {
s := (*sliceStruct)(unsafe.Pointer(&b))
s := (*slice)(unsafe.Pointer(&b))
return memhash(s.array, seed, uintptr(s.len))
}
......@@ -305,7 +305,7 @@ func ifaceHash(i interface {
// Testing adapter for memclr
func memclrBytes(b []byte) {
s := (*sliceStruct)(unsafe.Pointer(&b))
s := (*slice)(unsafe.Pointer(&b))
memclr(s.array, uintptr(s.len))
}
......
......@@ -396,8 +396,8 @@ Flush:
}
func uintptrBytes(p []uintptr) (ret []byte) {
pp := (*sliceStruct)(unsafe.Pointer(&p))
rp := (*sliceStruct)(unsafe.Pointer(&ret))
pp := (*slice)(unsafe.Pointer(&p))
rp := (*slice)(unsafe.Pointer(&ret))
rp.array = pp.array
rp.len = pp.len * int(unsafe.Sizeof(p[0]))
......
......@@ -76,8 +76,10 @@ func GCMask(x interface{}) (ret []byte) {
s := (*slice)(unsafe.Pointer(&ret))
systemstack(func() {
var len uintptr
getgcmask(e.data, e._type, &s.array, &len)
s.len = uint(len)
var a *byte
getgcmask(e.data, e._type, &a, &len)
s.array = unsafe.Pointer(a)
s.len = int(len)
s.cap = s.len
})
return
......
......@@ -355,7 +355,7 @@ func typedslicecopy(typ *_type, dst, src slice) int {
if !needwb() {
memmove(dstp, srcp, uintptr(n)*typ.size)
return int(n)
return n
}
systemstack(func() {
......@@ -365,7 +365,7 @@ func typedslicecopy(typ *_type, dst, src slice) int {
// out of the array they point into.
dstp = add(dstp, uintptr(n-1)*typ.size)
srcp = add(srcp, uintptr(n-1)*typ.size)
i := uint(0)
i := 0
for {
typedmemmove(typ, dstp, srcp)
if i++; i >= n {
......@@ -377,7 +377,7 @@ func typedslicecopy(typ *_type, dst, src slice) int {
} else {
// Copy forward, being careful not to move dstp/srcp
// out of the array they point into.
i := uint(0)
i := 0
for {
typedmemmove(typ, dstp, srcp)
if i++; i >= n {
......
......@@ -148,12 +148,12 @@ func recordspan(vh unsafe.Pointer, p unsafe.Pointer) {
}
var new []*mspan
sp := (*slice)(unsafe.Pointer(&new))
sp.array = (*byte)(sysAlloc(uintptr(n)*ptrSize, &memstats.other_sys))
sp.array = sysAlloc(uintptr(n)*ptrSize, &memstats.other_sys)
if sp.array == nil {
throw("runtime: cannot allocate memory")
}
sp.len = uint(len(h_allspans))
sp.cap = uint(n)
sp.len = len(h_allspans)
sp.cap = n
if len(h_allspans) > 0 {
copy(new, h_allspans)
// Don't free the old array if it's referenced by sweep.
......@@ -256,9 +256,9 @@ func mHeap_Init(h *mheap, spans_size uintptr) {
}
sp := (*slice)(unsafe.Pointer(&h_spans))
sp.array = (*byte)(unsafe.Pointer(h.spans))
sp.len = uint(spans_size / ptrSize)
sp.cap = uint(spans_size / ptrSize)
sp.array = unsafe.Pointer(h.spans)
sp.len = int(spans_size / ptrSize)
sp.cap = int(spans_size / ptrSize)
}
func mHeap_MapSpans(h *mheap) {
......
......@@ -13,9 +13,9 @@ type hex uint64
func bytes(s string) (ret []byte) {
rp := (*slice)(unsafe.Pointer(&ret))
sp := (*_string)(noescape(unsafe.Pointer(&s)))
rp.array = sp.str
rp.len = uint(sp.len)
rp.cap = uint(sp.len)
rp.array = unsafe.Pointer(sp.str)
rp.len = sp.len
rp.cap = sp.len
return
}
......
......@@ -87,12 +87,6 @@ type eface struct {
data unsafe.Pointer
}
type slice struct {
array *byte // actual data
len uint // number of elements
cap uint // allocated number of elements
}
// A guintptr holds a goroutine pointer, but typed as a uintptr
// to bypass write barriers. It is used in the Gobuf goroutine state.
//
......
......@@ -159,7 +159,7 @@ func selectdefaultImpl(sel *hselect, callerpc uintptr, so uintptr) {
}
func sellock(sel *hselect) {
lockslice := sliceStruct{unsafe.Pointer(sel.lockorder), int(sel.ncase), int(sel.ncase)}
lockslice := slice{unsafe.Pointer(sel.lockorder), int(sel.ncase), int(sel.ncase)}
lockorder := *(*[]*hchan)(unsafe.Pointer(&lockslice))
var c *hchan
for _, c0 := range lockorder {
......@@ -181,7 +181,7 @@ func selunlock(sel *hselect) {
// Now if the first M touches sel, it will access freed memory.
n := int(sel.ncase)
r := 0
lockslice := sliceStruct{unsafe.Pointer(sel.lockorder), n, n}
lockslice := slice{unsafe.Pointer(sel.lockorder), n, n}
lockorder := *(*[]*hchan)(unsafe.Pointer(&lockslice))
// skip the default case
if n > 0 && lockorder[0] == nil {
......@@ -221,7 +221,7 @@ func selectgoImpl(sel *hselect) (uintptr, uint16) {
print("select: sel=", sel, "\n")
}
scaseslice := sliceStruct{unsafe.Pointer(&sel.scase), int(sel.ncase), int(sel.ncase)}
scaseslice := slice{unsafe.Pointer(&sel.scase), int(sel.ncase), int(sel.ncase)}
scases := *(*[]scase)(unsafe.Pointer(&scaseslice))
var t0 int64
......@@ -241,7 +241,7 @@ func selectgoImpl(sel *hselect) (uintptr, uint16) {
// optimizing (and needing to test).
// generate permuted order
pollslice := sliceStruct{unsafe.Pointer(sel.pollorder), int(sel.ncase), int(sel.ncase)}
pollslice := slice{unsafe.Pointer(sel.pollorder), int(sel.ncase), int(sel.ncase)}
pollorder := *(*[]uint16)(unsafe.Pointer(&pollslice))
for i := 0; i < int(sel.ncase); i++ {
pollorder[i] = uint16(i)
......@@ -255,7 +255,7 @@ func selectgoImpl(sel *hselect) (uintptr, uint16) {
// sort the cases by Hchan address to get the locking order.
// simple heap sort, to guarantee n log n time and constant stack footprint.
lockslice := sliceStruct{unsafe.Pointer(sel.lockorder), int(sel.ncase), int(sel.ncase)}
lockslice := slice{unsafe.Pointer(sel.lockorder), int(sel.ncase), int(sel.ncase)}
lockorder := *(*[]*hchan)(unsafe.Pointer(&lockslice))
for i := 0; i < int(sel.ncase); i++ {
j := i
......
......@@ -8,14 +8,14 @@ import (
"unsafe"
)
type sliceStruct struct {
type slice struct {
array unsafe.Pointer
len int
cap int
}
// TODO: take uintptrs instead of int64s?
func makeslice(t *slicetype, len64 int64, cap64 int64) sliceStruct {
func makeslice(t *slicetype, len64, cap64 int64) slice {
// NOTE: The len > MaxMem/elemsize check here is not strictly necessary,
// but it produces a 'len out of range' error instead of a 'cap out of range' error
// when someone does make([]T, bignumber). 'cap out of range' is true too,
......@@ -30,10 +30,10 @@ func makeslice(t *slicetype, len64 int64, cap64 int64) sliceStruct {
panic(errorString("makeslice: cap out of range"))
}
p := newarray(t.elem, uintptr(cap))
return sliceStruct{p, len, cap}
return slice{p, len, cap}
}
func growslice(t *slicetype, old sliceStruct, n int) sliceStruct {
func growslice(t *slicetype, old slice, n int) slice {
if n < 1 {
panic(errorString("growslice: invalid n"))
}
......@@ -52,7 +52,7 @@ func growslice(t *slicetype, old sliceStruct, n int) sliceStruct {
if et.size == 0 {
// append should not create a slice with nil pointer but non-zero len.
// We assume that append doesn't need to preserve old.array in this case.
return sliceStruct{unsafe.Pointer(&zerobase), old.len, cap}
return slice{unsafe.Pointer(&zerobase), old.len, cap}
}
newcap := old.cap
......@@ -91,10 +91,10 @@ func growslice(t *slicetype, old sliceStruct, n int) sliceStruct {
}
}
return sliceStruct{p, old.len, newcap}
return slice{p, old.len, newcap}
}
func slicecopy(to sliceStruct, fm sliceStruct, width uintptr) int {
func slicecopy(to, fm slice, width uintptr) int {
if fm.len == 0 || to.len == 0 {
return 0
}
......
......@@ -148,7 +148,7 @@ func stringtoslicebytetmp(s string) []byte {
// for i, c := range []byte(str)
str := (*stringStruct)(unsafe.Pointer(&s))
ret := slice{array: (*byte)(str.str), len: uint(str.len), cap: uint(str.len)}
ret := slice{array: unsafe.Pointer(str.str), len: str.len, cap: str.len}
return *(*[]byte)(unsafe.Pointer(&ret))
}
......@@ -266,9 +266,7 @@ func rawstring(size int) (s string, b []byte) {
(*stringStruct)(unsafe.Pointer(&s)).str = p
(*stringStruct)(unsafe.Pointer(&s)).len = size
(*slice)(unsafe.Pointer(&b)).array = (*uint8)(p)
(*slice)(unsafe.Pointer(&b)).len = uint(size)
(*slice)(unsafe.Pointer(&b)).cap = uint(size)
*(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
for {
ms := maxstring
......@@ -286,9 +284,7 @@ func rawbyteslice(size int) (b []byte) {
memclr(add(p, uintptr(size)), cap-uintptr(size))
}
(*slice)(unsafe.Pointer(&b)).array = (*uint8)(p)
(*slice)(unsafe.Pointer(&b)).len = uint(size)
(*slice)(unsafe.Pointer(&b)).cap = uint(cap)
*(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
return
}
......@@ -303,9 +299,7 @@ func rawruneslice(size int) (b []rune) {
memclr(add(p, uintptr(size)*4), mem-uintptr(size)*4)
}
(*slice)(unsafe.Pointer(&b)).array = (*uint8)(p)
(*slice)(unsafe.Pointer(&b)).len = uint(size)
(*slice)(unsafe.Pointer(&b)).cap = uint(mem / 4)
*(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
return
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment