Browse Source

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

曾俊 10 months ago
parent
commit
bde4a334c1
1 changed files with 63 additions and 62 deletions
  1. 63 62
      kernel/src/libs/lib_ui/textui_no_alloc.rs

+ 63 - 62
kernel/src/libs/lib_ui/textui_no_alloc.rs

@@ -30,56 +30,83 @@ 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 unlikely(character == '\n') {
-        // 换行时还需要输出\r
-        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);
-        }
-        return Ok(());
-    }
-    // 输出制表符
-    else if character == '\t' {
-        if is_put_to_window {
-            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 {
-                char.no_init_textui_render_chromatic(
-                    LineId::new(NO_ALLOC_OPERATIONS_LINE.load(Ordering::SeqCst)),
-                    LineIndex::new(NO_ALLOC_OPERATIONS_INDEX.load(Ordering::SeqCst)),
-                );
-                NO_ALLOC_OPERATIONS_INDEX.fetch_add(1, Ordering::SeqCst);
-                space_to_print -= 1;
+    if is_put_to_window {
+        match character {
+            // 进行换行操作
+            '\n' => {
+                send_to_default_serial8250_port(&[b'\r']);
+                if is_put_to_window {
+                    next_line();
+                }
             }
-            return Ok(());
-        }
-    }
-    // 字符 '\x08' 代表 ASCII 码中的退格字符。它在输出中的作用是将光标向左移动一个位置,并在该位置上输出后续的字符,从而实现字符的删除或替换。
-    else if character == '\x08' {
-        if is_put_to_window {
-            NO_ALLOC_OPERATIONS_INDEX.fetch_sub(1, Ordering::SeqCst);
-            let op_char = NO_ALLOC_OPERATIONS_INDEX.load(Ordering::SeqCst);
-            if op_char >= 0 {
+            // 输出制表符
+            '\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 {
+                    char.no_init_textui_render_chromatic(
+                        LineId::new(NO_ALLOC_OPERATIONS_LINE.load(Ordering::SeqCst)),
+                        LineIndex::new(NO_ALLOC_OPERATIONS_INDEX.load(Ordering::SeqCst)),
+                    );
+                    NO_ALLOC_OPERATIONS_INDEX.fetch_add(1, Ordering::SeqCst);
+                    space_to_print -= 1;
+                }
+            }
+            // 字符 '\x08' 代表 ASCII 码中的退格字符。它在输出中的作用是将光标向左移动一个位置,并在该位置上输出后续的字符,从而实现字符的删除或替换。
+            '\x08' => {
+                NO_ALLOC_OPERATIONS_INDEX.fetch_sub(1, Ordering::SeqCst);
+                let op_char = NO_ALLOC_OPERATIONS_INDEX.load(Ordering::SeqCst);
+                if op_char >= 0 {
+                    let char = TextuiCharChromatic::new(Some(' '), frcolor, bkcolor);
+                    char.no_init_textui_render_chromatic(
+                        LineId::new(NO_ALLOC_OPERATIONS_LINE.load(Ordering::SeqCst)),
+                        LineIndex::new(NO_ALLOC_OPERATIONS_INDEX.load(Ordering::SeqCst)),
+                    );
+
+                    NO_ALLOC_OPERATIONS_INDEX.fetch_add(1, Ordering::SeqCst);
+                }
+                // 需要向上缩一行
+                if op_char < 0 {
+                    // 上缩一行
+                    NO_ALLOC_OPERATIONS_INDEX.store(0, Ordering::SeqCst);
+                    NO_ALLOC_OPERATIONS_LINE.fetch_sub(1, Ordering::SeqCst);
+
+                    if NO_ALLOC_OPERATIONS_LINE.load(Ordering::SeqCst) < 0 {
+                        NO_ALLOC_OPERATIONS_LINE.store(0, Ordering::SeqCst);
+                    }
+                }
+            }
+            // 输出其他字符
+            _ => {
+                let char = TextuiCharChromatic::new(Some(character), frcolor, bkcolor);
+
+                if NO_ALLOC_OPERATIONS_INDEX.load(Ordering::SeqCst)
+                    == CHAR_PER_LINE.load(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)),
@@ -87,33 +114,7 @@ pub fn no_init_textui_putchar_window(
 
                 NO_ALLOC_OPERATIONS_INDEX.fetch_add(1, Ordering::SeqCst);
             }
-            // 需要向上缩一行
-            if op_char < 0 {
-                // 上缩一行
-                NO_ALLOC_OPERATIONS_INDEX.store(0, Ordering::SeqCst);
-                NO_ALLOC_OPERATIONS_LINE.fetch_sub(1, Ordering::SeqCst);
-
-                if NO_ALLOC_OPERATIONS_LINE.load(Ordering::SeqCst) < 0 {
-                    NO_ALLOC_OPERATIONS_LINE.store(0, Ordering::SeqCst);
-                }
-            }
         }
-    } 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)
-        {
-            NO_ALLOC_OPERATIONS_INDEX.store(0, Ordering::SeqCst);
-            NO_ALLOC_OPERATIONS_LINE.fetch_add(1, Ordering::SeqCst);
-        }
-        char.no_init_textui_render_chromatic(
-            LineId::new(NO_ALLOC_OPERATIONS_LINE.load(Ordering::SeqCst)),
-            LineIndex::new(NO_ALLOC_OPERATIONS_INDEX.load(Ordering::SeqCst)),
-        );
-
-        NO_ALLOC_OPERATIONS_INDEX.fetch_add(1, Ordering::SeqCst);
     }
-
     return Ok(());
 }