mem

package
v0.0.0-...-8299741 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2024 License: GPL-3.0 Imports: 5 Imported by: 15

Documentation

Overview

Package mem provides reference-counted buffer and ways to work with memory as either string or []byte without copying.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(s string) []byte

Bytes converts string -> []byte without copying

func String

func String(b []byte) string

String converts []byte -> string without copying

Types

type Buf

type Buf struct {
	Data []byte
	// contains filtered or unexported fields
}

Buf is reference-counted memory buffer.

To lower pressure on Go garbage-collector allocate buffers with BufAlloc and free them with Buf.Release.

Custom allocation functions affect only performance, not correctness - everything should work if data buffer is allocated and/or free'ed via regular Go/GC-way.

func BufAlloc

func BufAlloc(size int) *Buf

BufAlloc allocates buffer of requested size from freelist.

buffer memory is not initialized.

func BufAlloc64

func BufAlloc64(size int64) *Buf

BufAlloc64 is same as BufAlloc but accepts int64 for size.

func (*Buf) Cap

func (buf *Buf) Cap() int

Cap returns buf's cap.

it works even if buf=nil similarly to len() on nil []byte slice.

func (*Buf) Incref

func (buf *Buf) Incref()

Incref increments buf's reference counter by 1.

buf must already have reference-counter > 0 before Incref call.

func (*Buf) Len

func (buf *Buf) Len() int

Len returns buf's len.

it works even if buf=nil similarly to len() on nil []byte slice.

func (*Buf) Release

func (buf *Buf) Release()

Release marks buf as no longer used by caller.

It decrements buf reference-counter and if it reaches zero returns buf to freelist.

The caller must not use buf after call to Release.

func (*Buf) XData

func (buf *Buf) XData() []byte

XData return's buf.Data or nil if buf == nil.

func (*Buf) XIncref

func (buf *Buf) XIncref()

XIncref increments buf's reference counter by 1 if buf != nil.

func (*Buf) XRelease

func (buf *Buf) XRelease()

XRelease releases buf it is != nil.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL