Whamcloud - gitweb
LU-3963 Revert bitops changes
[fs/lustre-release.git] / libcfs / include / libcfs / winnt / portals_utils.h
index ec80692..39ab41f 100644 (file)
@@ -1,34 +1,46 @@
-/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=4:tabstop=4:
+/*
+ * GPL HEADER START
  *
- *  Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
  *
- * Basic library routines.
+ * GPL HEADER END
+ */
+/*
+ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Copyright (c) 2012, Intel Corporation.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * libcfs/include/libcfs/winnt/portals_utils.h
  *
+ * Basic library routines.
  */
 
 #ifndef __LIBCFS_WINNT_PORTALS_UTILS_H__
 #define __LIBCFS_WINNT_PORTALS_UTILS_H__
 
-#ifndef __LIBCFS_PORTALS_UTILS_H__
-#error Do not #include this file directly. #include <libcfs/portals_utils.h> instead
-#endif
-
 #ifndef cfs_is_flag_set
 #define cfs_is_flag_set(x,f) (((x)&(f))==(f))
 #endif
 #define cfs_clear_flag(x,f)  ((x) &= ~(f))
 #endif
 
-
-static inline __u32 __do_div(__u32 * n, __u32 b) 
+static inline __u32 do_div64(__u64 * n, __u64 b) 
 {
-    __u32   mod;
+    __u64   mod;
 
     mod = *n % b;
     *n  = *n / b;
-    return mod;
+    return (__u32)mod;
 } 
 
-#define do_div(n,base)  __do_div((__u32 *)&(n), (__u32) (base))
-
+#define do_div(n, b) do_div64(&(n), (__u64)b)
 #ifdef __KERNEL__
 
 #include <stdlib.h>
 #include <libcfs/winnt/winnt-types.h>
 
 char * strsep(char **s, const char *ct);
-static inline size_t strnlen(const char * s, size_t count) {
-    size_t len = 0;
-    while(len < count && s[len++]);
-    return len;
-}
-char * ul2dstr(ulong_ptr address, char *buf, int len);
+char * ul2dstr(ulong_ptr_t address, char *buf, int len);
 
 #define simple_strtol(a1, a2, a3)               strtol(a1, a2, a3)
 #define simple_strtoll(a1, a2, a3)              (__s64)strtoull(a1, a2, a3)
@@ -72,25 +77,155 @@ char * ul2dstr(ulong_ptr address, char *buf, int len);
 
 unsigned long simple_strtoul(const char *cp,char **endp, unsigned int base);
 
+static inline int set_bit(int nr, void * addr)
+{
+    (((volatile ULONG *) addr)[nr >> 5]) |= (1UL << (nr & 31));
+    return *((int *) addr);
+}
+
 static inline int test_bit(int nr, void * addr)
 {
-    return ((1UL << (nr & 31)) & (((volatile ULONG *) addr)[nr >> 5])) != 0;
+    return (int)(((1UL << (nr & 31)) & (((volatile ULONG *) addr)[nr >> 5])) != 0);
 }
 
-static inline void clear_bit(int nr, void * addr)
+static inline int clear_bit(int nr, void * addr)
 {
     (((volatile ULONG *) addr)[nr >> 5]) &= (~(1UL << (nr & 31)));
+    return *((int *) addr);
 }
 
+static inline int test_and_set_bit(int nr, volatile void *addr)
+{
+    int rc;
+    unsigned char  mask;
+    volatile unsigned char *ADDR = addr;
+
+    ADDR += nr >> 3;
+    mask = 1 << (nr & 0x07);
+    rc = ((mask & *ADDR) != 0);
+    *ADDR |= mask;
 
-static inline void set_bit(int nr, void * addr)
+    return rc;
+}
+
+#define ext2_set_bit(nr, addr)         (set_bit(nr, addr), 0)
+#define ext2_clear_bit(nr, addr)       (clear_bit(nr, addr), 0)
+#define ext2_test_bit(nr, addr)                test_bit(nr, addr)
+
+static inline int ffs(int x)
 {
-    (((volatile ULONG *) addr)[nr >> 5]) |= (1UL << (nr & 31));
+        int r = 1;
+
+        if (!x)
+                return 0;
+        if (!(x & 0xffff)) {
+                x >>= 16;
+                r += 16;
+        }
+        if (!(x & 0xff)) {
+                x >>= 8;
+                r += 8;
+        }
+        if (!(x & 0xf)) {
+                x >>= 4;
+                r += 4;
+        }
+        if (!(x & 3)) {
+                x >>= 2;
+                r += 2;
+        }
+        if (!(x & 1)) {
+                x >>= 1;
+                r += 1;
+        }
+        return r;
+}
+
+static inline unsigned long __cfs_ffs(unsigned long word)
+{
+        int num = 0;
+
+#if BITS_PER_LONG == 64
+        if ((word & 0xffffffff) == 0) {
+                num += 32;
+                word >>= 32;
+        }
+#endif
+        if ((word & 0xffff) == 0) {
+                num += 16;
+                word >>= 16;
+        }
+        if ((word & 0xff) == 0) {
+                num += 8;
+                word >>= 8;
+        }
+        if ((word & 0xf) == 0) {
+                num += 4;
+                word >>= 4;
+        }
+        if ((word & 0x3) == 0) {
+                num += 2;
+                word >>= 2;
+        }
+        if ((word & 0x1) == 0)
+                num += 1;
+        return num;
+}
+
+/**
+ * fls - find last (most-significant) bit set
+ * @x: the word to search
+ *
+ * This is defined the same way as ffs.
+ * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
+ */
+static inline
+int fls(int x)
+{
+        int r = 32;
+
+        if (!x)
+                return 0;
+        if (!(x & 0xffff0000u)) {
+                x <<= 16;
+                r -= 16;
+        }
+        if (!(x & 0xff000000u)) {
+                x <<= 8;
+                r -= 8;
+        }
+        if (!(x & 0xf0000000u)) {
+                x <<= 4;
+                r -= 4;
+        }
+        if (!(x & 0xc0000000u)) {
+                x <<= 2;
+                r -= 2;
+        }
+        if (!(x & 0x80000000u)) {
+                x <<= 1;
+                r -= 1;
+        }
+        return r;
+}
+
+static inline unsigned find_first_bit(const unsigned long *addr,
+                                          unsigned size)
+{
+        unsigned x = 0;
+
+        while (x < size) {
+                unsigned long val = *addr++;
+                if (val)
+                        return __cfs_ffs(val) + x;
+                x += (sizeof(*addr)<<3);
+        }
+        return x;
 }
 
 static inline void read_random(char *buf, int len)
 {
-    ULONG   Seed = (ULONG) buf;
+    ULONG   Seed = (ULONG)(ULONG_PTR) buf;
     Seed = RtlRandom(&Seed);
     while (len >0) {
         if (len > sizeof(ULONG)) {
@@ -104,6 +239,7 @@ static inline void read_random(char *buf, int len)
         } 
     }
 }
+
 #define get_random_bytes(buf, len)  read_random(buf, len)
 
 /* do NOT use function or expression as parameters ... */
@@ -129,18 +265,24 @@ static inline void read_random(char *buf, int len)
        ((unsigned char *)&addr)[1],    \
        ((unsigned char *)&addr)[0]
 
-static int copy_from_user(void *to, void *from, int c) 
+static int copy_from_user(void *to, void *from, int c)
 {
-    memcpy(to, from, c);
-    return 0;
+       memcpy(to, from, c);
+       return 0;
 }
 
-static int copy_to_user(void *to, void *from, int c) 
+static int copy_to_user(void *to, const void *from, int c)
 {
-    memcpy(to, from, c);
-    return 0;
+       memcpy(to, from, c);
+       return 0;
 }
 
+static unsigned long
+clear_user(void __user *to, unsigned long n)
+{
+    memset(to, 0, n);
+       return n;
+}
 
 #define put_user(x, ptr)        \
 (                               \
@@ -155,14 +297,49 @@ static int copy_to_user(void *to, void *from, int c)
     0                           \
 )
 
-#define num_physpages                  (64 * 1024)
+#define totalram_pages               (64 * 1024)
+#define NUM_CACHEPAGES              totalram_pages
 
-#define snprintf  _snprintf
-#define vsnprintf _vsnprintf
+#else
 
+#define unlink _unlink 
+#define close  _close
+#define open   _open
+#define fdopen _fdopen
+#define strdup _strdup
+#define fileno _fileno
+#define isattry _isattry
+#define stat    _stat
 
 #endif /* !__KERNEL__ */
 
 int cfs_error_code(NTSTATUS);
 
+static inline int vsnprintf(char *buf, size_t cnt,
+                            const char *fmt, va_list va)
+{
+    int rc;
+
+#ifdef TRUE /* using msvcrt from windkk 3790 */
+    rc = _vsnprintf(buf, cnt, fmt, va);
+#else
+    rc = _vsnprintf_s(buf, cnt, cnt, fmt, va);
+#endif
+    if (rc == -1)
+        return cnt;
+    return rc;
+}
+
+static inline int snprintf(char *buf, size_t cnt, 
+                           const char *fmt, ...)
+{
+    int         rc;
+    va_list     va;
+
+    va_start(va, fmt);
+    rc = vsnprintf(buf, cnt, fmt, va);
+    va_end(va);
+    return rc;
+}
+
 #endif