From a1ffdcbe70233215031645af2cb9345950efffc5 Mon Sep 17 00:00:00 2001 From: Arvid Gerstmann Date: Thu, 26 Apr 2018 10:31:03 +0200 Subject: [PATCH 1/4] Explicitly namespace every extern identifier --- include/internal/filesystem.h | 6 +- include/internal/hwcaps.h | 2 +- include/internal/linux_features_aggregator.h | 4 +- include/internal/string_view.h | 28 ++++----- src/cpuinfo_aarch64.c | 28 ++++----- src/cpuinfo_arm.c | 46 +++++++------- src/cpuinfo_mips.c | 12 ++-- src/filesystem.c | 12 ++-- src/hwcaps.c | 2 +- src/linux_features_aggregator.c | 6 +- src/stack_line_reader.c | 12 ++-- src/string_view.c | 66 ++++++++++---------- 12 files changed, 112 insertions(+), 112 deletions(-) diff --git a/include/internal/filesystem.h b/include/internal/filesystem.h index 2c862e5..3378881 100644 --- a/include/internal/filesystem.h +++ b/include/internal/filesystem.h @@ -24,14 +24,14 @@ CPU_FEATURES_START_CPP_NAMESPACE // Same as linux "open(filename, O_RDONLY)", retries automatically on EINTR. -int OpenFile(const char* filename); +int CpuFeatures_OpenFile(const char* filename); // Same as linux "read(file_descriptor, buffer, buffer_size)", retries // automatically on EINTR. -int ReadFile(int file_descriptor, void* buffer, size_t buffer_size); +int CpuFeatures_ReadFile(int file_descriptor, void* buffer, size_t buffer_size); // Same as linux "close(file_descriptor)". -void CloseFile(int file_descriptor); +void CpuFeatures_CloseFile(int file_descriptor); CPU_FEATURES_END_CPP_NAMESPACE diff --git a/include/internal/hwcaps.h b/include/internal/hwcaps.h index 5c5223b..abbc718 100644 --- a/include/internal/hwcaps.h +++ b/include/internal/hwcaps.h @@ -66,7 +66,7 @@ typedef struct { uint32_t hwcaps2; } HardwareCapabilities; -HardwareCapabilities GetHardwareCapabilities(void); +HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void); CPU_FEATURES_END_CPP_NAMESPACE diff --git a/include/internal/linux_features_aggregator.h b/include/internal/linux_features_aggregator.h index 1fb1566..335c5d1 100644 --- a/include/internal/linux_features_aggregator.h +++ b/include/internal/linux_features_aggregator.h @@ -43,13 +43,13 @@ typedef struct { // For every config, looks into flags_line for the presence of the // corresponding proc_cpuinfo_flag, calls `set_bit` accordingly. // Note: features is a pointer to the underlying Feature struct. -void SetFromFlags(const size_t configs_size, const CapabilityConfig* configs, +void CpuFeatures_SetFromFlags(const size_t configs_size, const CapabilityConfig* configs, const StringView flags_line, void* const features); // For every config, looks into hwcaps for the presence of the feature. Calls // `set_bit` with true if the hardware capability is found. // Note: features is a pointer to the underlying Feature struct. -void OverrideFromHwCaps(const size_t configs_size, +void CpuFeatures_OverrideFromHwCaps(const size_t configs_size, const CapabilityConfig* configs, const HardwareCapabilities hwcaps, void* const features); diff --git a/include/internal/string_view.h b/include/internal/string_view.h index dc6e0b5..ebfcb37 100644 --- a/include/internal/string_view.h +++ b/include/internal/string_view.h @@ -46,54 +46,54 @@ static inline StringView view(const char* str, const size_t size) { static inline StringView str(const char* str) { return view(str, strlen(str)); } // Returns the index of the first occurrence of c in view or -1 if not found. -int IndexOfChar(const StringView view, char c); +int CpuFeatures_StringView_IndexOfChar(const StringView view, char c); // Returns the index of the first occurrence of sub_view in view or -1 if not // found. -int IndexOf(const StringView view, const StringView sub_view); +int CpuFeatures_StringView_IndexOf(const StringView view, const StringView sub_view); // Returns whether a is equal to b (same content). -bool IsEquals(const StringView a, const StringView b); +bool CpuFeatures_StringView_IsEquals(const StringView a, const StringView b); // Returns whether a starts with b. -bool StartsWith(const StringView a, const StringView b); +bool CpuFeatures_StringView_StartsWith(const StringView a, const StringView b); // Removes count characters from the beginning of view or kEmptyStringView if // count if greater than view.size. -StringView PopFront(const StringView view, size_t count); +StringView CpuFeatures_StringView_PopFront(const StringView view, size_t count); // Removes count characters from the end of view or kEmptyStringView if count if // greater than view.size. -StringView PopBack(const StringView str_view, size_t count); +StringView CpuFeatures_StringView_PopBack(const StringView str_view, size_t count); // Keeps the count first characters of view or view if count if greater than // view.size. -StringView KeepFront(const StringView view, size_t count); +StringView CpuFeatures_StringView_KeepFront(const StringView view, size_t count); // Retrieves the first character of view. If view is empty the behavior is // undefined. -char Front(const StringView view); +char CpuFeatures_StringView_Front(const StringView view); // Retrieves the last character of view. If view is empty the behavior is // undefined. -char Back(const StringView view); +char CpuFeatures_StringView_Back(const StringView view); // Removes leading and tailing space characters. -StringView TrimWhitespace(StringView view); +StringView CpuFeatures_StringView_TrimWhitespace(StringView view); // Convert StringView to positive integer. e.g. "42", "0x2a". // Returns -1 on error. -int ParsePositiveNumber(const StringView view); +int CpuFeatures_StringView_ParsePositiveNumber(const StringView view); // Copies src StringView to dst buffer. -void CopyString(const StringView src, char* dst, size_t dst_size); +void CpuFeatures_StringView_CopyString(const StringView src, char* dst, size_t dst_size); // Checks if line contains the specified whitespace separated word. -bool HasWord(const StringView line, const char* const word); +bool CpuFeatures_StringView_HasWord(const StringView line, const char* const word); // Get key/value from line. key and value are separated by ": ". // key and value are cleaned up from leading and trailing whitespaces. -bool GetAttributeKeyValue(const StringView line, StringView* key, +bool CpuFeatures_StringView_GetAttributeKeyValue(const StringView line, StringView* key, StringView* value); CPU_FEATURES_END_CPP_NAMESPACE diff --git a/src/cpuinfo_aarch64.c b/src/cpuinfo_aarch64.c index aad971e..1819efb 100644 --- a/src/cpuinfo_aarch64.c +++ b/src/cpuinfo_aarch64.c @@ -46,24 +46,24 @@ static bool HandleAarch64Line(const LineResult result, Aarch64Info* const info) { StringView line = result.line; StringView key, value; - if (GetAttributeKeyValue(line, &key, &value)) { - if (IsEquals(key, str("Features"))) { - SetFromFlags(kConfigsSize, kConfigs, value, &info->features); - } else if (IsEquals(key, str("CPU implementer"))) { - info->implementer = ParsePositiveNumber(value); - } else if (IsEquals(key, str("CPU variant"))) { - info->variant = ParsePositiveNumber(value); - } else if (IsEquals(key, str("CPU part"))) { - info->part = ParsePositiveNumber(value); - } else if (IsEquals(key, str("CPU revision"))) { - info->revision = ParsePositiveNumber(value); + if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) { + if (CpuFeatures_StringView_IsEquals(key, str("Features"))) { + CpuFeatures_SetFromFlags(kConfigsSize, kConfigs, value, &info->features); + } else if (CpuFeatures_StringView_IsEquals(key, str("CPU implementer"))) { + info->implementer = CpuFeatures_StringView_ParsePositiveNumber(value); + } else if (CpuFeatures_StringView_IsEquals(key, str("CPU variant"))) { + info->variant = CpuFeatures_StringView_ParsePositiveNumber(value); + } else if (CpuFeatures_StringView_IsEquals(key, str("CPU part"))) { + info->part = CpuFeatures_StringView_ParsePositiveNumber(value); + } else if (CpuFeatures_StringView_IsEquals(key, str("CPU revision"))) { + info->revision = CpuFeatures_StringView_ParsePositiveNumber(value); } } return !result.eof; } static void FillProcCpuInfoData(Aarch64Info* const info) { - const int fd = OpenFile("/proc/cpuinfo"); + const int fd = CpuFeatures_OpenFile("/proc/cpuinfo"); if (fd >= 0) { StackLineReader reader; StackLineReader_Initialize(&reader, fd); @@ -72,7 +72,7 @@ static void FillProcCpuInfoData(Aarch64Info* const info) { break; } } - CloseFile(fd); + CpuFeatures_CloseFile(fd); } } @@ -85,7 +85,7 @@ Aarch64Info GetAarch64Info(void) { Aarch64Info info = kEmptyAarch64Info; FillProcCpuInfoData(&info); - OverrideFromHwCaps(kConfigsSize, kConfigs, GetHardwareCapabilities(), + CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs, CpuFeatures_GetHardwareCapabilities(), &info.features); return info; diff --git a/src/cpuinfo_arm.c b/src/cpuinfo_arm.c index 8db8d08..d218101 100644 --- a/src/cpuinfo_arm.c +++ b/src/cpuinfo_arm.c @@ -62,8 +62,8 @@ typedef struct { static int IndexOfNonDigit(StringView str) { size_t index = 0; - while (str.size && isdigit(Front(str))) { - str = PopFront(str, 1); + while (str.size && isdigit(CpuFeatures_StringView_Front(str))) { + str = CpuFeatures_StringView_PopFront(str, 1); ++index; } return index; @@ -73,26 +73,26 @@ static bool HandleArmLine(const LineResult result, ArmInfo* const info, ProcCpuInfoData* const proc_info) { StringView line = result.line; StringView key, value; - if (GetAttributeKeyValue(line, &key, &value)) { - if (IsEquals(key, str("Features"))) { - SetFromFlags(kConfigsSize, kConfigs, value, &info->features); - } else if (IsEquals(key, str("CPU implementer"))) { - info->implementer = ParsePositiveNumber(value); - } else if (IsEquals(key, str("CPU variant"))) { - info->variant = ParsePositiveNumber(value); - } else if (IsEquals(key, str("CPU part"))) { - info->part = ParsePositiveNumber(value); - } else if (IsEquals(key, str("CPU revision"))) { - info->revision = ParsePositiveNumber(value); - } else if (IsEquals(key, str("CPU architecture"))) { + if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) { + if (CpuFeatures_StringView_IsEquals(key, str("Features"))) { + CpuFeatures_SetFromFlags(kConfigsSize, kConfigs, value, &info->features); + } else if (CpuFeatures_StringView_IsEquals(key, str("CPU implementer"))) { + info->implementer = CpuFeatures_StringView_ParsePositiveNumber(value); + } else if (CpuFeatures_StringView_IsEquals(key, str("CPU variant"))) { + info->variant = CpuFeatures_StringView_ParsePositiveNumber(value); + } else if (CpuFeatures_StringView_IsEquals(key, str("CPU part"))) { + info->part = CpuFeatures_StringView_ParsePositiveNumber(value); + } else if (CpuFeatures_StringView_IsEquals(key, str("CPU revision"))) { + info->revision = CpuFeatures_StringView_ParsePositiveNumber(value); + } else if (CpuFeatures_StringView_IsEquals(key, str("CPU architecture"))) { // CPU architecture is a number that may be followed by letters. e.g. // "6TEJ", "7". - const StringView digits = KeepFront(value, IndexOfNonDigit(value)); - info->architecture = ParsePositiveNumber(digits); - } else if (IsEquals(key, str("Processor"))) { - proc_info->processor_reports_armv6 = IndexOf(value, str("(v6l)")) >= 0; - } else if (IsEquals(key, str("Hardware"))) { - proc_info->hardware_reports_goldfish = IsEquals(value, str("Goldfish")); + const StringView digits = CpuFeatures_StringView_KeepFront(value, IndexOfNonDigit(value)); + info->architecture = CpuFeatures_StringView_ParsePositiveNumber(digits); + } else if (CpuFeatures_StringView_IsEquals(key, str("Processor"))) { + proc_info->processor_reports_armv6 = CpuFeatures_StringView_IndexOf(value, str("(v6l)")) >= 0; + } else if (CpuFeatures_StringView_IsEquals(key, str("Hardware"))) { + proc_info->hardware_reports_goldfish = CpuFeatures_StringView_IsEquals(value, str("Goldfish")); } } return !result.eof; @@ -148,7 +148,7 @@ static void FixErrors(ArmInfo* const info, static void FillProcCpuInfoData(ArmInfo* const info, ProcCpuInfoData* proc_cpu_info_data) { - const int fd = OpenFile("/proc/cpuinfo"); + const int fd = CpuFeatures_OpenFile("/proc/cpuinfo"); if (fd >= 0) { StackLineReader reader; StackLineReader_Initialize(&reader, fd); @@ -158,7 +158,7 @@ static void FillProcCpuInfoData(ArmInfo* const info, break; } } - CloseFile(fd); + CpuFeatures_CloseFile(fd); } } @@ -174,7 +174,7 @@ ArmInfo GetArmInfo(void) { ProcCpuInfoData proc_cpu_info_data = kEmptyProcCpuInfoData; FillProcCpuInfoData(&info, &proc_cpu_info_data); - OverrideFromHwCaps(kConfigsSize, kConfigs, GetHardwareCapabilities(), + CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs, CpuFeatures_GetHardwareCapabilities(), &info.features); FixErrors(&info, &proc_cpu_info_data); diff --git a/src/cpuinfo_mips.c b/src/cpuinfo_mips.c index 3c6a4fb..b10f309 100644 --- a/src/cpuinfo_mips.c +++ b/src/cpuinfo_mips.c @@ -32,16 +32,16 @@ static bool HandleMipsLine(const LineResult result, MipsFeatures* const features) { StringView key, value; // See tests for an example. - if (GetAttributeKeyValue(result.line, &key, &value)) { - if (IsEquals(key, str("ASEs implemented"))) { - SetFromFlags(kConfigsSize, kConfigs, value, features); + if (CpuFeatures_StringView_GetAttributeKeyValue(result.line, &key, &value)) { + if (CpuFeatures_StringView_IsEquals(key, str("ASEs implemented"))) { + CpuFeatures_SetFromFlags(kConfigsSize, kConfigs, value, features); } } return !result.eof; } static void FillProcCpuInfoData(MipsFeatures* const features) { - const int fd = OpenFile("/proc/cpuinfo"); + const int fd = CpuFeatures_OpenFile("/proc/cpuinfo"); if (fd >= 0) { StackLineReader reader; StackLineReader_Initialize(&reader, fd); @@ -50,7 +50,7 @@ static void FillProcCpuInfoData(MipsFeatures* const features) { break; } } - CloseFile(fd); + CpuFeatures_CloseFile(fd); } } @@ -63,7 +63,7 @@ MipsInfo GetMipsInfo(void) { MipsInfo info = kEmptyMipsInfo; FillProcCpuInfoData(&info.features); - OverrideFromHwCaps(kConfigsSize, kConfigs, GetHardwareCapabilities(), + CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs, CpuFeatures_GetHardwareCapabilities(), &info.features); return info; } diff --git a/src/filesystem.c b/src/filesystem.c index 5049354..003bf6d 100644 --- a/src/filesystem.c +++ b/src/filesystem.c @@ -21,18 +21,18 @@ #if defined(_MSC_VER) #include -int OpenFile(const char* filename) { return _open(filename, _O_RDONLY); } +int CpuFeatures_OpenFile(const char* filename) { return _open(filename, _O_RDONLY); } -void CloseFile(int file_descriptor) { _close(file_descriptor); } +void CpuFeatures_CloseFile(int file_descriptor) { _close(file_descriptor); } -int ReadFile(int file_descriptor, void* buffer, size_t buffer_size) { +int CpuFeatures_ReadFile(int file_descriptor, void* buffer, size_t buffer_size) { return _read(file_descriptor, buffer, buffer_size); } #else #include -int OpenFile(const char* filename) { +int CpuFeatures_OpenFile(const char* filename) { int result; do { result = open(filename, O_RDONLY); @@ -40,9 +40,9 @@ int OpenFile(const char* filename) { return result; } -void CloseFile(int file_descriptor) { close(file_descriptor); } +void CpuFeatures_CloseFile(int file_descriptor) { close(file_descriptor); } -int ReadFile(int file_descriptor, void* buffer, size_t buffer_size) { +int CpuFeatures_ReadFile(int file_descriptor, void* buffer, size_t buffer_size) { int result; do { result = read(file_descriptor, buffer, buffer_size); diff --git a/src/hwcaps.c b/src/hwcaps.c index d511bab..2bcac97 100644 --- a/src/hwcaps.c +++ b/src/hwcaps.c @@ -159,7 +159,7 @@ HardwareCapabilities GetHardwareCapabilities(void) { //////////////////////////////////////////////////////////////////////////////// const HardwareCapabilities kEmptyHardwareCapabilities; -HardwareCapabilities GetHardwareCapabilities(void) { +HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void) { return kEmptyHardwareCapabilities; } #endif diff --git a/src/linux_features_aggregator.c b/src/linux_features_aggregator.c index 6383347..c5544b8 100644 --- a/src/linux_features_aggregator.c +++ b/src/linux_features_aggregator.c @@ -15,12 +15,12 @@ #include "internal/linux_features_aggregator.h" #include "internal/string_view.h" -void SetFromFlags(const size_t configs_size, const CapabilityConfig* configs, +void CpuFeatures_SetFromFlags(const size_t configs_size, const CapabilityConfig* configs, const StringView flags_line, void* const features) { size_t i = 0; for (; i < configs_size; ++i) { const CapabilityConfig config = configs[i]; - config.set_bit(features, HasWord(flags_line, config.proc_cpuinfo_flag)); + config.set_bit(features, CpuFeatures_StringView_HasWord(flags_line, config.proc_cpuinfo_flag)); } } @@ -34,7 +34,7 @@ static bool IsHwCapsSet(const HardwareCapabilities hwcaps_mask, IsSet(hwcaps_mask.hwcaps2, hwcaps.hwcaps2); } -void OverrideFromHwCaps(const size_t configs_size, +void CpuFeatures_OverrideFromHwCaps(const size_t configs_size, const CapabilityConfig* configs, const HardwareCapabilities hwcaps, void* const features) { diff --git a/src/stack_line_reader.c b/src/stack_line_reader.c index 7f1d65b..6af7844 100644 --- a/src/stack_line_reader.c +++ b/src/stack_line_reader.c @@ -29,7 +29,7 @@ void StackLineReader_Initialize(StackLineReader* reader, int fd) { // Replaces the content of buffer with bytes from the file. static int LoadFullBuffer(StackLineReader* reader) { const int read = - ReadFile(reader->fd, reader->buffer, STACK_LINE_READER_BUFFER_SIZE); + CpuFeatures_ReadFile(reader->fd, reader->buffer, STACK_LINE_READER_BUFFER_SIZE); assert(read >= 0); reader->view.ptr = reader->buffer; reader->view.size = read; @@ -40,7 +40,7 @@ static int LoadFullBuffer(StackLineReader* reader) { static int LoadMore(StackLineReader* reader) { char* const ptr = reader->buffer + reader->view.size; const size_t size_to_read = STACK_LINE_READER_BUFFER_SIZE - reader->view.size; - const int read = ReadFile(reader->fd, ptr, size_to_read); + const int read = CpuFeatures_ReadFile(reader->fd, ptr, size_to_read); assert(read >= 0); assert(read <= (int)size_to_read); reader->view.size += read; @@ -48,7 +48,7 @@ static int LoadMore(StackLineReader* reader) { } static int IndexOfEol(StackLineReader* reader) { - return IndexOfChar(reader->view, '\n'); + return CpuFeatures_StringView_IndexOfChar(reader->view, '\n'); } // Relocate buffer's pending bytes at the beginning of the array and fills the @@ -71,7 +71,7 @@ static void SkipToNextLine(StackLineReader* reader) { } else { const int eol_index = IndexOfEol(reader); if (eol_index >= 0) { - reader->view = PopFront(reader->view, eol_index + 1); + reader->view = CpuFeatures_StringView_PopFront(reader->view, eol_index + 1); break; } } @@ -120,8 +120,8 @@ LineResult StackLineReader_NextLine(StackLineReader* reader) { return CreateTruncatedLineResult(reader->view); } { - StringView line = KeepFront(reader->view, eol_index); - reader->view = PopFront(reader->view, eol_index + 1); + StringView line = CpuFeatures_StringView_KeepFront(reader->view, eol_index); + reader->view = CpuFeatures_StringView_PopFront(reader->view, eol_index + 1); return CreateValidLineResult(line); } } diff --git a/src/string_view.c b/src/string_view.c index 9aae6e1..e6d100c 100644 --- a/src/string_view.c +++ b/src/string_view.c @@ -18,7 +18,7 @@ #include #include -int IndexOfChar(const StringView view, char c) { +int CpuFeatures_StringView_IndexOfChar(const StringView view, char c) { if (view.ptr && view.size) { const char* const found = (const char*)memchr(view.ptr, c, view.size); if (found) { @@ -28,67 +28,67 @@ int IndexOfChar(const StringView view, char c) { return -1; } -int IndexOf(const StringView view, const StringView sub_view) { +int CpuFeatures_StringView_IndexOf(const StringView view, const StringView sub_view) { if (sub_view.size) { StringView remainder = view; while (remainder.size >= sub_view.size) { - const int found_index = IndexOfChar(remainder, sub_view.ptr[0]); + const int found_index = CpuFeatures_StringView_IndexOfChar(remainder, sub_view.ptr[0]); if (found_index < 0) break; - remainder = PopFront(remainder, found_index); - if (StartsWith(remainder, sub_view)) { + remainder = CpuFeatures_StringView_PopFront(remainder, found_index); + if (CpuFeatures_StringView_StartsWith(remainder, sub_view)) { return remainder.ptr - view.ptr; } - remainder = PopFront(remainder, 1); + remainder = CpuFeatures_StringView_PopFront(remainder, 1); } } return -1; } -bool IsEquals(const StringView a, const StringView b) { +bool CpuFeatures_StringView_IsEquals(const StringView a, const StringView b) { if (a.size == b.size) { return a.ptr == b.ptr || memcmp(a.ptr, b.ptr, b.size) == 0; } return false; } -bool StartsWith(const StringView a, const StringView b) { +bool CpuFeatures_StringView_StartsWith(const StringView a, const StringView b) { return a.ptr && b.ptr && b.size && a.size >= b.size ? memcmp(a.ptr, b.ptr, b.size) == 0 : false; } -StringView PopFront(const StringView str_view, size_t count) { +StringView CpuFeatures_StringView_PopFront(const StringView str_view, size_t count) { if (count > str_view.size) { return kEmptyStringView; } return view(str_view.ptr + count, str_view.size - count); } -StringView PopBack(const StringView str_view, size_t count) { +StringView CpuFeatures_StringView_PopBack(const StringView str_view, size_t count) { if (count > str_view.size) { return kEmptyStringView; } return view(str_view.ptr, str_view.size - count); } -StringView KeepFront(const StringView str_view, size_t count) { +StringView CpuFeatures_StringView_KeepFront(const StringView str_view, size_t count) { return count <= str_view.size ? view(str_view.ptr, count) : str_view; } -char Front(const StringView view) { +char CpuFeatures_StringView_Front(const StringView view) { assert(view.size); assert(view.ptr); return view.ptr[0]; } -char Back(const StringView view) { +char CpuFeatures_StringView_Back(const StringView view) { assert(view.size); return view.ptr[view.size - 1]; } -StringView TrimWhitespace(StringView view) { - while (view.size && isspace(Front(view))) view = PopFront(view, 1); - while (view.size && isspace(Back(view))) view = PopBack(view, 1); +StringView CpuFeatures_StringView_TrimWhitespace(StringView view) { + while (view.size && isspace(CpuFeatures_StringView_Front(view))) view = CpuFeatures_StringView_PopFront(view, 1); + while (view.size && isspace(CpuFeatures_StringView_Back(view))) view = CpuFeatures_StringView_PopBack(view, 1); return view; } @@ -103,19 +103,19 @@ static int HexValue(const char c) { static int ParsePositiveNumberWithBase(const StringView view, int base) { int result = 0; StringView remainder = view; - for (; remainder.size; remainder = PopFront(remainder, 1)) { - const int value = HexValue(Front(remainder)); + for (; remainder.size; remainder = CpuFeatures_StringView_PopFront(remainder, 1)) { + const int value = HexValue(CpuFeatures_StringView_Front(remainder)); if (value < 0 || value >= base) return -1; result = (result * base) + value; } return result; } -int ParsePositiveNumber(const StringView view) { +int CpuFeatures_StringView_ParsePositiveNumber(const StringView view) { if (view.size) { const StringView hex_prefix = str("0x"); - if (StartsWith(view, hex_prefix)) { - const StringView span_no_prefix = PopFront(view, hex_prefix.size); + if (CpuFeatures_StringView_StartsWith(view, hex_prefix)) { + const StringView span_no_prefix = CpuFeatures_StringView_PopFront(view, hex_prefix.size); return ParsePositiveNumberWithBase(span_no_prefix, 16); } return ParsePositiveNumberWithBase(view, 10); @@ -123,7 +123,7 @@ int ParsePositiveNumber(const StringView view) { return -1; } -void CopyString(const StringView src, char* dst, size_t dst_size) { +void CpuFeatures_StringView_CopyString(const StringView src, char* dst, size_t dst_size) { if (dst_size > 0) { const size_t max_copy_size = dst_size - 1; const size_t copy_size = @@ -133,31 +133,31 @@ void CopyString(const StringView src, char* dst, size_t dst_size) { } } -bool HasWord(const StringView line, const char* const word_str) { +bool CpuFeatures_StringView_HasWord(const StringView line, const char* const word_str) { const StringView word = str(word_str); StringView remainder = line; for (;;) { - const int index_of_word = IndexOf(remainder, word); + const int index_of_word = CpuFeatures_StringView_IndexOf(remainder, word); if (index_of_word < 0) { return false; } else { - const StringView before = KeepFront(line, index_of_word); - const StringView after = PopFront(line, index_of_word + word.size); - const bool valid_before = before.size == 0 || Back(before) == ' '; - const bool valid_after = after.size == 0 || Front(after) == ' '; + const StringView before = CpuFeatures_StringView_KeepFront(line, index_of_word); + const StringView after = CpuFeatures_StringView_PopFront(line, index_of_word + word.size); + const bool valid_before = before.size == 0 || CpuFeatures_StringView_Back(before) == ' '; + const bool valid_after = after.size == 0 || CpuFeatures_StringView_Front(after) == ' '; if (valid_before && valid_after) return true; - remainder = PopFront(remainder, index_of_word + word.size); + remainder = CpuFeatures_StringView_PopFront(remainder, index_of_word + word.size); } } return false; } -bool GetAttributeKeyValue(const StringView line, StringView* key, +bool CpuFeatures_StringView_GetAttributeKeyValue(const StringView line, StringView* key, StringView* value) { const StringView sep = str(": "); - const int index_of_separator = IndexOf(line, sep); + const int index_of_separator = CpuFeatures_StringView_IndexOf(line, sep); if (index_of_separator < 0) return false; - *value = TrimWhitespace(PopFront(line, index_of_separator + sep.size)); - *key = TrimWhitespace(KeepFront(line, index_of_separator)); + *value = CpuFeatures_StringView_TrimWhitespace(CpuFeatures_StringView_PopFront(line, index_of_separator + sep.size)); + *key = CpuFeatures_StringView_TrimWhitespace(CpuFeatures_StringView_KeepFront(line, index_of_separator)); return true; } From fd4839032c2747e6d6d9a6e6069535bf284c038f Mon Sep 17 00:00:00 2001 From: Arvid Gerstmann Date: Thu, 3 May 2018 17:07:07 +0200 Subject: [PATCH 2/4] Fix tests & fix missing GetHardwareCapabilities() change --- src/hwcaps.c | 2 +- test/filesystem_for_testing.cc | 6 +- test/hwcaps_for_testing.cc | 2 +- test/linux_features_aggregator_test.cc | 10 +-- test/stack_line_reader_test.cc | 2 +- test/string_view_test.cc | 100 ++++++++++++------------- 6 files changed, 61 insertions(+), 61 deletions(-) diff --git a/src/hwcaps.c b/src/hwcaps.c index 2bcac97..c1ac6e0 100644 --- a/src/hwcaps.c +++ b/src/hwcaps.c @@ -145,7 +145,7 @@ static uint32_t GetHardwareCapabilitiesFor(uint32_t type) { return hwcaps; } -HardwareCapabilities GetHardwareCapabilities(void) { +HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void) { HardwareCapabilities capabilities; capabilities.hwcaps = GetHardwareCapabilitiesFor(AT_HWCAP); capabilities.hwcaps2 = GetHardwareCapabilitiesFor(AT_HWCAP2); diff --git a/test/filesystem_for_testing.cc b/test/filesystem_for_testing.cc index 886d510..7b05a88 100644 --- a/test/filesystem_for_testing.cc +++ b/test/filesystem_for_testing.cc @@ -81,7 +81,7 @@ FakeFilesystem& GetEmptyFilesystem() { return *kFilesystem; } -extern "C" int OpenFile(const char* filename) { +extern "C" int CpuFeatures_OpenFile(const char* filename) { auto* const file = kFilesystem->FindFileOrNull(filename); if (file) { file->Open(); @@ -90,11 +90,11 @@ extern "C" int OpenFile(const char* filename) { return -1; } -extern "C" void CloseFile(int file_descriptor) { +extern "C" void CpuFeatures_CloseFile(int file_descriptor) { kFilesystem->FindFileOrDie(file_descriptor)->Close(); } -extern "C" int ReadFile(int file_descriptor, void* buf, size_t count) { +extern "C" int CpuFeatures_ReadFile(int file_descriptor, void* buf, size_t count) { return kFilesystem->FindFileOrDie(file_descriptor) ->Read(file_descriptor, buf, count); } diff --git a/test/hwcaps_for_testing.cc b/test/hwcaps_for_testing.cc index f7e4729..0e78813 100644 --- a/test/hwcaps_for_testing.cc +++ b/test/hwcaps_for_testing.cc @@ -25,7 +25,7 @@ void SetHardwareCapabilities(uint32_t hwcaps, uint32_t hwcaps2) { g_hardware_capabilities->hwcaps2 = hwcaps2; } -HardwareCapabilities GetHardwareCapabilities(void) { +HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void) { return *g_hardware_capabilities; } diff --git a/test/linux_features_aggregator_test.cc b/test/linux_features_aggregator_test.cc index 8410f63..7205d46 100644 --- a/test/linux_features_aggregator_test.cc +++ b/test/linux_features_aggregator_test.cc @@ -42,7 +42,7 @@ class LinuxFeatureAggregatorTest : public testing::Test { TEST_F(LinuxFeatureAggregatorTest, FromFlagsEmpty) { Features features; - SetFromFlags(kConfigs.size(), kConfigs.data(), str(""), &features); + CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str(""), &features); EXPECT_FALSE(features.a); EXPECT_FALSE(features.b); EXPECT_FALSE(features.c); @@ -50,7 +50,7 @@ TEST_F(LinuxFeatureAggregatorTest, FromFlagsEmpty) { TEST_F(LinuxFeatureAggregatorTest, FromFlagsAllSet) { Features features; - SetFromFlags(kConfigs.size(), kConfigs.data(), str("a c b"), &features); + CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a c b"), &features); EXPECT_TRUE(features.a); EXPECT_TRUE(features.b); EXPECT_TRUE(features.c); @@ -58,7 +58,7 @@ TEST_F(LinuxFeatureAggregatorTest, FromFlagsAllSet) { TEST_F(LinuxFeatureAggregatorTest, FromFlagsOnlyA) { Features features; - SetFromFlags(kConfigs.size(), kConfigs.data(), str("a"), &features); + CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a"), &features); EXPECT_TRUE(features.a); EXPECT_FALSE(features.b); EXPECT_FALSE(features.c); @@ -69,7 +69,7 @@ TEST_F(LinuxFeatureAggregatorTest, FromHwcapsNone) { capability.hwcaps = 0; // matches none capability.hwcaps2 = 0; // matches none Features features; - OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability, &features); + CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability, &features); EXPECT_FALSE(features.a); EXPECT_FALSE(features.b); EXPECT_FALSE(features.c); @@ -80,7 +80,7 @@ TEST_F(LinuxFeatureAggregatorTest, FromHwcapsSet) { capability.hwcaps = 0b0010; // matches b but not a capability.hwcaps2 = 0b1111; // matches c Features features; - OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability, &features); + CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability, &features); EXPECT_FALSE(features.a); EXPECT_TRUE(features.b); EXPECT_TRUE(features.c); diff --git a/test/stack_line_reader_test.cc b/test/stack_line_reader_test.cc index d255a9b..c8f9691 100644 --- a/test/stack_line_reader_test.cc +++ b/test/stack_line_reader_test.cc @@ -20,7 +20,7 @@ namespace cpu_features { bool operator==(const StringView& a, const StringView& b) { - return IsEquals(a, b); + return CpuFeatures_StringView_IsEquals(a, b); } namespace { diff --git a/test/string_view_test.cc b/test/string_view_test.cc index f9fa3da..a80c6ff 100644 --- a/test/string_view_test.cc +++ b/test/string_view_test.cc @@ -19,7 +19,7 @@ namespace cpu_features { bool operator==(const StringView& a, const StringView& b) { - return IsEquals(a, b); + return CpuFeatures_StringView_IsEquals(a, b); } namespace { @@ -35,95 +35,95 @@ TEST(StringViewTest, Build) { EXPECT_EQ(view.size, 4); } -TEST(StringViewTest, IndexOfChar) { +TEST(StringViewTest, CpuFeatures_StringView_IndexOfChar) { // Found. - EXPECT_EQ(IndexOfChar(str("test"), 'e'), 1); + EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("test"), 'e'), 1); // Not found. - EXPECT_EQ(IndexOfChar(str("test"), 'z'), -1); + EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(str("test"), 'z'), -1); // Empty. - EXPECT_EQ(IndexOfChar(kEmptyStringView, 'z'), -1); + EXPECT_EQ(CpuFeatures_StringView_IndexOfChar(kEmptyStringView, 'z'), -1); } -TEST(StringViewTest, IndexOf) { +TEST(StringViewTest, CpuFeatures_StringView_IndexOf) { // Found. - EXPECT_EQ(IndexOf(str("test"), str("es")), 1); + EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("test"), str("es")), 1); // Not found. - EXPECT_EQ(IndexOf(str("test"), str("aa")), -1); + EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("test"), str("aa")), -1); // Empty. - EXPECT_EQ(IndexOf(kEmptyStringView, str("aa")), -1); - EXPECT_EQ(IndexOf(str("aa"), kEmptyStringView), -1); + EXPECT_EQ(CpuFeatures_StringView_IndexOf(kEmptyStringView, str("aa")), -1); + EXPECT_EQ(CpuFeatures_StringView_IndexOf(str("aa"), kEmptyStringView), -1); } -TEST(StringViewTest, StartsWith) { - EXPECT_TRUE(StartsWith(str("test"), str("te"))); - EXPECT_FALSE(StartsWith(str("test"), str(""))); - EXPECT_FALSE(StartsWith(str("test"), kEmptyStringView)); - EXPECT_FALSE(StartsWith(kEmptyStringView, str("test"))); +TEST(StringViewTest, CpuFeatures_StringView_StartsWith) { + EXPECT_TRUE(CpuFeatures_StringView_StartsWith(str("test"), str("te"))); + EXPECT_FALSE(CpuFeatures_StringView_StartsWith(str("test"), str(""))); + EXPECT_FALSE(CpuFeatures_StringView_StartsWith(str("test"), kEmptyStringView)); + EXPECT_FALSE(CpuFeatures_StringView_StartsWith(kEmptyStringView, str("test"))); } -TEST(StringViewTest, IsEquals) { - EXPECT_TRUE(IsEquals(kEmptyStringView, kEmptyStringView)); - EXPECT_TRUE(IsEquals(kEmptyStringView, str(""))); - EXPECT_TRUE(IsEquals(str(""), kEmptyStringView)); - EXPECT_TRUE(IsEquals(str("a"), str("a"))); - EXPECT_FALSE(IsEquals(str("a"), str("b"))); - EXPECT_FALSE(IsEquals(str("a"), kEmptyStringView)); - EXPECT_FALSE(IsEquals(kEmptyStringView, str("a"))); +TEST(StringViewTest, CpuFeatures_StringView_IsEquals) { + EXPECT_TRUE(CpuFeatures_StringView_IsEquals(kEmptyStringView, kEmptyStringView)); + EXPECT_TRUE(CpuFeatures_StringView_IsEquals(kEmptyStringView, str(""))); + EXPECT_TRUE(CpuFeatures_StringView_IsEquals(str(""), kEmptyStringView)); + EXPECT_TRUE(CpuFeatures_StringView_IsEquals(str("a"), str("a"))); + EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("a"), str("b"))); + EXPECT_FALSE(CpuFeatures_StringView_IsEquals(str("a"), kEmptyStringView)); + EXPECT_FALSE(CpuFeatures_StringView_IsEquals(kEmptyStringView, str("a"))); } -TEST(StringViewTest, PopFront) { - EXPECT_EQ(PopFront(str("test"), 2), str("st")); - EXPECT_EQ(PopFront(str("test"), 0), str("test")); - EXPECT_EQ(PopFront(str("test"), 4), str("")); - EXPECT_EQ(PopFront(str("test"), 100), str("")); +TEST(StringViewTest, CpuFeatures_StringView_PopFront) { + EXPECT_EQ(CpuFeatures_StringView_PopFront(str("test"), 2), str("st")); + EXPECT_EQ(CpuFeatures_StringView_PopFront(str("test"), 0), str("test")); + EXPECT_EQ(CpuFeatures_StringView_PopFront(str("test"), 4), str("")); + EXPECT_EQ(CpuFeatures_StringView_PopFront(str("test"), 100), str("")); } -TEST(StringViewTest, ParsePositiveNumber) { - EXPECT_EQ(ParsePositiveNumber(str("42")), 42); - EXPECT_EQ(ParsePositiveNumber(str("0x2a")), 42); - EXPECT_EQ(ParsePositiveNumber(str("0x2A")), 42); +TEST(StringViewTest, CpuFeatures_StringView_ParsePositiveNumber) { + EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("42")), 42); + EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("0x2a")), 42); + EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("0x2A")), 42); - EXPECT_EQ(ParsePositiveNumber(str("-0x2A")), -1); - EXPECT_EQ(ParsePositiveNumber(str("abc")), -1); - EXPECT_EQ(ParsePositiveNumber(str("")), -1); + EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("-0x2A")), -1); + EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("abc")), -1); + EXPECT_EQ(CpuFeatures_StringView_ParsePositiveNumber(str("")), -1); } -TEST(StringViewTest, CopyString) { +TEST(StringViewTest, CpuFeatures_StringView_CopyString) { char buf[4]; buf[0] = 'X'; // Empty - CopyString(str(""), buf, sizeof(buf)); + CpuFeatures_StringView_CopyString(str(""), buf, sizeof(buf)); EXPECT_STREQ(buf, ""); // Less - CopyString(str("a"), buf, sizeof(buf)); + CpuFeatures_StringView_CopyString(str("a"), buf, sizeof(buf)); EXPECT_STREQ(buf, "a"); // exact - CopyString(str("abc"), buf, sizeof(buf)); + CpuFeatures_StringView_CopyString(str("abc"), buf, sizeof(buf)); EXPECT_STREQ(buf, "abc"); // More - CopyString(str("abcd"), buf, sizeof(buf)); + CpuFeatures_StringView_CopyString(str("abcd"), buf, sizeof(buf)); EXPECT_STREQ(buf, "abc"); } -TEST(StringViewTest, HasWord) { +TEST(StringViewTest, CpuFeatures_StringView_HasWord) { // Find flags at beginning, middle and end. - EXPECT_TRUE(HasWord(str("first middle last"), "first")); - EXPECT_TRUE(HasWord(str("first middle last"), "middle")); - EXPECT_TRUE(HasWord(str("first middle last"), "last")); + EXPECT_TRUE(CpuFeatures_StringView_HasWord(str("first middle last"), "first")); + EXPECT_TRUE(CpuFeatures_StringView_HasWord(str("first middle last"), "middle")); + EXPECT_TRUE(CpuFeatures_StringView_HasWord(str("first middle last"), "last")); // Do not match partial flags - EXPECT_FALSE(HasWord(str("first middle last"), "irst")); - EXPECT_FALSE(HasWord(str("first middle last"), "mid")); - EXPECT_FALSE(HasWord(str("first middle last"), "las")); + EXPECT_FALSE(CpuFeatures_StringView_HasWord(str("first middle last"), "irst")); + EXPECT_FALSE(CpuFeatures_StringView_HasWord(str("first middle last"), "mid")); + EXPECT_FALSE(CpuFeatures_StringView_HasWord(str("first middle last"), "las")); } -TEST(StringViewTest, GetAttributeKeyValue) { +TEST(StringViewTest, CpuFeatures_StringView_GetAttributeKeyValue) { const StringView line = str(" key : first middle last "); StringView key, value; - EXPECT_TRUE(GetAttributeKeyValue(line, &key, &value)); + EXPECT_TRUE(CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)); EXPECT_EQ(key, str("key")); EXPECT_EQ(value, str("first middle last")); } @@ -131,7 +131,7 @@ TEST(StringViewTest, GetAttributeKeyValue) { TEST(StringViewTest, FailingGetAttributeKeyValue) { const StringView line = str("key first middle last"); StringView key, value; - EXPECT_FALSE(GetAttributeKeyValue(line, &key, &value)); + EXPECT_FALSE(CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)); } } // namespace From 235d57c591aab49ea34d63a07c39095f5b6aea7d Mon Sep 17 00:00:00 2001 From: Arvid Gerstmann Date: Fri, 4 May 2018 09:30:32 +0200 Subject: [PATCH 3/4] Fix failing tests on Linux & Android --- src/hwcaps.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hwcaps.c b/src/hwcaps.c index c1ac6e0..a4a30db 100644 --- a/src/hwcaps.c +++ b/src/hwcaps.c @@ -110,13 +110,13 @@ static uint32_t GetElfHwcapFromProcSelfAuxv(uint32_t hwcap_type) { } entry; uint32_t result = 0; const char filepath[] = "/proc/self/auxv"; - const int fd = OpenFile(filepath); + const int fd = CpuFeatures_OpenFile(filepath); if (fd < 0) { D("Could not open %s\n", filepath); return 0; } for (;;) { - const int ret = ReadFile(fd, (char*)&entry, sizeof entry); + const int ret = CpuFeatures_ReadFile(fd, (char*)&entry, sizeof entry); if (ret < 0) { D("Error while reading %s\n", filepath); break; @@ -130,7 +130,7 @@ static uint32_t GetElfHwcapFromProcSelfAuxv(uint32_t hwcap_type) { break; } } - CloseFile(fd); + CpuFeatures_CloseFile(fd); return result; } From d968991caaf1967a0762c32951ab9c373bc8324e Mon Sep 17 00:00:00 2001 From: Arvid Gerstmann Date: Fri, 4 May 2018 09:32:17 +0200 Subject: [PATCH 4/4] Reformat files --- include/internal/linux_features_aggregator.h | 12 ++-- include/internal/string_view.h | 20 ++++--- src/cpuinfo_aarch64.c | 5 +- src/cpuinfo_arm.c | 14 +++-- src/cpuinfo_mips.c | 5 +- src/filesystem.c | 10 +++- src/linux_features_aggregator.c | 15 +++-- src/stack_line_reader.c | 13 +++-- src/string_view.c | 59 +++++++++++++------- test/filesystem_for_testing.cc | 3 +- test/linux_features_aggregator_test.cc | 15 +++-- test/string_view_test.cc | 18 ++++-- 12 files changed, 122 insertions(+), 67 deletions(-) diff --git a/include/internal/linux_features_aggregator.h b/include/internal/linux_features_aggregator.h index 335c5d1..77661d4 100644 --- a/include/internal/linux_features_aggregator.h +++ b/include/internal/linux_features_aggregator.h @@ -43,16 +43,18 @@ typedef struct { // For every config, looks into flags_line for the presence of the // corresponding proc_cpuinfo_flag, calls `set_bit` accordingly. // Note: features is a pointer to the underlying Feature struct. -void CpuFeatures_SetFromFlags(const size_t configs_size, const CapabilityConfig* configs, - const StringView flags_line, void* const features); +void CpuFeatures_SetFromFlags(const size_t configs_size, + const CapabilityConfig* configs, + const StringView flags_line, + void* const features); // For every config, looks into hwcaps for the presence of the feature. Calls // `set_bit` with true if the hardware capability is found. // Note: features is a pointer to the underlying Feature struct. void CpuFeatures_OverrideFromHwCaps(const size_t configs_size, - const CapabilityConfig* configs, - const HardwareCapabilities hwcaps, - void* const features); + const CapabilityConfig* configs, + const HardwareCapabilities hwcaps, + void* const features); CPU_FEATURES_END_CPP_NAMESPACE #endif // CPU_FEATURES_INCLUDE_INTERNAL_LINUX_FEATURES_AGGREGATOR_H_ diff --git a/include/internal/string_view.h b/include/internal/string_view.h index ebfcb37..f5bcfc5 100644 --- a/include/internal/string_view.h +++ b/include/internal/string_view.h @@ -50,7 +50,8 @@ int CpuFeatures_StringView_IndexOfChar(const StringView view, char c); // Returns the index of the first occurrence of sub_view in view or -1 if not // found. -int CpuFeatures_StringView_IndexOf(const StringView view, const StringView sub_view); +int CpuFeatures_StringView_IndexOf(const StringView view, + const StringView sub_view); // Returns whether a is equal to b (same content). bool CpuFeatures_StringView_IsEquals(const StringView a, const StringView b); @@ -64,11 +65,13 @@ StringView CpuFeatures_StringView_PopFront(const StringView view, size_t count); // Removes count characters from the end of view or kEmptyStringView if count if // greater than view.size. -StringView CpuFeatures_StringView_PopBack(const StringView str_view, size_t count); +StringView CpuFeatures_StringView_PopBack(const StringView str_view, + size_t count); // Keeps the count first characters of view or view if count if greater than // view.size. -StringView CpuFeatures_StringView_KeepFront(const StringView view, size_t count); +StringView CpuFeatures_StringView_KeepFront(const StringView view, + size_t count); // Retrieves the first character of view. If view is empty the behavior is // undefined. @@ -86,15 +89,18 @@ StringView CpuFeatures_StringView_TrimWhitespace(StringView view); int CpuFeatures_StringView_ParsePositiveNumber(const StringView view); // Copies src StringView to dst buffer. -void CpuFeatures_StringView_CopyString(const StringView src, char* dst, size_t dst_size); +void CpuFeatures_StringView_CopyString(const StringView src, char* dst, + size_t dst_size); // Checks if line contains the specified whitespace separated word. -bool CpuFeatures_StringView_HasWord(const StringView line, const char* const word); +bool CpuFeatures_StringView_HasWord(const StringView line, + const char* const word); // Get key/value from line. key and value are separated by ": ". // key and value are cleaned up from leading and trailing whitespaces. -bool CpuFeatures_StringView_GetAttributeKeyValue(const StringView line, StringView* key, - StringView* value); +bool CpuFeatures_StringView_GetAttributeKeyValue(const StringView line, + StringView* key, + StringView* value); CPU_FEATURES_END_CPP_NAMESPACE diff --git a/src/cpuinfo_aarch64.c b/src/cpuinfo_aarch64.c index 1819efb..0d111ff 100644 --- a/src/cpuinfo_aarch64.c +++ b/src/cpuinfo_aarch64.c @@ -85,8 +85,9 @@ Aarch64Info GetAarch64Info(void) { Aarch64Info info = kEmptyAarch64Info; FillProcCpuInfoData(&info); - CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs, CpuFeatures_GetHardwareCapabilities(), - &info.features); + CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs, + CpuFeatures_GetHardwareCapabilities(), + &info.features); return info; } diff --git a/src/cpuinfo_arm.c b/src/cpuinfo_arm.c index d218101..3ea0641 100644 --- a/src/cpuinfo_arm.c +++ b/src/cpuinfo_arm.c @@ -87,12 +87,15 @@ static bool HandleArmLine(const LineResult result, ArmInfo* const info, } else if (CpuFeatures_StringView_IsEquals(key, str("CPU architecture"))) { // CPU architecture is a number that may be followed by letters. e.g. // "6TEJ", "7". - const StringView digits = CpuFeatures_StringView_KeepFront(value, IndexOfNonDigit(value)); + const StringView digits = + CpuFeatures_StringView_KeepFront(value, IndexOfNonDigit(value)); info->architecture = CpuFeatures_StringView_ParsePositiveNumber(digits); } else if (CpuFeatures_StringView_IsEquals(key, str("Processor"))) { - proc_info->processor_reports_armv6 = CpuFeatures_StringView_IndexOf(value, str("(v6l)")) >= 0; + proc_info->processor_reports_armv6 = + CpuFeatures_StringView_IndexOf(value, str("(v6l)")) >= 0; } else if (CpuFeatures_StringView_IsEquals(key, str("Hardware"))) { - proc_info->hardware_reports_goldfish = CpuFeatures_StringView_IsEquals(value, str("Goldfish")); + proc_info->hardware_reports_goldfish = + CpuFeatures_StringView_IsEquals(value, str("Goldfish")); } } return !result.eof; @@ -174,8 +177,9 @@ ArmInfo GetArmInfo(void) { ProcCpuInfoData proc_cpu_info_data = kEmptyProcCpuInfoData; FillProcCpuInfoData(&info, &proc_cpu_info_data); - CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs, CpuFeatures_GetHardwareCapabilities(), - &info.features); + CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs, + CpuFeatures_GetHardwareCapabilities(), + &info.features); FixErrors(&info, &proc_cpu_info_data); diff --git a/src/cpuinfo_mips.c b/src/cpuinfo_mips.c index b10f309..a61cdd8 100644 --- a/src/cpuinfo_mips.c +++ b/src/cpuinfo_mips.c @@ -63,8 +63,9 @@ MipsInfo GetMipsInfo(void) { MipsInfo info = kEmptyMipsInfo; FillProcCpuInfoData(&info.features); - CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs, CpuFeatures_GetHardwareCapabilities(), - &info.features); + CpuFeatures_OverrideFromHwCaps(kConfigsSize, kConfigs, + CpuFeatures_GetHardwareCapabilities(), + &info.features); return info; } diff --git a/src/filesystem.c b/src/filesystem.c index 003bf6d..286a9cc 100644 --- a/src/filesystem.c +++ b/src/filesystem.c @@ -21,11 +21,14 @@ #if defined(_MSC_VER) #include -int CpuFeatures_OpenFile(const char* filename) { return _open(filename, _O_RDONLY); } +int CpuFeatures_OpenFile(const char* filename) { + return _open(filename, _O_RDONLY); +} void CpuFeatures_CloseFile(int file_descriptor) { _close(file_descriptor); } -int CpuFeatures_ReadFile(int file_descriptor, void* buffer, size_t buffer_size) { +int CpuFeatures_ReadFile(int file_descriptor, void* buffer, + size_t buffer_size) { return _read(file_descriptor, buffer, buffer_size); } @@ -42,7 +45,8 @@ int CpuFeatures_OpenFile(const char* filename) { void CpuFeatures_CloseFile(int file_descriptor) { close(file_descriptor); } -int CpuFeatures_ReadFile(int file_descriptor, void* buffer, size_t buffer_size) { +int CpuFeatures_ReadFile(int file_descriptor, void* buffer, + size_t buffer_size) { int result; do { result = read(file_descriptor, buffer, buffer_size); diff --git a/src/linux_features_aggregator.c b/src/linux_features_aggregator.c index c5544b8..b7f8f3d 100644 --- a/src/linux_features_aggregator.c +++ b/src/linux_features_aggregator.c @@ -15,12 +15,15 @@ #include "internal/linux_features_aggregator.h" #include "internal/string_view.h" -void CpuFeatures_SetFromFlags(const size_t configs_size, const CapabilityConfig* configs, - const StringView flags_line, void* const features) { +void CpuFeatures_SetFromFlags(const size_t configs_size, + const CapabilityConfig* configs, + const StringView flags_line, + void* const features) { size_t i = 0; for (; i < configs_size; ++i) { const CapabilityConfig config = configs[i]; - config.set_bit(features, CpuFeatures_StringView_HasWord(flags_line, config.proc_cpuinfo_flag)); + config.set_bit(features, CpuFeatures_StringView_HasWord( + flags_line, config.proc_cpuinfo_flag)); } } @@ -35,9 +38,9 @@ static bool IsHwCapsSet(const HardwareCapabilities hwcaps_mask, } void CpuFeatures_OverrideFromHwCaps(const size_t configs_size, - const CapabilityConfig* configs, - const HardwareCapabilities hwcaps, - void* const features) { + const CapabilityConfig* configs, + const HardwareCapabilities hwcaps, + void* const features) { size_t i = 0; for (; i < configs_size; ++i) { const CapabilityConfig* config = &configs[i]; diff --git a/src/stack_line_reader.c b/src/stack_line_reader.c index 6af7844..b2c48ba 100644 --- a/src/stack_line_reader.c +++ b/src/stack_line_reader.c @@ -28,8 +28,8 @@ void StackLineReader_Initialize(StackLineReader* reader, int fd) { // Replaces the content of buffer with bytes from the file. static int LoadFullBuffer(StackLineReader* reader) { - const int read = - CpuFeatures_ReadFile(reader->fd, reader->buffer, STACK_LINE_READER_BUFFER_SIZE); + const int read = CpuFeatures_ReadFile(reader->fd, reader->buffer, + STACK_LINE_READER_BUFFER_SIZE); assert(read >= 0); reader->view.ptr = reader->buffer; reader->view.size = read; @@ -71,7 +71,8 @@ static void SkipToNextLine(StackLineReader* reader) { } else { const int eol_index = IndexOfEol(reader); if (eol_index >= 0) { - reader->view = CpuFeatures_StringView_PopFront(reader->view, eol_index + 1); + reader->view = + CpuFeatures_StringView_PopFront(reader->view, eol_index + 1); break; } } @@ -120,8 +121,10 @@ LineResult StackLineReader_NextLine(StackLineReader* reader) { return CreateTruncatedLineResult(reader->view); } { - StringView line = CpuFeatures_StringView_KeepFront(reader->view, eol_index); - reader->view = CpuFeatures_StringView_PopFront(reader->view, eol_index + 1); + StringView line = + CpuFeatures_StringView_KeepFront(reader->view, eol_index); + reader->view = + CpuFeatures_StringView_PopFront(reader->view, eol_index + 1); return CreateValidLineResult(line); } } diff --git a/src/string_view.c b/src/string_view.c index e6d100c..4f27cbd 100644 --- a/src/string_view.c +++ b/src/string_view.c @@ -28,11 +28,13 @@ int CpuFeatures_StringView_IndexOfChar(const StringView view, char c) { return -1; } -int CpuFeatures_StringView_IndexOf(const StringView view, const StringView sub_view) { +int CpuFeatures_StringView_IndexOf(const StringView view, + const StringView sub_view) { if (sub_view.size) { StringView remainder = view; while (remainder.size >= sub_view.size) { - const int found_index = CpuFeatures_StringView_IndexOfChar(remainder, sub_view.ptr[0]); + const int found_index = + CpuFeatures_StringView_IndexOfChar(remainder, sub_view.ptr[0]); if (found_index < 0) break; remainder = CpuFeatures_StringView_PopFront(remainder, found_index); if (CpuFeatures_StringView_StartsWith(remainder, sub_view)) { @@ -57,21 +59,24 @@ bool CpuFeatures_StringView_StartsWith(const StringView a, const StringView b) { : false; } -StringView CpuFeatures_StringView_PopFront(const StringView str_view, size_t count) { +StringView CpuFeatures_StringView_PopFront(const StringView str_view, + size_t count) { if (count > str_view.size) { return kEmptyStringView; } return view(str_view.ptr + count, str_view.size - count); } -StringView CpuFeatures_StringView_PopBack(const StringView str_view, size_t count) { +StringView CpuFeatures_StringView_PopBack(const StringView str_view, + size_t count) { if (count > str_view.size) { return kEmptyStringView; } return view(str_view.ptr, str_view.size - count); } -StringView CpuFeatures_StringView_KeepFront(const StringView str_view, size_t count) { +StringView CpuFeatures_StringView_KeepFront(const StringView str_view, + size_t count) { return count <= str_view.size ? view(str_view.ptr, count) : str_view; } @@ -87,8 +92,10 @@ char CpuFeatures_StringView_Back(const StringView view) { } StringView CpuFeatures_StringView_TrimWhitespace(StringView view) { - while (view.size && isspace(CpuFeatures_StringView_Front(view))) view = CpuFeatures_StringView_PopFront(view, 1); - while (view.size && isspace(CpuFeatures_StringView_Back(view))) view = CpuFeatures_StringView_PopBack(view, 1); + while (view.size && isspace(CpuFeatures_StringView_Front(view))) + view = CpuFeatures_StringView_PopFront(view, 1); + while (view.size && isspace(CpuFeatures_StringView_Back(view))) + view = CpuFeatures_StringView_PopBack(view, 1); return view; } @@ -103,7 +110,8 @@ static int HexValue(const char c) { static int ParsePositiveNumberWithBase(const StringView view, int base) { int result = 0; StringView remainder = view; - for (; remainder.size; remainder = CpuFeatures_StringView_PopFront(remainder, 1)) { + for (; remainder.size; + remainder = CpuFeatures_StringView_PopFront(remainder, 1)) { const int value = HexValue(CpuFeatures_StringView_Front(remainder)); if (value < 0 || value >= base) return -1; result = (result * base) + value; @@ -115,7 +123,8 @@ int CpuFeatures_StringView_ParsePositiveNumber(const StringView view) { if (view.size) { const StringView hex_prefix = str("0x"); if (CpuFeatures_StringView_StartsWith(view, hex_prefix)) { - const StringView span_no_prefix = CpuFeatures_StringView_PopFront(view, hex_prefix.size); + const StringView span_no_prefix = + CpuFeatures_StringView_PopFront(view, hex_prefix.size); return ParsePositiveNumberWithBase(span_no_prefix, 16); } return ParsePositiveNumberWithBase(view, 10); @@ -123,7 +132,8 @@ int CpuFeatures_StringView_ParsePositiveNumber(const StringView view) { return -1; } -void CpuFeatures_StringView_CopyString(const StringView src, char* dst, size_t dst_size) { +void CpuFeatures_StringView_CopyString(const StringView src, char* dst, + size_t dst_size) { if (dst_size > 0) { const size_t max_copy_size = dst_size - 1; const size_t copy_size = @@ -133,7 +143,8 @@ void CpuFeatures_StringView_CopyString(const StringView src, char* dst, size_t d } } -bool CpuFeatures_StringView_HasWord(const StringView line, const char* const word_str) { +bool CpuFeatures_StringView_HasWord(const StringView line, + const char* const word_str) { const StringView word = str(word_str); StringView remainder = line; for (;;) { @@ -141,23 +152,31 @@ bool CpuFeatures_StringView_HasWord(const StringView line, const char* const wor if (index_of_word < 0) { return false; } else { - const StringView before = CpuFeatures_StringView_KeepFront(line, index_of_word); - const StringView after = CpuFeatures_StringView_PopFront(line, index_of_word + word.size); - const bool valid_before = before.size == 0 || CpuFeatures_StringView_Back(before) == ' '; - const bool valid_after = after.size == 0 || CpuFeatures_StringView_Front(after) == ' '; + const StringView before = + CpuFeatures_StringView_KeepFront(line, index_of_word); + const StringView after = + CpuFeatures_StringView_PopFront(line, index_of_word + word.size); + const bool valid_before = + before.size == 0 || CpuFeatures_StringView_Back(before) == ' '; + const bool valid_after = + after.size == 0 || CpuFeatures_StringView_Front(after) == ' '; if (valid_before && valid_after) return true; - remainder = CpuFeatures_StringView_PopFront(remainder, index_of_word + word.size); + remainder = + CpuFeatures_StringView_PopFront(remainder, index_of_word + word.size); } } return false; } -bool CpuFeatures_StringView_GetAttributeKeyValue(const StringView line, StringView* key, - StringView* value) { +bool CpuFeatures_StringView_GetAttributeKeyValue(const StringView line, + StringView* key, + StringView* value) { const StringView sep = str(": "); const int index_of_separator = CpuFeatures_StringView_IndexOf(line, sep); if (index_of_separator < 0) return false; - *value = CpuFeatures_StringView_TrimWhitespace(CpuFeatures_StringView_PopFront(line, index_of_separator + sep.size)); - *key = CpuFeatures_StringView_TrimWhitespace(CpuFeatures_StringView_KeepFront(line, index_of_separator)); + *value = CpuFeatures_StringView_TrimWhitespace( + CpuFeatures_StringView_PopFront(line, index_of_separator + sep.size)); + *key = CpuFeatures_StringView_TrimWhitespace( + CpuFeatures_StringView_KeepFront(line, index_of_separator)); return true; } diff --git a/test/filesystem_for_testing.cc b/test/filesystem_for_testing.cc index 7b05a88..b65d39b 100644 --- a/test/filesystem_for_testing.cc +++ b/test/filesystem_for_testing.cc @@ -94,7 +94,8 @@ extern "C" void CpuFeatures_CloseFile(int file_descriptor) { kFilesystem->FindFileOrDie(file_descriptor)->Close(); } -extern "C" int CpuFeatures_ReadFile(int file_descriptor, void* buf, size_t count) { +extern "C" int CpuFeatures_ReadFile(int file_descriptor, void* buf, + size_t count) { return kFilesystem->FindFileOrDie(file_descriptor) ->Read(file_descriptor, buf, count); } diff --git a/test/linux_features_aggregator_test.cc b/test/linux_features_aggregator_test.cc index 7205d46..99367dc 100644 --- a/test/linux_features_aggregator_test.cc +++ b/test/linux_features_aggregator_test.cc @@ -42,7 +42,8 @@ class LinuxFeatureAggregatorTest : public testing::Test { TEST_F(LinuxFeatureAggregatorTest, FromFlagsEmpty) { Features features; - CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str(""), &features); + CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str(""), + &features); EXPECT_FALSE(features.a); EXPECT_FALSE(features.b); EXPECT_FALSE(features.c); @@ -50,7 +51,8 @@ TEST_F(LinuxFeatureAggregatorTest, FromFlagsEmpty) { TEST_F(LinuxFeatureAggregatorTest, FromFlagsAllSet) { Features features; - CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a c b"), &features); + CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a c b"), + &features); EXPECT_TRUE(features.a); EXPECT_TRUE(features.b); EXPECT_TRUE(features.c); @@ -58,7 +60,8 @@ TEST_F(LinuxFeatureAggregatorTest, FromFlagsAllSet) { TEST_F(LinuxFeatureAggregatorTest, FromFlagsOnlyA) { Features features; - CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a"), &features); + CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a"), + &features); EXPECT_TRUE(features.a); EXPECT_FALSE(features.b); EXPECT_FALSE(features.c); @@ -69,7 +72,8 @@ TEST_F(LinuxFeatureAggregatorTest, FromHwcapsNone) { capability.hwcaps = 0; // matches none capability.hwcaps2 = 0; // matches none Features features; - CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability, &features); + CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability, + &features); EXPECT_FALSE(features.a); EXPECT_FALSE(features.b); EXPECT_FALSE(features.c); @@ -80,7 +84,8 @@ TEST_F(LinuxFeatureAggregatorTest, FromHwcapsSet) { capability.hwcaps = 0b0010; // matches b but not a capability.hwcaps2 = 0b1111; // matches c Features features; - CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability, &features); + CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability, + &features); EXPECT_FALSE(features.a); EXPECT_TRUE(features.b); EXPECT_TRUE(features.c); diff --git a/test/string_view_test.cc b/test/string_view_test.cc index a80c6ff..abfcc2c 100644 --- a/test/string_view_test.cc +++ b/test/string_view_test.cc @@ -57,12 +57,15 @@ TEST(StringViewTest, CpuFeatures_StringView_IndexOf) { TEST(StringViewTest, CpuFeatures_StringView_StartsWith) { EXPECT_TRUE(CpuFeatures_StringView_StartsWith(str("test"), str("te"))); EXPECT_FALSE(CpuFeatures_StringView_StartsWith(str("test"), str(""))); - EXPECT_FALSE(CpuFeatures_StringView_StartsWith(str("test"), kEmptyStringView)); - EXPECT_FALSE(CpuFeatures_StringView_StartsWith(kEmptyStringView, str("test"))); + EXPECT_FALSE( + CpuFeatures_StringView_StartsWith(str("test"), kEmptyStringView)); + EXPECT_FALSE( + CpuFeatures_StringView_StartsWith(kEmptyStringView, str("test"))); } TEST(StringViewTest, CpuFeatures_StringView_IsEquals) { - EXPECT_TRUE(CpuFeatures_StringView_IsEquals(kEmptyStringView, kEmptyStringView)); + EXPECT_TRUE( + CpuFeatures_StringView_IsEquals(kEmptyStringView, kEmptyStringView)); EXPECT_TRUE(CpuFeatures_StringView_IsEquals(kEmptyStringView, str(""))); EXPECT_TRUE(CpuFeatures_StringView_IsEquals(str(""), kEmptyStringView)); EXPECT_TRUE(CpuFeatures_StringView_IsEquals(str("a"), str("a"))); @@ -111,11 +114,14 @@ TEST(StringViewTest, CpuFeatures_StringView_CopyString) { TEST(StringViewTest, CpuFeatures_StringView_HasWord) { // Find flags at beginning, middle and end. - EXPECT_TRUE(CpuFeatures_StringView_HasWord(str("first middle last"), "first")); - EXPECT_TRUE(CpuFeatures_StringView_HasWord(str("first middle last"), "middle")); + EXPECT_TRUE( + CpuFeatures_StringView_HasWord(str("first middle last"), "first")); + EXPECT_TRUE( + CpuFeatures_StringView_HasWord(str("first middle last"), "middle")); EXPECT_TRUE(CpuFeatures_StringView_HasWord(str("first middle last"), "last")); // Do not match partial flags - EXPECT_FALSE(CpuFeatures_StringView_HasWord(str("first middle last"), "irst")); + EXPECT_FALSE( + CpuFeatures_StringView_HasWord(str("first middle last"), "irst")); EXPECT_FALSE(CpuFeatures_StringView_HasWord(str("first middle last"), "mid")); EXPECT_FALSE(CpuFeatures_StringView_HasWord(str("first middle last"), "las")); }