Commit e00e6563 authored by Sebastien Binet's avatar Sebastien Binet Committed by Keith Randall

reflect: use arrayAt consistently

This change refactors reflect.Value to consistently use arrayAt when an element
of an array of bytes is indexed.

This effectively replaces:
 arr := unsafe.Pointer(...)
 arri := unsafe.Pointer(uintptr(arr) + uintptr(i)*elementSize)

with:
 arr := unsafe.Pointer(...)
 arri := arrayAt(arr, i, elementSize)

Change-Id: I53ffd0d6de693b43d5c10c0aa4cd6d4f5e95a1e3
Reviewed-on: https://go-review.googlesource.com/9183Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: 's avatarKeith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 873483c6
...@@ -848,7 +848,7 @@ func (v Value) Index(i int) Value { ...@@ -848,7 +848,7 @@ func (v Value) Index(i int) Value {
} }
tt := (*sliceType)(unsafe.Pointer(v.typ)) tt := (*sliceType)(unsafe.Pointer(v.typ))
typ := tt.elem typ := tt.elem
val := unsafe.Pointer(uintptr(s.Data) + uintptr(i)*typ.size) val := arrayAt(s.Data, i, typ.size)
fl := flagAddr | flagIndir | v.flag&flagRO | flag(typ.Kind()) fl := flagAddr | flagIndir | v.flag&flagRO | flag(typ.Kind())
return Value{typ, val, fl} return Value{typ, val, fl}
...@@ -857,7 +857,7 @@ func (v Value) Index(i int) Value { ...@@ -857,7 +857,7 @@ func (v Value) Index(i int) Value {
if uint(i) >= uint(s.Len) { if uint(i) >= uint(s.Len) {
panic("reflect: string index out of range") panic("reflect: string index out of range")
} }
p := unsafe.Pointer(uintptr(s.Data) + uintptr(i)) p := arrayAt(s.Data, i, 1)
fl := v.flag&flagRO | flag(Uint8) | flagIndir fl := v.flag&flagRO | flag(Uint8) | flagIndir
return Value{uint8Type, p, fl} return Value{uint8Type, p, fl}
} }
...@@ -1540,7 +1540,7 @@ func (v Value) Slice(i, j int) Value { ...@@ -1540,7 +1540,7 @@ func (v Value) Slice(i, j int) Value {
if i < 0 || j < i || j > s.Len { if i < 0 || j < i || j > s.Len {
panic("reflect.Value.Slice: string slice index out of bounds") panic("reflect.Value.Slice: string slice index out of bounds")
} }
t := stringHeader{unsafe.Pointer(uintptr(s.Data) + uintptr(i)), j - i} t := stringHeader{arrayAt(s.Data, i, 1), j - i}
return Value{v.typ, unsafe.Pointer(&t), v.flag} return Value{v.typ, unsafe.Pointer(&t), v.flag}
} }
...@@ -1556,7 +1556,7 @@ func (v Value) Slice(i, j int) Value { ...@@ -1556,7 +1556,7 @@ func (v Value) Slice(i, j int) Value {
s.Len = j - i s.Len = j - i
s.Cap = cap - i s.Cap = cap - i
if cap-i > 0 { if cap-i > 0 {
s.Data = unsafe.Pointer(uintptr(base) + uintptr(i)*typ.elem.Size()) s.Data = arrayAt(base, i, typ.elem.Size())
} else { } else {
// do not advance pointer, to avoid pointing beyond end of slice // do not advance pointer, to avoid pointing beyond end of slice
s.Data = base s.Data = base
...@@ -1608,7 +1608,7 @@ func (v Value) Slice3(i, j, k int) Value { ...@@ -1608,7 +1608,7 @@ func (v Value) Slice3(i, j, k int) Value {
s.Len = j - i s.Len = j - i
s.Cap = k - i s.Cap = k - i
if k-i > 0 { if k-i > 0 {
s.Data = unsafe.Pointer(uintptr(base) + uintptr(i)*typ.elem.Size()) s.Data = arrayAt(base, i, typ.elem.Size())
} else { } else {
// do not advance pointer, to avoid pointing beyond end of slice // do not advance pointer, to avoid pointing beyond end of slice
s.Data = base s.Data = base
......
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