-
Notifications
You must be signed in to change notification settings - Fork 63
Stop __set_heap_size from trashing soft stack #410
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
Open
johnwbyrd
wants to merge
1
commit into
llvm-mos:main
Choose a base branch
from
johnwbyrd:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+52
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,20 @@ | |
|
||
extern char __heap_start; | ||
extern char __heap_default_limit; | ||
extern char __stack_reserve_size; | ||
|
||
// Get current soft stack pointer value | ||
__asm__ ( | ||
".text\n" | ||
".global __get_current_stack_top\n" | ||
"__get_current_stack_top:\n" | ||
" lda __rc0\n" // Load low byte of soft stack pointer | ||
" ldx __rc1\n" // Load high byte of soft stack pointer | ||
" rts\n" | ||
); | ||
|
||
// Declare the assembly function for C | ||
extern "C" size_t __get_current_stack_top(); | ||
|
||
namespace { | ||
|
||
|
@@ -179,6 +193,7 @@ FreeChunk *find_fit(size_t size) { | |
return nullptr; | ||
} | ||
|
||
" rts\n" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is definitely a typo. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
// Allocate at chunk of size bytes from a free chunk. The pointer returned | ||
// points to the contents (past the chunk header). | ||
void *allocate_free_chunk(FreeChunk *free_chunk, size_t size) { | ||
|
@@ -221,24 +236,44 @@ extern "C" { | |
|
||
size_t __heap_limit() { return heap_limit; } | ||
|
||
void __set_heap_limit(size_t new_limit) { | ||
size_t __get_heap_max_safe_size() { | ||
size_t stack_location = __get_current_stack_top(); | ||
size_t reserve_size = (size_t)&__stack_reserve_size; | ||
size_t heap_start = (size_t)&__heap_start; | ||
|
||
if (stack_location <= heap_start + reserve_size) { | ||
// Stack is too close to heap start; no safe heap possible | ||
return 0; | ||
} | ||
|
||
return stack_location - heap_start - reserve_size; | ||
} | ||
|
||
size_t __set_heap_limit(size_t new_limit) { | ||
TRACE("__set_heap_limit(%u)\n", new_limit); | ||
|
||
// Chunk sizes must be a multiple of two. | ||
if (new_limit & 1) | ||
--new_limit; | ||
|
||
// Cap the request to avoid stack collision | ||
size_t max_safe = __get_heap_max_safe_size(); | ||
if (new_limit > max_safe) { | ||
TRACE("Capping requested limit %u to max safe %u\n", new_limit, max_safe); | ||
new_limit = max_safe; | ||
} | ||
|
||
if (!initialized) { | ||
heap_limit = (new_limit < MIN_CHUNK_SIZE) ? MIN_CHUNK_SIZE : new_limit; | ||
TRACE("Heap not yet initialized. Set limit to %u.\n", heap_limit); | ||
return; | ||
return heap_limit; | ||
} | ||
|
||
// TODO: We can make this actually shrink the heap too... | ||
if (new_limit <= heap_limit) { | ||
TRACE("New limit %u smaller than current %u; returning.", new_limit, | ||
TRACE("New limit %u smaller than current %u; returning.\n", new_limit, | ||
heap_limit); | ||
return; | ||
return heap_limit; | ||
} | ||
|
||
size_t grow = new_limit - heap_limit; | ||
|
@@ -255,14 +290,15 @@ void __set_heap_limit(size_t new_limit) { | |
TRACE("Last chunk not free.\n"); | ||
if (grow < MIN_CHUNK_SIZE) { | ||
TRACE("Not enough new size for a chunk; returning.\n"); | ||
return; | ||
return heap_limit; | ||
} | ||
TRACE("Inserting new chunk.\n"); | ||
FreeChunk::insert(heap_end(), grow); | ||
last_free = true; | ||
} | ||
|
||
heap_limit = new_limit; | ||
return heap_limit; | ||
} | ||
|
||
size_t __heap_bytes_used() { return heap_limit - free_size; } | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
.weak __heap_default_limit | ||
__heap_default_limit = 4096 | ||
|
||
.weak __stack_reserve_size | ||
__stack_reserve_size = 1024 |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, but inline assembly WITHIN a function is a bit of a dark art -- let me try...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is def optional; lemme know if you wanna take a crack at this later, and I"ll merge.