From b065b3da515d42eb7689e1aa6c9e03268f01dcc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 9 Jan 2026 16:18:25 +0800 Subject: [PATCH] Global: optimises usage of `FFstrbuf` and `FFlist` --- src/common/FFlist.h | 7 +++++++ src/detection/dns/dns_linux.c | 2 +- src/detection/localip/localip_linux.c | 4 ++-- src/detection/packages/packages_linux.c | 2 +- src/fastfetch.c | 7 +++++-- src/logo/logo.c | 2 +- 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/common/FFlist.h b/src/common/FFlist.h index a105d6cd8..4ef1239bd 100644 --- a/src/common/FFlist.h +++ b/src/common/FFlist.h @@ -126,3 +126,10 @@ static inline void ffListClear(FFlist* list) assert(sizeof(itemType) == (listVar).elementSize); \ (itemType*) ffListAdd(&(listVar)); \ }) + +#define FF_LIST_FIRST(itemType, listVar) FF_LIST_GET(itemType, listVar, 0) +#define FF_LIST_LAST(itemType, listVar) \ + ({ \ + assert((listVar).length > 0); \ + FF_LIST_GET(itemType, listVar, ((listVar).length - 1)); \ + }) diff --git a/src/detection/dns/dns_linux.c b/src/detection/dns/dns_linux.c index b4c205cf6..986d14e0b 100644 --- a/src/detection/dns/dns_linux.c +++ b/src/detection/dns/dns_linux.c @@ -75,7 +75,7 @@ const char* ffDetectDNS(FFDNSOptions* options, FFlist* results) // Handle different DNS management services if (results->length == 1) { - const FFstrbuf* firstEntry = FF_LIST_GET(FFstrbuf, *results, 0); + const FFstrbuf* firstEntry = FF_LIST_FIRST(FFstrbuf, *results); if (ffStrbufEqualS(firstEntry, "127.0.0.53")) { diff --git a/src/detection/localip/localip_linux.c b/src/detection/localip/localip_linux.c index 2e525dd04..0d3ca66f9 100644 --- a/src/detection/localip/localip_linux.c +++ b/src/detection/localip/localip_linux.c @@ -462,7 +462,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) appendIpv4(options, &item->ipv4, ifa); else if (adapter->ipv4.length > 0) { - appendIpv4(options, &item->ipv4, *FF_LIST_GET(struct ifaddrs*, adapter->ipv4, 0)); + appendIpv4(options, &item->ipv4, *FF_LIST_FIRST(struct ifaddrs*, adapter->ipv4)); FF_DEBUG("Using first IPv4 address for interface %s", adapter->mac->ifa_name); } } @@ -527,7 +527,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) appendIpv6(options, &item->ipv6, selected); else if (adapter->ipv6.length > 0) { - appendIpv6(options, &item->ipv6, *FF_LIST_GET(struct ifaddrs*, adapter->ipv6, 0)); + appendIpv6(options, &item->ipv6, *FF_LIST_FIRST(struct ifaddrs*, adapter->ipv6)); FF_DEBUG("Using first IPv6 address for interface %s", adapter->mac->ifa_name); } } diff --git a/src/detection/packages/packages_linux.c b/src/detection/packages/packages_linux.c index 8203f89c4..e2f4a6b23 100644 --- a/src/detection/packages/packages_linux.c +++ b/src/detection/packages/packages_linux.c @@ -261,7 +261,7 @@ static uint32_t getAMUser(void) if (instance.state.platform.configDirs.length == 0) return 0; // check if $XDG_CONFIG_HOME/appman/appman-config exists - FFstrbuf* baseDir = FF_LIST_GET(FFstrbuf, instance.state.platform.configDirs, 0); + FFstrbuf* baseDir = FF_LIST_FIRST(FFstrbuf, instance.state.platform.configDirs); uint32_t baseLen = baseDir->length; ffStrbufAppendS(baseDir, "appman/appman-config"); FF_STRBUF_AUTO_DESTROY packagesPath = ffStrbufCreate(); diff --git a/src/fastfetch.c b/src/fastfetch.c index 2891010e0..840790bef 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -442,7 +442,9 @@ static void generateConfigFile(bool force, const char* filePath, bool fullConfig exit(477); } - ffStrbufSet(&instance.state.genConfigPath, FF_LIST_GET(FFstrbuf, instance.state.platform.configDirs, 0)); + FFstrbuf* configDir = FF_LIST_FIRST(FFstrbuf, instance.state.platform.configDirs); + ffStrbufEnsureFixedLengthFree(&instance.state.genConfigPath, configDir->length + strlen("fastfetch/config.jsonc")); + ffStrbufSet(&instance.state.genConfigPath, configDir); ffStrbufAppendS(&instance.state.genConfigPath, "fastfetch/config.jsonc"); } else @@ -486,7 +488,8 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val //Try to load as an absolute path - FF_STRBUF_AUTO_DESTROY absolutePath = ffStrbufCreateS(value); + FF_STRBUF_AUTO_DESTROY absolutePath = ffStrbufCreateA(128); + ffStrbufSetS(&absolutePath, value); bool strictJson = ffStrbufEndsWithIgnCaseS(&absolutePath, ".json"); bool jsonc = !strictJson && ffStrbufEndsWithIgnCaseS(&absolutePath, ".jsonc"); bool json5 = !strictJson && !jsonc && ffStrbufEndsWithIgnCaseS(&absolutePath, ".json5"); diff --git a/src/logo/logo.c b/src/logo/logo.c index c6d364746..1b09c6eea 100644 --- a/src/logo/logo.c +++ b/src/logo/logo.c @@ -487,7 +487,7 @@ static bool updateLogoPath(void) return true; } - FF_STRBUF_AUTO_DESTROY fullPath = ffStrbufCreate(); + FF_STRBUF_AUTO_DESTROY fullPath = ffStrbufCreateA(128); if (ffPathExpandEnv(options->source.chars, &fullPath) && ffPathExists(fullPath.chars, FF_PATHTYPE_FILE)) { ffStrbufDestroy(&options->source);