Export of internal Abseil changes

--
f09549625d7417fca379ff68334379d281480608 by Martijn Vels <mvels@google.com>:

Fix GetNumCPUs() for WIN32 platforms

PiperOrigin-RevId: 368245360

--
e903c6690d0568f80356e03b07a895b031553977 by Abseil Team <absl-team@google.com>:

Optimize `Cord::InlineRep::InlineRep(Cord::InlineRep&& src)`.

PiperOrigin-RevId: 368198990
GitOrigin-RevId: f09549625d7417fca379ff68334379d281480608
Change-Id: I2d750c8c87f2804335a60ba8db949dedfaa462e2
pull/946/head
Abseil Team 4 years ago committed by Dino Radaković
parent b97a1ecda8
commit 25bc82d7ef
  1. 67
      absl/base/internal/sysinfo.cc
  2. 3
      absl/strings/cord.h

@ -61,9 +61,76 @@ namespace absl {
ABSL_NAMESPACE_BEGIN
namespace base_internal {
namespace {
#if defined(_WIN32)
// Returns number of bits set in `bitMask`
DWORD Win32CountSetBits(ULONG_PTR bitMask) {
for (DWORD bitSetCount = 0; ; ++bitSetCount) {
if (bitMask == 0) return bitSetCount;
bitMask &= bitMask - 1;
}
}
// Returns the number of logical CPUs using GetLogicalProcessorInformation(), or
// 0 if the number of processors is not available or can not be computed.
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getlogicalprocessorinformation
int Win32NumCPUs() {
#pragma comment(lib, "kernel32.lib")
using Info = SYSTEM_LOGICAL_PROCESSOR_INFORMATION;
DWORD info_size = sizeof(Info);
Info* info(static_cast<Info*>(malloc(info_size)));
if (info == nullptr) return 0;
bool success = GetLogicalProcessorInformation(info, &info_size);
if (!success && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
free(info);
info = static_cast<Info*>(malloc(info_size));
if (info == nullptr) return 0;
success = GetLogicalProcessorInformation(info, &info_size);
}
DWORD logicalProcessorCount = 0;
if (success) {
Info* ptr = info;
DWORD byteOffset = 0;
while (byteOffset + sizeof(Info) <= info_size) {
switch (ptr->Relationship) {
case RelationProcessorCore:
logicalProcessorCount += Win32CountSetBits(ptr->ProcessorMask);
break;
case RelationNumaNode:
case RelationCache:
case RelationProcessorPackage:
// Ignore other entries
break;
default:
// Ignore unknown entries
break;
}
byteOffset += sizeof(Info);
ptr++;
}
}
free(info);
return logicalProcessorCount;
}
#endif
} // namespace
static int GetNumCPUs() {
#if defined(__myriad2__)
return 1;
#elif defined(_WIN32)
const unsigned hardware_concurrency = Win32NumCPUs();
return hardware_concurrency ? hardware_concurrency : 1;
#else
// Other possibilities:
// - Read /sys/devices/system/cpu/online and use cpumask_parse()

@ -946,8 +946,7 @@ inline Cord::InlineRep::InlineRep(const Cord::InlineRep& src)
}
}
inline Cord::InlineRep::InlineRep(Cord::InlineRep&& src) {
data_ = src.data_;
inline Cord::InlineRep::InlineRep(Cord::InlineRep&& src) : data_(src.data_) {
src.ResetToEmpty();
}

Loading…
Cancel
Save