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
5//go:build aix || darwin || freebsd || linux || netbsd || openbsd || solaris || zos
6// +build aix darwin freebsd linux netbsd openbsd solaris zos
7
8package unix
9
10import (
11	"runtime"
12)
13
14// Round the length of a raw sockaddr up to align it properly.
15func cmsgAlignOf(salen int) int {
16	salign := SizeofPtr
17
18	// dragonfly needs to check ABI version at runtime, see cmsgAlignOf in
19	// sockcmsg_dragonfly.go
20	switch runtime.GOOS {
21	case "aix":
22		// There is no alignment on AIX.
23		salign = 1
24	case "darwin", "ios", "illumos", "solaris":
25		// NOTE: It seems like 64-bit Darwin, Illumos and Solaris
26		// kernels still require 32-bit aligned access to network
27		// subsystem.
28		if SizeofPtr == 8 {
29			salign = 4
30		}
31	case "netbsd", "openbsd":
32		// NetBSD and OpenBSD armv7 require 64-bit alignment.
33		if runtime.GOARCH == "arm" {
34			salign = 8
35		}
36		// NetBSD aarch64 requires 128-bit alignment.
37		if runtime.GOOS == "netbsd" && runtime.GOARCH == "arm64" {
38			salign = 16
39		}
40	case "zos":
41		// z/OS socket macros use [32-bit] sizeof(int) alignment,
42		// not pointer width.
43		salign = SizeofInt
44	}
45
46	return (salen + salign - 1) & ^(salign - 1)
47}
48