mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 18:38:55 +00:00
Add an owned pointer type—a wrapper around a pointer and an allocator. `Owned(*Foo)` and `Owned([]Foo)` contain both the pointer/slice and the allocator that was used to allocate it. Calling `deinit` on these types first calls `Foo.deinit` and then frees the memory. This makes it easier to remember to free the memory, and hard to accidentally free it with the wrong allocator. Optional pointers are also supported (`Owned(?*Foo)`, `Owned(?[]Foo)`), and an unmanaged variant which doesn't store the allocator (`Owned(*Foo).Unmanaged`) is available for cases where space efficiency is a concern. A `MaybeOwned` type is also provided for representing data that could be owned or borrowed. If the data is owned, `MaybeOwned.deinit` works like `Owned.deinit`; otherwise, it's a no-op. (For internal tracking: fixes STAB-920, STAB-921)