Stop __set_heap_size from trashing soft stack#410
Open
johnwbyrd wants to merge 1 commit intollvm-mos:mainfrom
Open
Stop __set_heap_size from trashing soft stack#410johnwbyrd wants to merge 1 commit intollvm-mos:mainfrom
johnwbyrd wants to merge 1 commit intollvm-mos:mainfrom
Conversation
__set_heap_limit now sanity checks against stack; users can now grab all memory for heap mos-sim flushes every output character -- no waiting for output These changes were necessary to be able to get pi.c to calculate to 50000 digits on the Commodore 64. Memory allocation had a tendency to walk all over the stack; this puts some sanity checks in place to prevent others from doing the same. Default behavior is still 4 KB for heap.
Member
Author
|
With this change, a user can: and they will get the maximum amount of platform memory available for malloc(). |
mysterymath
approved these changes
Sep 7, 2025
Member
mysterymath
left a comment
There was a problem hiding this comment.
Thanks, this is a really nice change! Few comments, otherwise LGTM!
| return nullptr; | ||
| } | ||
|
|
||
| " rts\n" |
| // Get current soft stack pointer value | ||
| __asm__ ( | ||
| ".text\n" | ||
| ".global __get_current_stack_top\n" |
Member
There was a problem hiding this comment.
We can avoid making this a global symbol at all by using inline assembly in a static function. This would also allow the compiler to inline this, which would usually be beneficial.
Member
Author
There was a problem hiding this comment.
Okay, but inline assembly WITHIN a function is a bit of a dark art -- let me try...
Member
There was a problem hiding this comment.
This is def optional; lemme know if you wanna take a crack at this later, and I"ll merge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Limit the __set_heap_size function to not trash the stack if too much memory is requested. Additionally, choose a default stack size and use it as a safety margin when increasing heap size.
The new stack size, __stack_reserve_size, is set weakly to 1024, and anyone who wants to change it can do so without recompiling.
Additionally, the simulator now flushes on stdout output. This helps establish that long-running tasks are still running on mos-sim.
These changes were necessary to be able to get pi.c to calculate to 50000 digits on the Commodore 64.