Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/rpc Cleanup and simplify.



details:   https://anonhg.NetBSD.org/src/rev/73c66a1c7649
branches:  trunk
changeset: 341455:73c66a1c7649
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Nov 06 23:11:09 2015 +0000

description:
Cleanup and simplify.
undef svc_fdset and svc_maxfd to get to the real data for the compat code.

diffstat:

 lib/libc/rpc/rpc_commondata.c |    6 +-
 lib/libc/rpc/svc_fdset.c      |  261 +++++++++++++++++++++--------------------
 2 files changed, 139 insertions(+), 128 deletions(-)

diffs (truncated from 430 to 300 lines):

diff -r 401bbaec2c3c -r 73c66a1c7649 lib/libc/rpc/rpc_commondata.c
--- a/lib/libc/rpc/rpc_commondata.c     Fri Nov 06 23:05:09 2015 +0000
+++ b/lib/libc/rpc/rpc_commondata.c     Fri Nov 06 23:11:09 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rpc_commondata.c,v 1.10 2015/11/06 23:05:09 joerg Exp $        */
+/*     $NetBSD: rpc_commondata.c,v 1.11 2015/11/06 23:11:09 christos Exp $     */
 
 /*
  * Copyright (c) 2010, Oracle America, Inc.
@@ -38,7 +38,7 @@
 #if 0
 static char *sccsid = "@(#)rpc_commondata.c    2.1 88/07/29 4.0 RPCSRC";
 #else
-__RCSID("$NetBSD: rpc_commondata.c,v 1.10 2015/11/06 23:05:09 joerg Exp $");
+__RCSID("$NetBSD: rpc_commondata.c,v 1.11 2015/11/06 23:11:09 christos Exp $");
 #endif
 #endif
 
@@ -49,7 +49,9 @@
  * by public interfaces 
  */
 struct opaque_auth _null_auth;
+#undef svc_fdset
 fd_set svc_fdset;
+#undef svc_maxfd
 int svc_maxfd = -1;
 #ifndef _REENTRANT
 #undef rpc_createerr
diff -r 401bbaec2c3c -r 73c66a1c7649 lib/libc/rpc/svc_fdset.c
--- a/lib/libc/rpc/svc_fdset.c  Fri Nov 06 23:05:09 2015 +0000
+++ b/lib/libc/rpc/svc_fdset.c  Fri Nov 06 23:11:09 2015 +0000
@@ -1,7 +1,36 @@
-/*     $NetBSD: svc_fdset.c,v 1.3 2015/11/06 23:05:09 joerg Exp $      */
+/*     $NetBSD: svc_fdset.c,v 1.4 2015/11/06 23:11:09 christos Exp $   */
+
+/*-
+ * Copyright (c) 2015 The NetBSD Foundation, Inc.
+ * All rights resefdsed.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: svc_fdset.c,v 1.3 2015/11/06 23:05:09 joerg Exp $");
+__RCSID("$NetBSD: svc_fdset.c,v 1.4 2015/11/06 23:11:09 christos Exp $");
 
 
 #include "reentrant.h"
@@ -13,25 +42,19 @@
 #include <stdlib.h>
 #include <string.h>
 
-struct my_svc_fdset {
+#undef svc_fdset
+#undef svc_maxfd
+extern fd_set svc_fdset;
+extern int svc_maxfd;
+
+struct svc_fdset {
        fd_set *fdset;
        int     fdmax;
        int     fdsize;
 };
 
-
 /* The single threaded, one global fd_set version */
-static fd_set *__svc_fdset;
-static int svc_fdsize = 0;
-
-/*
- * Update the old global svc_fdset if needed for binary compatibility
- */
-#define COMPAT_UPDATE(a)                               \
-       do                                              \
-               if ((a) == __svc_fdset)                 \
-                       svc_fdset = *__svc_fdset;       \
-       while (/*CONSTCOND*/0)
+static struct svc_fdset __svc_fdset;
 
 static thread_key_t fdsetkey = -2;
 
@@ -50,8 +73,8 @@
        va_end(ap);
 }
 
-static void __printflike(5, 6)
-svc_fdset_print(const char *func, size_t line, const fd_set *fds, int fdmax,
+static void __printflike(4, 5)
+svc_fdset_print(const char *func, size_t line, struct svc_fdset *fds, 
     const char *fmt, ...)
 {
        va_list ap;
@@ -61,12 +84,9 @@
        svc_header(func, line, fmt, ap);
        va_end(ap);
 
-       if (fdmax == 0)
-               fdmax = FD_SETSIZE;
-
-       fprintf(stderr, "%p[%d] <", fds, fdmax);
-       for (int i = 0; i <= fdmax; i++) {
-               if (!FD_ISSET(i, fds))
+       fprintf(stderr, "%p[%d] <", fds->fdset, fds->fdmax);
+       for (int i = 0; i <= fds->fdmax; i++) {
+               if (!FD_ISSET(i, fds->fdset))
                        continue;
                fprintf(stderr, "%s%d", did, i);
                did = ", ";
@@ -85,102 +105,102 @@
        fprintf(stderr, "\n");
 }
 
+#define DPRINTF(...)           svc_print(__func__, __LINE__, __VA_ARGS__)
 #define DPRINTF_FDSET(...)     svc_fdset_print(__func__, __LINE__, __VA_ARGS__)
-#define DPRINTF(...)           svc_print(__func__, __LINE__, __VA_ARGS__)
+
 #else
+
+#define DPRINTF(...)
 #define DPRINTF_FDSET(...)
-#define DPRINTF(...)
+
 #endif
 
 
+static inline void
+svc_fdset_sanitize(struct svc_fdset *fds)
+{
+       while (fds->fdmax >= 0 && !FD_ISSET(fds->fdmax, fds->fdset))
+               fds->fdmax--;
+       /* Compat update */
+       if (fds == &__svc_fdset) {
+               svc_fdset = *__svc_fdset.fdset;
+               svc_maxfd = __svc_fdset.fdmax;
+       }
+}
+
 static void
 svc_fdset_free(void *v)
 {
-       struct my_svc_fdset *rv = v;
-       DPRINTF_FDSET(rv->fdset, 0, "free");
+       struct svc_fdset *fds = v;
+       DPRINTF_FDSET(fds, "free");
 
-       free(rv->fdset);
-       free(rv);
+       free(fds->fdset);
+       free(fds);
 }
 
-static fd_set *
-svc_fdset_resize(int fd, fd_set **fdset, int *fdsize)
+static struct svc_fdset *
+svc_fdset_resize(int fd, struct svc_fdset *fds)
 {
-       if (*fdset && fd < *fdsize) {
-               DPRINTF_FDSET(*fdset, 0, "keeping %d < %d",
-                   fd, *fdsize);
-               return *fdset;
+       if (fds->fdset && fd < fds->fdsize) {
+               DPRINTF_FDSET(fds, "keeping %d < %d", fd, fds->fdsize);
+               return fds;
        }
 
        fd += FD_SETSIZE; 
-       if (fd == 517)
-               abort();
 
-       char *newfdset = realloc(*fdset, __NFD_BYTES(fd));
+       char *newfdset = realloc(fds->fdset, __NFD_BYTES(fd));
        if (newfdset == NULL)
                return NULL;
 
-       memset(newfdset + __NFD_BYTES(*fdsize), 0,
-           __NFD_BYTES(fd) - __NFD_BYTES(*fdsize));
+       memset(newfdset + __NFD_BYTES(fds->fdsize), 0,
+           __NFD_BYTES(fd) - __NFD_BYTES(fds->fdsize));
 
 
-       *fdset = (void *)newfdset;
-       DPRINTF_FDSET(*fdset, 0, "resize %d > %d", fd, *fdsize);
-       *fdsize = fd;
+       fds->fdset = (void *)newfdset;
+       DPRINTF_FDSET(fds, "resize %d > %d", fd, fds->fdsize);
+       fds->fdsize = fd;
 
-       COMPAT_UPDATE(*fdset);
-
-       return *fdset;
+       return fds;
 }
 
-static struct my_svc_fdset *
+static struct svc_fdset *
 svc_fdset_alloc(int fd)
 {
-       struct my_svc_fdset *rv;
+       struct svc_fdset *fds;
 
        if (fdsetkey == -1)
                thr_keycreate(&fdsetkey, svc_fdset_free);
 
-       if ((rv = thr_getspecific(fdsetkey)) == NULL) {
+       if ((fds = thr_getspecific(fdsetkey)) == NULL) {
 
-               rv = calloc(1, sizeof(*rv));
-               if (rv == NULL)
+               fds = calloc(1, sizeof(*fds));
+               if (fds == NULL)
                        return NULL;
 
-               (void)thr_setspecific(fdsetkey, rv);
+               (void)thr_setspecific(fdsetkey, fds);
 
-               if (svc_fdsize != 0) {
-                       rv->fdset = __svc_fdset;
-                       DPRINTF("switching to %p", rv->fdset);
-                       rv->fdmax = svc_maxfd;
-                       rv->fdsize = svc_fdsize;
-
-                       svc_fdsize = 0;
+               if (__svc_fdset.fdsize != 0) {
+                       *fds = __svc_fdset;
+                       DPRINTF("switching to %p", fds->fdset);
                } else {
-                       DPRINTF("first thread time %p", rv->fdset);
+                       DPRINTF("first thread time %p", fds->fdset);
                }
        } else {
-               DPRINTF("again for %p", rv->fdset);
-               if (fd < rv->fdsize)
-                       return rv;
+               DPRINTF("again for %p", fds->fdset);
+               if (fd < fds->fdsize)
+                       return fds;
        }
-       if (svc_fdset_resize(fd, &rv->fdset, &rv->fdsize) == NULL)
-               return NULL;
-       return rv;
+
+       return svc_fdset_resize(fd, fds);
 }
 
-static fd_set *
+static struct svc_fdset *
 svc_fdset_get_internal(int fd)
 {
-       struct my_svc_fdset *rv;
-
        if (!__isthreaded || fdsetkey == -2)
-               return svc_fdset_resize(fd, &__svc_fdset, &svc_fdsize);
+               return svc_fdset_resize(fd, &__svc_fdset);
 
-       rv = svc_fdset_alloc(fd);
-       if (rv == NULL)
-               return NULL;
-       return rv->fdset;
+       return svc_fdset_alloc(fd);
 }
 
 
@@ -197,104 +217,93 @@
 svc_fdset_zero(void)
 {
        DPRINTF("zero");
-       fd_set *fds = svc_fdset_get_internal(0);
-       int size = svc_fdset_getsize(0);
-       memset(fds, 0, __NFD_BYTES(size));
-       *svc_fdset_getmax() = 0;
-
-       COMPAT_UPDATE(fds);
-
+       struct svc_fdset *fds = svc_fdset_get_internal(0);
+       memset(fds->fdset, 0, fds->fdsize);
+       fds->fdmax = 0;
 }
 



Home | Main Index | Thread Index | Old Index