github.com/inaneverb/ekacore/ekaunsafe/v4@v4.0.0/runtime.go (about)

     1  // Copyright © 2020-2023. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekaunsafe
     7  
     8  import (
     9  	"encoding/binary"
    10  	"unsafe"
    11  )
    12  
    13  var (
    14  	goRuntimeAddrEnc binary.ByteOrder // uses in GoRuntimeAddressEncoder()
    15  )
    16  
    17  ////////////////////////////////////////////////////////////////////////////////
    18  ////////////////////////////////////////////////////////////////////////////////
    19  ////////////////////////////////////////////////////////////////////////////////
    20  
    21  func init() {
    22  	var x uint64 = 0x6B617479616C7675
    23  	var b = make([]byte, unsafe.Sizeof(x))
    24  
    25  	for i, n := 0, len(b); i < n; i++ {
    26  		b[i] = *(*byte)(unsafe.Add(unsafe.Pointer(&x), i))
    27  	}
    28  
    29  	switch {
    30  	case x == binary.LittleEndian.Uint64(b):
    31  		goRuntimeAddrEnc = binary.LittleEndian
    32  
    33  	case x == binary.BigEndian.Uint64(b):
    34  		goRuntimeAddrEnc = binary.BigEndian
    35  
    36  	default:
    37  		panic("Init: Unknown byte order of storing Go addresses in runtime")
    38  	}
    39  }
    40  
    41  ////////////////////////////////////////////////////////////////////////////////
    42  ////////////////////////////////////////////////////////////////////////////////
    43  ////////////////////////////////////////////////////////////////////////////////
    44  
    45  // GoRuntimeAddressEncoder returns binary.ByteOrder, that is binary.BigEndian
    46  // or binary.LittleEndian, representing how exactly Go saves addresses
    47  // (and other integers) in RAM for current OS.
    48  func GoRuntimeAddressEncoder() binary.ByteOrder {
    49  	return goRuntimeAddrEnc
    50  }