Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

How does wee_alloc know where to start the heap? #61

Closed

Description

@jayphelps

Hey folks! I'm looking to experiment with wee_alloc in a new language--ignoring the perf complications for now--so I need to fully grok how it knows what portion of linear memory is safe to use so it doesn't clobber the stack/static allocations.

I asked on Stack Overflow, but I hope you don't mind that even though it's only been a couple of days I thought I might cross-post here since it isn't likely to get a response otherwise. If someone has a moment to ask on Stack Overflow, it's much appreciated! I've dug into the code and tried to walk it backwards best I can but I'm not confident in my findings.

https://stackoverflow.com/questions/52022998/how-does-wee-alloc-a-malloc-alternative-know-where-to-start-the-heap-in-webass


I'm trying to utilize wee_alloc in a custom language, running in WebAssembly. However, I need to fully grok how it knows where to start the heap at so that my stack and static allocations do not clobber it and vice versa.

It's my understanding that how malloc, et al. know where to start the heap is platform dependent and often just a convention, or in some cases not applicable. However in WebAssembly we can only have a single contiguous piece of linear memory, so we have to share it and a convention needs to be used.

Reading through the code it appears that what wee_alloc does is make the assumption that what ever memory we start with is off-limits completely, and instead will use the grow_memory instruction to create the first piece of memory needed for the heap. That effectively means that the index/address of the start of the heap is highest index of what ever the initial size is, plus one.

e.g. if we start off with an initial memory size of 1 page:

current_memory = 1 page = 64KiB = 65,536 bytes
then the heap starts at index 65537.

Is my understanding correct?

Activity

  1. changed the title [-]How does wee_alloc (a malloc alternative) know where to start the heap?[/-] [+]How does wee_alloc know where to start the heap?[/+] on Aug 29, 2018
  2. pepyakin commented on Aug 29, 2018

    @pepyakin
    Member

    Your understanding is correct! With a small exception though: since the indexes are zero based, the last index of the first page is 65535, and the first index of the second page is 65536.

  3. jayphelps commented on Aug 29, 2018

    @jayphelps
    Author

    Doh! Of course! Thank you!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions