diff --git a/CMakeLists.txt b/CMakeLists.txt index c0737a6e4..d6a49cdc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -370,6 +370,7 @@ set(LIBFASTFETCH_SRC src/common/settings.c src/common/size.c src/common/temps.c + src/common/time.c src/detection/bluetoothradio/bluetoothradio.c src/detection/bootmgr/bootmgr.c src/detection/chassis/chassis.c diff --git a/src/common/time.c b/src/common/time.c new file mode 100644 index 000000000..84eab78be --- /dev/null +++ b/src/common/time.c @@ -0,0 +1,3 @@ +#include + +char ffTimeInternalBuffer[64]; // Reduce memory usage and prevent redundant allocations diff --git a/src/common/time.h b/src/common/time.h index 9457dd90d..149fb2843 100644 --- a/src/common/time.h +++ b/src/common/time.h @@ -11,6 +11,8 @@ #include #endif +#include "util/arrayUtils.h" + static inline double ffTimeGetTick(void) //In msec { #ifdef _WIN32 @@ -64,11 +66,12 @@ static inline const char* ffTimeToFullStr(uint64_t msec) time_t tsec = (time_t) (msec / 1000); const struct tm* tm = localtime(&tsec); - static char buf[32]; - strftime(buf, __builtin_strlen("0000-00-00T00:00:00") + 1, "%FT%T", tm); - snprintf(buf + __builtin_strlen("0000-00-00T00:00:00"), __builtin_strlen(".000") + 1, ".%03u", (unsigned) (msec % 1000)); - strftime(buf + __builtin_strlen("0000-00-00T00:00:00.000"), __builtin_strlen("+0000") + 1, "%z", tm); - return buf; + extern char ffTimeInternalBuffer[64]; + uint32_t len = 0; + len += (uint32_t) strftime(ffTimeInternalBuffer, ARRAY_SIZE(ffTimeInternalBuffer) - len, "%FT%T", tm); + len += (uint32_t) snprintf(ffTimeInternalBuffer + len, ARRAY_SIZE(ffTimeInternalBuffer) - len, ".%03u", (unsigned) (msec % 1000)); + len += (uint32_t) strftime(ffTimeInternalBuffer + len, ARRAY_SIZE(ffTimeInternalBuffer) - len, "%z", tm); + return ffTimeInternalBuffer; } // Not thread-safe @@ -77,9 +80,9 @@ static inline const char* ffTimeToShortStr(uint64_t msec) if (msec == 0) return ""; time_t tsec = (time_t) (msec / 1000); - static char buf[32]; - strftime(buf, sizeof(buf), "%F %T", localtime(&tsec)); - return buf; + extern char ffTimeInternalBuffer[64]; + strftime(ffTimeInternalBuffer, ARRAY_SIZE(ffTimeInternalBuffer), "%F %T", localtime(&tsec)); + return ffTimeInternalBuffer; } // Not thread-safe @@ -88,10 +91,10 @@ static inline const char* ffTimeToTimeStr(uint64_t msec) if (msec == 0) return ""; time_t tsec = (time_t) (msec / 1000); - static char buf[32]; - strftime(buf, sizeof(buf), "%T", localtime(&tsec)); - sprintf(buf + __builtin_strlen("00:00:00"), ".%03u", (unsigned) (msec % 1000)); - return buf; + extern char ffTimeInternalBuffer[64]; + strftime(ffTimeInternalBuffer, ARRAY_SIZE(ffTimeInternalBuffer), "%T", localtime(&tsec)); + sprintf(ffTimeInternalBuffer + __builtin_strlen("00:00:00"), ".%03u", (unsigned) (msec % 1000)); + return ffTimeInternalBuffer; } #ifdef _WIN32 diff --git a/src/fastfetch.h b/src/fastfetch.h index ea4d14621..f38bb864d 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -15,6 +15,7 @@ #define __attribute__(x) #endif +#include "util/arrayUtils.h" #include "util/FFstrbuf.h" #include "util/FFlist.h" #include "util/platform/FFPlatform.h" @@ -24,17 +25,6 @@ #include "options/display.h" #include "options/general.h" -#ifdef __has_builtin - #if __has_builtin(__is_array) - #define ARRAY_SIZE(x) ({ static_assert(__is_array(__typeof__(x)), "Must be an array"); (uint32_t) (sizeof(x) / sizeof(*(x))); }) - #elif __has_builtin(__builtin_types_compatible_p) - #define ARRAY_SIZE(x) ({ static_assert(!__builtin_types_compatible_p(__typeof__(x), __typeof__(&*(x))), "Must not be a pointer"); (uint32_t) (sizeof(x) / sizeof(*(x))); }) - #endif -#endif -#ifndef ARRAY_SIZE - #define ARRAY_SIZE(x) ((uint32_t) (sizeof(x) / sizeof(*(x)))) -#endif - typedef struct FFconfig { diff --git a/src/util/arrayUtils.h b/src/util/arrayUtils.h new file mode 100644 index 000000000..ad251ba1e --- /dev/null +++ b/src/util/arrayUtils.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +#ifdef __has_builtin + #if __has_builtin(__is_array) + #define ARRAY_SIZE(x) ({ static_assert(__is_array(__typeof__(x)), "Must be an array"); (uint32_t) (sizeof(x) / sizeof(*(x))); }) + #elif __has_builtin(__builtin_types_compatible_p) + #define ARRAY_SIZE(x) ({ static_assert(!__builtin_types_compatible_p(__typeof__(x), __typeof__(&*(x))), "Must not be a pointer"); (uint32_t) (sizeof(x) / sizeof(*(x))); }) + #endif +#endif +#ifndef ARRAY_SIZE + #define ARRAY_SIZE(x) ((uint32_t) (sizeof(x) / sizeof(*(x)))) +#endif