修复了未初始化时ui显示模块内存越界的问题,优化了代码结构 (#789)

This commit is contained in:
曾俊 2024-04-29 18:55:17 +08:00 committed by GitHub
parent 0722a06a09
commit bde4a334c1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -30,36 +30,38 @@ pub fn textui_init_no_alloc(video_enabled: bool) {
}
}
fn next_line() {
NO_ALLOC_OPERATIONS_INDEX.store(0, Ordering::SeqCst);
NO_ALLOC_OPERATIONS_LINE.fetch_add(1, Ordering::SeqCst);
if NO_ALLOC_OPERATIONS_LINE.load(Ordering::SeqCst) >= TRUE_LINE_NUM.load(Ordering::SeqCst) {
NO_ALLOC_OPERATIONS_LINE.store(0, Ordering::SeqCst);
}
}
pub fn no_init_textui_putchar_window(
character: char,
frcolor: FontColor,
bkcolor: FontColor,
is_put_to_window: bool,
) -> Result<(), SystemError> {
if NO_ALLOC_OPERATIONS_LINE.load(Ordering::SeqCst) >= TRUE_LINE_NUM.load(Ordering::SeqCst) {
NO_ALLOC_OPERATIONS_LINE.store(0, Ordering::SeqCst);
}
//字符'\0'代表ASCII码表中的空字符,表示字符串的结尾
if unlikely(character == '\0') {
return Ok(());
}
send_to_default_serial8250_port(&[character as u8]);
if is_put_to_window {
match character {
// 进行换行操作
if unlikely(character == '\n') {
// 换行时还需要输出\r
'\n' => {
send_to_default_serial8250_port(&[b'\r']);
if is_put_to_window {
NO_ALLOC_OPERATIONS_LINE.fetch_add(1, Ordering::SeqCst);
NO_ALLOC_OPERATIONS_INDEX.store(0, Ordering::SeqCst);
next_line();
}
return Ok(());
}
// 输出制表符
else if character == '\t' {
if is_put_to_window {
'\t' => {
let char = TextuiCharChromatic::new(Some(' '), frcolor, bkcolor);
//打印的空格数注意将每行分成一个个表格每个表格为8个字符
let mut space_to_print = 8 - NO_ALLOC_OPERATIONS_INDEX.load(Ordering::SeqCst) % 8;
while space_to_print > 0 {
@ -70,12 +72,9 @@ pub fn no_init_textui_putchar_window(
NO_ALLOC_OPERATIONS_INDEX.fetch_add(1, Ordering::SeqCst);
space_to_print -= 1;
}
return Ok(());
}
}
// 字符 '\x08' 代表 ASCII 码中的退格字符。它在输出中的作用是将光标向左移动一个位置,并在该位置上输出后续的字符,从而实现字符的删除或替换。
else if character == '\x08' {
if is_put_to_window {
'\x08' => {
NO_ALLOC_OPERATIONS_INDEX.fetch_sub(1, Ordering::SeqCst);
let op_char = NO_ALLOC_OPERATIONS_INDEX.load(Ordering::SeqCst);
if op_char >= 0 {
@ -98,15 +97,16 @@ pub fn no_init_textui_putchar_window(
}
}
}
} else if is_put_to_window {
// 输出其他字符
_ => {
let char = TextuiCharChromatic::new(Some(character), frcolor, bkcolor);
if NO_ALLOC_OPERATIONS_INDEX.load(Ordering::SeqCst) == CHAR_PER_LINE.load(Ordering::SeqCst)
if NO_ALLOC_OPERATIONS_INDEX.load(Ordering::SeqCst)
== CHAR_PER_LINE.load(Ordering::SeqCst)
{
NO_ALLOC_OPERATIONS_INDEX.store(0, Ordering::SeqCst);
NO_ALLOC_OPERATIONS_LINE.fetch_add(1, Ordering::SeqCst);
next_line();
}
char.no_init_textui_render_chromatic(
LineId::new(NO_ALLOC_OPERATIONS_LINE.load(Ordering::SeqCst)),
LineIndex::new(NO_ALLOC_OPERATIONS_INDEX.load(Ordering::SeqCst)),
@ -114,6 +114,7 @@ pub fn no_init_textui_putchar_window(
NO_ALLOC_OPERATIONS_INDEX.fetch_add(1, Ordering::SeqCst);
}
}
}
return Ok(());
}