Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jacobsa-fuse
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
jacobsa-fuse
Commits
24a6daea
Commit
24a6daea
authored
Jul 24, 2015
by
Aaron Jacobs
Browse files
Options
Browse Files
Download
Plain Diff
Stop using fuseshim.Buffer.
parents
27eba306
2c346ca7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
152 additions
and
79 deletions
+152
-79
fuseops/common_op.go
fuseops/common_op.go
+11
-11
fuseops/ops.go
fuseops/ops.go
+58
-68
internal/buffer/buffer.go
internal/buffer/buffer.go
+83
-0
No files found.
fuseops/common_op.go
View file @
24a6daea
...
@@ -19,9 +19,8 @@ import (
...
@@ -19,9 +19,8 @@ import (
"log"
"log"
"reflect"
"reflect"
"strings"
"strings"
"unsafe"
"github.com/jacobsa/fuse/internal/
fusekernel
"
"github.com/jacobsa/fuse/internal/
buffer
"
"github.com/jacobsa/fuse/internal/fuseshim"
"github.com/jacobsa/fuse/internal/fuseshim"
"github.com/jacobsa/reqtrace"
"github.com/jacobsa/reqtrace"
"golang.org/x/net/context"
"golang.org/x/net/context"
...
@@ -32,12 +31,12 @@ import (
...
@@ -32,12 +31,12 @@ import (
type
internalOp
interface
{
type
internalOp
interface
{
Op
Op
// Create a response message for the kernel,
with leading pading for a
// Create a response message for the kernel,
leaving the leading
// fusekernel.OutHeader
struct
.
// fusekernel.OutHeader
untouched
.
//
//
// Special case: a
return value of nil means that the kernel is not expecting
// Special case: a
zero return value means that the kernel is not expecting a
//
a
response.
// response.
kernelResponse
()
[]
byte
kernelResponse
()
(
b
buffer
.
Buffer
)
}
}
// A function that sends a reply message back to the kernel for the request
// A function that sends a reply message back to the kernel for the request
...
@@ -143,16 +142,17 @@ func (o *commonOp) Respond(err error) {
...
@@ -143,16 +142,17 @@ func (o *commonOp) Respond(err error) {
// If successful, we ask the op for an appopriate response to the kernel, and
// If successful, we ask the op for an appopriate response to the kernel, and
// it is responsible for leaving room for the fusekernel.OutHeader struct.
// it is responsible for leaving room for the fusekernel.OutHeader struct.
// Otherwise, create our own.
// Otherwise, create our own.
var
msg
[]
byte
var
b
buffer
.
Buffer
if
err
==
nil
{
if
err
==
nil
{
msg
=
o
.
op
.
kernelResponse
()
b
=
o
.
op
.
kernelResponse
()
}
else
{
}
else
{
msg
=
fuseshim
.
NewBuffer
(
0
)
b
=
buffer
.
New
(
0
)
}
}
// Fill in the header if a reply is needed.
// Fill in the header if a reply is needed.
msg
:=
b
.
Bytes
()
if
msg
!=
nil
{
if
msg
!=
nil
{
h
:=
(
*
fusekernel
.
OutHeader
)(
unsafe
.
Pointer
(
&
msg
[
0
])
)
h
:=
b
.
OutHeader
(
)
h
.
Unique
=
o
.
fuseID
h
.
Unique
=
o
.
fuseID
h
.
Len
=
uint32
(
len
(
msg
))
h
.
Len
=
uint32
(
len
(
msg
))
if
err
!=
nil
{
if
err
!=
nil
{
...
...
fuseops/ops.go
View file @
24a6daea
...
@@ -20,8 +20,8 @@ import (
...
@@ -20,8 +20,8 @@ import (
"time"
"time"
"unsafe"
"unsafe"
"github.com/jacobsa/fuse/internal/buffer"
"github.com/jacobsa/fuse/internal/fusekernel"
"github.com/jacobsa/fuse/internal/fusekernel"
"github.com/jacobsa/fuse/internal/fuseshim"
"golang.org/x/net/context"
"golang.org/x/net/context"
)
)
...
@@ -88,13 +88,12 @@ func (o *LookUpInodeOp) ShortDesc() (desc string) {
...
@@ -88,13 +88,12 @@ func (o *LookUpInodeOp) ShortDesc() (desc string) {
return
return
}
}
func
(
o
*
LookUpInodeOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
LookUpInodeOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
size
:=
fusekernel
.
EntryOutSize
(
o
.
protocol
)
size
:=
fusekernel
.
EntryOutSize
(
o
.
protocol
)
b
uf
:=
fuseshim
.
NewBuffer
(
size
)
b
=
buffer
.
New
(
size
)
out
:=
(
*
fusekernel
.
EntryOut
)(
b
uf
.
Alloc
(
size
))
out
:=
(
*
fusekernel
.
EntryOut
)(
b
.
Grow
(
size
))
convertChildInodeEntry
(
&
o
.
Entry
,
out
)
convertChildInodeEntry
(
&
o
.
Entry
,
out
)
msg
=
buf
return
return
}
}
...
@@ -124,14 +123,13 @@ func (o *GetInodeAttributesOp) DebugString() string {
...
@@ -124,14 +123,13 @@ func (o *GetInodeAttributesOp) DebugString() string {
o
.
Attributes
.
DebugString
())
o
.
Attributes
.
DebugString
())
}
}
func
(
o
*
GetInodeAttributesOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
GetInodeAttributesOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
size
:=
fusekernel
.
AttrOutSize
(
o
.
protocol
)
size
:=
fusekernel
.
AttrOutSize
(
o
.
protocol
)
b
uf
:=
fuseshim
.
NewBuffer
(
size
)
b
=
buffer
.
New
(
size
)
out
:=
(
*
fusekernel
.
AttrOut
)(
b
uf
.
Alloc
(
size
))
out
:=
(
*
fusekernel
.
AttrOut
)(
b
.
Grow
(
size
))
out
.
AttrValid
,
out
.
AttrValidNsec
=
convertExpirationTime
(
o
.
AttributesExpiration
)
out
.
AttrValid
,
out
.
AttrValidNsec
=
convertExpirationTime
(
o
.
AttributesExpiration
)
convertAttributes
(
o
.
Inode
,
&
o
.
Attributes
,
&
out
.
Attr
)
convertAttributes
(
o
.
Inode
,
&
o
.
Attributes
,
&
out
.
Attr
)
msg
=
buf
return
return
}
}
...
@@ -159,14 +157,13 @@ type SetInodeAttributesOp struct {
...
@@ -159,14 +157,13 @@ type SetInodeAttributesOp struct {
AttributesExpiration
time
.
Time
AttributesExpiration
time
.
Time
}
}
func
(
o
*
SetInodeAttributesOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
SetInodeAttributesOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
size
:=
fusekernel
.
AttrOutSize
(
o
.
protocol
)
size
:=
fusekernel
.
AttrOutSize
(
o
.
protocol
)
b
uf
:=
fuseshim
.
NewBuffer
(
size
)
b
=
buffer
.
New
(
size
)
out
:=
(
*
fusekernel
.
AttrOut
)(
b
uf
.
Alloc
(
size
))
out
:=
(
*
fusekernel
.
AttrOut
)(
b
.
Grow
(
size
))
out
.
AttrValid
,
out
.
AttrValidNsec
=
convertExpirationTime
(
o
.
AttributesExpiration
)
out
.
AttrValid
,
out
.
AttrValidNsec
=
convertExpirationTime
(
o
.
AttributesExpiration
)
convertAttributes
(
o
.
Inode
,
&
o
.
Attributes
,
&
out
.
Attr
)
convertAttributes
(
o
.
Inode
,
&
o
.
Attributes
,
&
out
.
Attr
)
msg
=
buf
return
return
}
}
...
@@ -219,7 +216,7 @@ type ForgetInodeOp struct {
...
@@ -219,7 +216,7 @@ type ForgetInodeOp struct {
N
uint64
N
uint64
}
}
func
(
o
*
ForgetInodeOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
ForgetInodeOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
// No response.
// No response.
return
return
}
}
...
@@ -262,13 +259,12 @@ func (o *MkDirOp) ShortDesc() (desc string) {
...
@@ -262,13 +259,12 @@ func (o *MkDirOp) ShortDesc() (desc string) {
return
return
}
}
func
(
o
*
MkDirOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
MkDirOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
size
:=
fusekernel
.
EntryOutSize
(
o
.
protocol
)
size
:=
fusekernel
.
EntryOutSize
(
o
.
protocol
)
b
uf
:=
fuseshim
.
NewBuffer
(
size
)
b
=
buffer
.
New
(
size
)
out
:=
(
*
fusekernel
.
EntryOut
)(
b
uf
.
Alloc
(
size
))
out
:=
(
*
fusekernel
.
EntryOut
)(
b
.
Grow
(
size
))
convertChildInodeEntry
(
&
o
.
Entry
,
out
)
convertChildInodeEntry
(
&
o
.
Entry
,
out
)
msg
=
buf
return
return
}
}
...
@@ -315,17 +311,16 @@ func (o *CreateFileOp) ShortDesc() (desc string) {
...
@@ -315,17 +311,16 @@ func (o *CreateFileOp) ShortDesc() (desc string) {
return
return
}
}
func
(
o
*
CreateFileOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
CreateFileOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
eSize
:=
fusekernel
.
EntryOutSize
(
o
.
protocol
)
eSize
:=
fusekernel
.
EntryOutSize
(
o
.
protocol
)
b
uf
:=
fuseshim
.
NewBuffer
(
eSize
+
unsafe
.
Sizeof
(
fusekernel
.
OpenOut
{}))
b
=
buffer
.
New
(
eSize
+
unsafe
.
Sizeof
(
fusekernel
.
OpenOut
{}))
e
:=
(
*
fusekernel
.
EntryOut
)(
b
uf
.
Alloc
(
eSize
))
e
:=
(
*
fusekernel
.
EntryOut
)(
b
.
Grow
(
eSize
))
convertChildInodeEntry
(
&
o
.
Entry
,
e
)
convertChildInodeEntry
(
&
o
.
Entry
,
e
)
oo
:=
(
*
fusekernel
.
OpenOut
)(
b
uf
.
Alloc
(
unsafe
.
Sizeof
(
fusekernel
.
OpenOut
{})))
oo
:=
(
*
fusekernel
.
OpenOut
)(
b
.
Grow
(
unsafe
.
Sizeof
(
fusekernel
.
OpenOut
{})))
oo
.
Fh
=
uint64
(
o
.
Handle
)
oo
.
Fh
=
uint64
(
o
.
Handle
)
msg
=
buf
return
return
}
}
...
@@ -362,13 +357,12 @@ func (o *CreateSymlinkOp) ShortDesc() (desc string) {
...
@@ -362,13 +357,12 @@ func (o *CreateSymlinkOp) ShortDesc() (desc string) {
return
return
}
}
func
(
o
*
CreateSymlinkOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
CreateSymlinkOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
size
:=
fusekernel
.
EntryOutSize
(
o
.
protocol
)
size
:=
fusekernel
.
EntryOutSize
(
o
.
protocol
)
b
uf
:=
fuseshim
.
NewBuffer
(
size
)
b
=
buffer
.
New
(
size
)
out
:=
(
*
fusekernel
.
EntryOut
)(
b
uf
.
Alloc
(
size
))
out
:=
(
*
fusekernel
.
EntryOut
)(
b
.
Grow
(
size
))
convertChildInodeEntry
(
&
o
.
Entry
,
out
)
convertChildInodeEntry
(
&
o
.
Entry
,
out
)
msg
=
buf
return
return
}
}
...
@@ -424,8 +418,8 @@ type RenameOp struct {
...
@@ -424,8 +418,8 @@ type RenameOp struct {
NewName
string
NewName
string
}
}
func
(
o
*
RenameOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
RenameOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
msg
=
fuseshim
.
NewBuffer
(
0
)
b
=
buffer
.
New
(
0
)
return
return
}
}
...
@@ -445,8 +439,8 @@ type RmDirOp struct {
...
@@ -445,8 +439,8 @@ type RmDirOp struct {
Name
string
Name
string
}
}
func
(
o
*
RmDirOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
RmDirOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
msg
=
fuseshim
.
NewBuffer
(
0
)
b
=
buffer
.
New
(
0
)
return
return
}
}
...
@@ -465,8 +459,8 @@ type UnlinkOp struct {
...
@@ -465,8 +459,8 @@ type UnlinkOp struct {
Name
string
Name
string
}
}
func
(
o
*
UnlinkOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
UnlinkOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
msg
=
fuseshim
.
NewBuffer
(
0
)
b
=
buffer
.
New
(
0
)
return
return
}
}
...
@@ -497,12 +491,11 @@ type OpenDirOp struct {
...
@@ -497,12 +491,11 @@ type OpenDirOp struct {
Handle
HandleID
Handle
HandleID
}
}
func
(
o
*
OpenDirOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
OpenDirOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
b
uf
:=
fuseshim
.
NewBuffer
(
unsafe
.
Sizeof
(
fusekernel
.
OpenOut
{}))
b
=
buffer
.
New
(
unsafe
.
Sizeof
(
fusekernel
.
OpenOut
{}))
out
:=
(
*
fusekernel
.
OpenOut
)(
b
uf
.
Alloc
(
unsafe
.
Sizeof
(
fusekernel
.
OpenOut
{})))
out
:=
(
*
fusekernel
.
OpenOut
)(
b
.
Grow
(
unsafe
.
Sizeof
(
fusekernel
.
OpenOut
{})))
out
.
Fh
=
uint64
(
o
.
Handle
)
out
.
Fh
=
uint64
(
o
.
Handle
)
msg
=
buf
return
return
}
}
...
@@ -596,9 +589,9 @@ type ReadDirOp struct {
...
@@ -596,9 +589,9 @@ type ReadDirOp struct {
Data
[]
byte
Data
[]
byte
}
}
func
(
o
*
ReadDirOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
ReadDirOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
msg
=
fuseshim
.
NewBuffer
(
uintptr
(
len
(
o
.
Data
)))
b
=
buffer
.
New
(
uintptr
(
len
(
o
.
Data
)))
msg
=
append
(
msg
,
o
.
Data
...
)
b
.
Append
(
o
.
Data
)
return
return
}
}
...
@@ -619,8 +612,8 @@ type ReleaseDirHandleOp struct {
...
@@ -619,8 +612,8 @@ type ReleaseDirHandleOp struct {
Handle
HandleID
Handle
HandleID
}
}
func
(
o
*
ReleaseDirHandleOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
ReleaseDirHandleOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
msg
=
fuseshim
.
NewBuffer
(
0
)
b
=
buffer
.
New
(
0
)
return
return
}
}
...
@@ -650,12 +643,11 @@ type OpenFileOp struct {
...
@@ -650,12 +643,11 @@ type OpenFileOp struct {
Handle
HandleID
Handle
HandleID
}
}
func
(
o
*
OpenFileOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
OpenFileOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
b
uf
:=
fuseshim
.
NewBuffer
(
unsafe
.
Sizeof
(
fusekernel
.
OpenOut
{}))
b
=
buffer
.
New
(
unsafe
.
Sizeof
(
fusekernel
.
OpenOut
{}))
out
:=
(
*
fusekernel
.
OpenOut
)(
b
uf
.
Alloc
(
unsafe
.
Sizeof
(
fusekernel
.
OpenOut
{})))
out
:=
(
*
fusekernel
.
OpenOut
)(
b
.
Grow
(
unsafe
.
Sizeof
(
fusekernel
.
OpenOut
{})))
out
.
Fh
=
uint64
(
o
.
Handle
)
out
.
Fh
=
uint64
(
o
.
Handle
)
msg
=
buf
return
return
}
}
...
@@ -688,9 +680,9 @@ type ReadFileOp struct {
...
@@ -688,9 +680,9 @@ type ReadFileOp struct {
Data
[]
byte
Data
[]
byte
}
}
func
(
o
*
ReadFileOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
ReadFileOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
msg
=
fuseshim
.
NewBuffer
(
uintptr
(
len
(
o
.
Data
)))
b
=
buffer
.
New
(
uintptr
(
len
(
o
.
Data
)))
msg
=
append
(
msg
,
o
.
Data
...
)
b
.
Append
(
o
.
Data
)
return
return
}
}
...
@@ -764,12 +756,11 @@ type WriteFileOp struct {
...
@@ -764,12 +756,11 @@ type WriteFileOp struct {
Data
[]
byte
Data
[]
byte
}
}
func
(
o
*
WriteFileOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
WriteFileOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
b
uf
:=
fuseshim
.
NewBuffer
(
unsafe
.
Sizeof
(
fusekernel
.
WriteOut
{}))
b
=
buffer
.
New
(
unsafe
.
Sizeof
(
fusekernel
.
WriteOut
{}))
out
:=
(
*
fusekernel
.
WriteOut
)(
b
uf
.
Alloc
(
unsafe
.
Sizeof
(
fusekernel
.
WriteOut
{})))
out
:=
(
*
fusekernel
.
WriteOut
)(
b
.
Grow
(
unsafe
.
Sizeof
(
fusekernel
.
WriteOut
{})))
out
.
Size
=
uint32
(
len
(
o
.
Data
))
out
.
Size
=
uint32
(
len
(
o
.
Data
))
msg
=
buf
return
return
}
}
...
@@ -797,8 +788,8 @@ type SyncFileOp struct {
...
@@ -797,8 +788,8 @@ type SyncFileOp struct {
Handle
HandleID
Handle
HandleID
}
}
func
(
o
*
SyncFileOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
SyncFileOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
msg
=
fuseshim
.
NewBuffer
(
0
)
b
=
buffer
.
New
(
0
)
return
return
}
}
...
@@ -857,8 +848,8 @@ type FlushFileOp struct {
...
@@ -857,8 +848,8 @@ type FlushFileOp struct {
Handle
HandleID
Handle
HandleID
}
}
func
(
o
*
FlushFileOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
FlushFileOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
msg
=
fuseshim
.
NewBuffer
(
0
)
b
=
buffer
.
New
(
0
)
return
return
}
}
...
@@ -879,8 +870,8 @@ type ReleaseFileHandleOp struct {
...
@@ -879,8 +870,8 @@ type ReleaseFileHandleOp struct {
Handle
HandleID
Handle
HandleID
}
}
func
(
o
*
ReleaseFileHandleOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
ReleaseFileHandleOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
msg
=
fuseshim
.
NewBuffer
(
0
)
b
=
buffer
.
New
(
0
)
return
return
}
}
...
@@ -897,7 +888,7 @@ func (o *unknownOp) ShortDesc() (desc string) {
...
@@ -897,7 +888,7 @@ func (o *unknownOp) ShortDesc() (desc string) {
return
return
}
}
func
(
o
*
unknownOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
unknownOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
panic
(
fmt
.
Sprintf
(
"Should never get here for unknown op: %s"
,
o
.
ShortDesc
()))
panic
(
fmt
.
Sprintf
(
"Should never get here for unknown op: %s"
,
o
.
ShortDesc
()))
}
}
...
@@ -916,9 +907,9 @@ type ReadSymlinkOp struct {
...
@@ -916,9 +907,9 @@ type ReadSymlinkOp struct {
Target
string
Target
string
}
}
func
(
o
*
ReadSymlinkOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
ReadSymlinkOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
msg
=
fuseshim
.
NewBuffer
(
uintptr
(
len
(
o
.
Target
)))
b
=
buffer
.
New
(
uintptr
(
len
(
o
.
Target
)))
msg
=
append
(
msg
,
o
.
Target
...
)
b
.
AppendString
(
o
.
Target
)
return
return
}
}
...
@@ -937,11 +928,10 @@ type InternalStatFSOp struct {
...
@@ -937,11 +928,10 @@ type InternalStatFSOp struct {
commonOp
commonOp
}
}
func
(
o
*
InternalStatFSOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
InternalStatFSOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
b
uf
:=
fuseshim
.
NewBuffer
(
unsafe
.
Sizeof
(
fusekernel
.
StatfsOut
{}))
b
=
buffer
.
New
(
unsafe
.
Sizeof
(
fusekernel
.
StatfsOut
{}))
b
uf
.
Alloc
(
unsafe
.
Sizeof
(
fusekernel
.
StatfsOut
{}))
b
.
Grow
(
unsafe
.
Sizeof
(
fusekernel
.
StatfsOut
{}))
msg
=
buf
return
return
}
}
...
@@ -951,6 +941,6 @@ type InternalInterruptOp struct {
...
@@ -951,6 +941,6 @@ type InternalInterruptOp struct {
FuseID
uint64
FuseID
uint64
}
}
func
(
o
*
InternalInterruptOp
)
kernelResponse
()
(
msg
[]
byte
)
{
func
(
o
*
InternalInterruptOp
)
kernelResponse
()
(
b
buffer
.
Buffer
)
{
panic
(
"Shouldn't get here."
)
panic
(
"Shouldn't get here."
)
}
}
internal/buffer/buffer.go
0 → 100644
View file @
24a6daea
// 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
(
"reflect"
"unsafe"
"github.com/jacobsa/fuse/internal/fusekernel"
)
// Buffer provides a mechanism for constructing a single contiguous fuse
// message from multiple segments, where the first segment is always a
// fusekernel.OutHeader message.
//
// Must be created with New. Exception: the zero value has Bytes() == nil.
type
Buffer
struct
{
slice
[]
byte
}
// Create a new buffer whose initial contents are a zeroed fusekernel.OutHeader
// message, and with room enough to grow by extra bytes.
func
New
(
extra
uintptr
)
(
b
Buffer
)
{
const
headerSize
=
unsafe
.
Sizeof
(
fusekernel
.
OutHeader
{})
b
.
slice
=
make
([]
byte
,
headerSize
,
headerSize
+
extra
)
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
}
// Equivalent to growing by the length of p, then copying p into the new segment.
func
(
b
*
Buffer
)
Append
(
p
[]
byte
)
{
sh
:=
reflect
.
SliceHeader
{
Data
:
uintptr
(
b
.
Grow
(
uintptr
(
len
(
p
)))),
Len
:
len
(
p
),
Cap
:
len
(
p
),
}
copy
(
*
(
*
[]
byte
)(
unsafe
.
Pointer
(
&
sh
)),
p
)
}
// Equivalent to growing by the length of s, then copying s into the new segment.
func
(
b
*
Buffer
)
AppendString
(
s
string
)
{
sh
:=
reflect
.
SliceHeader
{
Data
:
uintptr
(
b
.
Grow
(
uintptr
(
len
(
s
)))),
Len
:
len
(
s
),
Cap
:
len
(
s
),
}
copy
(
*
(
*
[]
byte
)(
unsafe
.
Pointer
(
&
sh
)),
s
)
}
// Return a reference to the current contents of the buffer.
func
(
b
*
Buffer
)
Bytes
()
[]
byte
{
return
b
.
slice
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment