]> Zhao Yanbai Git Server - kernel.git/commitdiff
优化vsprintf的代码
authoracevest <zhaoyanbai@126.com>
Mon, 26 Jan 2026 09:38:31 +0000 (17:38 +0800)
committeracevest <zhaoyanbai@126.com>
Mon, 26 Jan 2026 09:38:31 +0000 (17:38 +0800)
include/stdio.h
include/types.h
kernel/printk.c
lib/vsprintf.c

index 20c6552651f3212acba189a0323a36d8035c79fc..78b56a327a0e48495632a2327908f7902e0ad1af 100644 (file)
@@ -18,8 +18,9 @@
 #define _STDIO_H
 #include <string.h>
 #include <syscall.h>
+#include <types.h>
 extern int write(int fd, const char* buf, unsigned long size);
-extern int vsprintf(char* buf, const char* fmt, char* args);
+extern int vsprintf(char* buf, const char* fmt, va_list args);
 static inline int printf(const char* fmt, ...) {
     char ptfbuf[512];
     char* args = (char*)(((char*)&fmt) + 4);
index 069c70fb179a2fb0998f8519f536ecd6dc72640a..7ebcc74173152fdbc5718fcf1f4e024c5fa5ebf1 100644 (file)
@@ -72,9 +72,10 @@ typedef enum { false, true } bool;
 typedef void (*pf_intr)();
 
 //
+#define INTSIZEOF(n) ((sizeof(n) + sizeof(uint32_t) - 1) & (~(sizeof(uint32_t) - 1)))
 typedef char* va_list;
-#define va_start(ap, last) ((ap) = (va_list) & (last) + sizeof(last))
-#define va_arg(ap, type) (*(type*)(((ap) += sizeof(type)) - sizeof(type)))
+#define va_start(ap, last) ((ap) = (va_list) & (last) + INTSIZEOF(last))
+#define va_arg(ap, type) (*(type*)(((ap) += INTSIZEOF(type)) - INTSIZEOF(type)))
 #define va_end(ap) ((ap) = (va_list)0)
 
 #endif  //_TYPES_H
index ef1a57d772905c4b553bdd1fa39873255c81432a..663bbfa6e088db90c70b0ed2a9eb3feb050b845b 100644 (file)
@@ -19,7 +19,7 @@
 #include <system.h>
 #include <tty.h>
 
-int vsprintf(char* buf, const char* fmt, char* args);
+int vsprintf(char* buf, const char* fmt, va_list args);
 
 void serial_write(const char* buf, size_t size);
 
index b15c67218c42c8ef07a9d05f8caff3077a6ba0a8..58b17699bcd35e963d5bc5637346571d5a768d63 100644 (file)
@@ -44,7 +44,7 @@ int write_buf(char* buf, const char* str, char fillch, int charcnt, int align) {
     return charcnt > len ? charcnt : len;
 }
 
-int vsprintf(char* buf, const char* fmt, char* args) {
+int vsprintf(char* buf, const char* fmt, va_list args) {
     char* p = buf;
     int char_cnt;
     char tmp[64];
@@ -79,50 +79,44 @@ int vsprintf(char* buf, const char* fmt, char* args) {
 
         switch (*fmt) {
         case 'c':
-            *p++ = *args;
+            *p++ = va_arg(args, char);
             break;
         case 'd':
-            itoa(tmp, *((int*)args));
+            itoa(tmp, va_arg(args, int));
             p += write_buf(p, tmp, char_fill, char_cnt, align);
             break;
         case 'l':
             fmt++;
             if (*fmt == 'u' || *fmt == 'd') {  // d u都当成u来处理
-                i64tou(tmp, *((int64_t*)args));
+                i64tou(tmp, va_arg(args, uint64_t));
                 p += write_buf(p, tmp, char_fill, char_cnt, align);
-                args += 4;
             } else if (*fmt == 'o') {
-                i64too(tmp, *((uint64_t*)args));
+                i64too(tmp, va_arg(args, uint64_t));
                 p += write_buf(p, tmp, char_fill, char_cnt, align);
-                args += 4;
             } else if (*fmt == 'x' || *fmt == 'X') {
-                // i64tox(tmp, *((uint64_t *)args), *fmt == 'X' ? 1 : 0);
-                i64tox(tmp, *((uint64_t*)args), 1);  // x X都强制为大写
+                i64tox(tmp, va_arg(args, uint64_t), 1);  // x X都强制为大写
                 p += write_buf(p, tmp, char_fill, char_cnt, align);
-                args += 4;
             }
             break;
         case 's':
-            p += write_buf(p, (const char*)*((unsigned int*)args), char_fill, char_cnt, align);
+            p += write_buf(p, (const char*)va_arg(args, char*), char_fill, char_cnt, align);
             break;
         case 'u':
-            itou(tmp, *((unsigned int*)args));
+            itou(tmp, va_arg(args, uint32_t));
             p += write_buf(p, tmp, char_fill, char_cnt, align);
             break;
         case 'x':
         case 'X':
-            // itox(tmp, *((unsigned int *)args), *fmt == 'X' ? 1 : 0);
-            itox(tmp, *((unsigned int*)args), 1);  // x X都强制为大写
+            itox(tmp, va_arg(args, uint32_t), 1);  // x X都强制为大写
             p += write_buf(p, tmp, char_fill, char_cnt, align);
             break;
         case 'o':
-            itoo(tmp, *((unsigned*)args));
+            itoo(tmp, va_arg(args, uint32_t));
             p += write_buf(p, tmp, char_fill, char_cnt, align);
             break;
         default:
             break;
         }
-        args += 4;
         fmt++;
     }
     *p = 0;