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 committed May 29, 2024
1 parent e69708d commit 97d6168
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 45 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

6 changes: 2 additions & 4 deletions ledger_device_sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ledger_device_sdk"
version = "1.9.0"
version = "1.8.2"
authors = ["yhql", "yogh333"]
edition = "2021"
license.workspace = true
Expand All @@ -21,12 +21,10 @@ 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"
embedded-alloc = { version = "0.5.1", optional = true }
critical-section = { version = "1.1.2", optional = true }

[features]
speculos = []
pre1_54 = []
lib_bagl = []
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 @@ -25,7 +25,6 @@ pub mod testing;
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 @@ -53,43 +52,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
5 changes: 5 additions & 0 deletions ledger_secure_sdk_sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ description = "Bindings to Ledger C SDK"
bindgen = "0.65.1"
cc = "1.0.73"

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

[features]
lib_bagl = []
ccid = []
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 97d6168

Please sign in to comment.