summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/buffer
diff options
context:
space:
mode:
authorTamir Duberstein <tamird@google.com>2018-09-12 21:57:04 -0700
committerShentubot <shentubot@google.com>2018-09-12 21:57:55 -0700
commitd689f8422fd55f74f6fc677f33b2bbf3720de89e (patch)
tree729ba29346fa74ac4a413f6f619d9e85ceb0ae78 /pkg/tcpip/buffer
parent5adb3468d4de249df055d641e01ce6582b3a9388 (diff)
Always pass buffer.VectorisedView by value
PiperOrigin-RevId: 212757571 Change-Id: I04200df9e45c21eb64951cd2802532fa84afcb1a
Diffstat (limited to 'pkg/tcpip/buffer')
-rw-r--r--pkg/tcpip/buffer/view.go62
-rw-r--r--pkg/tcpip/buffer/view_test.go41
2 files changed, 36 insertions, 67 deletions
diff --git a/pkg/tcpip/buffer/view.go b/pkg/tcpip/buffer/view.go
index 4a921ddcb..cea4e3657 100644
--- a/pkg/tcpip/buffer/view.go
+++ b/pkg/tcpip/buffer/view.go
@@ -45,11 +45,9 @@ func (v *View) CapLength(length int) {
*v = (*v)[:length:length]
}
-// ToVectorisedView transforms a View in a VectorisedView from an
-// already-allocated slice of View.
-func (v *View) ToVectorisedView(views [1]View) VectorisedView {
- views[0] = *v
- return NewVectorisedView(len(*v), views[:])
+// ToVectorisedView returns a VectorisedView containing the receiver.
+func (v View) ToVectorisedView() VectorisedView {
+ return NewVectorisedView(len(v), []View{v})
}
// VectorisedView is a vectorised version of View using non contigous memory.
@@ -107,21 +105,12 @@ func (vv *VectorisedView) CapLength(length int) {
// Clone returns a clone of this VectorisedView.
// If the buffer argument is large enough to contain all the Views of this VectorisedView,
// the method will avoid allocations and use the buffer to store the Views of the clone.
-func (vv *VectorisedView) Clone(buffer []View) VectorisedView {
- var views []View
- if len(buffer) >= len(vv.views) {
- views = buffer[:len(vv.views)]
- } else {
- views = make([]View, len(vv.views))
- }
- for i, v := range vv.views {
- views[i] = v
- }
- return VectorisedView{views: views, size: vv.size}
+func (vv VectorisedView) Clone(buffer []View) VectorisedView {
+ return VectorisedView{views: append(buffer[:0], vv.views...), size: vv.size}
}
// First returns the first view of the vectorised view.
-func (vv *VectorisedView) First() View {
+func (vv VectorisedView) First() View {
if len(vv.views) == 0 {
return nil
}
@@ -137,23 +126,13 @@ func (vv *VectorisedView) RemoveFirst() {
vv.views = vv.views[1:]
}
-// SetSize unsafely sets the size of the VectorisedView.
-func (vv *VectorisedView) SetSize(size int) {
- vv.size = size
-}
-
-// SetViews unsafely sets the views of the VectorisedView.
-func (vv *VectorisedView) SetViews(views []View) {
- vv.views = views
-}
-
// Size returns the size in bytes of the entire content stored in the vectorised view.
-func (vv *VectorisedView) Size() int {
+func (vv VectorisedView) Size() int {
return vv.size
}
// ToView returns a single view containing the content of the vectorised view.
-func (vv *VectorisedView) ToView() View {
+func (vv VectorisedView) ToView() View {
u := make([]byte, 0, vv.size)
for _, v := range vv.views {
u = append(u, v...)
@@ -162,29 +141,6 @@ func (vv *VectorisedView) ToView() View {
}
// Views returns the slice containing the all views.
-func (vv *VectorisedView) Views() []View {
+func (vv VectorisedView) Views() []View {
return vv.views
}
-
-// ByteSlice returns a slice containing the all views as a []byte.
-func (vv *VectorisedView) ByteSlice() [][]byte {
- s := make([][]byte, len(vv.views))
- for i := range vv.views {
- s[i] = []byte(vv.views[i])
- }
- return s
-}
-
-// copy returns a deep-copy of the vectorised view.
-// It is an expensive method that should be used only in tests.
-func (vv *VectorisedView) copy() *VectorisedView {
- uu := &VectorisedView{
- views: make([]View, len(vv.views)),
- size: vv.size,
- }
- for i, v := range vv.views {
- uu.views[i] = make(View, len(v))
- copy(uu.views[i], v)
- }
- return uu
-}
diff --git a/pkg/tcpip/buffer/view_test.go b/pkg/tcpip/buffer/view_test.go
index 57fe12360..02c264593 100644
--- a/pkg/tcpip/buffer/view_test.go
+++ b/pkg/tcpip/buffer/view_test.go
@@ -20,22 +20,33 @@ import (
"testing"
)
+// copy returns a deep-copy of the vectorised view.
+func (vv VectorisedView) copy() VectorisedView {
+ uu := VectorisedView{
+ views: make([]View, 0, len(vv.views)),
+ size: vv.size,
+ }
+ for _, v := range vv.views {
+ uu.views = append(uu.views, append(View(nil), v...))
+ }
+ return uu
+}
+
// vv is an helper to build VectorisedView from different strings.
-func vv(size int, pieces ...string) *VectorisedView {
+func vv(size int, pieces ...string) VectorisedView {
views := make([]View, len(pieces))
for i, p := range pieces {
views[i] = []byte(p)
}
- vv := NewVectorisedView(size, views)
- return &vv
+ return NewVectorisedView(size, views)
}
var capLengthTestCases = []struct {
comment string
- in *VectorisedView
+ in VectorisedView
length int
- want *VectorisedView
+ want VectorisedView
}{
{
comment: "Simple case",
@@ -88,9 +99,9 @@ func TestCapLength(t *testing.T) {
var trimFrontTestCases = []struct {
comment string
- in *VectorisedView
+ in VectorisedView
count int
- want *VectorisedView
+ want VectorisedView
}{
{
comment: "Simple case",
@@ -149,7 +160,7 @@ func TestTrimFront(t *testing.T) {
var toViewCases = []struct {
comment string
- in *VectorisedView
+ in VectorisedView
want View
}{
{
@@ -181,7 +192,7 @@ func TestToView(t *testing.T) {
var toCloneCases = []struct {
comment string
- inView *VectorisedView
+ inView VectorisedView
inBuffer []View
}{
{
@@ -213,10 +224,12 @@ var toCloneCases = []struct {
func TestToClone(t *testing.T) {
for _, c := range toCloneCases {
- got := c.inView.Clone(c.inBuffer)
- if !reflect.DeepEqual(&got, c.inView) {
- t.Errorf("Test \"%s\" failed when calling Clone(%v) on %v. Got %v. Want %v",
- c.comment, c.inBuffer, c.inView, got, c.inView)
- }
+ t.Run(c.comment, func(t *testing.T) {
+ got := c.inView.Clone(c.inBuffer)
+ if !reflect.DeepEqual(got, c.inView) {
+ t.Fatalf("got (%+v).Clone(%+v) = %+v, want = %+v",
+ c.inView, c.inBuffer, got, c.inView)
+ }
+ })
}
}