Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / util / rand_xor.c
index de04bbc284f7a5e75fba95b284b0c74dc1734437..81b64f1ea71712a49338e5b77bf9ae166413e671 100644 (file)
  *
  */
 
-#if defined(__linux__)
-#include <sys/file.h>
+#include "detect_os.h"
+
+#if !DETECT_OS_WINDOWS
+#if defined(HAVE_GETRANDOM)
+#include <sys/random.h>
+#endif
 #include <unistd.h>
 #include <fcntl.h>
-#else
-#include <time.h>
 #endif
 
+#include <time.h>
+
 #include "rand_xor.h"
 
 /* Super fast random number generator.
@@ -38,7 +42,7 @@
  * to the public domain.
  */
 uint64_t
-rand_xorshift128plus(uint64_t *seed)
+rand_xorshift128plus(uint64_t seed[2])
 {
    uint64_t *s = seed;
 
@@ -52,35 +56,34 @@ rand_xorshift128plus(uint64_t *seed)
 }
 
 void
-s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed)
+s_rand_xorshift128plus(uint64_t seed[2], bool randomised_seed)
 {
-   if (!randomised_seed)
-      goto fixed_seed;
-
-#if defined(__linux__)
-   int fd = open("/dev/urandom", O_RDONLY);
-   if (fd < 0)
-      goto fixed_seed;
-
-   size_t seed_size = sizeof(uint64_t) * 2;
-   if (read(fd, seed, seed_size) != seed_size) {
-      close(fd);
-      goto fixed_seed;
+   if (!randomised_seed) {
+      /* Use a fixed seed */
+      seed[0] = 0x3bffb83978e24f88;
+      seed[1] = 0x9238d5d56c71cd35;
+      return;
    }
 
-   close(fd);
-   return;
-
-#else
-   seed[0] = 0x3bffb83978e24f88;
-   seed[1] = time(NULL);
+#if !DETECT_OS_WINDOWS
+   size_t seed_size = sizeof(uint64_t) * 2;
 
-   return;
+#if defined(HAVE_GETRANDOM)
+   ssize_t ret = getrandom(seed, seed_size, GRND_NONBLOCK);
+   if (ret == seed_size)
+      return;
 #endif
 
-fixed_seed:
+   int fd = open("/dev/urandom", O_RDONLY);
+   if (fd >= 0) {
+      if (read(fd, seed, seed_size) == seed_size) {
+         close(fd);
+         return;
+      }
+      close(fd);
+   }
+#endif
 
-   /* Fallback to a fixed seed */
    seed[0] = 0x3bffb83978e24f88;
-   seed[1] = 0x9238d5d56c71cd35;
+   seed[1] = time(NULL);
 }