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"
|
|
|
|
"reflect"
|
2015-07-24 06:26:25 +03:00
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/jacobsa/fuse/internal/fusekernel"
|
|
|
|
)
|
|
|
|
|
2016-12-19 04:47:00 +03:00
|
|
|
// OutMessageHeaderSize is the size of the leading header in every
|
|
|
|
// properly-constructed OutMessage. Reset brings the message back to this size.
|
2016-12-19 04:57:08 +03:00
|
|
|
const OutMessageHeaderSize = int(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 {
|
2021-08-25 11:05:53 +03:00
|
|
|
header fusekernel.OutHeader
|
|
|
|
Sglist [][]byte
|
2016-12-19 04:53:32 +03:00
|
|
|
}
|
|
|
|
|
2016-12-19 04:47:00 +03:00
|
|
|
// 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() {
|
2020-06-26 08:45:43 +03:00
|
|
|
m.header = fusekernel.OutHeader{}
|
2021-08-25 11:05:53 +03:00
|
|
|
m.Sglist = nil
|
2016-12-19 04:54:54 +03:00
|
|
|
}
|
2015-07-24 06:26:25 +03:00
|
|
|
|
2016-12-19 04:47:00 +03:00
|
|
|
// OutHeader returns a pointer to the header at the start of the message.
|
2016-12-19 05:04:54 +03:00
|
|
|
func (m *OutMessage) OutHeader() *fusekernel.OutHeader {
|
2016-12-19 05:06:39 +03:00
|
|
|
return &m.header
|
2016-12-19 05:04:54 +03:00
|
|
|
}
|
2015-07-24 06:36:53 +03:00
|
|
|
|
2021-08-25 11:05:53 +03:00
|
|
|
// Grow adds a new buffer of <n> bytes to the message, returning a pointer to
|
|
|
|
// the start of the new segment, which is guaranteed to be zeroed.
|
2020-01-28 12:10:08 +03:00
|
|
|
func (m *OutMessage) Grow(n int) unsafe.Pointer {
|
2021-08-25 11:05:53 +03:00
|
|
|
b := make([]byte, n)
|
|
|
|
m.Append(b)
|
|
|
|
p := unsafe.Pointer(&b[0])
|
2020-01-28 12:10:08 +03:00
|
|
|
return p
|
2016-12-19 05:01:45 +03:00
|
|
|
}
|
2015-07-24 06:36:53 +03:00
|
|
|
|
2016-12-19 04:47:00 +03:00
|
|
|
// ShrinkTo shrinks m to the given size. It panics if the size is greater than
|
|
|
|
// Len() or less than OutMessageHeaderSize.
|
2016-12-19 05:04:02 +03:00
|
|
|
func (m *OutMessage) ShrinkTo(n int) {
|
|
|
|
if n < OutMessageHeaderSize || n > m.Len() {
|
|
|
|
panic(fmt.Sprintf(
|
|
|
|
"ShrinkTo(%d) out of range (current Len: %d)",
|
|
|
|
n,
|
|
|
|
m.Len()))
|
|
|
|
}
|
2021-08-25 11:05:53 +03:00
|
|
|
if n == OutMessageHeaderSize {
|
|
|
|
m.Sglist = nil
|
|
|
|
} else {
|
|
|
|
i := 1
|
|
|
|
n -= OutMessageHeaderSize
|
|
|
|
for len(m.Sglist) > i && n >= len(m.Sglist[i]) {
|
|
|
|
n -= len(m.Sglist[i])
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if n > 0 {
|
|
|
|
m.Sglist[i] = m.Sglist[i][0:n]
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
m.Sglist = m.Sglist[0:i]
|
|
|
|
}
|
2016-12-19 05:04:02 +03:00
|
|
|
}
|
2015-07-29 04:02:29 +03:00
|
|
|
|
2016-12-19 04:47:00 +03:00
|
|
|
// Append is equivalent to growing by len(src), then copying src over the new
|
|
|
|
// segment. Int panics if there is not enough room available.
|
2021-08-25 11:05:53 +03:00
|
|
|
func (m *OutMessage) Append(src ...[]byte) {
|
|
|
|
if m.Sglist == nil {
|
|
|
|
// First element of Sglist is pre-filled with a pointer to the header
|
|
|
|
// to allow sending it with a single writev() call without copying the
|
|
|
|
// slice again
|
|
|
|
m.Sglist = append(m.Sglist, m.OutHeaderBytes())
|
2015-07-28 09:28:21 +03:00
|
|
|
}
|
2021-08-25 11:05:53 +03:00
|
|
|
m.Sglist = append(m.Sglist, src...)
|
2015-07-28 09:28:21 +03:00
|
|
|
return
|
2015-07-24 06:46:18 +03:00
|
|
|
}
|
|
|
|
|
2016-12-19 04:47:00 +03:00
|
|
|
// AppendString is like Append, but accepts string input.
|
2016-12-19 04:55:34 +03:00
|
|
|
func (m *OutMessage) AppendString(src string) {
|
2021-08-25 11:05:53 +03:00
|
|
|
m.Append([]byte(src))
|
2015-07-28 09:29:09 +03:00
|
|
|
return
|
2015-07-28 09:09:27 +03:00
|
|
|
}
|
2015-07-24 06:46:18 +03:00
|
|
|
|
2016-12-19 04:47:00 +03:00
|
|
|
// Len returns the current size of the message, including the leading header.
|
2016-12-19 04:55:34 +03:00
|
|
|
func (m *OutMessage) Len() int {
|
2021-08-25 11:05:53 +03:00
|
|
|
if m.Sglist == nil {
|
|
|
|
return OutMessageHeaderSize
|
|
|
|
}
|
|
|
|
// First element of Sglist is the header, so we don't need to count it here
|
|
|
|
r := 0
|
|
|
|
for _, b := range m.Sglist {
|
|
|
|
r += len(b)
|
|
|
|
}
|
|
|
|
return r
|
2015-07-24 06:46:18 +03:00
|
|
|
}
|
|
|
|
|
2021-08-25 11:05:53 +03:00
|
|
|
// OutHeaderBytes returns a byte slice containing the current header.
|
|
|
|
func (m *OutMessage) OutHeaderBytes() []byte {
|
|
|
|
l := OutMessageHeaderSize
|
2016-12-19 04:57:08 +03:00
|
|
|
sh := reflect.SliceHeader{
|
|
|
|
Data: uintptr(unsafe.Pointer(&m.header)),
|
|
|
|
Len: l,
|
|
|
|
Cap: l,
|
|
|
|
}
|
|
|
|
return *(*[]byte)(unsafe.Pointer(&sh))
|
2015-07-24 06:36:53 +03:00
|
|
|
}
|