From 3ca39dfc4dd8a2dda5b3a9b8c29e295839490af3 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Thu, 29 Sep 2016 13:28:03 +0200 Subject: sys: add padding to structs again Struct padding was accidentially lost after: 852e3d2eae98a913b7ec91822ba4dc61059a6955 Restore it. Now with tests. Fixes #78 --- sys/decl.go | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'sys/decl.go') diff --git a/sys/decl.go b/sys/decl.go index 2cc8f22e5..f792f541c 100644 --- a/sys/decl.go +++ b/sys/decl.go @@ -252,11 +252,14 @@ type ArrayType struct { } func (t ArrayType) Size() uintptr { - panic("should not be called") + if t.RangeBegin == t.RangeEnd { + return t.RangeBegin * t.Type.Size() + } + return 0 // for trailing embed arrays } func (t ArrayType) Align() uintptr { - panic("should not be called") + return t.Type.Align() } type PtrType struct { @@ -282,11 +285,27 @@ type StructType struct { } func (t *StructType) Size() uintptr { - panic("not called") + if !t.padded { + panic("struct is not padded yet") + } + var size uintptr + for _, f := range t.Fields { + size += f.Size() + } + return size } func (t *StructType) Align() uintptr { - panic("not called") + if t.align != 0 { + return t.align // overrided by user attribute + } + var align uintptr + for _, f := range t.Fields { + if a1 := f.Align(); align < a1 { + align = a1 + } + } + return align } type UnionType struct { @@ -296,11 +315,26 @@ type UnionType struct { } func (t *UnionType) Size() uintptr { - panic("not called") + if t.varlen { + panic("union size is not statically known") + } + size := t.Options[0].Size() + for _, opt := range t.Options { + if size < opt.Size() { + size = opt.Size() + } + } + return size } func (t *UnionType) Align() uintptr { - panic("not called") + var align uintptr + for _, opt := range t.Options { + if a1 := opt.Align(); align < a1 { + align = a1 + } + } + return align } type Dir int @@ -503,6 +537,7 @@ var ( func init() { initCalls() + initStructFields() initResources() initAlign() -- cgit mrf-deployment