malloc

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2025 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KPageBytes = 64 << 10 // 一个内存页大小

	DefaultMemoryPages    int32 = 1  // 内存页数
	DefaultMemoryPagesMax int32 = 10 // 内存最大页数

	DefaultStackPtr      int32 = 8 << 12  // 栈指针
	DefaultHeapBase      int32 = 10 << 12 // 实际需要编译根据静态数据大小计算
	DefaultHeapLFixedCap int32 = 100      // 固定大小的空闲链表最大长度
)

默认值

View Source
const (
	KBlockHeadSize    = 8                  // 块头大小
	KFreeListHeadSize = 6 * KBlockHeadSize // 全部空闲链表头大小
)

内部常量

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	MemoryPages    int32 // 内存页数
	MemoryPagesMax int32 // 内存最大页数
	StackPtr       int32 // 栈指针(仅用于辅助检查)
	HeapBase       int32 // 启始地址(8字节对齐)
	HeapLFixedCap  int32 // 固定大小空闲链表最大长度
}

Heap配置

type Heap

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

封装的Heap, 便于测试

func NewHeap

func NewHeap(cfg *Config) *Heap

构造新的Heap

func (*Heap) DotString

func (p *Heap) DotString() string

func (*Heap) Free

func (p *Heap) Free(ptr int32)

释放内存

func (*Heap) Global__heap_base

func (p *Heap) Global__heap_base() int32

func (*Heap) Global__heap_l128_freep

func (p *Heap) Global__heap_l128_freep() int32

func (*Heap) Global__heap_lfixed_cap

func (p *Heap) Global__heap_lfixed_cap() int32

func (*Heap) Global__heap_ptr

func (p *Heap) Global__heap_ptr() int32

func (*Heap) Global__heap_top

func (p *Heap) Global__heap_top() int32

func (*Heap) Global__stack_ptr

func (p *Heap) Global__stack_ptr() int32

获取全局变量

func (*Heap) L128HeaderAddr

func (p *Heap) L128HeaderAddr() int32

func (*Heap) L24HeaderAddr

func (p *Heap) L24HeaderAddr() int32

空闲链表头地址

func (*Heap) L32HeaderAddr

func (p *Heap) L32HeaderAddr() int32

func (*Heap) L48HeaderAddr

func (p *Heap) L48HeaderAddr() int32

func (*Heap) L80HeaderAddr

func (p *Heap) L80HeaderAddr() int32

func (*Heap) Malloc

func (p *Heap) Malloc(size int32) int32

分配 size 字节的内存, 返回地址 8 字节对齐

func (*Heap) ReadBlock

func (p *Heap) ReadBlock(offset int32) HeapBlock

读取 HeapBlock 数据

func (*Heap) ReadL128Header

func (p *Heap) ReadL128Header() HeapBlock

func (*Heap) ReadL24Header

func (p *Heap) ReadL24Header() HeapBlock

读取空闲链表头

func (*Heap) ReadL32Header

func (p *Heap) ReadL32Header() HeapBlock

func (*Heap) ReadL48Header

func (p *Heap) ReadL48Header() HeapBlock

func (*Heap) ReadL80Header

func (p *Heap) ReadL80Header() HeapBlock

func (*Heap) ReadMemoryI32

func (p *Heap) ReadMemoryI32(offset int32) int32

去读内存

func (*Heap) String

func (p *Heap) String() string

打印统计信息

func (*Heap) WasmBytes

func (p *Heap) WasmBytes() []byte

type HeapBlock

type HeapBlock struct {
	Addr int32
	Size int32
	Next int32
}

内存块

type HeapFreeList

type HeapFreeList struct {
	Fixed  bool
	Header HeapBlock
	// contains filtered or unexported fields
}

空闲链表

func (*HeapFreeList) Cap

func (p *HeapFreeList) Cap() int32

func (*HeapFreeList) Len

func (p *HeapFreeList) Len() int32

func (*HeapFreeList) Next

func (p *HeapFreeList) Next(iter *HeapBlock) *HeapBlock

func (*HeapFreeList) String

func (p *HeapFreeList) String() string

type HeapInfo

type HeapInfo struct {
	Heap *Heap

	Pages    int32
	HeapBase int32
	HeapPtr  int32
	HeapTop  int32

	// 空闲链表头
	L24, L32, L48, L80, L128 HeapBlock

	// 固定大小的容量
	LfixedCap int32
	// L128当前位置
	L128FreePtr int32

	// 已经分配的块
	UseBlocks []HeapBlock

	// 空闲内存块节点
	FreeBlocks   []HeapBlock
	FreeBlockMap map[int32]HeapBlock

	// 节点的关系
	Edges map[string]string
}

func (*HeapInfo) LoadFreeBlocks

func (p *HeapInfo) LoadFreeBlocks() []HeapBlock

func (*HeapInfo) String

func (p *HeapInfo) String() string

Jump to

Keyboard shortcuts

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