2.4.0test - uid_hash_find()

From: Dan Aloni (karrde@callisto.yi.org)
Date: Sun Oct 08 2000 - 17:10:55 EST


I've been touring around the kernel sources when I stumbled
across the uid_hash_find() function (kernel/user.c):

static inline struct user_struct *uid_hash_find(unsigned short uid, unsigned int hashent)
{
        struct user_struct *up, *next;
 
        next = uidhash[hashent];
        for (;;) {
                up = next;
                if (next) {
                        next = up->next;
                        if (up->uid != uid)
                                continue;
                        atomic_inc(&up->__count);
                 }
                break;
        }
        return up;
}

Now, it took me some time to figure out how this function
behaves, and I came into conclusion that the implementation
is not only an unreadable piece of code, but also inefficient.

Give one reason why we shouldn't use this:

static inline struct user_struct *uid_hash_find(unsigned short uid, unsigned int hashent)
{
        struct user_struct *up;
 
        for (up = uidhash[hashent]; up ; up = up->next)
                if (up->uid == uid)
                {
                        atomic_inc(&up->__count);
                        return up;
                 }

        return NULL;
}

[compiled, booted, and it works fine]

The patch:

--- linux/kernel/user.c Sun Aug 13 16:36:42 2000
+++ linux.vanilla/kernel/user.c Sun Oct 8 19:34:07 2000
@@ -53,20 +53,16 @@
 
 static inline struct user_struct *uid_hash_find(unsigned short uid, unsigned int hashent)
 {
- struct user_struct *up, *next;
+ struct user_struct *up;
 
- next = uidhash[hashent];
- for (;;) {
- up = next;
- if (next) {
- next = up->next;
- if (up->uid != uid)
- continue;
- atomic_inc(&up->__count);
+ for (up = uidhash[hashent]; up ; up = up->next)
+ if (up->uid == uid)
+ {
+ atomic_inc(&up->__count);
+ return up;
                 }
- break;
- }
- return up;
+
+ return NULL;
 }
 
 /*

-- 
Dan Aloni 
dax@karrde.org

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Sun Oct 15 2000 - 21:00:10 EST