Source-Changes-HG archive

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

[src/trunk]: src/lib/librt - KNF return



details:   https://anonhg.NetBSD.org/src/rev/83ea9b8e5d94
branches:  trunk
changeset: 449085:83ea9b8e5d94
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Feb 21 21:33:34 2019 +0000

description:
- KNF return
- be careful with errno, only set it when it is possibly set and not before
  a system call.
- factor out a common mask comparison.

diffstat:

 lib/librt/sem.c |  71 ++++++++++++++++++++++++++++++--------------------------
 1 files changed, 38 insertions(+), 33 deletions(-)

diffs (260 lines):

diff -r 1664c59c123a -r 83ea9b8e5d94 lib/librt/sem.c
--- a/lib/librt/sem.c   Thu Feb 21 15:41:56 2019 +0000
+++ b/lib/librt/sem.c   Thu Feb 21 21:33:34 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sem.c,v 1.8 2019/02/03 03:20:24 thorpej Exp $  */
+/*     $NetBSD: sem.c,v 1.9 2019/02/21 21:33:34 christos Exp $ */
 
 /*-
  * Copyright (c) 2003, 2019 The NetBSD Foundation, Inc.
@@ -59,7 +59,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: sem.c,v 1.8 2019/02/03 03:20:24 thorpej Exp $");
+__RCSID("$NetBSD: sem.c,v 1.9 2019/02/21 21:33:34 christos Exp $");
 
 #ifndef __LIBPTHREAD_SOURCE__
 /*
@@ -93,6 +93,7 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <stdint.h>
 #include <semaphore.h>
 #include <stdarg.h>
 
@@ -104,8 +105,10 @@
 #define        SEM_MAGIC               0x90af0421U
 #define        SEM_MAGIC_NAMED         (SEM_MAGIC ^ SEM_NAMED)
 
-#define        SEM_IS_KSEMID(k)        ((((intptr_t)(k)) & KSEM_MARKER_MASK)   \
+#define        SEMID_IS_KSEMID(id)     (((uint32_t)(id) & KSEM_MARKER_MASK)    \
                                                == KSEM_PSHARED_MARKER)
+#define        SEM_IS_KSEMID(k)        SEMID_IS_KSEMID((intptr_t)(k))  
+                       
 
 #define        SEM_IS_UNNAMED(k)       (SEM_IS_KSEMID(k) ||                    \
                                 (k)->ksem_magic == SEM_MAGIC)
@@ -177,16 +180,16 @@
        sem_t sem;
 
        if (value > SEM_VALUE_MAX)
-               return (EINVAL);
+               return EINVAL;
 
        if ((sem = malloc(sizeof(struct _sem_st))) == NULL)
-               return (ENOSPC);
+               return ENOSPC;
 
        sem->ksem_magic = magic;
        sem->ksem_semid = semid;
  
        *semp = sem;
-       return (0);
+       return 0;
 }
 
 /* ARGSUSED */
@@ -197,7 +200,7 @@
        int error;
 
        if (_ksem_init(value, &semid) == -1)
-               return (-1);
+               return -1;
 
        /*
         * pshared anonymous semaphores are treated a little differently.
@@ -213,22 +216,22 @@
         * non-pshared semaphores.
         */
        if (pshared) {
-               if ((semid & KSEM_MARKER_MASK) != KSEM_PSHARED_MARKER) {
+               if (!SEMID_IS_KSEMID(semid)) {
                        _ksem_destroy(semid);
                        errno = ENOTSUP;
-                       return (-1);
+                       return -1;
                }
                *sem = (sem_t)semid;
-               return (0);
+               return 0;
        }
 
        if ((error = sem_alloc(value, semid, SEM_MAGIC, sem)) != 0) {
                _ksem_destroy(semid);
                errno = error;
-               return (-1);
+               return -1;
        }
 
-       return (0);
+       return 0;
 }
 
 int
@@ -239,7 +242,7 @@
 #ifdef ERRORCHECK
        if (sem == NULL || *sem == NULL || !SEM_MAGIC_OK(*sem)) {
                errno = EINVAL;
-               return (-1);
+               return -1;
        }
 #endif
 
@@ -248,13 +251,14 @@
        } else {
                if (SEM_IS_NAMED(*sem)) {
                        errno = EINVAL;
-                       return (-1);
+                       return -1;
                }
 
                error = _ksem_destroy((*sem)->ksem_semid);
                save_errno = errno;
                sem_free(*sem);
-               errno = save_errno;
+               if (error == -1)
+                       errno = save_errno;
        }
 
        return error;
@@ -285,7 +289,7 @@
         * we'll just merge duplicate IDs into our list.
         */
        if (_ksem_open(name, oflag, mode, value, &semid) == -1)
-               return (SEM_FAILED);
+               return SEM_FAILED;
 
        /*
         * Search for a duplicate ID, we must return the same sem_t *
@@ -295,7 +299,7 @@
        LIST_FOREACH(s, &named_sems, ksem_list) {
                if (s->ksem_semid == semid) {
                        UNLOCK_NAMED_SEMS();
-                       return (s->ksem_identity);
+                       return s->ksem_identity;
                }
        }
 
@@ -310,7 +314,7 @@
        UNLOCK_NAMED_SEMS();
        (*sem)->ksem_identity = sem;
 
-       return (sem);
+       return sem;
 
  bad:
        UNLOCK_NAMED_SEMS();
@@ -321,7 +325,7 @@
                free(sem);
        }
        errno = error;
-       return (SEM_FAILED);
+       return SEM_FAILED;
 }
 
 int
@@ -332,13 +336,13 @@
 #ifdef ERRORCHECK
        if (sem == NULL || *sem == NULL || !SEM_MAGIC_OK(*sem)) {
                errno = EINVAL;
-               return (-1);
+               return -1;
        }
 #endif
 
        if (!SEM_IS_NAMED(*sem)) {
                errno = EINVAL;
-               return (-1);
+               return -1;
        }
 
        LOCK_NAMED_SEMS();
@@ -348,7 +352,8 @@
        UNLOCK_NAMED_SEMS();
        sem_free(*sem);
        free(sem);
-       errno = save_errno;
+       if (error == -1)
+               errno = save_errno;
        return error;
 }
 
@@ -356,7 +361,7 @@
 sem_unlink(const char *name)
 {
 
-       return (_ksem_unlink(name));
+       return _ksem_unlink(name);
 }
 
 int
@@ -366,11 +371,11 @@
 #ifdef ERRORCHECK
        if (sem == NULL || *sem == NULL || !SEM_MAGIC_OK(*sem)) {
                errno = EINVAL;
-               return (-1);
+               return -1;
        }
 #endif
 
-       return (_ksem_wait(sem_to_semid(sem)));
+       return _ksem_wait(sem_to_semid(sem));
 }
 
 int
@@ -380,11 +385,11 @@
 #ifdef ERRORCHECK
        if (sem == NULL || *sem == NULL || !SEM_MAGIC_OK(*sem)) {
                errno = EINVAL;
-               return (-1);
+               return -1;
        }
 #endif
 
-       return (_ksem_timedwait(sem_to_semid(sem), abstime));
+       return _ksem_timedwait(sem_to_semid(sem), abstime);
 }
 
 int
@@ -394,11 +399,11 @@
 #ifdef ERRORCHECK
        if (sem == NULL || *sem == NULL || !SEM_MAGIC_OK(*sem)) {
                errno = EINVAL;
-               return (-1);
+               return -1;
        }
 #endif
 
-       return (_ksem_trywait(sem_to_semid(sem)));
+       return _ksem_trywait(sem_to_semid(sem));
 }
 
 int
@@ -408,11 +413,11 @@
 #ifdef ERRORCHECK
        if (sem == NULL || *sem == NULL || !SEM_MAGIC_OK(*sem)) {
                errno = EINVAL;
-               return (-1);
+               return -1;
        }
 #endif
 
-       return (_ksem_post(sem_to_semid(sem)));
+       return _ksem_post(sem_to_semid(sem));
 }
 
 int
@@ -422,9 +427,9 @@
 #ifdef ERRORCHECK
        if (sem == NULL || *sem == NULL || !SEM_MAGIC_OK(*sem)) {
                errno = EINVAL;
-               return (-1);
+               return -1;
        }
 #endif
 
-       return (_ksem_getvalue(sem_to_semid(sem), sval));
+       return _ksem_getvalue(sem_to_semid(sem), sval);
 }



Home | Main Index | Thread Index | Old Index