Remove buffer.Prependable.UsedBytes

It is the same as buffer.Prependable.View.

PiperOrigin-RevId: 213064166
Change-Id: Ib33b8a2c4da864209d9a0be0a1c113be10b520d3
This commit is contained in:
Tamir Duberstein
2018-09-14 16:39:56 -07:00
committed by Shentubot
parent 3aa50f18a4
commit 75c66f871b
7 changed files with 23 additions and 30 deletions
+12 -20
View File
@@ -41,6 +41,17 @@ func NewPrependableFromView(v View) Prependable {
return Prependable{buf: v, usedIdx: 0}
}
// View returns a View of the backing buffer that contains all prepended
// data so far.
func (p Prependable) View() View {
return p.buf[p.usedIdx:]
}
// UsedLength returns the number of bytes used so far.
func (p Prependable) UsedLength() int {
return len(p.buf) - p.usedIdx
}
// Prepend reserves the requested space in front of the buffer, returning a
// slice that represents the reserved space.
func (p *Prependable) Prepend(size int) []byte {
@@ -49,24 +60,5 @@ func (p *Prependable) Prepend(size int) []byte {
}
p.usedIdx -= size
return p.buf[p.usedIdx:][:size:size]
}
// View returns a View of the backing buffer that contains all prepended
// data so far.
func (p Prependable) View() View {
v := p.buf
v.TrimFront(p.usedIdx)
return v
}
// UsedBytes returns a slice of the backing buffer that contains all prepended
// data so far.
func (p Prependable) UsedBytes() []byte {
return p.buf[p.usedIdx:]
}
// UsedLength returns the number of bytes used so far.
func (p Prependable) UsedLength() int {
return len(p.buf) - p.usedIdx
return p.View()[:size:size]
}
+2 -2
View File
@@ -178,10 +178,10 @@ func (e *endpoint) WritePacket(r *stack.Route, hdr buffer.Prependable, payload b
}
if payload.Size() == 0 {
return rawfile.NonBlockingWrite(e.fd, hdr.UsedBytes())
return rawfile.NonBlockingWrite(e.fd, hdr.View())
}
return rawfile.NonBlockingWrite2(e.fd, hdr.UsedBytes(), payload.ToView())
return rawfile.NonBlockingWrite2(e.fd, hdr.View(), payload.ToView())
}
func (e *endpoint) capViews(n int, buffers []int) int {
+3 -2
View File
@@ -17,6 +17,7 @@
package fdbased
import (
"bytes"
"fmt"
"math/rand"
"reflect"
@@ -157,7 +158,7 @@ func TestWritePacket(t *testing.T) {
for i := range payload {
payload[i] = uint8(rand.Intn(256))
}
want := append(hdr.UsedBytes(), payload...)
want := append(hdr.View(), payload...)
if err := c.ep.WritePacket(r, hdr, payload.ToVectorisedView(), proto); err != nil {
t.Fatalf("WritePacket failed: %v", err)
}
@@ -188,7 +189,7 @@ func TestWritePacket(t *testing.T) {
if len(b) != len(want) {
t.Fatalf("Read returned %v bytes, want %v", len(b), len(want))
}
if !reflect.DeepEqual(b, want) {
if !bytes.Equal(b, want) {
t.Fatalf("Read returned %x, want %x", b, want)
}
})
+1 -1
View File
@@ -196,7 +196,7 @@ func (e *endpoint) WritePacket(r *stack.Route, hdr buffer.Prependable, payload b
v := payload.ToView()
// Transmit the packet.
e.mu.Lock()
ok := e.tx.transmit(hdr.UsedBytes(), v)
ok := e.tx.transmit(hdr.View(), v)
e.mu.Unlock()
if !ok {
+2 -2
View File
@@ -190,10 +190,10 @@ func (e *endpoint) LinkAddress() tcpip.LinkAddress {
// the request to the lower endpoint.
func (e *endpoint) WritePacket(r *stack.Route, hdr buffer.Prependable, payload buffer.VectorisedView, protocol tcpip.NetworkProtocolNumber) *tcpip.Error {
if atomic.LoadUint32(&LogPackets) == 1 && e.file == nil {
logPacket("send", protocol, hdr.UsedBytes())
logPacket("send", protocol, hdr.View())
}
if e.file != nil && atomic.LoadUint32(&LogPacketsToFile) == 1 {
hdrBuf := hdr.UsedBytes()
hdrBuf := hdr.View()
length := len(hdrBuf) + payload.Size()
if length > int(e.maxPCAPLen) {
length = int(e.maxPCAPLen)
+2 -2
View File
@@ -151,13 +151,13 @@ func (t *testObject) WritePacket(_ *stack.Route, hdr buffer.Prependable, payload
var dstAddr tcpip.Address
if t.v4 {
h := header.IPv4(hdr.UsedBytes())
h := header.IPv4(hdr.View())
prot = tcpip.TransportProtocolNumber(h.Protocol())
srcAddr = h.SourceAddress()
dstAddr = h.DestinationAddress()
} else {
h := header.IPv6(hdr.UsedBytes())
h := header.IPv6(hdr.View())
prot = tcpip.TransportProtocolNumber(h.NextHeader())
srcAddr = h.SourceAddress()
dstAddr = h.DestinationAddress()
+1 -1
View File
@@ -187,7 +187,7 @@ func TestLinkResolution(t *testing.T) {
pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6EchoMinimumSize))
pkt.SetType(header.ICMPv6EchoRequest)
pkt.SetChecksum(icmpChecksum(pkt, r.LocalAddress, r.RemoteAddress, buffer.VectorisedView{}))
payload := tcpip.SlicePayload(hdr.UsedBytes())
payload := tcpip.SlicePayload(hdr.View())
// We can't send our payload directly over the route because that
// doesn't provoke NDP discovery.