2023-05-02 13:16:48 +02:00
|
|
|
//! Unsafe linked list.
|
|
|
|
//! Translated from ST's C by `c2rust` tool.
|
|
|
|
|
|
|
|
#![allow(
|
|
|
|
dead_code,
|
|
|
|
mutable_transmutes,
|
|
|
|
non_camel_case_types,
|
|
|
|
non_snake_case,
|
|
|
|
non_upper_case_globals,
|
|
|
|
unused_assignments,
|
|
|
|
unused_mut
|
|
|
|
)]
|
|
|
|
|
|
|
|
use cortex_m::interrupt;
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
#[repr(C, packed(4))]
|
|
|
|
pub struct LinkedListNode {
|
|
|
|
pub next: *mut LinkedListNode,
|
|
|
|
pub prev: *mut LinkedListNode,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for LinkedListNode {
|
|
|
|
fn default() -> Self {
|
|
|
|
LinkedListNode {
|
|
|
|
next: core::ptr::null_mut(),
|
|
|
|
prev: core::ptr::null_mut(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 17:11:29 +02:00
|
|
|
impl LinkedListNode {
|
2023-05-20 17:20:35 +02:00
|
|
|
pub unsafe fn init_head(mut list_head: *mut LinkedListNode) {
|
|
|
|
(*list_head).next = list_head;
|
|
|
|
(*list_head).prev = list_head;
|
2023-05-20 17:11:29 +02:00
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-05-20 17:20:35 +02:00
|
|
|
pub unsafe fn is_empty(mut list_head: *mut LinkedListNode) -> bool {
|
|
|
|
interrupt::free(|_| ((*list_head).next) == list_head)
|
2023-05-20 17:11:29 +02:00
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-05-20 17:20:35 +02:00
|
|
|
pub unsafe fn insert_head(mut list_head: *mut LinkedListNode, mut node: *mut LinkedListNode) {
|
2023-05-20 17:11:29 +02:00
|
|
|
interrupt::free(|_| {
|
2023-05-20 17:20:35 +02:00
|
|
|
(*node).next = (*list_head).next;
|
|
|
|
(*node).prev = list_head;
|
|
|
|
(*list_head).next = node;
|
2023-05-20 17:11:29 +02:00
|
|
|
(*(*node).next).prev = node;
|
|
|
|
});
|
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-05-20 17:20:35 +02:00
|
|
|
pub unsafe fn insert_tail(mut list_head: *mut LinkedListNode, mut node: *mut LinkedListNode) {
|
2023-05-20 17:11:29 +02:00
|
|
|
interrupt::free(|_| {
|
2023-05-20 17:20:35 +02:00
|
|
|
(*node).next = list_head;
|
|
|
|
(*node).prev = (*list_head).prev;
|
|
|
|
(*list_head).prev = node;
|
2023-05-20 17:11:29 +02:00
|
|
|
(*(*node).prev).next = node;
|
|
|
|
});
|
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-06-12 13:27:51 +02:00
|
|
|
/// Remove `node` from the linked list
|
2023-05-20 17:11:29 +02:00
|
|
|
pub unsafe fn remove_node(mut node: *mut LinkedListNode) {
|
|
|
|
interrupt::free(|_| {
|
|
|
|
(*(*node).prev).next = (*node).next;
|
|
|
|
(*(*node).next).prev = (*node).prev;
|
|
|
|
});
|
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-06-12 13:27:51 +02:00
|
|
|
/// Remove `list_head` into `node`
|
2023-05-20 17:20:35 +02:00
|
|
|
pub unsafe fn remove_head(mut list_head: *mut LinkedListNode, mut node: *mut *mut LinkedListNode) {
|
2023-05-20 17:11:29 +02:00
|
|
|
interrupt::free(|_| {
|
2023-05-20 17:20:35 +02:00
|
|
|
*node = (*list_head).next;
|
|
|
|
Self::remove_node((*list_head).next);
|
2023-05-20 17:11:29 +02:00
|
|
|
});
|
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-06-12 13:27:51 +02:00
|
|
|
/// Remove `list_tail` into `node`
|
|
|
|
pub unsafe fn remove_tail(mut list_tail: *mut LinkedListNode, mut node: *mut *mut LinkedListNode) {
|
2023-05-20 17:11:29 +02:00
|
|
|
interrupt::free(|_| {
|
2023-06-12 13:27:51 +02:00
|
|
|
*node = (*list_tail).prev;
|
|
|
|
Self::remove_node((*list_tail).prev);
|
2023-05-20 17:11:29 +02:00
|
|
|
});
|
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-05-20 17:11:29 +02:00
|
|
|
pub unsafe fn insert_node_after(mut node: *mut LinkedListNode, mut ref_node: *mut LinkedListNode) {
|
|
|
|
interrupt::free(|_| {
|
|
|
|
(*node).next = (*ref_node).next;
|
|
|
|
(*node).prev = ref_node;
|
|
|
|
(*ref_node).next = node;
|
|
|
|
(*(*node).next).prev = node;
|
|
|
|
});
|
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-05-20 17:11:29 +02:00
|
|
|
pub unsafe fn insert_node_before(mut node: *mut LinkedListNode, mut ref_node: *mut LinkedListNode) {
|
|
|
|
interrupt::free(|_| {
|
|
|
|
(*node).next = ref_node;
|
|
|
|
(*node).prev = (*ref_node).prev;
|
|
|
|
(*ref_node).prev = node;
|
|
|
|
(*(*node).prev).next = node;
|
|
|
|
});
|
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-05-20 17:20:35 +02:00
|
|
|
pub unsafe fn get_size(mut list_head: *mut LinkedListNode) -> usize {
|
2023-05-20 17:11:29 +02:00
|
|
|
interrupt::free(|_| {
|
|
|
|
let mut size = 0;
|
|
|
|
let mut temp: *mut LinkedListNode = core::ptr::null_mut::<LinkedListNode>();
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-05-20 17:20:35 +02:00
|
|
|
temp = (*list_head).next;
|
|
|
|
while temp != list_head {
|
2023-05-20 17:11:29 +02:00
|
|
|
size += 1;
|
|
|
|
temp = (*temp).next
|
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-05-20 17:11:29 +02:00
|
|
|
size
|
|
|
|
})
|
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-05-20 17:11:29 +02:00
|
|
|
pub unsafe fn get_next_node(mut ref_node: *mut LinkedListNode, mut node: *mut *mut LinkedListNode) {
|
|
|
|
interrupt::free(|_| {
|
|
|
|
*node = (*ref_node).next;
|
|
|
|
});
|
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-05-20 17:11:29 +02:00
|
|
|
pub unsafe fn get_prev_node(mut ref_node: *mut LinkedListNode, mut node: *mut *mut LinkedListNode) {
|
|
|
|
interrupt::free(|_| {
|
|
|
|
*node = (*ref_node).prev;
|
|
|
|
});
|
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
}
|