1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-06 22:36:25 +02:00

fix #140 uintptr will no lock the address from gc

This commit is contained in:
Tao Wen 2018-01-21 20:59:18 +08:00
parent 0ab880662f
commit 945d1aaa19
2 changed files with 7 additions and 5 deletions

View File

@ -48,7 +48,7 @@ func (decoder *mapDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
} }
iter.ReadMapCB(func(iter *Iterator, keyStr string) bool { iter.ReadMapCB(func(iter *Iterator, keyStr string) bool {
elem := reflect.New(decoder.elemType) elem := reflect.New(decoder.elemType)
decoder.elemDecoder.Decode(unsafe.Pointer(elem.Pointer()), iter) decoder.elemDecoder.Decode(extractInterface(elem.Interface()).word, iter)
// to put into map, we have to use reflection // to put into map, we have to use reflection
keyType := decoder.keyType keyType := decoder.keyType
// TODO: remove this from loop // TODO: remove this from loop

View File

@ -118,8 +118,9 @@ func growOne(slice *sliceHeader, sliceType reflect.Type, elementType reflect.Typ
} }
} }
} }
newVal := reflect.MakeSlice(sliceType, newLen, newCap) newVal := reflect.MakeSlice(sliceType, newLen, newCap).Interface()
dst := unsafe.Pointer(newVal.Pointer()) newValPtr := extractInterface(newVal).word
dst := (*sliceHeader)(newValPtr).Data
// copy old array into new array // copy old array into new array
originalBytesCount := slice.Len * int(elementType.Size()) originalBytesCount := slice.Len * int(elementType.Size())
srcSliceHeader := (unsafe.Pointer)(&sliceHeader{slice.Data, originalBytesCount, originalBytesCount}) srcSliceHeader := (unsafe.Pointer)(&sliceHeader{slice.Data, originalBytesCount, originalBytesCount})
@ -134,8 +135,9 @@ func reuseSlice(slice *sliceHeader, sliceType reflect.Type, expectedCap int) {
if expectedCap <= slice.Cap { if expectedCap <= slice.Cap {
return return
} }
newVal := reflect.MakeSlice(sliceType, 0, expectedCap) newVal := reflect.MakeSlice(sliceType, 0, expectedCap).Interface()
dst := unsafe.Pointer(newVal.Pointer()) newValPtr := extractInterface(newVal).word
dst := (*sliceHeader)(newValPtr).Data
slice.Data = dst slice.Data = dst
slice.Cap = expectedCap slice.Cap = expectedCap
} }