Skip to content

Commit

Permalink
RustPkg/Test: Make Alloc related updates for latest nightly
Browse files Browse the repository at this point in the history
1. Updates Alloc trait to AllocRef

  allocater_api discussion:
  rust-lang/rust#32838

  Impacting code change:
   rust-lang/rust@7ca25db

  AllocRef documentation:
  https://doc.rust-lang.org/nightly/core/alloc/trait.AllocRef.html

2. Adds AllocInit arg to alloc() calls

  Per https://doc.rust-lang.org/nightly/core/alloc/trait.AllocRef.html, a
  new core::alloc::AllocInit enum was added to the core::alloc::AllocRef
  trait. The enum allows the caller to specify whether the memory should
  be zeroed or left uninitialized.

  Code change in rust-lang that implemented the new parameter:
  rust-lang/rust@56cbf2f

  This change updates callers of alloc() to pass the second parameter,
  conforming to the latest interface and preventing a build failure.

3. Updates usage of AllocRef methods to account for recent changes

  The return value for core::alloc::AllocRef.alloc() is now a
  Result<MemoryBlock, AllocErr> whereas it was previously
  Result<(NonNull<u8>, usize), AllocErr>.

  Calling the as_ptr() method on the Ok variant is no longer
  valid. This change uses the underlying ptr field in the MemoryBlock
  to similarly get the core::ptr::NonNull<u8>.

Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
  • Loading branch information
makubacki committed Apr 23, 2020
1 parent 9ecfa56 commit df4483f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
7 changes: 4 additions & 3 deletions RustPkg/Test/TestRustLangApp2/lib.rs
@@ -1,4 +1,5 @@
// Copyright (c) 2019 Intel Corporation
// Copyright (c) Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -203,7 +204,7 @@ fn release_buffer (test_table : &mut TestTableFixed)
#[no_mangle]
#[export_name = "TestBufferDrop"]
pub extern fn test_buffer_drop (

)
{
match get_buffer () {
Expand Down Expand Up @@ -232,7 +233,7 @@ pub extern fn test_buffer_borrow (
//test_table2.r#type = 2; // error
}

use core::alloc::{GlobalAlloc, Layout, Alloc};
use core::alloc::{GlobalAlloc, Layout, AllocRef};

pub struct MyAllocator;

Expand Down Expand Up @@ -262,7 +263,7 @@ static ALLOCATOR: MyAllocator = MyAllocator;
#[no_mangle]
#[export_name = "TestBufferAlloc"]
pub extern fn test_buffer_alloc (

)
{
let layout = unsafe { core::alloc::Layout::from_size_align_unchecked(32, 4) };
Expand Down
23 changes: 12 additions & 11 deletions RustPkg/Test/TestRustLangLib/src/lib.rs
@@ -1,4 +1,5 @@
// Copyright (c) 2019 Intel Corporation
// Copyright (c) Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,7 +49,7 @@ extern crate alloc;
use alloc::vec::Vec;
use alloc::boxed::Box;
use alloc::{
alloc::{handle_alloc_error, Global, Layout},
alloc::{handle_alloc_error, AllocInit, AllocRef, Global, Layout},
};


Expand Down Expand Up @@ -206,7 +207,7 @@ fn release_buffer (test_table : &mut TestTableFixed)
#[no_mangle]
#[export_name = "TestBufferDrop"]
pub extern fn test_buffer_drop (

)
{
match get_buffer () {
Expand Down Expand Up @@ -238,16 +239,16 @@ pub extern fn test_buffer_borrow (
#[no_mangle]
#[export_name = "TestBufferAlloc"]
pub extern fn test_buffer_alloc (

)
{
let layout = unsafe { core::alloc::Layout::from_size_align_unchecked(32, 4) };
unsafe {
match Global.alloc (layout) {
match Global.alloc (layout, AllocInit::Zeroed) {
Ok(buffer) => {
let mut box_buffer = Box::from_raw(from_raw_parts_mut(buffer.as_ptr(), layout.size()));
let mut box_buffer = Box::from_raw(from_raw_parts_mut(buffer.ptr.as_ptr(), layout.size()));
box_buffer[0] = 1;
Global.dealloc (buffer, layout);
Global.dealloc (buffer.ptr, layout);
drop (buffer); // It is useless
box_buffer[0] = 1; // cannot catch
},
Expand All @@ -257,9 +258,9 @@ pub extern fn test_buffer_alloc (

let layout = core::alloc::Layout::new::<u32>();
unsafe {
match Global.alloc (layout) {
match Global.alloc (layout, AllocInit::Zeroed) {
Ok(buffer) => {
Global.dealloc (buffer, layout);
Global.dealloc (buffer.ptr, layout);
},
Err(_) => handle_alloc_error (layout),
}
Expand Down Expand Up @@ -324,10 +325,10 @@ pub extern fn test_box_convert (
{
let layout = unsafe { core::alloc::Layout::from_size_align_unchecked(size, 4) };
unsafe {
match Global.alloc (layout) {
match Global.alloc (layout, AllocInit::Zeroed) {
Ok(buffer) => {
let mut box_buffer = Box::<u8>::from_raw(from_raw_parts_mut(buffer.as_ptr(), layout.size()) as *mut [u8] as *mut u8 );
Global.dealloc (buffer, layout);
let mut box_buffer = Box::<u8>::from_raw(from_raw_parts_mut(buffer.ptr.as_ptr(), layout.size()) as *mut [u8] as *mut u8 );
Global.dealloc (buffer.ptr, layout);
*box_buffer = 1;
Box::<u8>::into_raw(box_buffer)
},
Expand Down

0 comments on commit df4483f

Please sign in to comment.