Skip to content

Commit

Permalink
WIP: 针对小内存优化堆内存管理
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Jan 13, 2025
1 parent 59ec109 commit 929af7c
Show file tree
Hide file tree
Showing 4 changed files with 939 additions and 0 deletions.
66 changes: 66 additions & 0 deletions internal/waroot/malloc/malloc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 版权 @2025 凹语言 作者。保留所有权利。

package malloc

import (
_ "embed"
)

//go:embed malloc.wat
var malloc_wat string

// 默认值
const (
DefaultMemoryPages int32 = 1 // 内存页数
DefaultMemoryPagesMax int32 = 10 // 内存最大页数

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

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

// 封装的Heap, 便于测试
type Heap struct {
cfg *Config
}

// 构造新的Heap
func NewHeap(cfg *Config) *Heap {
p := &Heap{
cfg: &Config{
MemoryPages: DefaultMemoryPages,
MemoryPagesMax: DefaultMemoryPagesMax,
StackPtr: DefaultStackPtr,
HeapBase: DefaultHeapBase,
HeapLFixedCap: DefaultHeapLFixedCap,
},
}
if cfg != nil {
*p.cfg = *cfg
}
return p
}

// 分配 size 字节的内存, 返回地址 8 字节对齐
func (p *Heap) Malloc(size int32) int32 {
return 0
}

// 释放内存
func (p *Heap) Free(ptr int32) {
return
}

// 打印统计信息
func (p *Heap) String() string {
return "TODO"
}
Loading

0 comments on commit 929af7c

Please sign in to comment.