aboutsummaryrefslogtreecommitdiff
path: root/lib/Support/Unix
diff options
context:
space:
mode:
authorDaniel Dunbar <daniel@zuster.org>2012-05-05 16:36:24 +0000
committerDaniel Dunbar <daniel@zuster.org>2012-05-05 16:36:24 +0000
commit92bb514612a7d66a0695b257a3d8f0b9cebcf9d7 (patch)
tree2af77e8f2f55e2a76ac13377933c1431f70c6a6a /lib/Support/Unix
parent9c69e6ae698f44703725eec8ff27630b6b81f0e3 (diff)
[Support] Rewrite sys::fs::unique_file to not be stupid with /dev/urandom.
- Just use sys::Process::GetRandomNumber instead of having two poor implementations. - This is ~70 times (!) faster on my OS X machine. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@156238 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/Unix')
-rw-r--r--lib/Support/Unix/PathV2.inc24
1 files changed, 5 insertions, 19 deletions
diff --git a/lib/Support/Unix/PathV2.inc b/lib/Support/Unix/PathV2.inc
index 5f22f8e674..b0f7ca91ff 100644
--- a/lib/Support/Unix/PathV2.inc
+++ b/lib/Support/Unix/PathV2.inc
@@ -17,6 +17,7 @@
//===----------------------------------------------------------------------===//
#include "Unix.h"
+#include "llvm/Support/Process.h"
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
@@ -367,32 +368,17 @@ error_code unique_file(const Twine &model, int &result_fd,
// Replace '%' with random chars. From here on, DO NOT modify model. It may be
// needed if the randomly chosen path already exists.
- SmallString<128> RandomPath;
- RandomPath.reserve(Model.size() + 1);
- ::srand(::time(NULL));
+ SmallString<128> RandomPath = Model;
retry_random_path:
// This is opened here instead of above to make it easier to track when to
// close it. Collisions should be rare enough for the possible extra syscalls
// not to matter.
- FILE *RandomSource = ::fopen("/dev/urandom", "r");
- RandomPath.set_size(0);
- for (SmallVectorImpl<char>::const_iterator i = Model.begin(),
- e = Model.end(); i != e; ++i) {
- if (*i == '%') {
- char val = 0;
- if (RandomSource)
- val = fgetc(RandomSource);
- else
- val = ::rand();
- RandomPath.push_back("0123456789abcdef"[val & 15]);
- } else
- RandomPath.push_back(*i);
+ for (unsigned i = 0, e = Model.size(); i != e; ++i) {
+ if (Model[i] == '%')
+ RandomPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
}
- if (RandomSource)
- ::fclose(RandomSource);
-
// Try to open + create the file.
rety_open_create:
int RandomFD = ::open(RandomPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);