From a3224ab9a0542d295e2e3eafb1ad5b9c7bb9ce75 Mon Sep 17 00:00:00 2001 From: Burt P Date: Tue, 31 Dec 2019 23:31:20 -0600 Subject: use newer cpubits.{h,c} from sysobj Signed-off-by: Burt P --- CMakeLists.txt | 1 + deps/sysobj_early/include/cpubits.h | 34 ++++++++++ deps/sysobj_early/src/cpubits.c | 132 ++++++++++++++++++++++++++++++++++++ hardinfo/cpu_util.c | 3 +- hardinfo/cpubits.c | 113 ------------------------------ 5 files changed, 168 insertions(+), 115 deletions(-) create mode 100644 deps/sysobj_early/include/cpubits.h create mode 100644 deps/sysobj_early/src/cpubits.c delete mode 100644 hardinfo/cpubits.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 078ae9ce..39cd191b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -237,6 +237,7 @@ add_library(sysobj_early STATIC deps/sysobj_early/gui/uri_handler.c deps/sysobj_early/src/util_edid.c deps/sysobj_early/src/format_early.c + deps/sysobj_early/src/cpubits.c ) set_target_properties(sysobj_early PROPERTIES COMPILE_FLAGS "-std=c99 -Wall -Wextra -Wno-parentheses -Wno-unused-function") target_link_libraries(sysobj_early m) diff --git a/deps/sysobj_early/include/cpubits.h b/deps/sysobj_early/include/cpubits.h new file mode 100644 index 00000000..a7effbf1 --- /dev/null +++ b/deps/sysobj_early/include/cpubits.h @@ -0,0 +1,34 @@ +/* + * rpiz - https://github.com/bp0/rpiz + * Copyright (C) 2017 Burt P. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include + +typedef uint32_t cpubits; +uint32_t cpubits_count(cpubits *b); +int cpubits_min(cpubits *b); +int cpubits_max(cpubits *b); +int cpubits_next(cpubits *b, int start, int end); +cpubits *cpubits_from_str(char *str); +char *cpubits_to_str(cpubits *bits, char *str, int max_len); + +#define CPUBITS_SIZE 4096 /* bytes, multiple of sizeof(uint32_t) */ +#define CPUBIT_SET(BITS, BIT) (BITS[BIT/32] |= (1 << BIT%32)) +#define CPUBIT_GET(BITS, BIT) ((BITS[BIT/32] & (1 << BIT%32)) >> BIT%32) +#define CPUBITS_CLEAR(BITS) memset(BITS, 0, CPUBITS_SIZE) diff --git a/deps/sysobj_early/src/cpubits.c b/deps/sysobj_early/src/cpubits.c new file mode 100644 index 00000000..fe8ba207 --- /dev/null +++ b/deps/sysobj_early/src/cpubits.c @@ -0,0 +1,132 @@ +/* + * rpiz - https://github.com/bp0/rpiz + * Copyright (C) 2017 Burt P. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#define _GNU_SOURCE +#include +#include +#include +#include "cpubits.h" + +uint32_t cpubits_count(cpubits *b) { + static const uint32_t max = CPUBITS_SIZE * 8; + uint32_t count = 0, i = 0; + while (i < max) { + count += CPUBIT_GET(b, i); + i++; + } + return count; +} + +int cpubits_min(cpubits *b) { + int i = 0; + while (i < CPUBITS_SIZE * 8) { + if (CPUBIT_GET(b, i)) + return i; + i++; + } + return -1; +} + +int cpubits_max(cpubits *b) { + int i = CPUBITS_SIZE * 8 - 1; + while (i >= 0) { + if (CPUBIT_GET(b, i)) + return i; + i--; + } + return i; +} + +int cpubits_next(cpubits *b, int start, int end) { + start++; /* not including the start bit */ + if (start >= 0) { + int i = start; + if (end == -1) + end = CPUBITS_SIZE * 8; + while (i < end) { + if (CPUBIT_GET(b, i)) + return i; + i++; + } + } + return -1; +} + +cpubits *cpubits_from_str(char *str) { + char *v, *nv, *hy; + int r0, r1; + cpubits *newbits = malloc(CPUBITS_SIZE); + if (newbits) { + memset(newbits, 0, CPUBITS_SIZE); + if (str != NULL) { + v = (char*)str; + while ( *v != 0 ) { + nv = strchr(v, ','); /* strchrnul() */ + if (nv == NULL) nv = strchr(v, 0); /* equivalent */ + hy = strchr(v, '-'); + if (hy && hy < nv) { + r0 = strtol(v, NULL, 0); + r1 = strtol(hy + 1, NULL, 0); + } else { + r0 = r1 = strtol(v, NULL, 0); + } + for (; r0 <= r1; r0++) { + CPUBIT_SET(newbits, r0); + } + v = (*nv == ',') ? nv + 1 : nv; + } + } + } + return newbits; +} + +char *cpubits_to_str(cpubits *bits, char *str, int max_len) { + static const uint32_t max = CPUBITS_SIZE * 8; + uint32_t i = 1, seq_start = 0, seq_last = 0, seq = 0, l = 0; + char buffer[65536] = ""; + if (CPUBIT_GET(bits, 0)) { + seq = 1; + strcpy(buffer, "0"); + } + while (i < max) { + if (CPUBIT_GET(bits, i) ) { + seq_last = i; + if (!seq) { + seq = 1; + seq_start = i; + l = strlen(buffer); + sprintf(buffer + l, "%s%d", l ? "," : "", i); + } + } else { + if (seq && seq_last != seq_start) { + l = strlen(buffer); + sprintf(buffer + l, "-%d", seq_last); + } + seq = 0; + } + i++; + } + if (str == NULL) + return strdup(buffer); + else { + strncpy(str, buffer, max_len); + return str; + } +} diff --git a/hardinfo/cpu_util.c b/hardinfo/cpu_util.c index a598d2ed..5064c173 100644 --- a/hardinfo/cpu_util.c +++ b/hardinfo/cpu_util.c @@ -20,8 +20,7 @@ #include #include "hardinfo.h" #include "cpu_util.h" - -#include "cpubits.c" +#include "cpubits.h" #define CPU_TOPO_NULL -9877 diff --git a/hardinfo/cpubits.c b/hardinfo/cpubits.c deleted file mode 100644 index 281a26e1..00000000 --- a/hardinfo/cpubits.c +++ /dev/null @@ -1,113 +0,0 @@ -/* - * rpiz - https://github.com/bp0/rpiz - * Copyright (C) 2017 Burt P. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -#include - -typedef uint32_t cpubits; -uint32_t cpubits_count(cpubits *b); -cpubits *cpubits_from_str(char *str); -char *cpubits_to_str(cpubits *bits, char *str, int max_len); - -#define CPUBITS_SIZE 4096 /* bytes, multiple of sizeof(uint32_t) */ -#define CPUBIT_SET(BITS, BIT) (BITS[(BIT)/32] |= (1 << (BIT)%32)) -#define CPUBIT_GET(BITS, BIT) ((BITS[(BIT)/32] & (1 << (BIT)%32)) >> (BIT)%32) -#define CPUBITS_CLEAR(BITS) memset(BITS, 0, CPUBITS_SIZE) - -uint32_t cpubits_count(cpubits *b) { - static const uint32_t max = CPUBITS_SIZE * 8; - uint32_t count = 0, i = 0; - while (i < max) { - count += CPUBIT_GET(b, i); - i++; - } - return count; -} - -int cpubits_max(cpubits *b) { - int i = CPUBITS_SIZE * 8 - 1; - while (i >= 0) { - if (CPUBIT_GET(b, i)) - break; - i--; - } - return i; -} - -cpubits *cpubits_from_str(char *str) { - char *v, *nv, *hy; - int r0, r1; - cpubits *newbits = malloc(CPUBITS_SIZE); - if (newbits) { - memset(newbits, 0, CPUBITS_SIZE); - if (str != NULL) { - v = (char*)str; - while ( *v != 0 ) { - nv = strchr(v, ','); /* strchrnul() */ - if (nv == NULL) nv = strchr(v, 0); /* equivalent */ - hy = strchr(v, '-'); - if (hy && hy < nv) { - r0 = strtol(v, NULL, 0); - r1 = strtol(hy + 1, NULL, 0); - } else { - r0 = r1 = strtol(v, NULL, 0); - } - for (; r0 <= r1; r0++) { - CPUBIT_SET(newbits, r0); - } - v = (*nv == ',') ? nv + 1 : nv; - } - } - } - return newbits; -} - -char *cpubits_to_str(cpubits *bits, char *str, int max_len) { - static const uint32_t max = CPUBITS_SIZE * 8; - uint32_t i = 1, seq_start = 0, seq_last = 0, seq = 0, l = 0; - char buffer[65536] = ""; - if (CPUBIT_GET(bits, 0)) { - seq = 1; - strcpy(buffer, "0"); - } - while (i < max) { - if (CPUBIT_GET(bits, i) ) { - seq_last = i; - if (!seq) { - seq = 1; - seq_start = i; - l = strlen(buffer); - sprintf(buffer + l, "%s%d", l ? "," : "", i); - } - } else { - if (seq && seq_last != seq_start) { - l = strlen(buffer); - sprintf(buffer + l, "-%d", seq_last); - } - seq = 0; - } - i++; - } - if (str == NULL) - return strdup(buffer); - else { - strncpy(str, buffer, max_len); - return str; - } -} -- cgit v1.2.3