Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/rasops Misc creen up for rasops.c:



details:   https://anonhg.NetBSD.org/src/rev/6588cc2b4da1
branches:  trunk
changeset: 457984:6588cc2b4da1
user:      rin <rin%NetBSD.org@localhost>
date:      Fri Jul 26 05:15:47 2019 +0000

description:
Misc creen up for rasops.c:

- sort headers
- return error value instead of panic
- use uintptr_t for cast pointer to integer
- use macros appropriately
- use __func__
- some consistency check ifndef DEBUG
- try to avoid undefined behaviors related to shift
- convert malloc/free to kmem_alloc/kmem_free
- convert MIN to uimin
- style

diffstat:

 sys/dev/rasops/rasops.c |  254 ++++++++++++++++++++++-------------------------
 1 files changed, 120 insertions(+), 134 deletions(-)

diffs (truncated from 688 to 300 lines):

diff -r ff117be106d7 -r 6588cc2b4da1 sys/dev/rasops/rasops.c
--- a/sys/dev/rasops/rasops.c   Fri Jul 26 04:08:39 2019 +0000
+++ b/sys/dev/rasops/rasops.c   Fri Jul 26 05:15:47 2019 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: rasops.c,v 1.88 2019/07/25 02:26:32 rin Exp $ */
+/*      $NetBSD: rasops.c,v 1.89 2019/07/26 05:15:47 rin Exp $ */
 
 /*-
  * Copyright (c) 1999 The NetBSD Foundation, Inc.
@@ -30,18 +30,18 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: rasops.c,v 1.88 2019/07/25 02:26:32 rin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: rasops.c,v 1.89 2019/07/26 05:15:47 rin Exp $");
 
 #include "opt_rasops.h"
 #include "rasops_glue.h"
 #include "opt_wsmsgattrs.h"
 
 #include <sys/param.h>
+#include <sys/bswap.h>
+#include <sys/kmem.h>
 #include <sys/systm.h>
 #include <sys/time.h>
-#include <sys/kmem.h>
 
-#include <sys/bswap.h>
 #include <machine/endian.h>
 
 #include <dev/wscons/wsdisplayvar.h>
@@ -68,7 +68,7 @@
 };     
 
 /* ANSI colormap (R,G,B). Upper 8 are high-intensity */
-const uint8_t rasops_cmap[256*3] = {
+const uint8_t rasops_cmap[256 * 3] = {
        0x00, 0x00, 0x00, /* black */
        0x7f, 0x00, 0x00, /* red */
        0x00, 0x7f, 0x00, /* green */
@@ -131,7 +131,7 @@
        1, 0, 0, 0,
        0, 0, 0, 1,
        1, 0, 0, 0,
-       0, 0, 0, 1
+       0, 0, 0, 1,
 };
 
 /* Generic functions */
@@ -222,7 +222,7 @@
                 * this means there is no supported font in the list
                 */
                if (cookie <= 0) {
-                       aprint_error("rasops_init: font table is empty\n");
+                       aprint_error("%s: font table is empty\n", __func__);
                        return -1;
                }
 
@@ -240,7 +240,7 @@
 #endif
 
                if (wsfont_lock(cookie, &ri->ri_font)) {
-                       aprint_error("rasops_init: couldn't lock font\n");
+                       aprint_error("%s: couldn't lock font\n", __func__);
                        return -1;
                }
 
@@ -249,19 +249,17 @@
 #endif
 
        /* This should never happen in reality... */
-#ifdef DEBUG
-       if ((long)ri->ri_bits & 3) {
-               aprint_error(
-                   "rasops_init: bits not aligned on 32-bit boundary\n");
+       if ((uintptr_t)ri->ri_bits & 3) {
+               aprint_error("%s: bits not aligned on 32-bit boundary\n",
+                   __func__);
                return -1;
        }
 
-       if ((int)ri->ri_stride & 3) {
-               aprint_error(
-                   "rasops_init: stride not aligned on 32-bit boundary\n");
+       if (ri->ri_stride & 3) {
+               aprint_error("%s: stride not aligned on 32-bit boundary\n",
+                   __func__);
                return -1;
        }
-#endif
 
        if (rasops_reconfig(ri, wantrows, wantcols))
                return -1;
@@ -276,7 +274,8 @@
 int
 rasops_reconfig(struct rasops_info *ri, int wantrows, int wantcols)
 {
-       int bpp, s, len;
+       int bpp, s;
+       size_t len;
 
        s = splhigh();
 
@@ -299,11 +298,19 @@
        ri->ri_optfont.fontheight = ri->ri_font->fontheight;
        ri->ri_optfont.stride = ri->ri_font->stride;
        len = ri->ri_optfont.fontheight * ri->ri_optfont.stride *
-                     ri->ri_optfont.numchars; 
+           ri->ri_optfont.numchars; 
+
+       if (ri->ri_font->fontwidth > 32 || ri->ri_font->fontwidth < 4) {
+               aprint_error("%s: fontwidth assumptions botched", __func__);
+               splx(s);
+               return -1;
+       }
 
        if ((ri->ri_flg & RI_NO_AUTO) == 0) {
                ri->ri_optfont.data = kmem_zalloc(len, KM_SLEEP);
-               if (ri->ri_optfont.stride < ri->ri_optfont.fontwidth) {
+               if (FONT_IS_ALPHA(&ri->ri_optfont))
+                       rasops_make_box_chars_alpha(ri);
+               else {
                        switch (ri->ri_optfont.stride) {
                        case 1:
                                rasops_make_box_chars_8(ri);
@@ -314,16 +321,17 @@
                        case 4:
                                rasops_make_box_chars_32(ri);
                                break;
+                       default:
+                               aprint_error(
+                                   "%s: font stride assumptions botched",
+                                   __func__);
+                               splx(s);
+                               return -1;
                        }
-               } else {
-                       rasops_make_box_chars_alpha(ri);
                }
        } else
                memset(&ri->ri_optfont, 0, sizeof(ri->ri_optfont));
 
-       if (ri->ri_font->fontwidth > 32 || ri->ri_font->fontwidth < 4)
-               panic("rasops_init: fontwidth assumptions botched!");
-
        /* Need this to frob the setup below */
        bpp = (ri->ri_depth == 15 ? 16 : ri->ri_depth);
 
@@ -374,10 +382,12 @@
        ri->ri_yscale = ri->ri_font->fontheight * ri->ri_stride;
        ri->ri_fontscale = ri->ri_font->fontheight * ri->ri_font->stride;
 
-#ifdef DEBUG
-       if ((ri->ri_delta & 3) != 0)
-               panic("rasops_init: ri_delta not aligned on 32-bit boundary");
-#endif
+       if ((ri->ri_delta & 3) != 0) {
+               aprint_error(
+                   "%s: ri_delta not aligned on 32-bit boundary", __func__);
+               splx(s);
+               return -1;
+       }
        ri->ri_origbits = ri->ri_bits;
        ri->ri_hworigbits = ri->ri_hwbits;
 
@@ -398,10 +408,10 @@
                            ((ri->ri_height - ri->ri_emuheight) >> 1) *
                            ri->ri_stride;
                }
-               ri->ri_yorigin = (int)(ri->ri_bits - ri->ri_origbits)
-                  / ri->ri_stride;
-               ri->ri_xorigin = (((int)(ri->ri_bits - ri->ri_origbits)
-                  % ri->ri_stride) * 8 / bpp);
+               ri->ri_yorigin = (int)(ri->ri_bits - ri->ri_origbits) /
+                   ri->ri_stride;
+               ri->ri_xorigin = (((int)(ri->ri_bits - ri->ri_origbits) %
+                   ri->ri_stride) * 8 / bpp);
        } else
                ri->ri_xorigin = ri->ri_yorigin = 0;
 
@@ -467,6 +477,7 @@
 #endif
        default:
                ri->ri_flg &= ~RI_CFGDONE;
+               aprint_error("%s: depth not supported\n", __func__);
                splx(s);
                return -1;
        }
@@ -502,28 +513,23 @@
 static int
 rasops_mapchar(void *cookie, int c, u_int *cp)
 {
-       struct rasops_info *ri;
-
-       ri = (struct rasops_info *)cookie;
+       struct rasops_info *ri = (struct rasops_info *)cookie;
 
        KASSERT(ri->ri_font != NULL);
 
-       if ( (c = wsfont_map_unichar(ri->ri_font, c)) < 0) {
+       if ((c = wsfont_map_unichar(ri->ri_font, c)) < 0 ||
+           c < ri->ri_font->firstchar) {
                *cp = ' ';
                return 0;
        }
 
-       if (c < ri->ri_font->firstchar) {
-               *cp = ' ';
-               return 0;
-       }
-
-#if 0
-       if (c - ri->ri_font->firstchar >= ri->ri_font->numchars) {
+#if 0 /* XXXRO */
+       if (CHAR_IN_FONT(c, ri->ri_font)) {
                *cp = ' ';
                return 0;
        }
 #endif
+
        *cp = c;
        return 5;
 }
@@ -532,13 +538,12 @@
  * Allocate a color attribute.
  */
 static int
-rasops_allocattr_color(void *cookie, int fg, int bg, int flg,
-    long *attr)
+rasops_allocattr_color(void *cookie, int fg0, int bg0, int flg, long *attr)
 {
-       int swap;
+       uint32_t fg = fg0, bg = bg0;
 
-       if (__predict_false((unsigned int)fg >= sizeof(rasops_isgray) ||
-           (unsigned int)bg >= sizeof(rasops_isgray)))
+       if (__predict_false(fg >= sizeof(rasops_isgray) ||
+           bg >= sizeof(rasops_isgray)))
                return EINVAL;
 
 #ifdef RASOPS_CLIPPING
@@ -562,7 +567,7 @@
        }
 
        if ((flg & WSATTR_REVERSE) != 0) {
-               swap = fg;
+               uint32_t swap = fg;
                fg = bg;
                bg = swap;
        }
@@ -586,10 +591,9 @@
  * Allocate a mono attribute.
  */
 static int
-rasops_allocattr_mono(void *cookie, int fg, int bg, int flg,
-    long *attr)
+rasops_allocattr_mono(void *cookie, int fg0, int bg0, int flg, long *attr)
 {
-       int swap;
+       uint32_t fg = fg0, bg = bg0;
 
        if ((flg & (WSATTR_BLINK | WSATTR_HILIT | WSATTR_WSCOLORS)) != 0)
                return EINVAL;
@@ -598,7 +602,7 @@
        bg = 0;
 
        if ((flg & WSATTR_REVERSE) != 0) {
-               swap = fg;
+               uint32_t swap = fg;
                fg = bg;
                bg = swap;
        }
@@ -629,7 +633,7 @@
                src = 0;
        }
 
-       if ((src + num) > ri->ri_rows)
+       if (src + num > ri->ri_rows)
                num = ri->ri_rows - src;
 
        if (dst < 0) {
@@ -637,7 +641,7 @@
                dst = 0;
        }
 
-       if ((dst + num) > ri->ri_rows)
+       if (dst + num > ri->ri_rows)
                num = ri->ri_rows - dst;
 
        if (num <= 0)
@@ -723,12 +727,11 @@
 void
 rasops_copycols(void *cookie, int row, int src, int dst, int num)
 {
-       struct rasops_info *ri;
+       struct rasops_info *ri = (struct rasops_info *)cookie;
        uint8_t *sp, *dp, *hp;
        int height;
 
-       ri = (struct rasops_info *)cookie;



Home | Main Index | Thread Index | Old Index