{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rust/issues/49668","repository_url":"https://api.github.com/repos/rust-lang/rust","labels_url":"https://api.github.com/repos/rust-lang/rust/issues/49668/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/49668/comments","events_url":"https://api.github.com/repos/rust-lang/rust/issues/49668/events","html_url":"https://github.com/rust-lang/rust/issues/49668","id":311400578,"number":49668,"title":"Tracking issue for the GlobalAlloc trait and related APIs","user":{"login":"SimonSapin","id":291359,"avatar_url":"https://avatars0.githubusercontent.com/u/291359?v=4","gravatar_id":"","url":"https://api.github.com/users/SimonSapin","html_url":"https://github.com/SimonSapin","followers_url":"https://api.github.com/users/SimonSapin/followers","following_url":"https://api.github.com/users/SimonSapin/following{/other_user}","gists_url":"https://api.github.com/users/SimonSapin/gists{/gist_id}","starred_url":"https://api.github.com/users/SimonSapin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SimonSapin/subscriptions","organizations_url":"https://api.github.com/users/SimonSapin/orgs","repos_url":"https://api.github.com/users/SimonSapin/repos","events_url":"https://api.github.com/users/SimonSapin/events{/privacy}","received_events_url":"https://api.github.com/users/SimonSapin/received_events","type":"User","site_admin":false},"labels":[{"id":129836139,"url":"https://api.github.com/repos/rust-lang/rust/labels/A-allocators","name":"A-allocators","color":"d7e102","default":false},{"id":246778137,"url":"https://api.github.com/repos/rust-lang/rust/labels/B-unstable","name":"B-unstable","color":"d304cb","default":false},{"id":650846969,"url":"https://api.github.com/repos/rust-lang/rust/labels/C-tracking-issue","name":"C-tracking-issue","color":"f5f1fd","default":false},{"id":211668062,"url":"https://api.github.com/repos/rust-lang/rust/labels/T-libs","name":"T-libs","color":"bfd4f2","default":false},{"id":638321554,"url":"https://api.github.com/repos/rust-lang/rust/labels/proposed-final-comment-period","name":"proposed-final-comment-period","color":"ededed","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":49,"created_at":"2018-04-04T21:39:17Z","updated_at":"2018-04-20T17:09:25Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"PR https://github.com/rust-lang/rust/pull/49669 adds a `GlobalAlloc` trait, separate from `Alloc`. This issue track the stabilization of this trait and some related APIs, to provide the ability to [change the global allocator](https://github.com/rust-lang/rust/issues/27389), as well as allocating memory without abusing `Vec::with_capacity` + `mem::forget`.\r\n\r\nDefined in or reexported from the `std::alloc` module:\r\n\r\n```rust\r\n/// #[global_allocator] can only be applied to a `static` item that implements this trait\r\npub unsafe trait GlobalAlloc {\r\n    unsafe fn alloc(&self, layout: Layout) -> *mut Opaque;\r\n    unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout);\r\n\r\n    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut Opaque { \r\n        // Default impl: self.alloc() and ptr::write_bytes()\r\n    }\r\n    unsafe fn realloc(&self, ptr: *mut Opaque, layout: Layout, new_size: usize) -> *mut Opaque {\r\n        // Default impl: self.alloc() and ptr::copy_nonoverlapping() and self.dealloc()\r\n    }\r\n\r\n    fn oom(&self) -> ! {\r\n        // intrinsics::abort()\r\n    }\r\n    \r\n    // More methods with default impls may be added in the future\r\n}\r\n\r\nextern {\r\n    pub type Opaque;\r\n}\r\n\r\n#[derive(Debug, Copy, Clone, PartialEq, Eq)]\r\npub struct Layout { /* private */ }\r\n\r\nimpl Layout {\r\n    pub fn from_size_align(size: usize: align: usize) -> Result<Self, LayoutError> {…}\r\n    pub unsafe fn from_size_align_unchecked(size: usize: align: usize) -> Self {…}\r\n    pub fn size(&self) -> usize {…}\r\n    pub fn align(&self) -> usize {…}\r\n    pub fn new<T>() -> Self {…}\r\n    pub fn for_value<T: ?Sized>(t: &T) -> Self {…}\r\n}\r\n\r\n#[derive(Copy, Clone, Debug)]\r\npub struct LayoutError { /* private */ }\r\n\r\n/// Forwards method calls to the `static` item registered\r\n/// with `#[global_allocator]` if any, or the operating system’s default.\r\npub struct Global;\r\n\r\n/// The operating system’s default allocator.\r\npub struct System;\r\n\r\nimpl GlobalAlloc for Global {…}\r\nimpl GlobalAlloc for System {…}\r\n```\r\n\r\nCC @rust-lang/libs, @glandium\r\n\r\n## Unresolved questions or otherwise blocking\r\n\r\n* [ ] Wait for extern types https://github.com/rust-lang/rust/issues/43467 to be stable in the language before stabilazing one in the standard library.\r\n* [ ] Name of the `Global` type. `GlobalAllocator`?\r\n* [x] ~~Name of the `Void` type. Lower-case `void` would match C (our `*mut void` is very close to C’s `void*` type), but violates the convension of camel-case for non-primitive types. But `Void` in camel-case is already the name of [an empty enum](https://docs.rs/void/1.0.2/void/enum.Void.html) (not an extern type like here) in a [popular](https://crates.io/crates/void#crate-downloads) crate. (An extern type is desirable so that `<*mut _>::offset` cannot be called without first casting to another pointer type.) Maybe call it `Opaque`? `Unknown`?~~ Renamed to `Opaque`.\r\n  * [ ] Rename again to something else?\r\n* [x] ~Taking `Layout` by reference, or making it `Copy` https://github.com/rust-lang/rust/issues/48458.~ `Copy`: https://github.com/rust-lang/rust/pull/50109\r\n* [x] ~`GlobalAlloc::owns`: https://github.com/rust-lang/rust/issues/44302 proposes making it required for allocators to be able to tell if it “owns” a given pointer.~ Not to be required by this trait since glibc and Windows do not support this.\r\n* [x] ~Settle semantics of layout \"fit\" and other safety conditions.~ Without an `usable_size` method (for now), the layout passed to dealloc must be the same as was passed to alloc. (Same as with `Alloc::usable_size`’s default impl returning `(layout.size(), layout.size())`.)\r\n* [x] ~An `oom` method is part of `GlobalAlloc` so that implementation-specific printing to stderr does not need to be part of liballoc and instead goes through `#[global_allocator]`, but this does not really belong in the `GlobalAlloc` trait. Should another mechanism like `#[global_allocator]` be added to have pluggable OOM handling?~ Keeping this method in this trait maybe isn’t so bad, and the implementation of `#[global_allocator]` is a bit involved: replicating it for another attribute isn’t trivial.\r\n\r\n## Not proposed (yet) for stabilization\r\n\r\n* The `Alloc` trait\r\n* The rest of `Layout` methods\r\n* The `AllocErr` type\r\n\r\nWe’re not ready to settle on a design for collections generic over the allocation type. Discussion of this use case should continue on the tracking issue for the `Alloc` trait: https://github.com/rust-lang/rust/issues/32838."},"comment":{"url":"https://api.github.com/repos/rust-lang/rust/issues/comments/383161519","html_url":"https://github.com/rust-lang/rust/issues/49668#issuecomment-383161519","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/49668","id":383161519,"user":{"login":"RalfJung","id":330628,"avatar_url":"https://avatars1.githubusercontent.com/u/330628?v=4","gravatar_id":"","url":"https://api.github.com/users/RalfJung","html_url":"https://github.com/RalfJung","followers_url":"https://api.github.com/users/RalfJung/followers","following_url":"https://api.github.com/users/RalfJung/following{/other_user}","gists_url":"https://api.github.com/users/RalfJung/gists{/gist_id}","starred_url":"https://api.github.com/users/RalfJung/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RalfJung/subscriptions","organizations_url":"https://api.github.com/users/RalfJung/orgs","repos_url":"https://api.github.com/users/RalfJung/repos","events_url":"https://api.github.com/users/RalfJung/events{/privacy}","received_events_url":"https://api.github.com/users/RalfJung/received_events","type":"User","site_admin":false},"created_at":"2018-04-20T17:09:25Z","updated_at":"2018-04-20T17:09:25Z","author_association":"CONTRIBUTOR","body":"OOM and the global allocator seem to be two examples of \"compiler-endorsed singletons\" (\"stable lang-items\"?) or whatever one may want to call them; I think I've seen this brought up elsewhere where people wanted to use a similar mechanism for something else. Maybe it makes more sense to make them both instances of the same mechanism, rather than bolting them together in one trait?"}}
{"actor":{"display_login":"RalfJung"}}