Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose memory implementation #1331

Closed
inkeliz opened this issue Apr 1, 2023 · 1 comment
Closed

Expose memory implementation #1331

inkeliz opened this issue Apr 1, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@inkeliz
Copy link
Contributor

inkeliz commented Apr 1, 2023

Is your feature request related to a problem? Please describe.

I'm playing with mmap, and because of that I need to manipulate the memory from Wazero. Currently, I have the following code:

		// internalWazeroMemoryInstance is a private struct in wazero
		type internalWazeroMemoryInstance struct {
			Buffer        []byte
			Min, Cap, Max uint32
			mux           sync.RWMutex
		}

		// wasm.Memory is private, we need to use unsafe
		_hostMemoryInstance := mod.Memory()
		hostMemoryInstance := (*[2]*internalWazeroMemoryInstance)(unsafe.Pointer(&_hostMemoryInstance))[1]

		hostMemoryInstance.mux.Lock()
		defer hostMemoryInstance.mux.Unlock()

                //...
               
                // redefine wazero memory (including capacity)
		hostMemoryInstance.Buffer = mem
		hostMemoryInstance.Cap = f.pages

https://github.com/inkeliz/karmap/blob/b0ed8241fbc27fd4f3b915bdba8da9ebec17f8ba/wazero/wasmkarmap/karmap.go#L62-L74

That is necessaty because I need to modify the Buffer and also Cap. And I need to do it for two reasons:

  1. I have a custom Buffer implementation, using mmap.
  2. I need to have a fixed size of memory (see Initialize memory from limit (with WithMemoryLimitPages + WithMemoryCapacityFromMax) #1330).

Describe the solution you'd like
Maybe a simpler interface/struct of Memory, which could be:

type Memory struct {
Buffer []byte
Grow func(pages int) int
}

Or, using Interface:

type MemoryInterface interface {
 Buffer() []byte
 Grow(page int) int
}

In that case, anyone can have any custom implementation, which will be called when "Grow" (fixing #1150), and also provides one way to implement Buffer using mmap, arenas (golang/go#51317) or anything else.

The current Memory implementation is very large, and contains a lot of Read* and such, which makes very hard to implement and, since we have []byte by default, anyone can read from that (so ReadFloat32Le can just take from .Buffer()).

Describe alternatives you've considered
Using unsafe, which works, but it's unsafe and unstable.

Additional context
Add any other context or screenshots about the feature request here.

@mathetake
Copy link
Member

#2177

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants