Simplify mmap.

This commit is contained in:
Nuno Cruces
2024-04-26 16:25:45 +01:00
parent fa259bdc94
commit 019246d1be
9 changed files with 29 additions and 45 deletions

View File

@@ -12,22 +12,13 @@ import (
"golang.org/x/sys/unix"
)
func withMmappedAllocator(ctx context.Context) context.Context {
return experimental.WithMemoryAllocator(ctx,
experimental.MemoryAllocatorFunc(mmappedAllocator))
}
type mmapState struct {
regions []*MappedRegion
enabled bool
}
func (s *mmapState) init(ctx context.Context, enabled bool) context.Context {
if s.enabled = enabled; enabled {
return experimental.WithMemoryAllocator(ctx,
experimental.MemoryAllocatorFunc(mmappedAllocator))
}
return ctx
}
func CanMapFiles(ctx context.Context) bool {
s := ctx.Value(moduleKey{}).(*moduleState)
return s.mmapState.enabled
}
func (s *mmapState) new(ctx context.Context, mod api.Module, size int32) *MappedRegion {

View File

@@ -6,10 +6,6 @@ import "context"
type mmapState struct{}
func (s *mmapState) init(ctx context.Context, _ bool) context.Context {
func withMmappedAllocator(ctx context.Context) context.Context {
return ctx
}
func CanMapFiles(ctx context.Context) bool {
return false
}

View File

@@ -12,10 +12,10 @@ type moduleState struct {
mmapState
}
func NewContext(ctx context.Context, mappableMemory bool) context.Context {
func NewContext(ctx context.Context) context.Context {
state := new(moduleState)
ctx = context.WithValue(ctx, moduleKey{}, state)
ctx = withMmappedAllocator(ctx)
ctx = experimental.WithCloseNotifier(ctx, state)
ctx = state.mmapState.init(ctx, mappableMemory)
ctx = context.WithValue(ctx, moduleKey{}, state)
return ctx
}