Source-Changes-HG archive

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

[src/trunk]: src/sys/kern Made the code of the grsortu function smaller and s...



details:   https://anonhg.NetBSD.org/src/rev/06cd53d87481
branches:  trunk
changeset: 583980:06cd53d87481
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Sep 02 20:51:53 2005 +0000

description:
Made the code of the grsortu function smaller and simpler. Ok'ed by
christos.

diffstat:

 sys/kern/kern_prot.c |  37 +++++++++++++++++++++----------------
 1 files changed, 21 insertions(+), 16 deletions(-)

diffs (68 lines):

diff -r 877654c5ee6a -r 06cd53d87481 sys/kern/kern_prot.c
--- a/sys/kern/kern_prot.c      Fri Sep 02 17:48:40 2005 +0000
+++ b/sys/kern/kern_prot.c      Fri Sep 02 20:51:53 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_prot.c,v 1.86 2005/08/23 07:58:58 christos Exp $  */
+/*     $NetBSD: kern_prot.c,v 1.87 2005/09/02 20:51:53 rillig Exp $    */
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
@@ -41,7 +41,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_prot.c,v 1.86 2005/08/23 07:58:58 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_prot.c,v 1.87 2005/09/02 20:51:53 rillig Exp $");
 
 #include "opt_compat_43.h"
 
@@ -544,8 +544,10 @@
 static int
 grsortu(gid_t *grp, int ngrp)
 {
-       int i, j, k;
-       static const size_t gs = sizeof(grp[0]);
+       const gid_t *src, *end;
+       gid_t *dst;
+       gid_t group;
+       int i, j;
 
        /* bubble sort */
        for (i = 0; i < ngrp; i++)
@@ -555,21 +557,24 @@
                                grp[i] = grp[j];
                                grp[j] = tmp;
                        }
+
        /* uniq */
-       for (i = 0; i < ngrp; i++) {
-               for (j = i + 1; j < ngrp && grp[i] == grp[j]; j++)
-                       continue;
-               k = j - (i + 1);
-               if (k == 0)
-                       continue;
-               (void)memcpy(&grp[i + 1], &grp[j], gs * (ngrp - j));
-               ngrp -= k;
+       end = grp + ngrp;
+       src = grp;
+       dst = grp;
+       while (src < end) {
+               group = *src++;
+               while (src < end && *src == group)
+                       src++;
+               *dst++ = group;
+       }
+
 #ifdef DIAGNOSTIC
-               /* zero out the rest of the array */
-               (void)memset(&grp[ngrp], 0, gs * k);
+       /* zero out the rest of the array */
+       (void)memset(dst, 0, sizeof(*grp) * (end - dst));
 #endif
-       }
-       return ngrp;
+
+       return dst - grp;
 }
 
 /* ARGSUSED */



Home | Main Index | Thread Index | Old Index