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"
|
"github.com/jacobsa/fuse/internal/fusekernel"
|
||||||
)
|
)
|
||||||
|
|
||||||
const outHeaderSize = unsafe.Sizeof(fusekernel.OutHeader{})
|
// OutMessageHeaderSize is the size of the leading header in every
|
||||||
|
// properly-constructed OutMessage. Reset brings the message back to this size.
|
||||||
// OutMessage structs begin life with Len() == OutMessageInitialSize.
|
const OutMessageHeaderSize = unsafe.Sizeof(fusekernel.OutHeader{})
|
||||||
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
|
|
||||||
|
|
||||||
// OutMessage provides a mechanism for constructing a single contiguous fuse
|
// OutMessage provides a mechanism for constructing a single contiguous fuse
|
||||||
// message from multiple segments, where the first segment is always a
|
// message from multiple segments, where the first segment is always a
|
||||||
|
@ -38,8 +33,6 @@ const outMessageSize = outHeaderSize + MaxReadSize
|
||||||
//
|
//
|
||||||
// Must be initialized with Reset.
|
// Must be initialized with Reset.
|
||||||
type OutMessage struct {
|
type OutMessage struct {
|
||||||
offset uintptr
|
|
||||||
storage [outMessageSize]byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure alignment works out correctly, at least for the header.
|
// 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
|
// Reset resets m so that it's ready to be used again. Afterward, the contents
|
||||||
// contents are solely a zeroed header.
|
// are solely a zeroed fusekernel.OutHeader struct.
|
||||||
func (m *OutMessage) Reset() {
|
func (m *OutMessage) Reset()
|
||||||
m.offset = OutMessageInitialSize
|
|
||||||
memclr(unsafe.Pointer(&m.storage), OutMessageInitialSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return a pointer to the header at the start of the message.
|
// OutHeader returns a pointer to the header at the start of the message.
|
||||||
func (b *OutMessage) OutHeader() (h *fusekernel.OutHeader) {
|
func (m *OutMessage) OutHeader() (h *fusekernel.OutHeader)
|
||||||
h = (*fusekernel.OutHeader)(unsafe.Pointer(&b.storage))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Grow the buffer by the supplied number of bytes, returning a pointer to the
|
// Grow grows m's buffer by the given number of bytes, returning a pointer to
|
||||||
// start of the new segment, which is zeroed. If there is no space left, return
|
// the start of the new segment, which is guaranteed to be zeroed. If there is
|
||||||
// the nil pointer.
|
// insufficient space, it returns nil.
|
||||||
func (b *OutMessage) Grow(size uintptr) (p unsafe.Pointer) {
|
func (b *OutMessage) Grow(n int) (p unsafe.Pointer)
|
||||||
p = b.GrowNoZero(size)
|
|
||||||
if p != nil {
|
|
||||||
memclr(p, size)
|
|
||||||
}
|
|
||||||
|
|
||||||
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!
|
// ShrinkTo shrinks m to the given size. It panics if the size is greater than
|
||||||
func (b *OutMessage) GrowNoZero(size uintptr) (p unsafe.Pointer) {
|
// Len() or less than OutMessageHeaderSize.
|
||||||
if outMessageSize-b.offset < size {
|
func (b *OutMessage) ShrinkTo(n int)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
p = unsafe.Pointer(uintptr(unsafe.Pointer(&b.storage)) + b.offset)
|
// Append is equivalent to growing by len(src), then copying src over the new
|
||||||
b.offset += size
|
// segment. Int panics if there is not enough room available.
|
||||||
|
|
||||||
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.
|
|
||||||
func (b *OutMessage) Append(src []byte) {
|
func (b *OutMessage) Append(src []byte) {
|
||||||
p := b.GrowNoZero(uintptr(len(src)))
|
p := b.GrowNoZero(len(src))
|
||||||
if p == nil {
|
if p == nil {
|
||||||
panic(fmt.Sprintf("Can't grow %d bytes", len(src)))
|
panic(fmt.Sprintf("Can't grow %d bytes", len(src)))
|
||||||
}
|
}
|
||||||
|
@ -114,10 +80,9 @@ func (b *OutMessage) Append(src []byte) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equivalent to growing by the length of s, then copying s over the new
|
// AppendString is like Append, but accepts string input.
|
||||||
// segment. Panics if there is not enough room available.
|
|
||||||
func (b *OutMessage) AppendString(src string) {
|
func (b *OutMessage) AppendString(src string) {
|
||||||
p := b.GrowNoZero(uintptr(len(src)))
|
p := b.GrowNoZero(len(src))
|
||||||
if p == nil {
|
if p == nil {
|
||||||
panic(fmt.Sprintf("Can't grow %d bytes", len(src)))
|
panic(fmt.Sprintf("Can't grow %d bytes", len(src)))
|
||||||
}
|
}
|
||||||
|
@ -128,12 +93,13 @@ func (b *OutMessage) AppendString(src string) {
|
||||||
return
|
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 {
|
func (b *OutMessage) Len() int {
|
||||||
return int(b.offset)
|
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 {
|
func (b *OutMessage) Bytes() []byte {
|
||||||
return b.storage[:int(b.offset)]
|
return b.storage[:int(b.offset)]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue