From 7d7b3d61564563dc775a7c51538e017b6be93354 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 2 Jun 2014 19:27:50 -0400 Subject: [PATCH] Stringify ArmCPUDetect.cpp. --- Source/Core/Common/ArmCPUDetect.cpp | 187 +++++++++++++++------------- 1 file changed, 97 insertions(+), 90 deletions(-) diff --git a/Source/Core/Common/ArmCPUDetect.cpp b/Source/Core/Common/ArmCPUDetect.cpp index 15391c3fb7..158df9c4c5 100644 --- a/Source/Core/Common/ArmCPUDetect.cpp +++ b/Source/Core/Common/ArmCPUDetect.cpp @@ -2,9 +2,12 @@ // Licensed under GPLv2 // Refer to the license.txt file included. +#include +#include +#include + #include "Common/Common.h" #include "Common/CPUDetect.h" -#include "Common/FileUtil.h" #include "Common/StringUtil.h" // Only Linux platforms have /proc/cpuinfo @@ -13,51 +16,47 @@ const char procfile[] = "/proc/cpuinfo"; std::string GetCPUString() { - const char marker[] = "Hardware\t: "; - char *cpu_string = nullptr; - // Count the number of processor lines in /proc/cpuinfo - char buf[1024]; + const std::string marker = "Hardware\t: "; + std::string cpu_string = "Unknown"; - File::IOFile file(procfile, "r"); - auto const fp = file.GetHandle(); - if (!fp) - return 0; + std::string line; + std::ifstream file(procfile); - while (fgets(buf, sizeof(buf), fp)) + if (!file) + return cpu_string; + + while (std::getline(file, line)) { - if (strncmp(buf, marker, sizeof(marker) - 1)) - continue; - cpu_string = buf + sizeof(marker) - 1; - cpu_string = strndup(cpu_string, strlen(cpu_string) - 1); // Strip the newline - break; + if (line.find(marker) != std::string::npos) + { + cpu_string = line.substr(marker.length()); + cpu_string.pop_back(); // Drop the new-line character + break; + } } - if (cpu_string) - return std::string(cpu_string); - else - return ""; + + return cpu_string; } unsigned char GetCPUImplementer() { - const char marker[] = "CPU implementer\t: "; - char *implementer_string = nullptr; + const std::string marker = "CPU implementer\t: "; unsigned char implementer = 0; - char buf[1024]; - File::IOFile file(procfile, "r"); - auto const fp = file.GetHandle(); - if (!fp) + std::string line; + std::ifstream file(procfile); + + if (!file) return 0; - while (fgets(buf, sizeof(buf), fp)) + while (std::getline(file, line)) { - if (strncmp(buf, marker, sizeof(marker) - 1)) - continue; - implementer_string = buf + sizeof(marker) - 1; - implementer_string = strndup(implementer_string, strlen(implementer_string) - 1); // Strip the newline - sscanf(implementer_string, "0x%02hhx", &implementer); - free(implementer_string); - break; + if (line.find(marker) != std::string::npos) + { + line = line.substr(marker.length()); + sscanf(line.c_str(), "0x%02hhx", &implementer); + break; + } } return implementer; @@ -65,51 +64,50 @@ unsigned char GetCPUImplementer() unsigned short GetCPUPart() { - const char marker[] = "CPU part\t: "; - char *part_string = nullptr; + const std::string marker = "CPU part\t: "; unsigned short part = 0; - char buf[1024]; - File::IOFile file(procfile, "r"); - auto const fp = file.GetHandle(); - if (!fp) + std::string line; + std::ifstream file(procfile); + + if (!file) return 0; - while (fgets(buf, sizeof(buf), fp)) + while (std::getline(file, line)) { - if (strncmp(buf, marker, sizeof(marker) - 1)) - continue; - part_string = buf + sizeof(marker) - 1; - part_string = strndup(part_string, strlen(part_string) - 1); // Strip the newline - sscanf(part_string, "0x%03hx", &part); - free(part_string); - break; + if (line.find(marker) != std::string::npos) + { + line = line.substr(marker.length()); + sscanf(line.c_str(), "0x%03hx", &part); + break; + } } return part; } -bool CheckCPUFeature(const char *feature) +bool CheckCPUFeature(const std::string& feature) { - const char marker[] = "Features\t: "; - char buf[1024]; + const std::string marker = "Features\t: "; - File::IOFile file(procfile, "r"); - auto const fp = file.GetHandle(); - if (!fp) + std::string line; + std::ifstream file(procfile); + + if (!file) return 0; - while (fgets(buf, sizeof(buf), fp)) + while (std::getline(file, line)) { - if (strncmp(buf, marker, sizeof(marker) - 1)) - continue; - char *featurestring = buf + sizeof(marker) - 1; - char *token = strtok(featurestring, " "); - while (token != nullptr) + if (line.find(marker) != std::string::npos) { - if (strstr(token, feature)) - return true; - token = strtok(nullptr, " "); + std::stringstream line_stream(line); + std::string token; + + while (std::getline(line_stream, token, ' ')) + { + if (token == feature) + return true; + } } } @@ -124,20 +122,21 @@ int GetCoreCount() #elif defined(BLACKBERRY) || defined(IOS) return 2; #else - const char marker[] = "processor\t: "; + const std::string marker = "processor\t: "; int cores = 0; - char buf[1024]; - File::IOFile file(procfile, "r"); - auto const fp = file.GetHandle(); - if (!fp) + std::string line; + std::ifstream file(procfile); + + if (!file) return 0; - while (fgets(buf, sizeof(buf), fp)) + while (std::getline(file, line)) { - if (strncmp(buf, marker, sizeof(marker) - 1)) - continue; - ++cores; + if (line.find(marker) != std::string::npos) + { + ++cores; + } } return cores; @@ -146,7 +145,8 @@ int GetCoreCount() CPUInfo cpu_info; -CPUInfo::CPUInfo() { +CPUInfo::CPUInfo() +{ Detect(); } @@ -174,28 +174,35 @@ void CPUInfo::Detect() bool isVFP4 = false; #ifdef IOS isVFP3 = true; - // Check for swift arch (VFP4`) - #ifdef __ARM_ARCH_7S__ - isVFP4 = true; - #endif // #ifdef __ARM_ARCH_7S__ + // Check for swift arch (VFP4`) + #ifdef __ARM_ARCH_7S__ + isVFP4 = true; + #endif // #ifdef __ARM_ARCH_7S__ #elif defined(BLACKBERRY) isVFP3 = true; const char cpuInfoPath[] = "/pps/services/hw_info/inventory"; - const char marker[] = "Processor_Name::"; - const char qcCPU[] = "MSM"; - char buf[1024]; - FILE* fp; - if (fp = fopen(cpuInfoPath, "r")) + const std::string marker = "Processor_Name::"; + const std::string qcCPU = "MSM"; + + std::string line; + std::ifstream file(cpuInfoPath); + + if (file) { - while (fgets(buf, sizeof(buf), fp)) + while (std::getline(file, line)) { - if (strncmp(buf, marker, sizeof(marker) - 1)) - continue; - if (strncmp(buf + sizeof(marker) - 1, qcCPU, sizeof(qcCPU) - 1) == 0) - isVFP4 = true; - break; + if (line.find(marker) != std::string::npos) + { + std::string first_three_chars = line.substr(marker.length(), qcCPU.length()); + + if (first_three_chars == qcCPU) + { + isVFP4 = true; + } + + break; + } } - fclose(fp); } #endif // Hardcode this for now @@ -236,8 +243,8 @@ void CPUInfo::Detect() bFP = CheckCPUFeature("fp"); bASIMD = CheckCPUFeature("asimd"); #endif -// On android, we build a separate library for ARMv7 so this is fine. -// TODO: Check for ARMv7 on other platforms. + // On android, we build a separate library for ARMv7 so this is fine. + // TODO: Check for ARMv7 on other platforms. #if defined(__ARM_ARCH_7A__) bArmV7 = true; #else