memory

package
v3.3.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package memory contains types used for representing and simulating memory observed in the capture.

Index

Constants

View Source
const (
	// ApplicationPool is the PoolID of Pool representing the application's memory
	// address space.
	ApplicationPool = PoolID(PoolNames_Application)
)

Variables

View Source
var Nullptr = BytePtr(0)

Nullptr is a zero-address pointer.

Functions

func AlignOf

func AlignOf(t reflect.Type, m *device.MemoryLayout) uint64

AlignOf returns the byte alignment of the type t.

func CharToBytes

func CharToBytes(ϟchars []Char) []byte

CharToBytes changes the Char values to their byte[] representation.

func InvertMemoryRanges

func InvertMemoryRanges(inputList interval.U64RangeList) interval.U64RangeList

InvertMemoryRanges converts a used memory range list into a free list and the other way around.

func IsSize

func IsSize(v interface{}) bool

IsSize returns true if v is a Size or alias to a Size.

func LoadPointer

func LoadPointer(ctx context.Context, p Pointer, pools Pools, l *device.MemoryLayout) (interface{}, error)

LoadPointer loads the element from p.

func LoadSlice

func LoadSlice(ctx context.Context, s Slice, pools Pools, l *device.MemoryLayout) (interface{}, error)

LoadSlice loads the slice elements from s into a go-slice of the slice type.

func PointerToString

func PointerToString(p Pointer) string

PointerToString returns a string representation of the pointer.

func Read

func Read(d *Decoder, p interface{})

Read reads the value pointed at p from the decoder d using C alignment rules. If v is an array or slice, then each of the elements will be read, sequentially.

func SizeOf

func SizeOf(t reflect.Type, m *device.MemoryLayout) uint64

SizeOf returns the byte size of the type t.

func Write

func Write(e *Encoder, v interface{})

Write writes the value v to the encoder e using C alignment rules. If v is an array or slice, then each of the elements will be written, sequentially. Zeros are used as for paddinge.

func Writer

func Writer(p *Pool, rng Range) io.Writer

Writer returns a binary writer for the specified memory pool and range.

Types

type AlignedTy

type AlignedTy interface {
	// Alignment returns the alignment of the type in bytes.
	TypeAlignment(*device.MemoryLayout) uint64
}

AlignedTy is the interface implemented by types that can calculate their size.

type Allocator

type Allocator interface {
	// Alloc allocates count bytes at an offset that is a multiple of align.
	Alloc(count, align uint64) (uint64, error)

	// Free marks the block starting at the given offset as free.
	Free(base uint64) error

	// AllocList returns the ranges currently allocated from this allocator.
	AllocList() interval.U64RangeList

	// FreeList returns the free ranges this allocator can allocate from.
	FreeList() interval.U64RangeList

	// ReserveRanges reserves the given ranges in the free-list, meaning
	// they cannot be allocated from
	ReserveRanges(interval.U64RangeList)
}

Allocator is a memory allocator.

func NewBasicAllocator

func NewBasicAllocator(freeList interval.U64RangeList) Allocator

NewBasicAllocator creates a new allocator which allocates memory from the given list of free ranges. Memory is allocated by finding the leftmost free block large enough to fit the required size with the specified alignment. The returned allocator is not thread-safe.

type Char

type Char uint8

Char is the possibly signed but maybe unsigned C/C++ char.

func (Char) IsChar

func (Char) IsChar()

IsChar is a placeholder function to make Char implement CharTy interface

type CharTy

type CharTy interface {
	IsChar()
}

CharTy is the interface implemented by types that should be treated as char type.

type Data

type Data interface {
	// Get writes the bytes representing the slice to out, starting at offset
	// bytes. This is equivalent to: copy(out, data[offset:]).
	Get(ctx context.Context, offset uint64, out []byte) error

	// NewReader returns an io.Reader to efficiently read from the slice.
	// There shouldn't be a need to wrap this in additional buffers.
	NewReader(ctx context.Context) io.Reader

	// ResourceID returns the identifier of the resource representing the slice,
	// creating a new resource if it isn't already backed by one.
	ResourceID(ctx context.Context) (id.ID, error)

	// Size returns the number of bytes that would be returned by calling Get.
	Size() uint64

	// Slice returns a new Data referencing a subset range of the data.
	// The range r is relative to the base of the Slice. For example a slice of
	// [0, 4] would return a Slice referencing the first 5 bytes of this Slice.
	// Attempting to slice outside the range of this Slice will result in a
	// panic.
	Slice(r Range) Data

	// ValidRanges returns the list of slice-relative memory ranges that contain
	// valid (non-zero) data that can be read with Get.
	ValidRanges() RangeList

	// Strlen returns the number of bytes before the first zero byte in the
	// data.
	// If the Data does not contain a zero byte, then -1 is returned.
	Strlen(ctx context.Context) (int, error)
}

Data is the interface for a data source that can be resolved to a byte slice with Get, or 'sliced' to a subset of the data source.

func Blob

func Blob(data []byte) Data

Blob returns a read-only Slice that wraps data.

func NewData

func NewData(layout *device.MemoryLayout, data ...interface{}) Data

NewData returns a read-only Slice that contains the encoding of data.

func Resource

func Resource(resID id.ID, size uint64) Data

Resource returns a Data that wraps a resource stored in the database. resID is the identifier of the data and size is the size in bytes of the data.

type Decodable

type Decodable interface {
	// Decode decodes this object from the decoder.
	Decode(*Decoder)
}

Decodable is the interface implemented by types that can decode themselves from an encoder.

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder provides methods to read primitives from a binary.Reader, respecting a given MemoryLayout. Decoder will automatically handle alignment and types sizes.

func NewDecoder

func NewDecoder(r binary.Reader, m *device.MemoryLayout) *Decoder

NewDecoder constructs and returns a new Decoder that reads from r using the memory layout m.

func (*Decoder) Align

func (d *Decoder) Align(to uint64)

Align skips bytes until the read position is a multiple of to.

func (*Decoder) Bool

func (d *Decoder) Bool() bool

Bool loads and returns a boolean value.

func (*Decoder) Char

func (d *Decoder) Char() Char

Char loads and returns an char.

func (*Decoder) Data

func (d *Decoder) Data(buf []byte)

Data reads raw bytes into buf.

func (*Decoder) Error

func (d *Decoder) Error() error

Error returns the error state of the underlying reader.

func (*Decoder) F32

func (d *Decoder) F32() float32

F32 loads and returns a float32.

func (*Decoder) F64

func (d *Decoder) F64() float64

F64 loads and returns a float64.

func (*Decoder) I16

func (d *Decoder) I16() int16

I16 loads and returns a int16.

func (*Decoder) I32

func (d *Decoder) I32() int32

I32 loads and returns a int32.

func (*Decoder) I64

func (d *Decoder) I64() int64

I64 loads and returns a int64.

func (*Decoder) I8

func (d *Decoder) I8() int8

I8 loads and returns a int8.

func (*Decoder) Int

func (d *Decoder) Int() Int

Int loads and returns an int.

func (*Decoder) MemoryLayout

func (d *Decoder) MemoryLayout() *device.MemoryLayout

MemoryLayout returns the MemoryLayout used by the decoder.

func (*Decoder) Offset

func (d *Decoder) Offset() uint64

Offset returns the byte offset of the reader from the initial Decoder creation.

func (*Decoder) Pointer

func (d *Decoder) Pointer() uint64

Pointer loads and returns a pointer address.

func (*Decoder) Size

func (d *Decoder) Size() Size

Size loads and returns a size_t.

func (*Decoder) Skip

func (d *Decoder) Skip(n uint64)

Skip skips n bytes from the reader.

func (*Decoder) String

func (d *Decoder) String() string

String loads and returns a null-terminated string.

func (*Decoder) U16

func (d *Decoder) U16() uint16

U16 loads and returns a uint16.

func (*Decoder) U32

func (d *Decoder) U32() uint32

U32 loads and returns a uint32.

func (*Decoder) U64

func (d *Decoder) U64() uint64

U64 loads and returns a uint64.

func (*Decoder) U8

func (d *Decoder) U8() uint8

U8 loads and returns a uint8.

func (*Decoder) Uint

func (d *Decoder) Uint() Uint

Uint loads and returns a uint.

type Encodable

type Encodable interface {
	// Encode encodes this object to the encoder.
	Encode(*Encoder)
}

Encodable is the interface implemented by types that can encode themselves to an encoder.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder provides methods to write primitives to a binary.Writer, respecting a given MemoryLayout. Encoder will automatically handle alignment and types sizes.

func NewEncoder

func NewEncoder(w binary.Writer, m *device.MemoryLayout) *Encoder

NewEncoder constructs and returns a new Encoder that writes to w using the memory layout m.

func (*Encoder) Align

func (e *Encoder) Align(to uint64)

Align writes zero bytes until the write position is a multiple of to.

func (*Encoder) Bool

func (e *Encoder) Bool(v bool)

Bool stores a boolean value.

func (*Encoder) Char

func (e *Encoder) Char(v Char)

Char stores an char.

func (*Encoder) Data

func (e *Encoder) Data(buf []byte)

Data writes raw bytes from buf.

func (*Encoder) Error

func (e *Encoder) Error() error

Error returns the error state of the underlying writer.

func (*Encoder) F32

func (e *Encoder) F32(v float32)

F32 stores a float32.

func (*Encoder) F64

func (e *Encoder) F64(v float64)

F64 stores a float64.

func (*Encoder) I16

func (e *Encoder) I16(v int16)

I16 stores a int16.

func (*Encoder) I32

func (e *Encoder) I32(v int32)

I32 stores a int32.

func (*Encoder) I64

func (e *Encoder) I64(v int64)

I64 stores a int64.

func (*Encoder) I8

func (e *Encoder) I8(v int8)

I8 stores a int8.

func (*Encoder) Int

func (e *Encoder) Int(v Int)

Int stores an int.

func (*Encoder) MemoryLayout

func (e *Encoder) MemoryLayout() *device.MemoryLayout

MemoryLayout returns the MemoryLayout used by the encoder.

func (*Encoder) Offset

func (e *Encoder) Offset() uint64

Offset returns the byte offset of the writer from the initial Encoder creation.

func (*Encoder) Pad

func (e *Encoder) Pad(n uint64)

Pad writes n zero bytes to the writer.

func (*Encoder) Pointer

func (e *Encoder) Pointer(addr uint64)

Pointer stores a pointer address.

func (*Encoder) Size

func (e *Encoder) Size(v Size)

Size stores a size_t.

func (*Encoder) String

func (e *Encoder) String(v string)

String stores a null-terminated string.

func (*Encoder) U16

func (e *Encoder) U16(v uint16)

U16 stores a uint16.

func (*Encoder) U32

func (e *Encoder) U32(v uint32)

U32 stores a uint32.

func (*Encoder) U64

func (e *Encoder) U64(v uint64)

U64 stores a uint64.

func (*Encoder) U8

func (e *Encoder) U8(v uint8)

U8 stores a uint8.

func (*Encoder) Uint

func (e *Encoder) Uint(v Uint)

Uint stores a uint.

type Int

type Int int64

Int is a signed integer type.

func (Int) IsInt

func (Int) IsInt()

IsInt is a placeholder function to make Int implement IntTy interface

type IntTy

type IntTy interface {
	IsInt()
}

IntTy is the interface implemented by types that should be treated as int type.

type Pointer

type Pointer interface {
	ReflectPointer

	// IsNullptr returns true if the address is 0.
	IsNullptr() bool

	// Address returns the pointer's memory address.
	Address() uint64

	// Offset returns the pointer offset by n bytes.
	Offset(n uint64) Pointer

	// ElementSize returns the size in bytes of the element type.
	ElementSize(m *device.MemoryLayout) uint64

	// ElementType returns the type of the pointee.
	ElementType() reflect.Type

	// ISlice returns a new Slice of elements based from this pointer using
	// start and end indices.
	ISlice(start, end uint64, m *device.MemoryLayout) Slice
}

Pointer is the type representing a memory pointer.

func BytePtr

func BytePtr(addr uint64) Pointer

BytePtr returns a pointer to bytes.

func NewPtr

func NewPtr(addr uint64, elTy reflect.Type) Pointer

NewPtr returns a new pointer.

type Pool

type Pool struct {
	OnRead  func(rng Range, root uint64, t uint64, api id.ID)
	OnWrite func(rng Range, root uint64, t uint64, api id.ID)
	// contains filtered or unexported fields
}

Pool represents an unbounded and isolated memory space. Pool can be used to represent the application address space, or hidden GPU Pool.

Pool can be sliced into smaller regions which can be read or written to. All writes to Pool or its slices do not actually perform binary data copies, but instead all writes are stored as lightweight records. Only when a Pool slice has Get called will any resolving, loading or copying of binary data occur.

OnRead and OnWrite functions take 3 parameters. The first is the range of memory. The second is the root of the read.

 that is, the pointer from which the range was derived. For
 example:
    uint32_t* foo;
    foo[2]; Range(Base: foo+2, Size 4), Root foo
Last is the service.type ID of the observations. This will
    always be a slice at this point.

func (*Pool) At

func (m *Pool) At(addr uint64) Data

At returns an unbounded Data starting at p.

func (*Pool) Slice

func (m *Pool) Slice(rng Range) Data

Slice returns a Data referencing the subset of the Pool range.

func (*Pool) String

func (m *Pool) String() string

String returns the full history of writes performed to this pool.

func (*Pool) Strlen

func (m *Pool) Strlen(ctx context.Context, ptr uint64) (uint64, error)

Strlen returns the run length of bytes starting from ptr before a 0 byte is reached.

func (*Pool) TempSlice

func (m *Pool) TempSlice(rng Range) Data

TempSlice returns a slice that is only valid until the next Read/Write operation on this pool. This puts much less pressure on the garbage collector since we don't have to make a copy of the range.

func (*Pool) Write

func (m *Pool) Write(dst uint64, src Data)

Write copies the data src to the address dst.

type PoolID

type PoolID uint32

PoolID is an identifier of a Pool.

type Pools

type Pools struct {
	OnCreate func(PoolID, *Pool)
	// contains filtered or unexported fields
}

Pools contains a collection of Pools identified by PoolIDs.

func NewPools

func NewPools() Pools

NewPools creates and returns a new Pools instance.

func (*Pools) ApplicationPool

func (m *Pools) ApplicationPool() *Pool

ApplicationPool returns the application memory pool.

func (*Pools) Clone

func (p *Pools) Clone() Pools

func (*Pools) Count

func (m *Pools) Count() int

Count returns the number of pools.

func (*Pools) Get

func (m *Pools) Get(id PoolID) (*Pool, error)

Get returns the Pool with the given id.

func (*Pools) MustGet

func (m *Pools) MustGet(id PoolID) *Pool

MustGet returns the Pool with the given id, or panics if it's not found.

func (*Pools) New

func (m *Pools) New() (id PoolID, p *Pool)

New creates and returns a new Pool and its id.

func (*Pools) NewAt

func (m *Pools) NewAt(id PoolID) *Pool

NewAt creates and returns a new Pool with a specific ID, fails if it cannot

func (*Pools) NextPoolID

func (m *Pools) NextPoolID() PoolID

NextPoolID returns the next free pool ID (but does not assign it). All existing pools in the set have pool ID which is less than this value.

func (*Pools) SetOnCreate

func (m *Pools) SetOnCreate(onCreate func(PoolID, *Pool))

SetOnCreate sets the OnCreate callback and invokes it for every pool already created.

func (*Pools) String

func (m *Pools) String() string

String returns a string representation of all pools.

type Range

type Range struct {
	Base uint64 // The address of the first byte in the memory range.
	Size uint64 // The size in bytes of the memory range.
}

Range represents a region of memory.

func Store

func Store(ctx context.Context, l *device.MemoryLayout, at Pointer, v ...interface{}) (Range, id.ID)

Store encodes and stores the value v to the database d, returning the memory range and new resource identifier. Data can be used to as a helper to AddRead and AddWrite methods on commands.

func (Range) Contains

func (i Range) Contains(addr uint64) bool

Contains returns true if the address addr is within the Range.

func (Range) End

func (i Range) End() uint64

End returns the address of one byte beyond the end of the Range.

func (Range) Expand

func (i Range) Expand(addr uint64) Range

Expand returns a new Range that is grown to include the address addr.

func (Range) First

func (i Range) First() uint64

First returns the address of the first byte in the Range.

func (Range) Includes

func (i Range) Includes(r Range) bool

Includes returns true if the Range r is included within the Range i.

func (Range) Intersect

func (i Range) Intersect(other Range) Range

Intersect returns the range that is common between this Range and other. If the two memory ranges do not intersect, then this function panics.

func (Range) Last

func (i Range) Last() uint64

Last returns the address of the last byte in the Range.

func (Range) Overlaps

func (i Range) Overlaps(other Range) bool

Overlaps returns true if other overlaps this memory range.

func (Range) Span

func (i Range) Span() interval.U64Span

Span returns the Range as a U64Span.

func (Range) String

func (i Range) String() string

func (Range) TrimLeft

func (i Range) TrimLeft(count uint64) Range

TrimLeft cuts count bytes from the left of the Range.

func (Range) Window

func (i Range) Window(win Range) Range

Window returns the intersection of i and win, with the origin (0) address at win.Base. If the two memory ranges do not intersect, then this function panics.

type RangeList

type RangeList []Range

func (*RangeList) Copy

func (l *RangeList) Copy(to, from, count int)

Copy performs a copy of ranges within the RangeList.

func (*RangeList) GetSpan

func (l *RangeList) GetSpan(index int) interval.U64Span

GetSpan returns the span of the range with the specified index in the RangeList.

func (*RangeList) Length

func (l *RangeList) Length() int

Length returns the number of ranges in the RangeList.

func (*RangeList) New

func (l *RangeList) New(index int, span interval.U64Span)

New replaces the range of the span with the specified index in the RangeList.

func (*RangeList) Resize

func (l *RangeList) Resize(length int)

Resize resizes the RangeList to the specified length.

func (*RangeList) SetSpan

func (l *RangeList) SetSpan(index int, span interval.U64Span)

SetSpan adjusts the range of the span with the specified index in the RangeList.

type ReflectPointer

type ReflectPointer interface {
	APointer()
}

ReflectPointer is a helper interface, if you want your pointer to be reflected then it must ALSO implement this interface. Since reflection is slow having a much smaller interface to check is significantly better We name this APointer since reflect.Implements checks functions in alphabetical order, meaning this should get hit first (or close to).

type Size

type Size uint64

Size is a size_t type.

func (Size) IsMemorySize

func (Size) IsMemorySize()

IsMemorySize is a placeholder function to make Size implement SizeTy interface

type SizeTy

type SizeTy interface {
	IsMemorySize()
}

SizeTy is the interface implemented by types that should be treated as size_t type.

type SizedTy

type SizedTy interface {
	// Size returns the size of the type in bytes.
	TypeSize(*device.MemoryLayout) uint64
}

SizedTy is the interface implemented by types that can calculate their size.

type Slice

type Slice interface {
	// Root returns the original pointer this slice derives from.
	Root() uint64

	// Base returns the address of first element.
	Base() uint64

	// Size returns the size of the slice in bytes.
	Size() uint64

	// Count returns the number of elements in the slice.
	Count() uint64

	// Pool returns the the pool identifier.
	Pool() PoolID

	// ElementType returns the reflect.Type of the elements in the slice.
	ElementType() reflect.Type

	// ISlice returns a sub-slice from this slice using start and end indices.
	ISlice(start, end uint64) Slice
}

Slice is the interface implemented by types that represent a slice on a memory pool.

func NewSlice

func NewSlice(root, base, size, count uint64, pool PoolID, elTy reflect.Type) Slice

NewSlice returns a new Slice.

type Uint

type Uint uint64

Uint is an unsigned integer type.

func (Uint) IsUint

func (Uint) IsUint()

IsUint is a placeholder function to make Uint implement UintTy interface

type UintTy

type UintTy interface {
	IsUint()
}

UintTy is the interface implemented by types that should be treated as uint type.

Directories

Path Synopsis
Package memory_pb describes the serialization format for the memory package.
Package memory_pb describes the serialization format for the memory package.

Jump to

Keyboard shortcuts

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