diff options
Diffstat (limited to 'pkg/marshal')
-rw-r--r-- | pkg/marshal/marshal.go | 14 | ||||
-rw-r--r-- | pkg/marshal/marshal_impl_util.go | 8 | ||||
-rw-r--r-- | pkg/marshal/primitive/primitive.go | 16 | ||||
-rw-r--r-- | pkg/marshal/primitive/primitive_abi_autogen_unsafe.go | 144 |
4 files changed, 116 insertions, 66 deletions
diff --git a/pkg/marshal/marshal.go b/pkg/marshal/marshal.go index 7da450ce8..9e34eae80 100644 --- a/pkg/marshal/marshal.go +++ b/pkg/marshal/marshal.go @@ -59,13 +59,15 @@ type Marshallable interface { // likely make use of the type of these fields). SizeBytes() int - // MarshalBytes serializes a copy of a type to dst. + // MarshalBytes serializes a copy of a type to dst and returns the remaining + // buffer. // Precondition: dst must be at least SizeBytes() in length. - MarshalBytes(dst []byte) + MarshalBytes(dst []byte) []byte - // UnmarshalBytes deserializes a type from src. + // UnmarshalBytes deserializes a type from src and returns the remaining + // buffer. // Precondition: src must be at least SizeBytes() in length. - UnmarshalBytes(src []byte) + UnmarshalBytes(src []byte) []byte // Packed returns true if the marshalled size of the type is the same as the // size it occupies in memory. This happens when the type has no fields @@ -86,7 +88,7 @@ type Marshallable interface { // return false, MarshalUnsafe should fall back to the safer but slower // MarshalBytes. // Precondition: dst must be at least SizeBytes() in length. - MarshalUnsafe(dst []byte) + MarshalUnsafe(dst []byte) []byte // UnmarshalUnsafe deserializes a type by directly copying to the underlying // memory allocated for the object by the runtime. @@ -96,7 +98,7 @@ type Marshallable interface { // UnmarshalUnsafe should fall back to the safer but slower unmarshal // mechanism implemented in UnmarshalBytes. // Precondition: src must be at least SizeBytes() in length. - UnmarshalUnsafe(src []byte) + UnmarshalUnsafe(src []byte) []byte // CopyIn deserializes a Marshallable type from a task's memory. This may // only be called from a task goroutine. This is more efficient than calling diff --git a/pkg/marshal/marshal_impl_util.go b/pkg/marshal/marshal_impl_util.go index 9e6a6fa29..6c1cf7a4c 100644 --- a/pkg/marshal/marshal_impl_util.go +++ b/pkg/marshal/marshal_impl_util.go @@ -38,12 +38,12 @@ func (StubMarshallable) SizeBytes() int { } // MarshalBytes implements Marshallable.MarshalBytes. -func (StubMarshallable) MarshalBytes(dst []byte) { +func (StubMarshallable) MarshalBytes(dst []byte) []byte { panic("Please implement your own MarshalBytes function") } // UnmarshalBytes implements Marshallable.UnmarshalBytes. -func (StubMarshallable) UnmarshalBytes(src []byte) { +func (StubMarshallable) UnmarshalBytes(src []byte) []byte { panic("Please implement your own UnmarshalBytes function") } @@ -53,12 +53,12 @@ func (StubMarshallable) Packed() bool { } // MarshalUnsafe implements Marshallable.MarshalUnsafe. -func (StubMarshallable) MarshalUnsafe(dst []byte) { +func (StubMarshallable) MarshalUnsafe(dst []byte) []byte { panic("Please implement your own MarshalUnsafe function") } // UnmarshalUnsafe implements Marshallable.UnmarshalUnsafe. -func (StubMarshallable) UnmarshalUnsafe(src []byte) { +func (StubMarshallable) UnmarshalUnsafe(src []byte) []byte { panic("Please implement your own UnmarshalUnsafe function") } diff --git a/pkg/marshal/primitive/primitive.go b/pkg/marshal/primitive/primitive.go index 1c49cf082..7ece26933 100644 --- a/pkg/marshal/primitive/primitive.go +++ b/pkg/marshal/primitive/primitive.go @@ -76,13 +76,13 @@ func (b *ByteSlice) SizeBytes() int { } // MarshalBytes implements marshal.Marshallable.MarshalBytes. -func (b *ByteSlice) MarshalBytes(dst []byte) { - copy(dst, *b) +func (b *ByteSlice) MarshalBytes(dst []byte) []byte { + return dst[copy(dst, *b):] } // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. -func (b *ByteSlice) UnmarshalBytes(src []byte) { - copy(*b, src) +func (b *ByteSlice) UnmarshalBytes(src []byte) []byte { + return src[copy(*b, src):] } // Packed implements marshal.Marshallable.Packed. @@ -91,13 +91,13 @@ func (b *ByteSlice) Packed() bool { } // MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe. -func (b *ByteSlice) MarshalUnsafe(dst []byte) { - b.MarshalBytes(dst) +func (b *ByteSlice) MarshalUnsafe(dst []byte) []byte { + return b.MarshalBytes(dst) } // UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe. -func (b *ByteSlice) UnmarshalUnsafe(src []byte) { - b.UnmarshalBytes(src) +func (b *ByteSlice) UnmarshalUnsafe(src []byte) []byte { + return b.UnmarshalBytes(src) } // CopyIn implements marshal.Marshallable.CopyIn. diff --git a/pkg/marshal/primitive/primitive_abi_autogen_unsafe.go b/pkg/marshal/primitive/primitive_abi_autogen_unsafe.go index 51e9e7144..792c0a693 100644 --- a/pkg/marshal/primitive/primitive_abi_autogen_unsafe.go +++ b/pkg/marshal/primitive/primitive_abi_autogen_unsafe.go @@ -29,13 +29,15 @@ func (i *Int16) SizeBytes() int { } // MarshalBytes implements marshal.Marshallable.MarshalBytes. -func (i *Int16) MarshalBytes(dst []byte) { +func (i *Int16) MarshalBytes(dst []byte) []byte { hostarch.ByteOrder.PutUint16(dst[:2], uint16(*i)) + return dst[2:] } // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. -func (i *Int16) UnmarshalBytes(src []byte) { +func (i *Int16) UnmarshalBytes(src []byte) []byte { *i = Int16(int16(hostarch.ByteOrder.Uint16(src[:2]))) + return src[2:] } // Packed implements marshal.Marshallable.Packed. @@ -46,13 +48,17 @@ func (i *Int16) Packed() bool { } // MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe. -func (i *Int16) MarshalUnsafe(dst []byte) { - gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(i), uintptr(i.SizeBytes())) +func (i *Int16) MarshalUnsafe(dst []byte) []byte { + size := i.SizeBytes() + gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(i), uintptr(size)) + return dst[size:] } // UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe. -func (i *Int16) UnmarshalUnsafe(src []byte) { - gohacks.Memmove(unsafe.Pointer(i), unsafe.Pointer(&src[0]), uintptr(i.SizeBytes())) +func (i *Int16) UnmarshalUnsafe(src []byte) []byte { + size := i.SizeBytes() + gohacks.Memmove(unsafe.Pointer(i), unsafe.Pointer(&src[0]), uintptr(size)) + return src[size:] } // CopyOutN implements marshal.Marshallable.CopyOutN. @@ -196,13 +202,15 @@ func (i *Int32) SizeBytes() int { } // MarshalBytes implements marshal.Marshallable.MarshalBytes. -func (i *Int32) MarshalBytes(dst []byte) { +func (i *Int32) MarshalBytes(dst []byte) []byte { hostarch.ByteOrder.PutUint32(dst[:4], uint32(*i)) + return dst[4:] } // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. -func (i *Int32) UnmarshalBytes(src []byte) { +func (i *Int32) UnmarshalBytes(src []byte) []byte { *i = Int32(int32(hostarch.ByteOrder.Uint32(src[:4]))) + return src[4:] } // Packed implements marshal.Marshallable.Packed. @@ -213,13 +221,17 @@ func (i *Int32) Packed() bool { } // MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe. -func (i *Int32) MarshalUnsafe(dst []byte) { - gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(i), uintptr(i.SizeBytes())) +func (i *Int32) MarshalUnsafe(dst []byte) []byte { + size := i.SizeBytes() + gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(i), uintptr(size)) + return dst[size:] } // UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe. -func (i *Int32) UnmarshalUnsafe(src []byte) { - gohacks.Memmove(unsafe.Pointer(i), unsafe.Pointer(&src[0]), uintptr(i.SizeBytes())) +func (i *Int32) UnmarshalUnsafe(src []byte) []byte { + size := i.SizeBytes() + gohacks.Memmove(unsafe.Pointer(i), unsafe.Pointer(&src[0]), uintptr(size)) + return src[size:] } // CopyOutN implements marshal.Marshallable.CopyOutN. @@ -363,13 +375,15 @@ func (i *Int64) SizeBytes() int { } // MarshalBytes implements marshal.Marshallable.MarshalBytes. -func (i *Int64) MarshalBytes(dst []byte) { +func (i *Int64) MarshalBytes(dst []byte) []byte { hostarch.ByteOrder.PutUint64(dst[:8], uint64(*i)) + return dst[8:] } // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. -func (i *Int64) UnmarshalBytes(src []byte) { +func (i *Int64) UnmarshalBytes(src []byte) []byte { *i = Int64(int64(hostarch.ByteOrder.Uint64(src[:8]))) + return src[8:] } // Packed implements marshal.Marshallable.Packed. @@ -380,13 +394,17 @@ func (i *Int64) Packed() bool { } // MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe. -func (i *Int64) MarshalUnsafe(dst []byte) { - gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(i), uintptr(i.SizeBytes())) +func (i *Int64) MarshalUnsafe(dst []byte) []byte { + size := i.SizeBytes() + gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(i), uintptr(size)) + return dst[size:] } // UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe. -func (i *Int64) UnmarshalUnsafe(src []byte) { - gohacks.Memmove(unsafe.Pointer(i), unsafe.Pointer(&src[0]), uintptr(i.SizeBytes())) +func (i *Int64) UnmarshalUnsafe(src []byte) []byte { + size := i.SizeBytes() + gohacks.Memmove(unsafe.Pointer(i), unsafe.Pointer(&src[0]), uintptr(size)) + return src[size:] } // CopyOutN implements marshal.Marshallable.CopyOutN. @@ -530,13 +548,15 @@ func (i *Int8) SizeBytes() int { } // MarshalBytes implements marshal.Marshallable.MarshalBytes. -func (i *Int8) MarshalBytes(dst []byte) { +func (i *Int8) MarshalBytes(dst []byte) []byte { dst[0] = byte(*i) + return dst[1:] } // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. -func (i *Int8) UnmarshalBytes(src []byte) { +func (i *Int8) UnmarshalBytes(src []byte) []byte { *i = Int8(int8(src[0])) + return src[1:] } // Packed implements marshal.Marshallable.Packed. @@ -547,13 +567,17 @@ func (i *Int8) Packed() bool { } // MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe. -func (i *Int8) MarshalUnsafe(dst []byte) { - gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(i), uintptr(i.SizeBytes())) +func (i *Int8) MarshalUnsafe(dst []byte) []byte { + size := i.SizeBytes() + gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(i), uintptr(size)) + return dst[size:] } // UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe. -func (i *Int8) UnmarshalUnsafe(src []byte) { - gohacks.Memmove(unsafe.Pointer(i), unsafe.Pointer(&src[0]), uintptr(i.SizeBytes())) +func (i *Int8) UnmarshalUnsafe(src []byte) []byte { + size := i.SizeBytes() + gohacks.Memmove(unsafe.Pointer(i), unsafe.Pointer(&src[0]), uintptr(size)) + return src[size:] } // CopyOutN implements marshal.Marshallable.CopyOutN. @@ -697,13 +721,15 @@ func (u *Uint16) SizeBytes() int { } // MarshalBytes implements marshal.Marshallable.MarshalBytes. -func (u *Uint16) MarshalBytes(dst []byte) { +func (u *Uint16) MarshalBytes(dst []byte) []byte { hostarch.ByteOrder.PutUint16(dst[:2], uint16(*u)) + return dst[2:] } // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. -func (u *Uint16) UnmarshalBytes(src []byte) { +func (u *Uint16) UnmarshalBytes(src []byte) []byte { *u = Uint16(uint16(hostarch.ByteOrder.Uint16(src[:2]))) + return src[2:] } // Packed implements marshal.Marshallable.Packed. @@ -714,13 +740,17 @@ func (u *Uint16) Packed() bool { } // MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe. -func (u *Uint16) MarshalUnsafe(dst []byte) { - gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(u), uintptr(u.SizeBytes())) +func (u *Uint16) MarshalUnsafe(dst []byte) []byte { + size := u.SizeBytes() + gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(u), uintptr(size)) + return dst[size:] } // UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe. -func (u *Uint16) UnmarshalUnsafe(src []byte) { - gohacks.Memmove(unsafe.Pointer(u), unsafe.Pointer(&src[0]), uintptr(u.SizeBytes())) +func (u *Uint16) UnmarshalUnsafe(src []byte) []byte { + size := u.SizeBytes() + gohacks.Memmove(unsafe.Pointer(u), unsafe.Pointer(&src[0]), uintptr(size)) + return src[size:] } // CopyOutN implements marshal.Marshallable.CopyOutN. @@ -864,13 +894,15 @@ func (u *Uint32) SizeBytes() int { } // MarshalBytes implements marshal.Marshallable.MarshalBytes. -func (u *Uint32) MarshalBytes(dst []byte) { +func (u *Uint32) MarshalBytes(dst []byte) []byte { hostarch.ByteOrder.PutUint32(dst[:4], uint32(*u)) + return dst[4:] } // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. -func (u *Uint32) UnmarshalBytes(src []byte) { +func (u *Uint32) UnmarshalBytes(src []byte) []byte { *u = Uint32(uint32(hostarch.ByteOrder.Uint32(src[:4]))) + return src[4:] } // Packed implements marshal.Marshallable.Packed. @@ -881,13 +913,17 @@ func (u *Uint32) Packed() bool { } // MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe. -func (u *Uint32) MarshalUnsafe(dst []byte) { - gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(u), uintptr(u.SizeBytes())) +func (u *Uint32) MarshalUnsafe(dst []byte) []byte { + size := u.SizeBytes() + gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(u), uintptr(size)) + return dst[size:] } // UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe. -func (u *Uint32) UnmarshalUnsafe(src []byte) { - gohacks.Memmove(unsafe.Pointer(u), unsafe.Pointer(&src[0]), uintptr(u.SizeBytes())) +func (u *Uint32) UnmarshalUnsafe(src []byte) []byte { + size := u.SizeBytes() + gohacks.Memmove(unsafe.Pointer(u), unsafe.Pointer(&src[0]), uintptr(size)) + return src[size:] } // CopyOutN implements marshal.Marshallable.CopyOutN. @@ -1031,13 +1067,15 @@ func (u *Uint64) SizeBytes() int { } // MarshalBytes implements marshal.Marshallable.MarshalBytes. -func (u *Uint64) MarshalBytes(dst []byte) { +func (u *Uint64) MarshalBytes(dst []byte) []byte { hostarch.ByteOrder.PutUint64(dst[:8], uint64(*u)) + return dst[8:] } // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. -func (u *Uint64) UnmarshalBytes(src []byte) { +func (u *Uint64) UnmarshalBytes(src []byte) []byte { *u = Uint64(uint64(hostarch.ByteOrder.Uint64(src[:8]))) + return src[8:] } // Packed implements marshal.Marshallable.Packed. @@ -1048,13 +1086,17 @@ func (u *Uint64) Packed() bool { } // MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe. -func (u *Uint64) MarshalUnsafe(dst []byte) { - gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(u), uintptr(u.SizeBytes())) +func (u *Uint64) MarshalUnsafe(dst []byte) []byte { + size := u.SizeBytes() + gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(u), uintptr(size)) + return dst[size:] } // UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe. -func (u *Uint64) UnmarshalUnsafe(src []byte) { - gohacks.Memmove(unsafe.Pointer(u), unsafe.Pointer(&src[0]), uintptr(u.SizeBytes())) +func (u *Uint64) UnmarshalUnsafe(src []byte) []byte { + size := u.SizeBytes() + gohacks.Memmove(unsafe.Pointer(u), unsafe.Pointer(&src[0]), uintptr(size)) + return src[size:] } // CopyOutN implements marshal.Marshallable.CopyOutN. @@ -1198,13 +1240,15 @@ func (u *Uint8) SizeBytes() int { } // MarshalBytes implements marshal.Marshallable.MarshalBytes. -func (u *Uint8) MarshalBytes(dst []byte) { +func (u *Uint8) MarshalBytes(dst []byte) []byte { dst[0] = byte(*u) + return dst[1:] } // UnmarshalBytes implements marshal.Marshallable.UnmarshalBytes. -func (u *Uint8) UnmarshalBytes(src []byte) { +func (u *Uint8) UnmarshalBytes(src []byte) []byte { *u = Uint8(uint8(src[0])) + return src[1:] } // Packed implements marshal.Marshallable.Packed. @@ -1215,13 +1259,17 @@ func (u *Uint8) Packed() bool { } // MarshalUnsafe implements marshal.Marshallable.MarshalUnsafe. -func (u *Uint8) MarshalUnsafe(dst []byte) { - gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(u), uintptr(u.SizeBytes())) +func (u *Uint8) MarshalUnsafe(dst []byte) []byte { + size := u.SizeBytes() + gohacks.Memmove(unsafe.Pointer(&dst[0]), unsafe.Pointer(u), uintptr(size)) + return dst[size:] } // UnmarshalUnsafe implements marshal.Marshallable.UnmarshalUnsafe. -func (u *Uint8) UnmarshalUnsafe(src []byte) { - gohacks.Memmove(unsafe.Pointer(u), unsafe.Pointer(&src[0]), uintptr(u.SizeBytes())) +func (u *Uint8) UnmarshalUnsafe(src []byte) []byte { + size := u.SizeBytes() + gohacks.Memmove(unsafe.Pointer(u), unsafe.Pointer(&src[0]), uintptr(size)) + return src[size:] } // CopyOutN implements marshal.Marshallable.CopyOutN. |