From 23e4f0817113450fea5439b1e4c7f69ac7445fe8 Mon Sep 17 00:00:00 2001 From: Alexis Beingessner Date: Tue, 1 May 2018 23:00:59 -0400 Subject: [PATCH] Global.oom -> heap::alloc::oom and explanation fixup --- src/vec-alloc.md | 20 +++++++------------- src/vec-final.md | 11 ++--------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/src/vec-alloc.md b/src/vec-alloc.md index 349cb50..a6f88aa 100644 --- a/src/vec-alloc.md +++ b/src/vec-alloc.md @@ -37,10 +37,11 @@ that, we'll need to use the rest of the heap APIs. These basically allow us to talk directly to Rust's allocator (jemalloc by default). We'll also need a way to handle out-of-memory (OOM) conditions. The standard -library calls the `abort` intrinsic, which just calls an illegal instruction to -crash the whole program. The reason we abort and don't panic is because -unwinding can cause allocations to happen, and that seems like a bad thing to do -when your allocator just came back with "hey I don't have any more memory". +library calls `std::alloc::oom()`, which in turn calls the the `oom` langitem. +By default this just aborts the program by executing an illegal cpu instruction. +The reason we abort and don't panic is because unwinding can cause allocations +to happen, and that seems like a bad thing to do when your allocator just came +back with "hey I don't have any more memory". Of course, this is a bit silly since most platforms don't actually run out of memory in a conventional way. Your operating system will probably kill the @@ -51,15 +52,6 @@ of memory at once (e.g. half the theoretical address space). As such it's like the standard library as much as possible, so we'll just kill the whole program. -We said we don't want to use intrinsics, so doing exactly what `std` does is -out. Instead, we'll call `std::process::exit` with some random number. - -```rust -fn oom() { - ::std::process::exit(-9999); -} -``` - Okay, now we can write growing. Roughly, we want to have this logic: ```text @@ -165,6 +157,8 @@ such we will guard against this case explicitly. Ok with all the nonsense out of the way, let's actually allocate some memory: ```rust,ignore +use std::alloc::oom; + fn grow(&mut self) { // this is all pretty delicate, so let's say it's all unsafe unsafe { diff --git a/src/vec-final.md b/src/vec-final.md index 110d5e2..199528a 100644 --- a/src/vec-final.md +++ b/src/vec-final.md @@ -9,7 +9,7 @@ use std::ptr::{Unique, self}; use std::mem; use std::ops::{Deref, DerefMut}; use std::marker::PhantomData; -use std::alloc::{GlobalAlloc, Layout, Global}; +use std::alloc::{GlobalAlloc, Layout, Global, oom}; struct RawVec { ptr: Unique, @@ -46,7 +46,7 @@ impl RawVec { // If allocate or reallocate fail, oom if ptr.is_null() { - Global.oom() + oom() } self.ptr = Unique::new_unchecked(ptr as *mut _); @@ -299,12 +299,5 @@ impl<'a, T> Drop for Drain<'a, T> { } } -/// Abort the process, we're out of memory! -/// -/// In practice this is probably dead code on most OSes -fn oom() { - ::std::process::exit(-9999); -} - # fn main() {} ```