buffer: give OutMessage's public API a makeover.
Use int where appropriate, fix up documentation, and clarify. Delete the implementation where it will soon need to change.geesefs-0-30-9
parent
10bc0e766c
commit
0f6db15c59
|
@ -23,14 +23,9 @@ import (
|
|||
"github.com/jacobsa/fuse/internal/fusekernel"
|
||||
)
|
||||
|
||||
const outHeaderSize = unsafe.Sizeof(fusekernel.OutHeader{})
|
||||
|
||||
// OutMessage structs begin life with Len() == OutMessageInitialSize.
|
||||
const OutMessageInitialSize = outHeaderSize
|
||||
|
||||
// We size out messages to be large enough to hold a header for the response
|
||||
// plus the largest read that may come in.
|
||||
const outMessageSize = outHeaderSize + MaxReadSize
|
||||
// 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{})
|
||||
|
||||
// OutMessage provides a mechanism for constructing a single contiguous fuse
|
||||
// message from multiple segments, where the first segment is always a
|
||||
|
@ -38,8 +33,6 @@ const outMessageSize = outHeaderSize + MaxReadSize
|
|||
//
|
||||
// Must be initialized with Reset.
|
||||
type OutMessage struct {
|
||||
offset uintptr
|
||||
storage [outMessageSize]byte
|
||||
}
|
||||
|
||||
// Make sure alignment works out correctly, at least for the header.
|
||||
|
@ -53,57 +46,30 @@ func init() {
|
|||
}
|
||||
}
|
||||
|
||||
// Reset the message so that it is ready to be used again. Afterward, the
|
||||
// contents are solely a zeroed header.
|
||||
func (m *OutMessage) Reset() {
|
||||
m.offset = OutMessageInitialSize
|
||||
memclr(unsafe.Pointer(&m.storage), OutMessageInitialSize)
|
||||
}
|
||||
// Reset resets m so that it's ready to be used again. Afterward, the contents
|
||||
// are solely a zeroed fusekernel.OutHeader struct.
|
||||
func (m *OutMessage) Reset()
|
||||
|
||||
// Return a pointer to the header at the start of the message.
|
||||
func (b *OutMessage) OutHeader() (h *fusekernel.OutHeader) {
|
||||
h = (*fusekernel.OutHeader)(unsafe.Pointer(&b.storage))
|
||||
return
|
||||
}
|
||||
// OutHeader returns a pointer to the header at the start of the message.
|
||||
func (m *OutMessage) OutHeader() (h *fusekernel.OutHeader)
|
||||
|
||||
// Grow the buffer by the supplied number of bytes, returning a pointer to the
|
||||
// start of the new segment, which is zeroed. If there is no space left, return
|
||||
// the nil pointer.
|
||||
func (b *OutMessage) Grow(size uintptr) (p unsafe.Pointer) {
|
||||
p = b.GrowNoZero(size)
|
||||
if p != nil {
|
||||
memclr(p, size)
|
||||
}
|
||||
// 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)
|
||||
|
||||
return
|
||||
}
|
||||
// GrowNoZero is equivalent to Grow, except the new segment is not zeroed. Use
|
||||
// with caution!
|
||||
func (b *OutMessage) GrowNoZero(n int) (p unsafe.Pointer)
|
||||
|
||||
// Equivalent to Grow, except the new segment is not zeroed. Use with caution!
|
||||
func (b *OutMessage) GrowNoZero(size uintptr) (p unsafe.Pointer) {
|
||||
if outMessageSize-b.offset < size {
|
||||
return
|
||||
}
|
||||
// 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)
|
||||
|
||||
p = unsafe.Pointer(uintptr(unsafe.Pointer(&b.storage)) + b.offset)
|
||||
b.offset += size
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Shrink to the supplied size. Panic if the size is greater than Len() or less
|
||||
// than OutMessageInitialSize.
|
||||
func (b *OutMessage) ShrinkTo(n uintptr) {
|
||||
if n < OutMessageInitialSize || n > b.offset {
|
||||
panic(fmt.Sprintf("ShrinkTo(%d) out of range for offset %d", n, b.offset))
|
||||
}
|
||||
|
||||
b.offset = n
|
||||
}
|
||||
|
||||
// Equivalent to growing by the length of p, then copying p over the new
|
||||
// segment. Panics if there is not enough room available.
|
||||
// Append is equivalent to growing by len(src), then copying src over the new
|
||||
// segment. Int panics if there is not enough room available.
|
||||
func (b *OutMessage) Append(src []byte) {
|
||||
p := b.GrowNoZero(uintptr(len(src)))
|
||||
p := b.GrowNoZero(len(src))
|
||||
if p == nil {
|
||||
panic(fmt.Sprintf("Can't grow %d bytes", len(src)))
|
||||
}
|
||||
|
@ -114,10 +80,9 @@ func (b *OutMessage) Append(src []byte) {
|
|||
return
|
||||
}
|
||||
|
||||
// Equivalent to growing by the length of s, then copying s over the new
|
||||
// segment. Panics if there is not enough room available.
|
||||
// AppendString is like Append, but accepts string input.
|
||||
func (b *OutMessage) AppendString(src string) {
|
||||
p := b.GrowNoZero(uintptr(len(src)))
|
||||
p := b.GrowNoZero(len(src))
|
||||
if p == nil {
|
||||
panic(fmt.Sprintf("Can't grow %d bytes", len(src)))
|
||||
}
|
||||
|
@ -128,12 +93,13 @@ func (b *OutMessage) AppendString(src string) {
|
|||
return
|
||||
}
|
||||
|
||||
// Return the current size of the buffer.
|
||||
// Len returns the current size of the message, including the leading header.
|
||||
func (b *OutMessage) Len() int {
|
||||
return int(b.offset)
|
||||
}
|
||||
|
||||
// Return a reference to the current contents of the buffer.
|
||||
// Bytes returns a reference to the current contents of the buffer, including
|
||||
// the leading header.
|
||||
func (b *OutMessage) Bytes() []byte {
|
||||
return b.storage[:int(b.offset)]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue