Back to

Package padded

Latest Go to latest

The latest major version is .

Published: Jun 18, 2020 | License: MIT | Module:


Package padded provides a padded byte slice and pool allocator.


type Pool

type Pool struct {
	// contains filtered or unexported fields

Pool implements a memory pool for padded byte slices. Both the capacity and padding are reclaimed when a slice is freed.

func (*Pool) Free

func (p *Pool) Free(s Slice)

Free returns a Slice to p for future reallocation.

func (*Pool) Make

func (p *Pool) Make(len, cap, pad int) Slice

Make allocates (or reallocates) a Slice for the len, cap, and pad.

type Slice

type Slice []byte

Slice is a padded byte slice. The padding is a scratch space allocated in front of byte slice and behaves roughly the same as the capacity. The len & cap built-in funcs work the same for Slice as for []byte, but append does not. Use the Append method instead.

Prepending elements to the slice fills in the padding, just as appending elements to the slice fills in the capacity. The elements must be naturally aligned to fill in the scratch padding, otherwise an append is performed. The word size on an amd64 machine is 8 bytes, so element bytes should be prepended in multiples of 8.

func Make

func Make(len, cap, pad int) Slice

Make allocates and initializes a padded byte slice. Unlike cap, the pad value is only the size of the scratch space in front of the slice. (The scratch space in back is cap - len.)

The allocated scratch space for padding may be >= pad.

func (Slice) Append

func (s Slice) Append(elems ...byte) Slice

Append appends elements to the end of s. The behavior of Append is the same as the built-in append function.

func (Slice) Pad

func (s Slice) Pad() int

Pad returns the length of the remaining bytes of padding.

func (Slice) Prepend

func (s Slice) Prepend(elems ...byte) Slice

Prepend concatenates elems onto the beginning of s.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier