Allocator Designs #1016
Replies: 49 comments 3 replies
-
Thanks for the post! As usual, it is very interesting!
looks like typo |
Beta Was this translation helpful? Give feedback.
-
The
Or, if you use powers of two as argument instead:
|
Beta Was this translation helpful? Give feedback.
-
I should mention that when using a First, while you normally can't delete entries from the allocator, you can delete the last entry. In other words, if you know what you are doing, you can allow a Second, it is common practice to allocate from both the front and the back of the memory region. This is useful where some of the allocations are temporary. You put temporary allocations at the end, while longer-lasting allocation are put in the front. Anyway, both these methods are quite dangerous, but in terms of raw speed, there's no comparison. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your comments!
Good point! I'll extend that section. The fastest variant I'm aware of is relying on the optimized #[no_mangle]
fn align_up(addr: usize, align: usize) -> usize {
let offset = (addr as *const u8).align_offset(align);
addr + offset
}
One could also check in deallocate whether the end address of the freed allocation equals
Good point! While this is difficult to implement for a global allocator, it definitely works for manual allocations. I try to update the bump allocator discussion section with both your suggestions. |
Beta Was this translation helpful? Give feedback.
-
@engstad I prepared two updates for the post related to your comments. #721 adds more variants to implement |
Beta Was this translation helpful? Give feedback.
-
@senseiod Thanks! |
Beta Was this translation helpful? Give feedback.
-
@MikailBag Thank you! Could you maybe clarify what the typo is? I don't see it right now… |
Beta Was this translation helpful? Give feedback.
-
Nice comparison, thanks! For more on bump allocation, see this post (in Rust, even): Also small typo in the beginning: |
Beta Was this translation helpful? Give feedback.
-
@amosonn Thanks for you comment!
I already link this post in the Dicussion section as "can be optimized to just a few assembly operations". I deliberatly decided against bumping from the end because the intention of the post is to explain a basic implementation, not to maximally optimize it. Regarding the alignment function: I think the
Good point! I'll prepare a PR to fix this.
Thanks! Fixed in 4b8c902. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Ah sorry, I missed the link :). Regarding the various alignment implementations: |
Beta Was this translation helpful? Give feedback.
-
Thanks for investigating! I'll update #721 to use the |
Beta Was this translation helpful? Give feedback.
-
In the code for LinkedListAllocator, |
Beta Was this translation helpful? Give feedback.
-
@Menschenkindlein Thanks! Fixed in 00fedc8 and 670ac60. |
Beta Was this translation helpful? Give feedback.
-
First off, thanks for the excellent series, it's been extremely interesting to go through! Just a small note, if you skip the implementation of the linked list allocator, you won't already have:
in your lib.rs. Just something I ran into. |
Beta Was this translation helpful? Give feedback.
-
@diminishedprime Thanks for reporting! I pushed 10d84fa to fix this issue. |
Beta Was this translation helpful? Give feedback.
-
As small note, |
Beta Was this translation helpful? Give feedback.
-
@jiayihu Sorry for the late reply!
This depends on whether you declare the See: blog_os/src/allocator/fixed_size_block.rs Lines 14 to 20 in ca3dfc7 |
Beta Was this translation helpful? Give feedback.
-
I found an issue with the allocators which was caused by using a mutable reference in a constant function (both in linked list allocator and in fixed size allocator), and removing that caused the issue |
Beta Was this translation helpful? Give feedback.
-
@DeathBySpork Thanks for sharing your problem and solution. Yes, adding
(See https://os.phil-opp.com/allocator-designs/#the-allocator-type and https://os.phil-opp.com/allocator-designs/#the-allocator-type-1) |
Beta Was this translation helpful? Give feedback.
-
ah, I guess I didnt notice the change, I am sorry! |
Beta Was this translation helpful? Give feedback.
-
@DeathBySpork No worries, thanks for reporting your problems! |
Beta Was this translation helpful? Give feedback.
-
I still got error "calls in statics are limited to constant functions, tuple structs and tuple variants". I've add #![feature(const_in_array_repeat_expressions)] and #![feature(const_mut_refs)] in main.rs but still got error how to solve it ? My Code in https://github.com/Ananta98/PetraOS. Thank you. |
Beta Was this translation helpful? Give feedback.
-
@Ananta98 Looks like you forgot to make your diff --git a/src/mm/allocator.rs b/src/mm/allocator.rs
index 1ee24e3..8b911b1 100644
--- a/src/mm/allocator.rs
+++ b/src/mm/allocator.rs
@@ -15,7 +15,7 @@ pub struct Locked<A> {
}
impl<A> Locked<A> {
- pub fn new(inner : A) -> Self {
+ pub const fn new(inner : A) -> Self {
Locked {
inner : Mutex::new(inner),
} After this change, it works for me. |
Beta Was this translation helpful? Give feedback.
-
Thank you @phil-opp it works now. sorry this is human fault. |
Beta Was this translation helpful? Give feedback.
-
Great to hear that! No worries, I'm happy to help. |
Beta Was this translation helpful? Give feedback.
-
Thank you so much for your work on this post series and the supporting crates! It's been extremely educational, following along. I'm either confused, or this is input: the below asserts in the fixed block allocator // verify that block has size and alignment required for storing node
assert!(mem::size_of::<ListNode>() <= BLOCK_SIZES[index]);
assert!(mem::align_of::<ListNode>() <= BLOCK_SIZES[index]); seem like they'll never be useful.
If I'm right, then we run these assertions each time we deallocate memory, paying runtime cost for no value, I think? There is a useful assertion to be made (once, maybe in |
Beta Was this translation helpful? Give feedback.
-
How can I translate the posts in Korean |
Beta Was this translation helpful? Give feedback.
-
Amazing article, thank you. |
Beta Was this translation helpful? Give feedback.
-
This is a general purpose comment thread for the Allocator Designs post.
Beta Was this translation helpful? Give feedback.
All reactions