summary refs log tree commit diff
path: root/src/librustc_allocator
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2018-04-03 17:12:57 +0200
committerSimon Sapin <simon.sapin@exyr.org>2018-04-12 22:53:12 +0200
commit86753ce1cc520bfe50ae89f09ec47f313ce900eb (patch)
tree5611452336d4e8dbf10c2f21b9516d46743092f8 /src/librustc_allocator
parenteb69593f73be1e41d9e2ef065010a47478c14924 (diff)
downloadrust-86753ce1cc520bfe50ae89f09ec47f313ce900eb.tar.gz
rust-86753ce1cc520bfe50ae89f09ec47f313ce900eb.zip
Use the GlobalAlloc trait for #[global_allocator]
Diffstat (limited to 'src/librustc_allocator')
-rw-r--r--src/librustc_allocator/expand.rs283
-rw-r--r--src/librustc_allocator/lib.rs39
2 files changed, 33 insertions, 289 deletions
diff --git a/src/librustc_allocator/expand.rs b/src/librustc_allocator/expand.rs
index ee38cca7828..ce41fe1f3bc 100644
--- a/src/librustc_allocator/expand.rs
+++ b/src/librustc_allocator/expand.rs
@@ -11,7 +11,7 @@
 use rustc::middle::allocator::AllocatorKind;
 use rustc_errors;
 use syntax::abi::Abi;
-use syntax::ast::{Crate, Attribute, LitKind, StrStyle, ExprKind};
+use syntax::ast::{Crate, Attribute, LitKind, StrStyle};
 use syntax::ast::{Unsafety, Constness, Generics, Mutability, Ty, Mac, Arg};
 use syntax::ast::{self, Ident, Item, ItemKind, TyKind, VisibilityKind, Expr};
 use syntax::attr;
@@ -88,7 +88,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
             span,
             kind: AllocatorKind::Global,
             global: item.ident,
-            alloc: Ident::from_str("alloc"),
+            core: Ident::from_str("core"),
             cx: ExtCtxt::new(self.sess, ecfg, self.resolver),
         };
         let super_path = f.cx.path(f.span, vec![
@@ -96,7 +96,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
             f.global,
         ]);
         let mut items = vec![
-            f.cx.item_extern_crate(f.span, f.alloc),
+            f.cx.item_extern_crate(f.span, f.core),
             f.cx.item_use_simple(
                 f.span,
                 respan(f.span.shrink_to_lo(), VisibilityKind::Inherited),
@@ -126,7 +126,7 @@ struct AllocFnFactory<'a> {
     span: Span,
     kind: AllocatorKind,
     global: Ident,
-    alloc: Ident,
+    core: Ident,
     cx: ExtCtxt<'a>,
 }
 
@@ -143,8 +143,7 @@ impl<'a> AllocFnFactory<'a> {
             self.arg_ty(ty, &mut abi_args, mk)
         }).collect();
         let result = self.call_allocator(method.name, args);
-        let (output_ty, output_expr) =
-            self.ret_ty(&method.output, &mut abi_args, mk, result);
+        let (output_ty, output_expr) = self.ret_ty(&method.output, result);
         let kind = ItemKind::Fn(self.cx.fn_decl(abi_args, ast::FunctionRetTy::Ty(output_ty)),
                                 Unsafety::Unsafe,
                                 dummy_spanned(Constness::NotConst),
@@ -159,16 +158,15 @@ impl<'a> AllocFnFactory<'a> {
 
     fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> {
         let method = self.cx.path(self.span, vec![
-            self.alloc,
-            Ident::from_str("heap"),
-            Ident::from_str("Alloc"),
+            self.core,
+            Ident::from_str("alloc"),
+            Ident::from_str("GlobalAlloc"),
             Ident::from_str(method),
         ]);
         let method = self.cx.expr_path(method);
         let allocator = self.cx.path_ident(self.span, self.global);
         let allocator = self.cx.expr_path(allocator);
         let allocator = self.cx.expr_addr_of(self.span, allocator);
-        let allocator = self.cx.expr_mut_addr_of(self.span, allocator);
         args.insert(0, allocator);
 
         self.cx.expr_call(self.span, method, args)
@@ -205,8 +203,8 @@ impl<'a> AllocFnFactory<'a> {
                 args.push(self.cx.arg(self.span, align, ty_usize));
 
                 let layout_new = self.cx.path(self.span, vec![
-                    self.alloc,
-                    Ident::from_str("heap"),
+                    self.core,
+                    Ident::from_str("alloc"),
                     Ident::from_str("Layout"),
                     Ident::from_str("from_size_align_unchecked"),
                 ]);
@@ -219,286 +217,67 @@ impl<'a> AllocFnFactory<'a> {
                 layout
             }
 
-            AllocatorTy::LayoutRef => {
-                let ident = ident();
-                args.push(self.cx.arg(self.span, ident, self.ptr_u8()));
-
-                // Convert our `arg: *const u8` via:
-                //
-                //      &*(arg as *const Layout)
-                let expr = self.cx.expr_ident(self.span, ident);
-                let expr = self.cx.expr_cast(self.span, expr, self.layout_ptr());
-                let expr = self.cx.expr_deref(self.span, expr);
-                self.cx.expr_addr_of(self.span, expr)
-            }
-
-            AllocatorTy::AllocErr => {
-                // We're creating:
-                //
-                //      (*(arg as *const AllocErr)).clone()
+            AllocatorTy::Ptr => {
                 let ident = ident();
                 args.push(self.cx.arg(self.span, ident, self.ptr_u8()));
-                let expr = self.cx.expr_ident(self.span, ident);
-                let expr = self.cx.expr_cast(self.span, expr, self.alloc_err_ptr());
-                let expr = self.cx.expr_deref(self.span, expr);
-                self.cx.expr_method_call(
-                    self.span,
-                    expr,
-                    Ident::from_str("clone"),
-                    Vec::new()
-                )
+                let arg = self.cx.expr_ident(self.span, ident);
+                self.cx.expr_cast(self.span, arg, self.ptr_void())
             }
 
-            AllocatorTy::Ptr => {
+            AllocatorTy::Usize => {
                 let ident = ident();
-                args.push(self.cx.arg(self.span, ident, self.ptr_u8()));
+                args.push(self.cx.arg(self.span, ident, self.usize()));
                 self.cx.expr_ident(self.span, ident)
             }
 
             AllocatorTy::ResultPtr |
-            AllocatorTy::ResultExcess |
-            AllocatorTy::ResultUnit |
-            AllocatorTy::Bang |
-            AllocatorTy::UsizePair |
             AllocatorTy::Unit => {
                 panic!("can't convert AllocatorTy to an argument")
             }
         }
     }
 
-    fn ret_ty(&self,
-              ty: &AllocatorTy,
-              args: &mut Vec<Arg>,
-              ident: &mut FnMut() -> Ident,
-              expr: P<Expr>) -> (P<Ty>, P<Expr>)
-    {
+    fn ret_ty(&self, ty: &AllocatorTy, expr: P<Expr>) -> (P<Ty>, P<Expr>) {
         match *ty {
-            AllocatorTy::UsizePair => {
-                // We're creating:
-                //
-                //      let arg = #expr;
-                //      *min = arg.0;
-                //      *max = arg.1;
-
-                let min = ident();
-                let max = ident();
-
-                args.push(self.cx.arg(self.span, min, self.ptr_usize()));
-                args.push(self.cx.arg(self.span, max, self.ptr_usize()));
-
-                let ident = ident();
-                let stmt = self.cx.stmt_let(self.span, false, ident, expr);
-                let min = self.cx.expr_ident(self.span, min);
-                let max = self.cx.expr_ident(self.span, max);
-                let layout = self.cx.expr_ident(self.span, ident);
-                let assign_min = self.cx.expr(self.span, ExprKind::Assign(
-                    self.cx.expr_deref(self.span, min),
-                    self.cx.expr_tup_field_access(self.span, layout.clone(), 0),
-                ));
-                let assign_min = self.cx.stmt_semi(assign_min);
-                let assign_max = self.cx.expr(self.span, ExprKind::Assign(
-                    self.cx.expr_deref(self.span, max),
-                    self.cx.expr_tup_field_access(self.span, layout.clone(), 1),
-                ));
-                let assign_max = self.cx.stmt_semi(assign_max);
-
-                let stmts = vec![stmt, assign_min, assign_max];
-                let block = self.cx.block(self.span, stmts);
-                let ty_unit = self.cx.ty(self.span, TyKind::Tup(Vec::new()));
-                (ty_unit, self.cx.expr_block(block))
-            }
-
-            AllocatorTy::ResultExcess => {
-                // We're creating:
-                //
-                //      match #expr {
-                //          Ok(ptr) => {
-                //              *excess = ptr.1;
-                //              ptr.0
-                //          }
-                //          Err(e) => {
-                //              ptr::write(err_ptr, e);
-                //              0 as *mut u8
-                //          }
-                //      }
-
-                let excess_ptr = ident();
-                args.push(self.cx.arg(self.span, excess_ptr, self.ptr_usize()));
-                let excess_ptr = self.cx.expr_ident(self.span, excess_ptr);
-
-                let err_ptr = ident();
-                args.push(self.cx.arg(self.span, err_ptr, self.ptr_u8()));
-                let err_ptr = self.cx.expr_ident(self.span, err_ptr);
-                let err_ptr = self.cx.expr_cast(self.span,
-                                                err_ptr,
-                                                self.alloc_err_ptr());
-
-                let name = ident();
-                let ok_expr = {
-                    let ptr = self.cx.expr_ident(self.span, name);
-                    let write = self.cx.expr(self.span, ExprKind::Assign(
-                        self.cx.expr_deref(self.span, excess_ptr),
-                        self.cx.expr_tup_field_access(self.span, ptr.clone(), 1),
-                    ));
-                    let write = self.cx.stmt_semi(write);
-                    let ret = self.cx.expr_tup_field_access(self.span,
-                                                            ptr.clone(),
-                                                            0);
-                    let ret = self.cx.stmt_expr(ret);
-                    let block = self.cx.block(self.span, vec![write, ret]);
-                    self.cx.expr_block(block)
-                };
-                let pat = self.cx.pat_ident(self.span, name);
-                let ok = self.cx.path_ident(self.span, Ident::from_str("Ok"));
-                let ok = self.cx.pat_tuple_struct(self.span, ok, vec![pat]);
-                let ok = self.cx.arm(self.span, vec![ok], ok_expr);
-
-                let name = ident();
-                let err_expr = {
-                    let err = self.cx.expr_ident(self.span, name);
-                    let write = self.cx.path(self.span, vec![
-                        self.alloc,
-                        Ident::from_str("heap"),
-                        Ident::from_str("__core"),
-                        Ident::from_str("ptr"),
-                        Ident::from_str("write"),
-                    ]);
-                    let write = self.cx.expr_path(write);
-                    let write = self.cx.expr_call(self.span, write,
-                                                  vec![err_ptr, err]);
-                    let write = self.cx.stmt_semi(write);
-                    let null = self.cx.expr_usize(self.span, 0);
-                    let null = self.cx.expr_cast(self.span, null, self.ptr_u8());
-                    let null = self.cx.stmt_expr(null);
-                    let block = self.cx.block(self.span, vec![write, null]);
-                    self.cx.expr_block(block)
-                };
-                let pat = self.cx.pat_ident(self.span, name);
-                let err = self.cx.path_ident(self.span, Ident::from_str("Err"));
-                let err = self.cx.pat_tuple_struct(self.span, err, vec![pat]);
-                let err = self.cx.arm(self.span, vec![err], err_expr);
-
-                let expr = self.cx.expr_match(self.span, expr, vec![ok, err]);
-                (self.ptr_u8(), expr)
-            }
-
             AllocatorTy::ResultPtr => {
                 // We're creating:
                 //
-                //      match #expr {
-                //          Ok(ptr) => ptr,
-                //          Err(e) => {
-                //              ptr::write(err_ptr, e);
-                //              0 as *mut u8
-                //          }
-                //      }
-
-                let err_ptr = ident();
-                args.push(self.cx.arg(self.span, err_ptr, self.ptr_u8()));
-                let err_ptr = self.cx.expr_ident(self.span, err_ptr);
-                let err_ptr = self.cx.expr_cast(self.span,
-                                                err_ptr,
-                                                self.alloc_err_ptr());
+                //      #expr as *mut u8
 
-                let name = ident();
-                let ok_expr = self.cx.expr_ident(self.span, name);
-                let pat = self.cx.pat_ident(self.span, name);
-                let ok = self.cx.path_ident(self.span, Ident::from_str("Ok"));
-                let ok = self.cx.pat_tuple_struct(self.span, ok, vec![pat]);
-                let ok = self.cx.arm(self.span, vec![ok], ok_expr);
-
-                let name = ident();
-                let err_expr = {
-                    let err = self.cx.expr_ident(self.span, name);
-                    let write = self.cx.path(self.span, vec![
-                        self.alloc,
-                        Ident::from_str("heap"),
-                        Ident::from_str("__core"),
-                        Ident::from_str("ptr"),
-                        Ident::from_str("write"),
-                    ]);
-                    let write = self.cx.expr_path(write);
-                    let write = self.cx.expr_call(self.span, write,
-                                                  vec![err_ptr, err]);
-                    let write = self.cx.stmt_semi(write);
-                    let null = self.cx.expr_usize(self.span, 0);
-                    let null = self.cx.expr_cast(self.span, null, self.ptr_u8());
-                    let null = self.cx.stmt_expr(null);
-                    let block = self.cx.block(self.span, vec![write, null]);
-                    self.cx.expr_block(block)
-                };
-                let pat = self.cx.pat_ident(self.span, name);
-                let err = self.cx.path_ident(self.span, Ident::from_str("Err"));
-                let err = self.cx.pat_tuple_struct(self.span, err, vec![pat]);
-                let err = self.cx.arm(self.span, vec![err], err_expr);
-
-                let expr = self.cx.expr_match(self.span, expr, vec![ok, err]);
+                let expr = self.cx.expr_cast(self.span, expr, self.ptr_u8());
                 (self.ptr_u8(), expr)
             }
 
-            AllocatorTy::ResultUnit => {
-                // We're creating:
-                //
-                //      #expr.is_ok() as u8
-
-                let cast = self.cx.expr_method_call(
-                    self.span,
-                    expr,
-                    Ident::from_str("is_ok"),
-                    Vec::new()
-                );
-                let u8 = self.cx.path_ident(self.span, Ident::from_str("u8"));
-                let u8 = self.cx.ty_path(u8);
-                let cast = self.cx.expr_cast(self.span, cast, u8.clone());
-                (u8, cast)
-            }
-
-            AllocatorTy::Bang => {
-                (self.cx.ty(self.span, TyKind::Never), expr)
-            }
-
             AllocatorTy::Unit => {
                 (self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr)
             }
 
-            AllocatorTy::AllocErr |
             AllocatorTy::Layout |
-            AllocatorTy::LayoutRef |
+            AllocatorTy::Usize |
             AllocatorTy::Ptr => {
                 panic!("can't convert AllocatorTy to an output")
             }
         }
     }
 
+    fn usize(&self) -> P<Ty> {
+        let usize = self.cx.path_ident(self.span, Ident::from_str("usize"));
+        self.cx.ty_path(usize)
+    }
+
     fn ptr_u8(&self) -> P<Ty> {
         let u8 = self.cx.path_ident(self.span, Ident::from_str("u8"));
         let ty_u8 = self.cx.ty_path(u8);
         self.cx.ty_ptr(self.span, ty_u8, Mutability::Mutable)
     }
 
-    fn ptr_usize(&self) -> P<Ty> {
-        let usize = self.cx.path_ident(self.span, Ident::from_str("usize"));
-        let ty_usize = self.cx.ty_path(usize);
-        self.cx.ty_ptr(self.span, ty_usize, Mutability::Mutable)
-    }
-
-    fn layout_ptr(&self) -> P<Ty> {
-        let layout = self.cx.path(self.span, vec![
-            self.alloc,
-            Ident::from_str("heap"),
-            Ident::from_str("Layout"),
-        ]);
-        let layout = self.cx.ty_path(layout);
-        self.cx.ty_ptr(self.span, layout, Mutability::Mutable)
-    }
-
-    fn alloc_err_ptr(&self) -> P<Ty> {
-        let err = self.cx.path(self.span, vec![
-            self.alloc,
-            Ident::from_str("heap"),
-            Ident::from_str("AllocErr"),
+    fn ptr_void(&self) -> P<Ty> {
+        let void = self.cx.path(self.span, vec![
+            self.core,
+            Ident::from_str("alloc"),
+            Ident::from_str("Void"),
         ]);
-        let err = self.cx.ty_path(err);
-        self.cx.ty_ptr(self.span, err, Mutability::Mutable)
+        let ty_void = self.cx.ty_path(void);
+        self.cx.ty_ptr(self.span, ty_void, Mutability::Mutable)
     }
 }
diff --git a/src/librustc_allocator/lib.rs b/src/librustc_allocator/lib.rs
index 0c7a9a91711..969086815de 100644
--- a/src/librustc_allocator/lib.rs
+++ b/src/librustc_allocator/lib.rs
@@ -24,23 +24,13 @@ pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
         output: AllocatorTy::ResultPtr,
     },
     AllocatorMethod {
-        name: "oom",
-        inputs: &[AllocatorTy::AllocErr],
-        output: AllocatorTy::Bang,
-    },
-    AllocatorMethod {
         name: "dealloc",
         inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout],
         output: AllocatorTy::Unit,
     },
     AllocatorMethod {
-        name: "usable_size",
-        inputs: &[AllocatorTy::LayoutRef],
-        output: AllocatorTy::UsizePair,
-    },
-    AllocatorMethod {
         name: "realloc",
-        inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout, AllocatorTy::Layout],
+        inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout, AllocatorTy::Usize],
         output: AllocatorTy::ResultPtr,
     },
     AllocatorMethod {
@@ -48,26 +38,6 @@ pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
         inputs: &[AllocatorTy::Layout],
         output: AllocatorTy::ResultPtr,
     },
-    AllocatorMethod {
-        name: "alloc_excess",
-        inputs: &[AllocatorTy::Layout],
-        output: AllocatorTy::ResultExcess,
-    },
-    AllocatorMethod {
-        name: "realloc_excess",
-        inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout, AllocatorTy::Layout],
-        output: AllocatorTy::ResultExcess,
-    },
-    AllocatorMethod {
-        name: "grow_in_place",
-        inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout, AllocatorTy::Layout],
-        output: AllocatorTy::ResultUnit,
-    },
-    AllocatorMethod {
-        name: "shrink_in_place",
-        inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout, AllocatorTy::Layout],
-        output: AllocatorTy::ResultUnit,
-    },
 ];
 
 pub struct AllocatorMethod {
@@ -77,14 +47,9 @@ pub struct AllocatorMethod {
 }
 
 pub enum AllocatorTy {
-    AllocErr,
-    Bang,
     Layout,
-    LayoutRef,
     Ptr,
-    ResultExcess,
     ResultPtr,
-    ResultUnit,
     Unit,
-    UsizePair,
+    Usize,
 }