Sha256: 1550fee9e8608df1b947121c50afe963a8965ea3625ce48b811615f8db31ba47

Contents?: true

Size: 1.78 KB

Versions: 16

Compression:

Stored size: 1.78 KB

Contents

package runtime

import (
	"reflect"
	"unsafe"
)

type SliceHeader struct {
	Data unsafe.Pointer
	Len  int
	Cap  int
}

const (
	maxAcceptableTypeAddrRange = 1024 * 1024 * 2 // 2 Mib
)

type TypeAddr struct {
	BaseTypeAddr uintptr
	MaxTypeAddr  uintptr
	AddrRange    uintptr
	AddrShift    uintptr
}

var (
	typeAddr        *TypeAddr
	alreadyAnalyzed bool
)

//go:linkname typelinks reflect.typelinks
func typelinks() ([]unsafe.Pointer, [][]int32)

//go:linkname rtypeOff reflect.rtypeOff
func rtypeOff(unsafe.Pointer, int32) unsafe.Pointer

func AnalyzeTypeAddr() *TypeAddr {
	defer func() {
		alreadyAnalyzed = true
	}()
	if alreadyAnalyzed {
		return typeAddr
	}
	sections, offsets := typelinks()
	if len(sections) != 1 {
		return nil
	}
	if len(offsets) != 1 {
		return nil
	}
	section := sections[0]
	offset := offsets[0]
	var (
		min         uintptr = uintptr(^uint(0))
		max         uintptr = 0
		isAligned64         = true
		isAligned32         = true
	)
	for i := 0; i < len(offset); i++ {
		typ := (*Type)(rtypeOff(section, offset[i]))
		addr := uintptr(unsafe.Pointer(typ))
		if min > addr {
			min = addr
		}
		if max < addr {
			max = addr
		}
		if typ.Kind() == reflect.Ptr {
			addr = uintptr(unsafe.Pointer(typ.Elem()))
			if min > addr {
				min = addr
			}
			if max < addr {
				max = addr
			}
		}
		isAligned64 = isAligned64 && (addr-min)&63 == 0
		isAligned32 = isAligned32 && (addr-min)&31 == 0
	}
	addrRange := max - min
	if addrRange == 0 {
		return nil
	}
	var addrShift uintptr
	if isAligned64 {
		addrShift = 6
	} else if isAligned32 {
		addrShift = 5
	}
	cacheSize := addrRange >> addrShift
	if cacheSize > maxAcceptableTypeAddrRange {
		return nil
	}
	typeAddr = &TypeAddr{
		BaseTypeAddr: min,
		MaxTypeAddr:  max,
		AddrRange:    addrRange,
		AddrShift:    addrShift,
	}
	return typeAddr
}

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ruby_snowflake_client-1.3.7 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.3.6 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.3.5 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.3.4 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.3.4.pre.debug ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.3.3.pre.debug ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.3.2 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.3.1 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.3.0 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.2.1 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.2.0 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.1.1 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.1.0 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.0.2 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.0.1 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go
ruby_snowflake_client-1.0.0 ext/vendor/github.com/goccy/go-json/internal/runtime/type.go