fusego/internal/buffer/out_message.go

127 lines
3.8 KiB
Go
Raw Normal View History

2015-07-24 06:36:53 +03:00
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package buffer
2015-07-24 06:26:25 +03:00
import (
2015-07-28 09:28:21 +03:00
"fmt"
2015-07-28 09:23:10 +03:00
"log"
2015-07-28 09:28:21 +03:00
"reflect"
2015-07-24 06:26:25 +03:00
"unsafe"
"github.com/jacobsa/fuse/internal/fusekernel"
)
// OutMessageHeaderSize is the size of the leading header in every
// properly-constructed OutMessage. Reset brings the message back to this size.
const OutMessageHeaderSize = unsafe.Sizeof(fusekernel.OutHeader{})
2015-07-28 09:06:23 +03:00
2015-07-24 07:11:03 +03:00
// OutMessage provides a mechanism for constructing a single contiguous fuse
2015-07-24 06:36:53 +03:00
// message from multiple segments, where the first segment is always a
// fusekernel.OutHeader message.
//
2015-07-28 09:06:23 +03:00
// Must be initialized with Reset.
2015-07-24 07:11:03 +03:00
type OutMessage struct {
2016-12-19 04:53:32 +03:00
// The offset into payload to which we're currently writing.
payloadOffset int
header [OutMessageHeaderSize]byte
payload [MaxReadSize]byte
2015-07-24 06:26:25 +03:00
}
2016-12-19 04:53:32 +03:00
// Make sure that the header field is aligned correctly for
// fusekernel.OutHeader type punning.
2015-07-28 09:23:10 +03:00
func init() {
a := unsafe.Alignof(OutMessage{})
2016-12-19 04:53:32 +03:00
o := unsafe.Offsetof(OutMessage{}.header)
2015-07-28 09:23:10 +03:00
e := unsafe.Alignof(fusekernel.OutHeader{})
if a%e != 0 || o%e != 0 {
log.Panicf("Bad alignment or offset: %d, %d, need %d", a, o, e)
}
}
2016-12-19 04:53:32 +03:00
// Make sure that the header and payload are contiguous.
func init() {
a := unsafe.Offsetof(OutMessage{}.header) + OutMessageHeaderSize
b := unsafe.Offsetof(OutMessage{}.payload)
if a != b {
log.Panicf(
"header ends at offset %d, but payload starts at offset %d",
a, b)
}
}
// Reset resets m so that it's ready to be used again. Afterward, the contents
// are solely a zeroed fusekernel.OutHeader struct.
2016-12-19 04:54:54 +03:00
func (m *OutMessage) Reset() {
m.payloadOffset = 0
memclr(unsafe.Pointer(&m.header), OutMessageHeaderSize)
}
2015-07-24 06:26:25 +03:00
// OutHeader returns a pointer to the header at the start of the message.
func (m *OutMessage) OutHeader() (h *fusekernel.OutHeader)
2015-07-24 06:36:53 +03:00
// Grow grows m's buffer by the given number of bytes, returning a pointer to
// the start of the new segment, which is guaranteed to be zeroed. If there is
// insufficient space, it returns nil.
func (b *OutMessage) Grow(n int) (p unsafe.Pointer)
2015-07-28 09:23:59 +03:00
// GrowNoZero is equivalent to Grow, except the new segment is not zeroed. Use
// with caution!
func (b *OutMessage) GrowNoZero(n int) (p unsafe.Pointer)
2015-07-24 06:36:53 +03:00
// ShrinkTo shrinks m to the given size. It panics if the size is greater than
// Len() or less than OutMessageHeaderSize.
func (b *OutMessage) ShrinkTo(n int)
// Append is equivalent to growing by len(src), then copying src over the new
// segment. Int panics if there is not enough room available.
2015-07-28 09:28:21 +03:00
func (b *OutMessage) Append(src []byte) {
p := b.GrowNoZero(len(src))
2015-07-28 09:28:21 +03:00
if p == nil {
panic(fmt.Sprintf("Can't grow %d bytes", len(src)))
}
sh := (*reflect.SliceHeader)(unsafe.Pointer(&src))
memmove(p, unsafe.Pointer(sh.Data), uintptr(sh.Len))
return
2015-07-24 06:46:18 +03:00
}
// AppendString is like Append, but accepts string input.
2015-07-28 09:29:09 +03:00
func (b *OutMessage) AppendString(src string) {
p := b.GrowNoZero(len(src))
2015-07-28 09:29:09 +03:00
if p == nil {
panic(fmt.Sprintf("Can't grow %d bytes", len(src)))
}
sh := (*reflect.StringHeader)(unsafe.Pointer(&src))
memmove(p, unsafe.Pointer(sh.Data), uintptr(sh.Len))
return
2015-07-28 09:09:27 +03:00
}
2015-07-24 06:46:18 +03:00
// Len returns the current size of the message, including the leading header.
2015-07-28 09:09:27 +03:00
func (b *OutMessage) Len() int {
2015-07-28 09:29:30 +03:00
return int(b.offset)
2015-07-24 06:46:18 +03:00
}
// Bytes returns a reference to the current contents of the buffer, including
// the leading header.
2015-07-24 07:11:03 +03:00
func (b *OutMessage) Bytes() []byte {
2015-07-28 09:29:30 +03:00
return b.storage[:int(b.offset)]
2015-07-24 06:36:53 +03:00
}