Added a Buffer type.
parent
3fe63db6fb
commit
9c1134ca3f
|
@ -1,39 +1,61 @@
|
||||||
package fuseshim
|
// 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
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/jacobsa/fuse/internal/fusekernel"
|
"github.com/jacobsa/fuse/internal/fusekernel"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Buffer provides a mechanism for constructing a message from multiple
|
// Buffer provides a mechanism for constructing a single contiguous fuse
|
||||||
// segments.
|
// message from multiple segments, where the first segment is always a
|
||||||
type Buffer []byte
|
// fusekernel.OutHeader message.
|
||||||
|
//
|
||||||
// alloc allocates size bytes and returns a pointer to the new
|
// Must be created with New.
|
||||||
// segment.
|
type Buffer struct {
|
||||||
func (w *Buffer) Alloc(size uintptr) unsafe.Pointer {
|
slice []byte
|
||||||
s := int(size)
|
|
||||||
if len(*w)+s > cap(*w) {
|
|
||||||
old := *w
|
|
||||||
*w = make([]byte, len(*w), 2*cap(*w)+s)
|
|
||||||
copy(*w, old)
|
|
||||||
}
|
|
||||||
l := len(*w)
|
|
||||||
*w = (*w)[:l+s]
|
|
||||||
return unsafe.Pointer(&(*w)[l])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// reset clears out the contents of the buffer.
|
// Create a new buffer whose initial contents are a zeroed fusekernel.OutHeader
|
||||||
func (w *Buffer) reset() {
|
// message, and with room enough to grow by extra bytes.
|
||||||
for i := range (*w)[:cap(*w)] {
|
func New(extra uintptr) (b Buffer) {
|
||||||
(*w)[i] = 0
|
const headerSize = unsafe.Sizeof(fusekernel.OutHeader{})
|
||||||
}
|
b.slice = make([]byte, headerSize, headerSize+extra)
|
||||||
*w = (*w)[:0]
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBuffer(extra uintptr) (buf Buffer) {
|
|
||||||
const hdrSize = unsafe.Sizeof(fusekernel.OutHeader{})
|
|
||||||
buf = make(Buffer, hdrSize, hdrSize+extra)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return a pointer to the header at the start of the buffer.
|
||||||
|
func (b *Buffer) OutHeader() (h *fusekernel.OutHeader) {
|
||||||
|
sh := (*reflect.SliceHeader)(unsafe.Pointer(&b.slice))
|
||||||
|
h = (*fusekernel.OutHeader)(unsafe.Pointer(sh.Data))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Grow the buffer by the supplied number of bytes, returning a pointer to the
|
||||||
|
// start of the new segment. The sum of the arguments given to Grow must not
|
||||||
|
// exceed the argument given to New when creating the buffer.
|
||||||
|
func (b *Buffer) Grow(size uintptr) (p unsafe.Pointer) {
|
||||||
|
sh := (*reflect.SliceHeader)(unsafe.Pointer(&b.slice))
|
||||||
|
p = unsafe.Pointer(sh.Data + uintptr(sh.Len))
|
||||||
|
b.slice = b.slice[:len(b.slice)+int(size)]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a reference to the current contents of the buffer.
|
||||||
|
func (b *Buffer) Bytes() []byte {
|
||||||
|
return b.slice
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue