Repository: google/benchmark
File: src/sysinfo.cc (C++)

Code before:
#elif defined(BENCHMARK_OS_SOLARIS)
#define HOST_NAME_MAX MAXHOSTNAMELEN
#elif defined(BENCHMARK_OS_ZOS)
#define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
#else
#pragma message("HOST_NAME_MAX not defined. using 64")
#define HOST_NAME_MAX 64
#endif
#endif  // def HOST_NAME_MAX
  char hostname[HOST_NAME_MAX];
  int retVal = gethostname(hostname, HOST_NAME_MAX);
  if (retVal != 0) return std::string("");
  return std::string(hostname);
#endif  // Catch-all POSIX block.
}

int CheckNumCPUs(const int num_cpus) {
  if (num_cpus < 1) {
    PrintErrorAndDie(
        "Unable to extract number of CPUs.  If your platform uses "
        "/proc/cpuinfo, custom support may need to be added.");
  }
  return num_cpus;
}

int GetNumCPUs() {
#ifdef BENCHMARK_HAS_SYSCTL
  int num_cpu = -1;
  if (GetSysctl("hw.ncpu", &num_cpu)) return CheckNumCPUs(num_cpu);
  PrintErrorAndDie("Err: ", sterror(errno));
#elif defined(BENCHMARK_OS_WINDOWS)
  SYSTEM_INFO sysinfo;
  // Use memset as opposed to = {} to avoid GCC missing initializer false
  // positives.
  std::memset(&sysinfo, 0, sizeof(SYSTEM_INFO));
  GetSystemInfo(&sysinfo);
  return CheckNumCPUs(sysinfo.dwNumberOfProcessors);  // number of logical
                                                      // processors in the
                                                      // current group
#elif defined(BENCHMARK_OS_SOLARIS)
  // Returns -1 in case of a failure.
  long num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
  if (num_cpu < 0) {
    PrintErrorAndDie("sysconf(_SC_NPROCESSORS_ONLN) failed with error: ",
                     strerror(errno));
  }
  return CheckNumCPUs(static_cast<int>(num_cpu));
#elif defined(BENCHMARK_OS_QNX)
  return CheckNumCPUs(static_cast<int>(_syspage_ptr->num_cpu));
#elif defined(BENCHMARK_OS_QURT)
  qurt_sysenv_max_hthreads_t hardware_threads;

Review comment (refactor):
```
int GetNumCPUs() {
  int num_cpus = GetNumCPUsImpl();
  if (num_cpus < 1) {
    PrintErrorAndDie(
        "Unable to extract number of CPUs.  If your platform uses "
        "/proc/cpuinfo, custom support may need to be added.");
  }
  return num_cpus;
}

int GetNumCPUsImpl() {
```

Code after:
#elif defined(BENCHMARK_OS_SOLARIS)
#define HOST_NAME_MAX MAXHOSTNAMELEN
#elif defined(BENCHMARK_OS_ZOS)
#define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
#else
#pragma message("HOST_NAME_MAX not defined. using 64")
#define HOST_NAME_MAX 64
#endif
#endif  // def HOST_NAME_MAX
  char hostname[HOST_NAME_MAX];
  int retVal = gethostname(hostname, HOST_NAME_MAX);
  if (retVal != 0) return std::string("");
  return std::string(hostname);
#endif  // Catch-all POSIX block.
}

int GetNumCPUsImpl() {
#ifdef BENCHMARK_HAS_SYSCTL
  int num_cpu = -1;
  if (GetSysctl("hw.ncpu", &num_cpu)) return num_cpu;
  PrintErrorAndDie("Err: ", strerror(errno));
#elif defined(BENCHMARK_OS_WINDOWS)
  SYSTEM_INFO sysinfo;
  // Use memset as opposed to = {} to avoid GCC missing initializer false
  // positives.
  std::memset(&sysinfo, 0, sizeof(SYSTEM_INFO));
  GetSystemInfo(&sysinfo);
  return sysinfo.dwNumberOfProcessors;  // number of logical
                                        // processors in the current
                                        // group
#elif defined(BENCHMARK_OS_SOLARIS)
  // Returns -1 in case of a failure.
  long num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
  if (num_cpu < 0) {
    PrintErrorAndDie("sysconf(_SC_NPROCESSORS_ONLN) failed with error: ",
                     strerror(errno));
  }
  return (int)num_cpu;
#elif defined(BENCHMARK_OS_QNX)
  return static_cast<int>(_syspage_ptr->num_cpu);
#elif defined(BENCHMARK_OS_QURT)
  qurt_sysenv_max_hthreads_t hardware_threads;
  if (qurt_sysenv_get_max_hw_threads(&hardware_threads) != QURT_EOK) {
    hardware_threads.max_hthreads = 1;
  }
  return hardware_threads.max_hthreads;
#else
  int num_cpus = 0;
  int max_id = -1;
  std::ifstream f("/proc/cpuinfo");
  if (!f.is_open()) {