utils: add make_random_uuid()

This commit is contained in:
Avi Kivity
2015-01-12 14:20:28 +02:00
parent 63055f0306
commit 904db75cbb
3 changed files with 37 additions and 0 deletions

View File

@@ -228,6 +228,7 @@ deps = {
'thrift/handler.cc',
'thrift/server.cc',
'utils/murmur_hash.cc',
'utils/uuid.cc',
'db/db.cc',
'io/io.cc',
'utils/utils.cc',

View File

@@ -42,4 +42,6 @@ public:
}
};
UUID make_random_uuid();
}

34
utils/uuid.cc Normal file
View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2015 Cloudius Systems, Ltd.
*/
#include "UUID.hh"
#include "net/byteorder.hh"
#include <random>
#include <boost/iterator/function_input_iterator.hpp>
namespace utils {
UUID
make_random_uuid() {
// FIXME: keep in userspace
static thread_local std::random_device urandom;
static thread_local std::uniform_int_distribution<uint8_t> dist(0, 255);
union {
uint8_t b[16];
struct {
uint64_t msb, lsb;
} w;
} v;
for (auto& b : v.b) {
b = dist(urandom);
}
v.b[6] &= 0x0f;
v.b[6] |= 0x40; // version 4
v.b[8] &= 0x3f;
v.b[8] |= 0x80; // IETF variant
return UUID(net::hton(v.w.msb), net::hton(v.w.lsb));
}
}