sockcmsg_unix_other.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build aix || darwin || freebsd || linux || netbsd || openbsd || solaris || zos
  5. // +build aix darwin freebsd linux netbsd openbsd solaris zos
  6. package unix
  7. import (
  8. "runtime"
  9. )
  10. // Round the length of a raw sockaddr up to align it properly.
  11. func cmsgAlignOf(salen int) int {
  12. salign := SizeofPtr
  13. // dragonfly needs to check ABI version at runtime, see cmsgAlignOf in
  14. // sockcmsg_dragonfly.go
  15. switch runtime.GOOS {
  16. case "aix":
  17. // There is no alignment on AIX.
  18. salign = 1
  19. case "darwin", "ios", "illumos", "solaris":
  20. // NOTE: It seems like 64-bit Darwin, Illumos and Solaris
  21. // kernels still require 32-bit aligned access to network
  22. // subsystem.
  23. if SizeofPtr == 8 {
  24. salign = 4
  25. }
  26. case "netbsd", "openbsd":
  27. // NetBSD and OpenBSD armv7 require 64-bit alignment.
  28. if runtime.GOARCH == "arm" {
  29. salign = 8
  30. }
  31. // NetBSD aarch64 requires 128-bit alignment.
  32. if runtime.GOOS == "netbsd" && runtime.GOARCH == "arm64" {
  33. salign = 16
  34. }
  35. case "zos":
  36. // z/OS socket macros use [32-bit] sizeof(int) alignment,
  37. // not pointer width.
  38. salign = SizeofInt
  39. }
  40. return (salen + salign - 1) & ^(salign - 1)
  41. }