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
9c1134ca
Commit
9c1134ca
authored
Jul 24, 2015
by
Aaron Jacobs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added a Buffer type.
parent
3fe63db6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
26 deletions
+48
-26
internal/buffer/buffer.go
internal/buffer/buffer.go
+48
-26
No files found.
internal/buffer/buffer.go
View file @
9c1134ca
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
(
"reflect"
"unsafe"
"github.com/jacobsa/fuse/internal/fusekernel"
)
// Buffer provides a mechanism for constructing a message from multiple
// segments.
type
Buffer
[]
byte
// alloc allocates size bytes and returns a pointer to the new
// segment.
func
(
w
*
Buffer
)
Alloc
(
size
uintptr
)
unsafe
.
Pointer
{
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
])
// 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.
type
Buffer
struct
{
slice
[]
byte
}
//
reset clears out the contents of the buffer.
func
(
w
*
Buffer
)
reset
()
{
for
i
:=
range
(
*
w
)[
:
cap
(
*
w
)]
{
(
*
w
)[
i
]
=
0
}
*
w
=
(
*
w
)[
:
0
]
//
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
}
func
NewBuffer
(
extra
uintptr
)
(
buf
Buffer
)
{
const
hdrSize
=
unsafe
.
Sizeof
(
fusekernel
.
OutHeader
{})
buf
=
make
(
Buffer
,
hdrSize
,
hdrSize
+
extra
)
// 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
}
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