-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[io] use UUIDv4 in ROOT files #22035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,6 +124,7 @@ system clock catches up. | |
| #include "Bytes.h" | ||
| #include "TVirtualMutex.h" | ||
| #include "ThreadLocalStorage.h" | ||
| #include <cassert> | ||
| #include <cstring> | ||
| #include <cstdlib> | ||
| #ifdef R__WIN32 | ||
|
|
@@ -145,6 +146,29 @@ system clock catches up. | |
| #endif | ||
| #include <chrono> | ||
|
|
||
| //////////////////////////////////////////////////////////////////////////////// | ||
| /// Create a v4 UUID. | ||
|
|
||
| TUUID TUUID::UUIDv4() | ||
| { | ||
| TUUID uuid{TIndeterminedMarker()}; | ||
|
|
||
| // Ensure we can treat the memory starting at uuid.fTimeLow as an array of 16 octets | ||
| assert(&uuid.fNode[5] - reinterpret_cast<unsigned char *>(&uuid.fTimeLow) + 1 == 16); | ||
|
|
||
| R__ASSERT(gSystem); | ||
| const auto rv = gSystem->GetCryptoRandom(&uuid.fTimeLow, 16); | ||
| R__ASSERT(rv == 16); | ||
| // Fix up variant | ||
| uuid.fClockSeqHiAndReserved = (uuid.fClockSeqHiAndReserved & 0x3F) | (2 << 6); | ||
| // Fix up version | ||
| uuid.fTimeHiAndVersion = (uuid.fTimeHiAndVersion & 0x0FFF) | (4 << 12); | ||
|
|
||
| // TODO(jblomer): we do what the default constructor does but is this still used? Can it be deprecated? | ||
| uuid.fUUIDIndex = 1 << 30; | ||
|
|
||
| return uuid; | ||
| } | ||
|
|
||
| //////////////////////////////////////////////////////////////////////////////// | ||
| /// Create a UUID. | ||
|
|
@@ -569,7 +593,7 @@ void TUUID::Print() const | |
|
|
||
| const char *TUUID::AsString() const | ||
| { | ||
| static char uuid[40]; | ||
| TTHREAD_TLS(char) uuid[40]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't we use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what it resolves to. I can use |
||
|
|
||
| snprintf(uuid,40, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", | ||
| fTimeLow, fTimeMid, fTimeHiAndVersion, fClockSeqHiAndReserved, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #include "gtest/gtest.h" | ||
|
|
||
| #include "TUUID.h" | ||
|
|
||
| #include <set> | ||
| #include <string> | ||
|
|
||
| TEST(TUUID, UUIDv4) | ||
| { | ||
| std::set<TUUID> uuids; | ||
| for (int i = 0; i < 10000; ++i) { | ||
| uuids.insert(TUUID::UUIDv4()); | ||
| } | ||
| EXPECT_EQ(10000u, uuids.size()); | ||
|
|
||
| TUUID u; | ||
| EXPECT_EQ('1', u.AsString()[14]); | ||
| u = TUUID::UUIDv4(); | ||
| std::string str = u.AsString(); | ||
| EXPECT_EQ('4', str[14]); | ||
| EXPECT_TRUE(str[19] == '8' || str[19] == '9' || str[19] == 'a' || str[19] == 'b'); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.