-
Notifications
You must be signed in to change notification settings - Fork 173
sdk: Use forward allocation #301
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
Conversation
4ab6fab to
1b0bfc6
Compare
joncinque
left a comment
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.
Looks great to me! Mostly nits and one comment
| /// `layout` must have non-zero size. Attempting to allocate for a zero-sized layout will | ||
| /// result in undefined behavior. |
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.
I believe this is ok, since most users of allocators no-op if the type is zero-sized, ie https://doc.rust-lang.org/src/alloc/boxed.rs.html#571
sdk/src/entrypoint/mod.rs
Outdated
| let padding = layout.align() - 1; | ||
|
|
||
| if unlikely(self.end - pos < padding) { | ||
| return null_mut(); |
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.
Maybe I'm being dense, but is this check necessary? allocation finds the start address, so we should just need to later check that self.end > allocation + layout.size()
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.
I was avoiding doing the allocation + layout.size() operation without checks since it can overflow if you try to allocate u64::MAX for example. And allocation might be bigger than self.end if there is no "space" left for the alignment padding.
We could probably save 1 CU by testing that layout.size() is not greater than MAX_HEAP_SIZE and then do the check self.end > allocation + layout.size() instead. I will try that.
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.
It does save 1 extra CU. 😊
| unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { | ||
| self.alloc(layout) |
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.
hehe, very cool!
joncinque
left a comment
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.
Perfect!
Problem
Currently, the
BumpAllocatorallocates memory backwards and it has a fixed limit of32KiBsize. Even if a program requests a larger heap size, this won't be used unless a custom allocator is implemented.Solution
Refactor the
BumpAllocatorto allocate memory forward. This has 2 benefits:~7CUs per allocation;The implementation relies on the runtime to zero initialize the heap region and to enforce the available heap length. The execution of a program is sand-boxed and the runtime prevents memory access outside the program memory space.