diff options
author | Ayush Ranjan <ayushranjan@google.com> | 2021-02-08 18:03:29 -0800 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-02-08 18:08:29 -0800 |
commit | cfa4633c3d206aa2f9abdaac60d053162244ee6d (patch) | |
tree | cddf8d20bde9b55bee4d510876b77af92dc635a3 /tools/go_marshal/test/test.go | |
parent | e51f775cbb8db89d1dac6dcf584be5eef1f82d3c (diff) |
[go-marshal] Add dynamic tag in go_marshal.
This makes it easier to implement dynamically sized types in go-marshal. You
really only need to implement MarshalBytes, UnmarshalBytes and SizeBytes to
implement the entire interface.
By using the `dynamic` tag, the autogenerator will generate the rest of the
methods for us.
This change also simplifies how KernelIPTGetEntries implements Marshallable
using the newly added utility.
PiperOrigin-RevId: 356397114
Diffstat (limited to 'tools/go_marshal/test/test.go')
-rw-r--r-- | tools/go_marshal/test/test.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/tools/go_marshal/test/test.go b/tools/go_marshal/test/test.go index e7e3ed74a..b8eb989d9 100644 --- a/tools/go_marshal/test/test.go +++ b/tools/go_marshal/test/test.go @@ -16,6 +16,8 @@ package test import ( + "gvisor.dev/gvisor/pkg/marshal/primitive" + // We're intentionally using a package name alias here even though it's not // necessary to test the code generator's ability to handle package aliases. ex "gvisor.dev/gvisor/tools/go_marshal/test/external" @@ -198,3 +200,36 @@ type Type11 struct { ex.External y int64 } + +// Type12Dynamic is a dynamically sized struct which depends on the autogenerator +// to generate some Marshallable methods for it. +// +// +marshal dynamic +type Type12Dynamic struct { + X primitive.Int64 + Y []primitive.Int64 +} + +// SizeBytes implements marshal.Marshallable.SizeBytes. +func (t *Type12Dynamic) SizeBytes() int { + return (len(t.Y) * 8) + t.X.SizeBytes() +} + +// MarshalBytes implements marshal.Marshallable.MarshalBytes. +func (t *Type12Dynamic) MarshalBytes(dst []byte) { + t.X.MarshalBytes(dst) + dst = dst[t.X.SizeBytes():] + for i, x := range t.Y { + x.MarshalBytes(dst[i*8 : (i+1)*8]) + } +} + +// UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. +func (t *Type12Dynamic) UnmarshalBytes(src []byte) { + t.X.UnmarshalBytes(src) + for i := t.X.SizeBytes(); i < len(src); i += 8 { + var x primitive.Int64 + x.UnmarshalBytes(src[i:]) + t.Y = append(t.Y, x) + } +} |