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" "context"
"encoding/json" "encoding/json"
"net" "net"
"unsafe"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
) )
@ -272,10 +271,3 @@ func getIOCountersAll(n []IOCountersStat) ([]IOCountersStat, error) {
return []IOCountersStat{r}, nil 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 var ip net.IP
if family == syscall.AF_INET { if family == syscall.AF_INET {
if IsLittleEndian() { if common.IsLittleEndian() {
ip = net.IP(ReverseWithContext(ctx, decoded)) ip = net.IP(ReverseWithContext(ctx, decoded))
} else { } else {
ip = net.IP(decoded) ip = net.IP(decoded)

View File

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