News for hackers. Stuff that matters.
Re:Always Bump Downwards (2019) by dev_dwarf on Tuesday September 19, 2023 @12:40PM (#37572535)
In the "bump up" version you could remove both the checked_add branches and replace them with a single check at the end, making the amount of branches the same.

Quick example: https://godbolt.org/z/bqWrEqoov.

Re:Always Bump Downwards (2019) by ridiculous_fish on Tuesday September 19, 2023 @01:42PM (#37573435)
I think this doesn't work because `aligned + size` may wrap all the way around into the valid region again. For example if aligned == ptr + 1, and size is usize::MAX, we will end up with new_ptr == ptr and the allocation will wrongly succeed.
Re:Always Bump Downwards (2019) by loeg on Tuesday September 19, 2023 @02:34PM (#37574168)
The straightforward answer is: don't tolerate ridiculous alignments.
Re:Always Bump Downwards (2019) by saagarjha on Tuesday September 19, 2023 @08:39PM (#37579042)
You'd need to check for that, though.
Re:Always Bump Downwards (2019) by loeg on Tuesday September 19, 2023 @11:16PM (#37580029)
No, just restrict the domain of the alignment input to like, u8. Maybe u16. Either way, it is easy to ensure your bump allocation space is far enough away from SIZE_MAX.
Re:Always Bump Downwards (2019) by dev_dwarf on Tuesday September 19, 2023 @02:03PM (#37573754)
Interesting point. I modified my example to test what you described. I had to play with the compilation flags to get the allocs to not be optimized out and to not panic when the integer overflow happens, but otherwise I didn't change the logic. I'm pretty sure my implementation is correctly handling the case you mention, evidenced by it returning a null pointer.

Link: https://godbolt.org/z/f1jGW6Pa3

Update: NVM, definitely not being handled correctly. https://godbolt.org/z/cMTe1o979

Re:Always Bump Downwards (2019) by sltkr on Tuesday September 19, 2023 @01:56PM (#37573660)
That version is unsafe: what if size == 0xfff..fff and alignment is needed? You will end up with ptr <= new_ptr < end, seemingly a valid result, but actually with not enough space.

edit: code moved to a toplevel comment

Re:Always Bump Downwards (2019) by loeg on Tuesday September 19, 2023 @02:35PM (#37574197)
No allocator can be expected to allocate usize::MAX, so it doesn't really matter.
Re:Always Bump Downwards (2019) by dev_dwarf on Tuesday September 19, 2023 @02:51PM (#37574483)
Agreed. The question really is if you should demand the user to enforce that constraint on the size they pass to you, or if the function itself should signal an error in that case.
Re:Always Bump Downwards (2019) by loeg on Tuesday September 19, 2023 @04:22PM (#37575963)
I think it would be pretty reasonable to have an input type for that parameter that isn't a full usize and is instead some more restricted type that can only represent smaller values. The alignment parameter could be, like, u8, or maybe u16.
Re:Always Bump Downwards (2019) by dev_dwarf on Tuesday September 19, 2023 @04:45PM (#37576284)
For the alignment parameter I agree.
Re:Always Bump Downwards (2019) by loeg on Tuesday September 19, 2023 @05:38PM (#37577142)
For both.
Re:Always Bump Downwards (2019) by sltkr on Tuesday September 19, 2023 @08:15PM (#37578861)
This still doesn't solve the overflow problem.

It's also too limiting: with u8 you can't ask for page-aligned data, and with u16 not for hugepage-aligned data. Granted, those aren't exactly prime use cases for a bump allocator, but it seems like poor design to limit the API unnecessarily.

Re:Always Bump Downwards (2019) by loeg on Tuesday September 19, 2023 @11:14PM (#37580016)
It fully solves the overflow problem. You don't need page-aligned data in a bump allocator.
Re:Always Bump Downwards (2019) by sltkr on Tuesday September 19, 2023 @04:20PM (#37575943)
It matters because if the allocator cannot allocate a given amount, it should reliably return NULL to iform the caller that the allocation failed, not return a random invalid pointer that's not usable, which will lead to undefined behavior.
Re:Always Bump Downwards (2019) by loeg on Tuesday September 19, 2023 @04:30PM (#37576090)
The API should restrict callers from providing bogus values at all.
Re:Always Bump Downwards (2019) by rictic on Tuesday September 19, 2023 @06:38PM (#37577995)
How would the API do that without more overhead than the check that GP is suggesting?
Re:Always Bump Downwards (2019) by loeg on Tuesday September 19, 2023 @11:13PM (#37580012)
For some uses it might be reasonable to restrict the size input to compile-time constants, which can be verified at compile time. Or you could have a newtype with a checked safe constructor and an unchecked unsafe constructor, which allows bypassing the overhead when you know the sizes are reasonable. On 64-bit systems, it is reasonable in many domains to restrict allocation size to u32. There are lots of possible ways to approach this without falling back to "tolerate any usize input."