Skip to content

allow safety checks to compare the identity ofstd.mem.Allocators #23068

@190n

Description

@190n

I'm working on getting Bun ready for Zig 0.14. We have an unmanaged container for borrowed subslices of strings, which uses one of the bits in the len to store whether it needs to be freed. In debug builds, CowSlice also stores an allocator so that, even though it is unmanaged, it can make sure you don't try to free it with the wrong allocator.

However, I understand that comparing the ptr field on an Allocator is unsound because some implementations set it to undefined. I see that this approach was maintained in #22691, instead of the counter-proposal to set unneeded ptr fields to null instead. We can of course compare only the vtable field instead, but that makes the safety check weaker, because it would consider two different instances of the same kind of allocator to be equal (e.g. two arena or fixed buffer allocators). I don't see a way to provide the strongest possible safety guarantees without sometimes comparing undefined pointers.

This safety check seems like it might be useful for std's unmanaged containers as well. Is this a use case that should be supported?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions