rust-lang / wg-allocators

Home of the Allocators working group: Paving a path for a standard set of allocator traits to be used in collections!
http://bit.ly/hello-wg-allocators
203 stars 9 forks source link

Are allocators allowed to panic / unwind? #94

Open 5225225 opened 2 years ago

5225225 commented 2 years ago

It was a safety requirement that they don't for GlobalAlloc.

Should unsafe code need to deal with allocators unwinding (as opposed to aborting or otherwise halting the program)?

Amanieu commented 2 years ago

rustc now has better support for dealing with nounwind functions than when GlobalAlloc was originally implemented. Attempting to unwind from the global allocator will abort the program instead of causing undefined behavior.

Lokathor commented 2 years ago

And ideally the allocator would always return null rather than trying to panic and unwind.

miobrado commented 1 year ago

Can allocator return an enumerated value (Ok holding the allocated buffer, or Error in case of allocation failure)?

Makes no sense to panic or halt because some applications use multiple allocators for different purposes within the same application e.g., to limit memory usage by a specific component, so it's expected for some of the allocators to potentially run out of memory. This should not result in app failure, but with graceful out of memory condition handling by the component that ran out of memory, while the rest of the application continues working unaffected.

CAD97 commented 1 year ago

Allocators can of course still return null indicating that allocation failed.

berkus commented 1 year ago

Allocators can of course still return null indicating that allocation failed.

What about architectures (MCU) where nullptr is a valid RAM address?

(Would be much better to return a Result<> imo)

Lokathor commented 1 year ago

Null is never valid for pointers at the rust level. The alloc API is already specified to use null as the failure return value, and other pointer operations are already forbidden from using null.

If you need to use a null pointer you need inline assembly