Skip to content

Commit

Permalink
fix(net,linux): move IsLittleEndian to internal
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou committed Jul 4, 2022
1 parent e4e6ed2 commit f11e3ba
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
10 changes: 10 additions & 0 deletions internal/common/endian.go
@@ -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
}
8 changes: 0 additions & 8 deletions net/net.go
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"net"
"unsafe"

"github.com/shirou/gopsutil/v3/internal/common"
)
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion net/net_linux.go
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion net/net_linux_test.go
Expand Up @@ -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,
Expand Down

0 comments on commit f11e3ba

Please sign in to comment.