Skip to content

Commit

Permalink
Moved heap code to ledger_secure_sdk_sys
Browse files Browse the repository at this point in the history
  • Loading branch information
kingofpayne authored and yogh333 committed Jun 4, 2024
1 parent 914538a commit ecc50cc
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 46 deletions.
7 changes: 5 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions ledger_device_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,12 @@ num-traits = { version = "0.2.14", default_features = false }
rand_core = { version = "0.6.3", default_features = false }
zeroize = { version = "1.6.0", default_features = false }
numtoa = "0.2.4"
<<<<<<< HEAD
const-zero = "0.1.1"
=======
embedded-alloc = { version = "0.5.1", optional = true }
critical-section = { version = "1.1.2", optional = true }
>>>>>>> e69708d (Heap and global allocator)

[features]
speculos = []
pre1_54 = []
lib_bagl = []
nbgl = []
ccid = []
heap = ["dep:embedded-alloc", "dep:critical-section"]
heap = ["ledger_secure_sdk_sys/heap"]
38 changes: 0 additions & 38 deletions ledger_device_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub mod ui;

pub mod uxapp;

use core::mem::MaybeUninit;
use core::panic::PanicInfo;

/// In case of runtime problems, return an internal error and exit the app
Expand Down Expand Up @@ -57,43 +56,6 @@ macro_rules! set_panic {
};
}

#[cfg(feature = "heap")]
use critical_section::RawRestoreState;
#[cfg(feature = "heap")]
use embedded_alloc::Heap;

#[cfg(feature = "heap")]
#[global_allocator]
static HEAP: Heap = Heap::empty();

#[cfg(feature = "heap")]
struct CriticalSection;
#[cfg(feature = "heap")]
critical_section::set_impl!(CriticalSection);

/// Default empty implementation as we don't have concurrency.
#[cfg(feature = "heap")]
unsafe impl critical_section::Impl for CriticalSection {
unsafe fn acquire() -> RawRestoreState {}
unsafe fn release(restore_state: RawRestoreState) {}
}

/// Initializes the heap memory for the global allocator.
///
/// The heap is stored in the stack, and has a fixed size.
/// This method is called just before [sample_main].
#[no_mangle]
#[cfg(feature = "heap")]
extern "C" fn heap_init() {
const HEAP_SIZE: usize = 8192;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
}

#[no_mangle]
#[cfg(not(feature = "heap"))]
extern "C" fn heap_init() {}

extern "C" {
fn c_main();
}
Expand Down
7 changes: 7 additions & 0 deletions ledger_secure_sdk_sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ description = "Bindings to Ledger C SDK"
bindgen = "0.65.1"
cc = "1.0.73"
glob = "0.3.1"

[dependencies]
embedded-alloc = { version = "0.5.1", optional = true }
critical-section = { version = "1.1.2", optional = true }

[features]
heap = ["dep:embedded-alloc", "dep:critical-section"]
38 changes: 38 additions & 0 deletions ledger_secure_sdk_sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(non_snake_case)]

use core::ffi::c_void;
use core::mem::MaybeUninit;

pub mod buttons;
mod infos;
Expand Down Expand Up @@ -33,4 +34,41 @@ pub fn pic_rs_mut<T>(x: &mut T) -> &mut T {
unsafe { &mut *ptr }
}

#[cfg(feature = "heap")]
use critical_section::RawRestoreState;
#[cfg(feature = "heap")]
use embedded_alloc::Heap;

#[cfg(feature = "heap")]
#[global_allocator]
static HEAP: Heap = Heap::empty();

#[cfg(feature = "heap")]
struct CriticalSection;
#[cfg(feature = "heap")]
critical_section::set_impl!(CriticalSection);

/// Default empty implementation as we don't have concurrency.
#[cfg(feature = "heap")]
unsafe impl critical_section::Impl for CriticalSection {
unsafe fn acquire() -> RawRestoreState {}
unsafe fn release(restore_state: RawRestoreState) {}
}

/// Initializes the heap memory for the global allocator.
///
/// The heap is stored in the stack, and has a fixed size.
/// This method is called just before [sample_main].
#[no_mangle]
#[cfg(feature = "heap")]
extern "C" fn heap_init() {
const HEAP_SIZE: usize = 8192;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
}

#[no_mangle]
#[cfg(not(feature = "heap"))]
extern "C" fn heap_init() {}

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

0 comments on commit ecc50cc

Please sign in to comment.