From b3b66dbd1f7f1d5411fd1f7ea76fc5ea7027ec35 Mon Sep 17 00:00:00 2001 From: Bert Muthalaly Date: Wed, 5 Sep 2018 16:46:47 -0700 Subject: [PATCH] Enable constructing a Prependable from a View without allocating. PiperOrigin-RevId: 211722525 Change-Id: Ie73753fd09d67d6a2ce70cfe2d4ecf7275f09ce0 --- pkg/tcpip/buffer/prependable.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/tcpip/buffer/prependable.go b/pkg/tcpip/buffer/prependable.go index d2b26cf47..5af3ff114 100644 --- a/pkg/tcpip/buffer/prependable.go +++ b/pkg/tcpip/buffer/prependable.go @@ -32,6 +32,15 @@ func NewPrependable(size int) Prependable { return Prependable{buf: NewView(size), usedIdx: size} } +// NewPrependableFromView creates an entirely-used Prependable from a View. +// +// NewPrependableFromView takes ownership of v. Note that since the entire +// prependable is used, further attempts to call Prepend will note that size > +// p.usedIdx and return nil. +func NewPrependableFromView(v View) Prependable { + return Prependable{buf: v, usedIdx: 0} +} + // 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 {