mirror of
https://github.com/mochi-mqtt/server.git
synced 2025-10-01 22:42:14 +08:00

On 32-bit systems, `atomic` requires its 64-bit arguments to have 64-bit alignment, but the compiler doesn't help ensure that's the case. In this commit, fields that don't need to hold large numbers have been converted to 32-bit types, which are always aligned correctly on all platforms. For fields that may hold large numeric values, padding has been added to get the necessary alignment, and tests have been added to avoid regressions.
22 lines
416 B
Go
22 lines
416 B
Go
package system
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestInfoAlignment(t *testing.T) {
|
|
typ := reflect.TypeOf(Info{})
|
|
for i := 0; i < typ.NumField(); i++ {
|
|
f := typ.Field(i)
|
|
switch f.Type.Kind() {
|
|
case reflect.Int64, reflect.Uint64:
|
|
require.Equalf(t, uintptr(0), f.Offset%8,
|
|
"%s requires 64-bit alignment for atomic: offset %d",
|
|
f.Name, f.Offset)
|
|
}
|
|
}
|
|
}
|