diff --git a/fonts/pixel_font.cpp b/fonts/pixel_font.cpp index fc6d69b..37b7e68 100644 --- a/fonts/pixel_font.cpp +++ b/fonts/pixel_font.cpp @@ -2,6 +2,7 @@ #include #include #include +#include void pf_print_char(char c, SUINT32 basex, SUINT32 basey, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color) { for (SUINT32 y = 0; y < 16; y++) { @@ -23,7 +24,7 @@ void pf_print_char(char c, SUINT32 basex, SUINT32 basey, EFI_GRAPHICS_OUTPUT_BLT } void pf_print(const char* text, SUINT32 basex, SUINT32 basey, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color) { - for (SUINT32 i = 0; i < strlen(text); i++) { + for (SUINT32 i = 0; i < str_len(text); i++) { char c = text[i]; pf_print_char(c, basex + i * 8, basey, color); // 只要 字数 * 8 + basex 不爆hr就没事 } diff --git a/include/common.h b/include/common.h index ccecc24..8f3c7e9 100644 --- a/include/common.h +++ b/include/common.h @@ -1,10 +1,5 @@ #pragma once #include -#define ASM asm volatile - -static SUINT32 strlen(const char* arr) { // 获取string长度 - SSINT32 i = 0; - while (arr[i++] != '\0'); - return i - 1; -} \ No newline at end of file +#include +#define ASM asm volatile \ No newline at end of file diff --git a/include/fonts/pixel_font.h b/include/fonts/pixel_font.h index 02faf58..0436bf1 100644 --- a/include/fonts/pixel_font.h +++ b/include/fonts/pixel_font.h @@ -1,7 +1,8 @@ #pragma once #include +#include void pf_print_char(char c, SUINT32 basex, SUINT32 basey, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color = {255, 255, 255, 255}); // Pixel Font 打印字符 -void pf_print(const char* text, SUINT32 basex, SUINT32 basey, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color = {255, 255, 255, 255}); // Pixel Font 打印string \ No newline at end of file +void pf_print(String text, SUINT32 basex, SUINT32 basey, EFI_GRAPHICS_OUTPUT_BLT_PIXEL color = {255, 255, 255, 255}); // Pixel Font 打印string \ No newline at end of file diff --git a/include/fs.h b/include/fs.h index c7df69f..59a1657 100644 --- a/include/fs.h +++ b/include/fs.h @@ -1,6 +1,7 @@ #pragma once #include +#include EFI_STATUS fs_init(); void fs_list(); -EFI_STATUS fs_read(const CHAR16 *Path, void **Buffer, UINTN *Size); +EFI_STATUS fs_read(WString Path, void **Buffer, UINTN *Size); diff --git a/include/graphics/layer.h b/include/graphics/layer.h index 2ede234..665b3c9 100644 --- a/include/graphics/layer.h +++ b/include/graphics/layer.h @@ -2,6 +2,7 @@ #include #include +#include #define LAYER_MAX 32 #define LAYER_NAME_LEN 32 @@ -29,7 +30,7 @@ typedef struct layer { void layer_init(void); // Layer management -layer_t* layer_create(const char* name, layer_type_t type, UINT32 w, UINT32 h); +layer_t* layer_create(String name, layer_type_t type, UINT32 w, UINT32 h); void layer_destroy(layer_t* layer); void layer_set_z(layer_t* layer, SSINT32 z); void layer_set_pos(layer_t* layer, SSINT32 x, SSINT32 y); diff --git a/include/scheduler.h b/include/scheduler.h index 2165d78..dd3d6e7 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -3,6 +3,7 @@ #include #include #include +#include #define TASK_STACK_SIZE (PAGE_SIZE * 4) // 16 KB kernel stack per task #define TASK_MAX 32 @@ -26,7 +27,7 @@ typedef struct task { } task_t; // Create a new task. Returns task pointer or NULL on failure. -task_t* task_create(const char* name, void (*entry)(void)); +task_t* task_create(String name, void (*entry)(void)); // Yield CPU to next ready task (cooperative) void yield(void); diff --git a/include/serial.h b/include/serial.h index 6cfccc7..72f296c 100644 --- a/include/serial.h +++ b/include/serial.h @@ -2,6 +2,7 @@ #include #include +#include struct serial_context { // 串行内容结构体 EFI_SERIAL_IO_PROTOCOL *SerialIo; @@ -10,7 +11,7 @@ struct serial_context { // 串行内容结构体 extern serial_context g_serial; void serial_init(EFI_SERIAL_IO_PROTOCOL *SerialIo); // 初始化串行驱动 -void serial_write(const char *str); // 往串行写string +void serial_write(String str); // 往串行写string void serial_write_char(char c); // 往串行写char(不推荐使用) void serial_write_hex(UINTN val); // 往串行写十六进制数字 char serial_read_char(); // 读串行 \ No newline at end of file diff --git a/include/string_utils.h b/include/string_utils.h new file mode 100644 index 0000000..f8e9be9 --- /dev/null +++ b/include/string_utils.h @@ -0,0 +1,91 @@ +#pragma once + +#include + +// === Unified string types === +// Use these throughout the kernel instead of raw char*/const char*. + +typedef const char* String; // 8-bit narrow string +typedef const uint16_t* WString; // 16-bit wide string (CHAR16) + +// === Narrow string utilities (String) === + +static inline SUINT32 str_len(String s) { + SUINT32 i = 0; + while (s[i]) i++; + return i; +} + +static inline SSINT32 str_cmp(String a, String b) { + while (*a && *a == *b) { a++; b++; } + return (SUINT8)*a - (SUINT8)*b; +} + +static inline bool str_eq(String a, String b) { + return str_cmp(a, b) == 0; +} + +// Copy up to max-1 chars, always null-terminates. +static inline void str_copy(char* dst, String src, SUINT32 max) { + if (max == 0) return; + SUINT32 i = 0; + while (src[i] && i < max - 1) { + dst[i] = src[i]; + i++; + } + dst[i] = '\0'; +} + +// === Wide string utilities (WString) === + +static inline SUINT32 wstr_len(WString s) { + SUINT32 i = 0; + while (s[i]) i++; + return i; +} + +static inline SSINT32 wstr_cmp(WString a, WString b) { + while (*a && *a == *b) { a++; b++; } + return (SUINT16)*a - (SUINT16)*b; +} + +static inline bool wstr_eq(WString a, WString b) { + return wstr_cmp(a, b) == 0; +} + +static inline void wstr_copy(uint16_t* dst, WString src, SUINT32 max) { + if (max == 0) return; + SUINT32 i = 0; + while (src[i] && i < max - 1) { + dst[i] = src[i]; + i++; + } + dst[i] = 0; +} + +// Convert wide string to ASCII (non-printable -> '?') +static inline void wstr_to_ascii(char* dst, WString src, SUINT32 max) { + if (max == 0) return; + SUINT32 i = 0; + while (src[i] && i < max - 1) { + if (src[i] >= 0x20 && src[i] <= 0x7E) + dst[i] = (char)src[i]; + else + dst[i] = '?'; + i++; + } + dst[i] = '\0'; +} + +// === Memory utilities === + +static inline void mem_set(void* dst, SUINT8 val, SUINT32 size) { + SUINT8* d = (SUINT8*)dst; + for (SUINT32 i = 0; i < size; i++) d[i] = val; +} + +static inline void mem_copy(void* dst, const void* src, SUINT32 size) { + SUINT8* d = (SUINT8*)dst; + const SUINT8* s = (const SUINT8*)src; + for (SUINT32 i = 0; i < size; i++) d[i] = s[i]; +} diff --git a/kernel/fs.cpp b/kernel/fs.cpp index 3f3e421..a1b1ffd 100644 --- a/kernel/fs.cpp +++ b/kernel/fs.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include extern EFI_SYSTEM_TABLE *ST; @@ -537,14 +537,7 @@ struct list_ctx { }; static void name_to_ascii(const CHAR16 *Name, char *Ascii, UINTN ascii_sz) { - UINTN i = 0; - for (; Name[i] && i < ascii_sz - 1; i++) { - if (Name[i] >= 0x20 && Name[i] <= 0x7E) - Ascii[i] = (char)Name[i]; - else - Ascii[i] = '?'; - } - Ascii[i] = 0; + wstr_to_ascii(Ascii, (WString)Name, ascii_sz); } static void list_callback(void *ctx, const CHAR16 *Name, UINT8 Attr, @@ -633,10 +626,7 @@ struct find_ctx { }; static BOOLEAN name_match(const CHAR16 *a, const CHAR16 *b) { - UINTN i = 0; - for (; a[i] && b[i]; i++) - if (a[i] != b[i]) return FALSE; - return a[i] == 0 && b[i] == 0; + return wstr_eq((WString)a, (WString)b); } static void find_callback(void *ctx, const CHAR16 *Name, UINT8 Attr, @@ -651,7 +641,7 @@ static void find_callback(void *ctx, const CHAR16 *Name, UINT8 Attr, } } -EFI_STATUS fs_read(const CHAR16 *Path, void **Buffer, UINTN *Size) { +EFI_STATUS fs_read(WString Path, void **Buffer, UINTN *Size) { if (!g_fs_inited) return EFI_NOT_READY; const CHAR16 *p = Path; diff --git a/kernel/graphics/layer.cpp b/kernel/graphics/layer.cpp index 9f3aabc..3d51a59 100644 --- a/kernel/graphics/layer.cpp +++ b/kernel/graphics/layer.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include // --- Layer list (sorted by z, lowest first) --- @@ -84,12 +84,7 @@ layer_t* layer_create(const char* name, layer_type_t type, UINT32 w, UINT32 h) { layer->visible = true; layer->next = NULL; - const char* s = name; - char* d = layer->name; - for (SSINT32 i = 0; i < LAYER_NAME_LEN - 1 && *s; i++) { - *d++ = *s++; - } - *d = '\0'; + str_copy(layer->name, name, LAYER_NAME_LEN); UINTN buf_size = (UINTN)w * h * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL); layer->buffer = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL*)kmalloc(buf_size); diff --git a/kernel/interrupt/gdt.cpp b/kernel/interrupt/gdt.cpp index 97c9089..139446c 100644 --- a/kernel/interrupt/gdt.cpp +++ b/kernel/interrupt/gdt.cpp @@ -1,5 +1,6 @@ #include #include +#include #include static gdt_entry g_gdt[7]; // 5 segments + TSS (2 entries) diff --git a/kernel/interrupt/idt.cpp b/kernel/interrupt/idt.cpp index 2cbd303..3379b89 100644 --- a/kernel/interrupt/idt.cpp +++ b/kernel/interrupt/idt.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include diff --git a/kernel/interrupt/pic.cpp b/kernel/interrupt/pic.cpp index 6fb5d27..298232a 100644 --- a/kernel/interrupt/pic.cpp +++ b/kernel/interrupt/pic.cpp @@ -1,5 +1,6 @@ #include #include +#include #include static inline void outb(UINT16 port, UINT8 val) { diff --git a/kernel/interrupt/pit.cpp b/kernel/interrupt/pit.cpp index 1389506..4871437 100644 --- a/kernel/interrupt/pit.cpp +++ b/kernel/interrupt/pit.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include diff --git a/kernel/main.cpp b/kernel/main.cpp index 457aa66..0aa185f 100644 --- a/kernel/main.cpp +++ b/kernel/main.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/kernel/memory/heap.cpp b/kernel/memory/heap.cpp index 5053c7c..cc581bd 100644 --- a/kernel/memory/heap.cpp +++ b/kernel/memory/heap.cpp @@ -1,6 +1,7 @@ #include #include #include +#include struct heap_block { UINTN size; // includes header; bit 0 = 1 used, 0 free @@ -184,10 +185,7 @@ void* kcalloc(UINTN num, UINTN size) { UINTN total = num * size; void* ptr = kmalloc(total); if (ptr) { - UINT8* p = (UINT8*)ptr; - for (UINTN i = 0; i < total; i++) { - p[i] = 0; - } + mem_set(ptr, 0, total); } return ptr; } @@ -216,11 +214,7 @@ void* krealloc(void* ptr, UINTN new_size) { void* new_ptr = kmalloc(new_size); if (new_ptr) { - UINT8* src = (UINT8*)ptr; - UINT8* dst = (UINT8*)new_ptr; - for (UINTN i = 0; i < old_size; i++) { - dst[i] = src[i]; - } + mem_copy(new_ptr, ptr, old_size); kfree(ptr); } return new_ptr; diff --git a/kernel/memory/pmm.cpp b/kernel/memory/pmm.cpp index 85ae72a..d3cec15 100644 --- a/kernel/memory/pmm.cpp +++ b/kernel/memory/pmm.cpp @@ -1,5 +1,7 @@ #include +#include #include +#include extern EFI_SYSTEM_TABLE *ST; diff --git a/kernel/scheduler/scheduler.cpp b/kernel/scheduler/scheduler.cpp index 9aa73ad..52e5c02 100644 --- a/kernel/scheduler/scheduler.cpp +++ b/kernel/scheduler/scheduler.cpp @@ -53,12 +53,7 @@ task_t* task_create(const char* name, void (*entry)(void)) { task->time_slice = TIME_SLICE_DEFAULT; // Copy name - const char* s = name; - char* d = task->name; - for (SSINT32 i = 0; i < TASK_NAME_LEN - 1 && *s; i++) { - *d++ = *s++; - } - *d = '\0'; + str_copy(task->name, name, TASK_NAME_LEN); // Set up initial stack for first context_switch into this task. // Stack grows downward. context_switch will pop 6 regs then ret. diff --git a/kernel/serial.cpp b/kernel/serial.cpp index 2a00a64..15e494e 100644 --- a/kernel/serial.cpp +++ b/kernel/serial.cpp @@ -1,5 +1,5 @@ #include -#include +#include extern EFI_SYSTEM_TABLE *ST; @@ -22,7 +22,7 @@ void serial_write_char(char c) { } } -void serial_write(const char *str) { +void serial_write(String str) { if (!g_serial.SerialIo) { uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, (CHAR16*)L"serial: null io\n"); return;