Skip to content
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

Memory usage optimization #17

Open
axos88 opened this issue Aug 30, 2019 · 5 comments
Open

Memory usage optimization #17

axos88 opened this issue Aug 30, 2019 · 5 comments

Comments

@axos88
Copy link

axos88 commented Aug 30, 2019

Based on the discussion in #16:

I think the pointer in the Hole structure can be removed and calculated on the fly from (&this as usize) + this.size.

Any reason we are not doing that?

@phil-opp
Copy link
Member

I don't think this will work. Consider https://os.phil-opp.com/kernel-heap/overview.svg: The size of the unused memory block is completely independent from the size of the used memory block afterwards.

Basically, the size field stores the size of the unused block, and the pointer implicitly stores the size of the unusable memory block that follows.

@axos88
Copy link
Author

axos88 commented Aug 30, 2019

This is only in case there is padding due to alignment requirements isn't it? I'll have to look it up how I solved this issue.

@phil-opp
Copy link
Member

If you keep track of both used and unused chunks, you can use this approach. However, if you only keep track of unused chunks, there is no way to know where the next unused block begins without storing a pointer.

@axos88
Copy link
Author

axos88 commented Aug 30, 2019

Well you don't really care about used chunks, since they're not available. And whenever somebody claims to try to free up a chunk, the allocator gets both the address, and the layout of said chunk, and blindly obliges, creating a new chunk (or extends a previous or next one, if the freed up memory are neighbours another free chunk). This would mean that an incorrect call (freeing up nonclaimed space, or double frees) would create corrupt the heap and cause chaos, but that's already the case, isn't it?

@phil-opp
Copy link
Member

This would mean that an incorrect call (freeing up nonclaimed space, or double frees) would create corrupt the heap and cause chaos, but that's already the case, isn't it?

Yes, it is. Even a deallocate call with a valid address can cause undefined behavior due to use-after-free.

Well you don't really care about used chunks, since they're not available.

Yes, that's why this crate only keeps unused blocks in the list. I just don't understand how we can remove the pointer in the list nodes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants