String HHVM_FUNCTION(uniqid, const String& prefix /* = null_string */, bool more_entropy /* = false */) { if (!more_entropy) { Transport *transport = g_context->getTransport(); if (transport) { transport->incuSleepTime(1); } usleep(1); } struct timeval tv; gettimeofday((struct timeval *)&tv, NULL); int sec = (int)tv.tv_sec; int usec = (int)(tv.tv_usec % 0x100000); String uniqid(prefix.size() + 64, ReserveString); auto ptr = uniqid.mutableData(); // StringData::capacity() returns the buffer size without the null // terminator. snprintf expects a the buffer capacity including room // for the null terminator, writes the null termintor, and returns // the full length not counting the null terminator. auto capacity = uniqid.capacity() + 1; int64_t len; if (more_entropy) { len = snprintf(ptr, capacity, "%s%08x%05x%.8F", prefix.c_str(), sec, usec, math_combined_lcg() * 10); } else { len = snprintf(ptr, capacity, "%s%08x%05x", prefix.c_str(), sec, usec); } uniqid.setSize(len); return uniqid; }
String HHVM_FUNCTION(uniqid, const String& prefix /* = null_string */, bool more_entropy /* = false */) { if (!more_entropy) { Transport *transport = g_context->getTransport(); if (transport) { transport->incuSleepTime(1); } usleep(1); } struct timeval tv; gettimeofday((struct timeval *)&tv, NULL); int sec = (int)tv.tv_sec; int usec = (int)(tv.tv_usec % 0x100000); String uniqid(prefix.size() + 64, ReserveString); auto ptr = uniqid.bufferSlice().ptr; auto capacity = uniqid.get()->capacity(); int64_t len; if (more_entropy) { len = snprintf(ptr, capacity, "%s%08x%05x%.8F", prefix.c_str(), sec, usec, math_combined_lcg() * 10); } else { len = snprintf(ptr, capacity, "%s%08x%05x", prefix.c_str(), sec, usec); } uniqid.setSize(len); return uniqid; }
String f_uniqid(const String& prefix /* = null_string */, bool more_entropy /* = false */) { if (!more_entropy) { Transport *transport = g_context->getTransport(); if (transport) { transport->incuSleepTime(1); } usleep(1); } struct timeval tv; gettimeofday((struct timeval *)&tv, NULL); int sec = (int)tv.tv_sec; int usec = (int)(tv.tv_usec % 0x100000); char uniqid[256]; if (more_entropy) { snprintf(uniqid, sizeof(uniqid), "%s%08x%05x%.8F", prefix.c_str(), sec, usec, math_combined_lcg() * 10); } else { snprintf(uniqid, sizeof(uniqid), "%s%08x%05x", prefix.c_str(), sec, usec); } return String(uniqid, CopyString); }
void HHVM_FUNCTION(usleep, int micro_seconds) { IOStatusHelper io("usleep"); Transport *transport = g_context->getTransport(); if (transport) { transport->incuSleepTime(micro_seconds); } usleep(micro_seconds); }