Browse Source

Cargo fmt

pull/81/head
Andrea Ciliberti 2 years ago
parent
commit
be858ff6ae
  1. 9
      ctru-rs/examples/linear-memory.rs
  2. 12
      ctru-rs/src/linear.rs

9
ctru-rs/examples/linear-memory.rs

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
#![feature(allocator_api)]
use ctru::prelude::*;
use ctru::linear::LinearAllocator;
use ctru::prelude::*;
fn main() {
ctru::init();
@ -15,13 +15,16 @@ fn main() { @@ -15,13 +15,16 @@ fn main() {
// Normal `Box` on the heap
let heap_box = Box::new(1492);
// `Box` living on the linear memory sector
let linear_box: Box::<i32, LinearAllocator> = Box::new_in(2022, LinearAllocator);
let linear_box: Box<i32, LinearAllocator> = Box::new_in(2022, LinearAllocator);
println!("This value is from the heap: {heap_box}");
println!("This value is from the LINEAR memory: {linear_box}");
println!("\nLINEAR space free before allocation: {linear_space_before}");
println!("LINEAR space free after allocation: {}", LinearAllocator::free_space());
println!(
"LINEAR space free after allocation: {}",
LinearAllocator::free_space()
);
println!("\x1b[29;16HPress Start to exit");

12
ctru-rs/src/linear.rs

@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
//!
//! Linear memory is a sector of the 3DS' RAM that binds virtual addresses exactly to the physical address.
//! As such, it is used for fast and safe memory sharing between services (and is especially needed for GPU and DSP).
//!
//!
//! Resources:<br>
//! <https://github.com/devkitPro/libctru/blob/master/libctru/source/allocator/linear.cpp><br>
//! <https://www.3dbrew.org/wiki/Memory_layout>
@ -25,10 +25,14 @@ impl LinearAllocator { @@ -25,10 +25,14 @@ impl LinearAllocator {
}
unsafe impl Allocator for LinearAllocator {
fn allocate(&self, layout: std::alloc::Layout) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> {
fn allocate(
&self,
layout: std::alloc::Layout,
) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> {
let pointer = unsafe { ctru_sys::linearAlloc(layout.size() as u32) };
let slice: &mut [u8] = unsafe { std::slice::from_raw_parts_mut(pointer as *mut u8, layout.size()) };
let slice: &mut [u8] =
unsafe { std::slice::from_raw_parts_mut(pointer as *mut u8, layout.size()) };
std::ptr::NonNull::new(slice).ok_or(std::alloc::AllocError)
}

Loading…
Cancel
Save