forked from mirror/go-json
Compare commits
1 Commits
master
...
feature/fi
Author | SHA1 | Date |
---|---|---|
Masaaki Goshima | 90fb31ffa0 |
|
@ -1,12 +1,15 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type ptrDecoder struct {
|
||||
dec decoder
|
||||
typ *rtype
|
||||
mu sync.Mutex
|
||||
keepRefs []unsafe.Pointer
|
||||
}
|
||||
|
||||
func newPtrDecoder(dec decoder, typ *rtype) *ptrDecoder {
|
||||
|
@ -14,24 +17,27 @@ func newPtrDecoder(dec decoder, typ *rtype) *ptrDecoder {
|
|||
}
|
||||
|
||||
//go:linkname unsafe_New reflect.unsafe_New
|
||||
func unsafe_New(*rtype) uintptr
|
||||
func unsafe_New(*rtype) unsafe.Pointer
|
||||
|
||||
func (d *ptrDecoder) decodeStream(s *stream, p uintptr) error {
|
||||
newptr := unsafe_New(d.typ)
|
||||
if err := d.dec.decodeStream(s, newptr); err != nil {
|
||||
if err := d.dec.decodeStream(s, uintptr(newptr)); err != nil {
|
||||
return err
|
||||
}
|
||||
**(**uintptr)(unsafe.Pointer(&p)) = newptr
|
||||
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = newptr
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *ptrDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) {
|
||||
d.mu.Lock()
|
||||
newptr := unsafe_New(d.typ)
|
||||
c, err := d.dec.decode(buf, cursor, newptr)
|
||||
d.keepRefs = append(d.keepRefs, newptr)
|
||||
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = newptr
|
||||
d.mu.Unlock()
|
||||
c, err := d.dec.decode(buf, cursor, uintptr(newptr))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
cursor = c
|
||||
**(**uintptr)(unsafe.Pointer(&p)) = newptr
|
||||
return cursor, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue