github.com/eh-steve/goloader/unload@v0.0.0-20240111193454-90ff3cfdae39/jsonunload/clean_cache.go (about)

     1  package jsonunload
     2  
     3  import (
     4  	"reflect"
     5  	"sync"
     6  	"unsafe"
     7  	_ "unsafe"
     8  )
     9  
    10  //go:linkname encoderCache encoding/json.encoderCache
    11  var encoderCache sync.Map // map[reflect.Type]encoderFunc
    12  
    13  type emptyInterface struct {
    14  	typ  unsafe.Pointer
    15  	word unsafe.Pointer
    16  }
    17  
    18  func uncacheTypes(dataStart, dataEnd uintptr) {
    19  	encoderCache.Range(func(key, value any) bool {
    20  		_, ok := key.(reflect.Type)
    21  		if ok {
    22  			eface := (*emptyInterface)(unsafe.Pointer(&key))
    23  			typeAddr := uintptr(eface.word)
    24  			if typeAddr > dataStart && typeAddr < dataEnd {
    25  				encoderCache.Delete(key)
    26  			}
    27  		}
    28  		return true
    29  	})
    30  }