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