News for hackers. Stuff that matters.
Re:Always Bump Downwards (2019) by sltkr on Tuesday September 19, 2023 @02:09PM (#37573837)
You can also implement BumpUp() with only two conditionals and no overflow like this (in C, because I don't know Rust, but the logic is identical):

    size_t alignment_needed = align - (ptr & (align - 1));
    if (alignment_needed > SIZE_MAX - size) return nullptr;
    size_t space_needed = size + alignment_needed;
    size_t space_remaining = end - ptr;
    if (space_needed > space_remaining) return nullptr;
    char *result = ptr + alignment_needed;
    ptr += space_needed;
    return result;
Re:Always Bump Downwards (2019) by loeg on Tuesday September 19, 2023 @02:31PM (#37574140)
Yeah, and you only need one check if you can restrict the allocation pointer to be at least max alignment away from SIZE_MAX, which is very easy to do in practice. Max alignment needed is usually no more than 64 bytes, but even 4kB isn't especially burdensome. On Linux/amd64, you don't even need to do anything special -- the high virtual address space is likely reserved for the kernel anyway.
Re:Always Bump Downwards (2019) by dev_dwarf on Tuesday September 19, 2023 @02:22PM (#37574024)
Nice. Seems to work for case you mentioned under my comment: https://godbolt.org/z/TYorcd8b6