Whamcloud - gitweb
b=18751 Move prng.c to libcfs
[fs/lustre-release.git] / libcfs / libcfs / prng.c
similarity index 66%
rename from lustre/lvfs/prng.c
rename to libcfs/libcfs/prng.c
index c07730e..f1568f4 100644 (file)
@@ -33,7 +33,7 @@
  * This file is part of Lustre, http://www.lustre.org/
  * Lustre is a trademark of Sun Microsystems, Inc.
  *
- * lustre/lvfs/prng.c
+ * libcfs/libcfs/prng.c
  *
  * concatenation of following two 16-bit multiply with carry generators
  * x(n)=a*x(n-1)+carry mod 2^16 and y(n)=b*y(n-1)+carry mod 2^16,
 # define EXPORT_SYMTAB
 #endif
 
-#ifndef __KERNEL__
-#include <liblustre.h>
-#define cfs_get_random_bytes(val, size)     (*val) = 0
-#endif
-#include <obd_class.h>
-#if defined(HAVE_LINUX_RANDOM_H)
-#include <linux/random.h>
-#endif
+#include <libcfs/libcfs.h>
 
 /*
 From: George Marsaglia <geo@stat.fsu.edu>
@@ -78,32 +71,49 @@ Date: Tue, 30 Sep 1997 05:29:35 -0700
 
 static unsigned int seed_x = 521288629;
 static unsigned int seed_y = 362436069;
-unsigned int ll_rand(void)
-{
 
-       seed_x = RANDOM_CONST_A * (seed_x & 65535) + (seed_x >> 16);
-       seed_y = RANDOM_CONST_B * (seed_y & 65535) + (seed_y >> 16);
+/**
+ * cfs_rand - creates new seeds
+ *
+ * First it creates new seeds from the previous seeds. Then it generates a
+ * new psuedo random number for use.
+ *
+ * Returns a pseudo-random 32-bit integer
+ */
+unsigned int cfs_rand(void)
+{
+        seed_x = RANDOM_CONST_A * (seed_x & 65535) + (seed_x >> 16);
+        seed_y = RANDOM_CONST_B * (seed_y & 65535) + (seed_y >> 16);
 
-       return ((seed_x << 16) + (seed_y & 65535));
+        return ((seed_x << 16) + (seed_y & 65535));
 }
-EXPORT_SYMBOL(ll_rand);
+CFS_EXPORT_SYMBOL(cfs_rand);
 
-/* Note that if the input seeds are not completely random, then there is
- * a preferred location for the entropy in the two seeds, in order to avoid
- * the initial values from the PRNG to be the same each time.
+/**
+ * cfs_srand - sets the inital seed
+ * @seed1 : (seed_x) should have the most entropy in the low bits of the word
+ * @seed2 : (seed_y) should have the most entropy in the high bits of the word
  *
- * seed1 (seed_x) should have the most entropy in the low bits of the word
- * seed2 (seed_y) should have the most entropy in the high bits of the word */
-void ll_srand(unsigned int seed1, unsigned int seed2)
+ * Replaces the original seeds with new values. Used to generate a new pseudo
+ * random numbers.
+ */
+void cfs_srand(unsigned int seed1, unsigned int seed2)
 {
-       if (seed1)
-               seed_x = seed1; /* use default seeds if parameter is 0 */
-       if (seed2)
-               seed_y = seed2;
+        if (seed1)
+                seed_x = seed1; /* use default seeds if parameter is 0 */
+        if (seed2)
+                seed_y = seed2;
 }
-EXPORT_SYMBOL(ll_srand);
+CFS_EXPORT_SYMBOL(cfs_srand);
 
-void ll_get_random_bytes(void *buf, int size)
+/**
+ * cfs_get_random_bytes - generate a bunch of random numbers
+ * @buf : buffer to fill with random numbers
+ * @size: size of passed in buffer
+ *
+ * Fills a buffer with random bytes
+ */
+void cfs_get_random_bytes(void *buf, int size)
 {
         int *p = buf;
         int rem, tmp;
@@ -112,30 +122,24 @@ void ll_get_random_bytes(void *buf, int size)
 
         rem = min((int)((unsigned long)buf & (sizeof(int) - 1)), size);
         if (rem) {
-                cfs_get_random_bytes(&tmp, sizeof(tmp));
-                tmp ^= ll_rand();
+                cfs_get_random_bytes_prim(&tmp, sizeof(tmp));
+                tmp ^= cfs_rand();
                 memcpy(buf, &tmp, rem);
                 p = buf + rem;
                 size -= rem;
         }
 
         while (size >= sizeof(int)) {
-                cfs_get_random_bytes(&tmp, sizeof(tmp));
-                *p = ll_rand() ^ tmp;
+                cfs_get_random_bytes_prim(&tmp, sizeof(tmp));
+                *p = cfs_rand() ^ tmp;
                 size -= sizeof(int);
                 p++;
         }
         buf = p;
         if (size) {
-                cfs_get_random_bytes(&tmp, sizeof(tmp));
-                tmp ^= ll_rand();
+                cfs_get_random_bytes_prim(&tmp, sizeof(tmp));
+                tmp ^= cfs_rand();
                 memcpy(buf, &tmp, size);
         }
 }
-EXPORT_SYMBOL(ll_get_random_bytes); 
-
-void ll_generate_random_uuid(class_uuid_t uuid_out)
-{
-        ll_get_random_bytes(uuid_out, sizeof(class_uuid_t));
-}
-EXPORT_SYMBOL(ll_generate_random_uuid);
+CFS_EXPORT_SYMBOL(cfs_get_random_bytes);