Skip to content

Static memory segment idea #1232

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

Closed
dcodeIO opened this issue Apr 18, 2020 · 8 comments · Fixed by #1233
Closed

Static memory segment idea #1232

dcodeIO opened this issue Apr 18, 2020 · 8 comments · Fixed by #1233

Comments

@dcodeIO
Copy link
Member

dcodeIO commented Apr 18, 2020

So we have StaticArray now, which is great on its own but still a managed object, and it would be even greater if there'd also be a way to get some raw static memory. Proposing the following:

Extend the memory namespace to be also a function, that one can use like

const myMem = memory(64);

yielding a static pointer to 64 bytes of reserved static memory. When using this in a function

function foo(): i32 {
  const ptr = memory(4);
  var value = load<i32>(ptr);
  store<i32>(ptr, value + 1);
  return value;
}

the segment will always be exactly the same, so calling the function above multiple times would yield 0, 1, 2, 3, ... quite similar to what static in C does. This can then be used for various things, like as an alternative to (temporary) stack allocation as long as one makes sure that it is not being accessed in parallel. The argument must be a compile-time constant.

Thoughts?

@MaxGraey
Copy link
Member

How about const mem = memory.init(64)?

@jtenner
Copy link
Contributor

jtenner commented Apr 18, 2020

Yes! This would help reduce globals and enable creating threaded memory allocators. My vote is on memory.init().

@dcodeIO
Copy link
Member Author

dcodeIO commented Apr 18, 2020

How about const mem = memory.init(64)?

memory.init is a legitimate instruction specified by bulk-memory that copies "a region from a data segment". Just memory is relatively safe in that it cannot conflict with an instruction by design.

@MaxGraey
Copy link
Member

MaxGraey commented Apr 18, 2020

alright, May be memory.reserve, memory.segment or memory.alloc? Because memory(64) looks slightly inconsistent

@dcodeIO
Copy link
Member Author

dcodeIO commented Apr 18, 2020

An alternative might be memory.data which is a section, but not an instruction?

@MaxGraey
Copy link
Member

memory.data also lgtm

@dcodeIO
Copy link
Member Author

dcodeIO commented Apr 18, 2020

For the record, I liked memory since it semantically says "give me n bytes within memory" and reuses something that would otherwise be just a namespace, making it fast to type :)

@jedisct1
Copy link

This is a very useful addition!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants