blob: 0840fe4a57491013b633e989733d44961bb38f53 [file] [log] [blame]
Holger Hildebrandtfa074992020-03-27 15:42:06 +00001// 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
khenaidoo7d3c5582021-08-11 18:09:44 -04005//go:build aix || darwin || freebsd || linux || netbsd || openbsd || solaris || zos
6// +build aix darwin freebsd linux netbsd openbsd solaris zos
Holger Hildebrandtfa074992020-03-27 15:42:06 +00007
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
Andrea Campanellaaec20bd2021-02-25 12:41:34 +010024 case "darwin", "ios", "illumos", "solaris":
Holger Hildebrandtfa074992020-03-27 15:42:06 +000025 // 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 }
Andrea Campanellaaec20bd2021-02-25 12:41:34 +010036 // NetBSD aarch64 requires 128-bit alignment.
37 if runtime.GOOS == "netbsd" && runtime.GOARCH == "arm64" {
38 salign = 16
39 }
khenaidoo7d3c5582021-08-11 18:09:44 -040040 case "zos":
41 // z/OS socket macros use [32-bit] sizeof(int) alignment,
42 // not pointer width.
43 salign = SizeofInt
Holger Hildebrandtfa074992020-03-27 15:42:06 +000044 }
45
46 return (salen + salign - 1) & ^(salign - 1)
47}