[PATCH 5 of 5] Improvement of portability

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

 This patch aims to improve portability to other platforms. It is
necessary to fix the variable argument functions: printk and panic.
The only option is to use the var args mechanism defined by ansi C,
also supported by bcc. The files "include/linuxmt/kernel.h" and
"kernel/printk.c" were modified accordingly. I also took this opportunity
to make some general improvements to the code.

 Upon applying this patch, there was an increase of about 130 bytes
in code size using bcc. But as mentioned earlier, applying the whole
set of 5 patches leads to an increase of only ~30 bytes of code size.

 This is the last patch to ansify the upper level directories in elks.

 The Image builded without errors. The kernel was tested with QEMU and
dioscuri emulators. Also in a PPro pc booting from floppy.

Greetings,

Juan

diff -Nurb elks.orig/include/linuxmt/kernel.h elks/include/linuxmt/kernel.h
--- elks.orig/include/linuxmt/kernel.h	2012-03-26 14:28:24.000000000 -0600
+++ elks/include/linuxmt/kernel.h	2012-05-06 12:36:31.000000000 -0500
@@ -20,12 +20,8 @@

 extern int kill_sl(void);

-#ifdef S_SPLINT_S
-
-/*@printflike@*/ extern void panic();
-/*@printflike@*/ extern int printk();
-
-#endif
+extern void panic(char *, ...);
+extern void printk(char *, ...);

 extern int wait_for_keypress(void);

diff -Nurb elks.orig/kernel/printk.c elks/kernel/printk.c
--- elks.orig/kernel/printk.c	2012-03-26 14:28:24.000000000 -0600
+++ elks/kernel/printk.c	2012-05-06 12:38:12.000000000 -0500
@@ -25,18 +25,26 @@
  *	MTK:	Sep 97 - Misc hacks to shrink generated code
  */

+#include <linuxmt/autoconf.h>
 #include <arch/segment.h>
 #include <linuxmt/mm.h>
+#include <stdarg.h>

 /*
  *	Just to make it work for now
  */
-
 extern void con_charout(char);

-static void con_write(register char *buf, int len)
+static void kputchar(register char ch)
+{
+    if (ch == '\n')
+	con_charout('\r');
+    con_charout(ch);
+}
+
+static void kputs(register char *buf)
 {
-    register char *p;
+    char ch, *p;

 #ifdef CONFIG_DCON_ANSI_PRINTK

@@ -56,13 +64,8 @@

 #endif

-    p = (char *) len;
-
-    while (p--) {
-	if (*buf == '\n')
-	    con_charout('\r');
-	con_charout(*buf++);
-    }
+    while ((ch = *buf++))
+	kputchar(ch);
 }

 /************************************************************************
@@ -71,22 +74,18 @@
  */

 char *hex_string = "0123456789ABCDEF";		/* Also used by devices. */
-char *hex_lower  = "0123456789abcdef";
+static char *hex_lower = "0123456789abcdef";

-static void numout(char *ptr, int len, int width, int base, int useSign,
+static void numout(unsigned long v, int width, int base, int useSign,
 		   int Upper, int Zero)
 {
-    unsigned long int v;
-    register char *bp;
+    char *bp, *bp2;
     char buf[12];

     if (width > sizeof(buf))		/* Error-check width specified */
 	width = sizeof(buf);

-	v = (len == 2) ? *((unsigned short *) ptr) : *((unsigned long *) ptr);
     if (useSign) {
-	if (len == 2)
-	    v = ((long)(*((short *) ptr)));
 	if ((long)v < 0)
 	    v = (-(long)v);
 	else
@@ -94,49 +93,40 @@
     }

     bp = buf + sizeof(buf);
-
-    {
-	register char *bp2;
+    *--bp = '\x00';

 	bp2 = Upper ? hex_string : hex_lower;
     do {
 	    *--bp = *(bp2 + (v % base));	/* Store digit */
 	} while ((v /= base));
-    }

     if (useSign && !Zero)
 	*--bp = '-';

     width -= buf - bp + sizeof(buf);
     while (--width >= 0)			/* Process width */
-	if (Zero)
-	    *--bp = '0';
-	else
-	    *--bp = ' ';
+	*--bp = Zero ? '0' : ' ';

-    if (useSign && Zero) {
-	if (*bp != '0')
-	    bp--;
+    if (useSign && Zero && (*bp == '0'))
 	*bp = '-';
-    }

-    con_write(bp, buf + sizeof(buf) - bp);
+    kputs(bp);
 }

-void printk(register char *fmt,int a1)
+static void vprintk(char *fmt, va_list p)
 {
-    register char *p = (char *) &a1;
-    int len, width, zero;
+    unsigned long v;
+    int width, zero;
     char c, tmp, *cp;

     while ((c = *fmt++)) {
 	if (c != '%')
-	    con_write(fmt - 1, 1);
+	    kputchar(c);
 	else {
 	    c = *fmt++;

 	    if (c == '%') {
-		con_write("%", 1);
+		con_charout(c);
 		continue;
 	    }

@@ -149,90 +139,99 @@
 		c = *fmt++;
 	    }

-	    len = 2;
-	    if (c == 'h')
+	    if ((c == 'h') || (c == 'l'))
 		c = *fmt++;
-	    else if (c == 'l') {
-		len = 4;
-		c = *fmt++;
-	    }
-
+	    tmp = 16;
 	    switch (c) {
-	    case 'o':
-		tmp = 8;
-		goto NUMOUT;
 	    case 'i':
 		c = 'd';
 	    case 'd':
-	    case 'u':
 		tmp = 10;
+		if(*(fmt-2) == 'l')
+		    v = va_arg(p, unsigned long);
+		else
+		    v = (long)(va_arg(p, short));
 		goto NUMOUT;
+	    case 'o':
+		tmp -= 2;
+	    case 'u':
+		tmp -= 6;
 	    case 'P':
 	    case 'p':
 		c += 'X' - 'P';
 	    case 'X':
 	    case 'x':
-		tmp = 16;
+		if(*(fmt-2) == 'l')
+		    v = va_arg(p, unsigned long);
+		else
+		    v = (unsigned long)(va_arg(p, unsigned short));
 	    NUMOUT:
-		numout(p, len, width, tmp, (c == 'd'), (c == 'X'), zero);
-		p += len;
+		numout(v, width, tmp, (c == 'd'), (c == 'X'), zero);
 		break;
 	    case 's':
-		cp = *((char **) p);
-		p += sizeof(char *);
-		while (*cp) {
-		    con_write(cp++, 1);
+		cp = va_arg(p, char*);
+		while ((c == *cp++)) {
+		    kputchar(c);
 		    width--;
 		}
-		while (--width >= 0)
-		    con_write(" ", 1);
-		break;
+		goto FILLSP;
 	    case 't':
-		cp = *((char **) p);
-		p += sizeof(char *);
-		while ((tmp = (char) get_fs_byte(cp))) {
-		    con_charout(tmp);
+		cp = va_arg(p, char*);
+		while ((c = (char) get_fs_byte(cp))) {
+		    kputchar(c);
 		    cp++;
 		    width--;
 		}
+	    FILLSP:
 		while (--width >= 0)
-		    con_write(" ", 1);
+		    con_charout(' ');
 		break;
 	    case 'c':
-		while (--width >= 1)
-		    con_write(" ", 1);
-		con_write(p, 1);
-		p += 2;
+		while (--width > 0)
+		    con_charout(' ');
+		kputchar(va_arg(p, int));
 		break;
 	    default:
-		con_write( "?", 1);
+		con_charout('?');
 		break;
 	    }
 	}
     }
 }

-void panic(char *error, int a1 /* VARARGS... */ )
+void printk(char *fmt, ...)
+{
+    va_list p;
+
+    va_start(p, fmt);
+    vprintk(fmt, p);
+    va_end(p);
+}
+
+void panic(char *error, ...)
 {
-    register int *bp = (int *) &error - 2, i = 0, j;
+    va_list p;
+    int *bp = (int *) &error - 2, i = 0, j;

-    printk("\npanic: ");
-    printk(error, a1);
-    printk("\napparent call stack:\n");
-    printk("Line: Addr    Parameters\n");
-    printk("~~~~: ~~~~    ~~~~~~~~~~\n");
+    kputs("\npanic: ");
+    va_start(p, error);
+    vprintk(error, p);
+    va_end(p);
+    kputs("\napparent call stack:\n");
+    kputs("Line: Addr    Parameters\n");
+    kputs("~~~~: ~~~~    ~~~~~~~~~~\n");

     do {
 	printk("%4u: %04P =>", i, bp[1]);
 	bp = (int *) bp[0];
 	for (j = 2; j <= 8; j++)
 	    printk(" %04X", bp[j]);
-	printk("\n");
+	kputchar('\n');
     } while (++i < 9);

     /* Lock up with infinite loop */

-    printk("\nSYSTEM LOCKED - Press CTRL-ALT-DEL to reboot: ");
+    kputs("\nSYSTEM LOCKED - Press CTRL-ALT-DEL to reboot: ");

     while (1)
 	/* Do nothing */;
--
To unsubscribe from this list: send the line "unsubscribe linux-8086" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Kernel]     [Linux ia64]     [DCCP]     [Linux for ARM]     [Yosemite News]     [Linux SCSI]     [Linux Hams]

  Powered by Linux