fix(net,linux): move IsLittleEndian to internal

This commit is contained in:
shirou 2022-07-04 08:41:25 +00:00
parent e4e6ed2efa
commit f11e3ba120
4 changed files with 12 additions and 10 deletions

10
internal/common/endian.go Normal file
View File

@ -0,0 +1,10 @@
package common
import "unsafe"
// IsLittleEndian checks if the current platform uses little-endian.
// copied from https://github.com/ntrrg/ntgo/blob/v0.8.0/runtime/infrastructure.go#L16 (MIT License)
func IsLittleEndian() bool {
var x int16 = 0x0011
return *(*byte)(unsafe.Pointer(&x)) == 0x11
}

View File

@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"net"
"unsafe"
"github.com/shirou/gopsutil/v3/internal/common"
)
@ -272,10 +271,3 @@ func getIOCountersAll(n []IOCountersStat) ([]IOCountersStat, error) {
return []IOCountersStat{r}, nil
}
// IsLittleEndian checks if the current platform uses little-endian.
// copied from https://github.com/ntrrg/ntgo/blob/v0.8.0/runtime/infrastructure.go#L16 (MIT License)
func IsLittleEndian() bool {
var x int16 = 0x0011
return *(*byte)(unsafe.Pointer(&x)) == 0x11
}

View File

@ -723,7 +723,7 @@ func decodeAddressWithContext(ctx context.Context, family uint32, src string) (A
var ip net.IP
if family == syscall.AF_INET {
if IsLittleEndian() {
if common.IsLittleEndian() {
ip = net.IP(ReverseWithContext(ctx, decoded))
} else {
ip = net.IP(decoded)

View File

@ -151,7 +151,7 @@ func TestDecodeAddress(t *testing.T) {
Error: true,
},
}
if IsLittleEndian() {
if common.IsLittleEndian() {
addr["0500000A:0016"] = AddrTest{
IP: "10.0.0.5",
Port: 22,