Skip to content

Commit

Permalink
Check strings have nul-terminators at compile time.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpallant committed Nov 28, 2023
1 parent 5a6e0ed commit e4877d1
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions demo-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@ const DEMO_STACK_SIZE: usize = 1024;

#[no_mangle]
extern "C" fn tx_application_define(_first_unused_memory: *mut core::ffi::c_void) {
static mut THREAD_0: MaybeUninit<threadx_sys::TX_THREAD> = MaybeUninit::uninit();
static mut THREAD_1: MaybeUninit<threadx_sys::TX_THREAD> = MaybeUninit::uninit();
static mut THREAD0: MaybeUninit<threadx_sys::TX_THREAD> = MaybeUninit::uninit();
const THREAD0_NAME: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"thread0\0") };
static mut THREAD1: MaybeUninit<threadx_sys::TX_THREAD> = MaybeUninit::uninit();
const THREAD1_NAME: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"thread1\0") };
static mut BYTE_POOL: MaybeUninit<threadx_sys::TX_BYTE_POOL> = MaybeUninit::uninit();
const BYTE_POOL_NAME: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"byte-pool0\0") };
static mut BYTE_POOL_STORAGE: MaybeUninit<[u8; 32768]> = MaybeUninit::uninit();

defmt::println!("In tx_application_define()...");
// ThreadX requires a non-const pointer to char for the names, which it
// wil hold on to in the object, so it must have static lifetime. So we
// cast-away-const on a static string slice to appease the API.
unsafe {
let pool_name = CStr::from_bytes_with_nul(b"byte-pool0\0").unwrap();
threadx_sys::_tx_byte_pool_create(
BYTE_POOL.as_mut_ptr(),
pool_name.as_ptr() as *mut threadx_sys::CHAR,
BYTE_POOL_NAME.as_ptr() as *mut threadx_sys::CHAR,
BYTE_POOL_STORAGE.as_mut_ptr() as *mut _,
core::mem::size_of_val(&BYTE_POOL_STORAGE) as u32,
);
Expand All @@ -44,10 +46,9 @@ extern "C" fn tx_application_define(_first_unused_memory: *mut core::ffi::c_void
threadx_sys::TX_NO_WAIT,
);
defmt::println!("Stack allocated @ {:08x}", pointer);
let thread_name = CStr::from_bytes_with_nul(b"thread0\0").unwrap();
threadx_sys::_tx_thread_create(
THREAD_0.as_mut_ptr(),
thread_name.as_ptr() as *mut threadx_sys::CHAR,
THREAD0.as_mut_ptr(),
THREAD0_NAME.as_ptr() as *mut threadx_sys::CHAR,
Some(my_thread),
0x12345678,
pointer,
Expand All @@ -67,10 +68,9 @@ extern "C" fn tx_application_define(_first_unused_memory: *mut core::ffi::c_void
threadx_sys::TX_NO_WAIT,
);
defmt::println!("Stack allocated @ {:08x}", pointer);
let thread_name = CStr::from_bytes_with_nul(b"thread1\0").unwrap();
threadx_sys::_tx_thread_create(
THREAD_1.as_mut_ptr(),
thread_name.as_ptr() as *mut threadx_sys::CHAR,
THREAD1.as_mut_ptr(),
THREAD1_NAME.as_ptr() as *mut threadx_sys::CHAR,
Some(my_thread),
0xAABBCCDD,
pointer,
Expand Down

0 comments on commit e4877d1

Please sign in to comment.