Modules: refactor module option handling for static allocation

Moves module option structs out of global config and removes dynamic allocation, using static buffers and size assertions for safer, simpler management.

Refactors module initialization and destruction to work with isolated option structs, eliminating the need for the previous modules container and related code.

Unifies module option header usage and updates detection logic to leverage statically-sized option buffers, improving encapsulation and maintainability.

Simplifies module function signatures and reduces coupling between modules and config.

Enhances future extensibility and reliability by enforcing max size constraints and removing unnecessary indirections.
This commit is contained in:
李通洲
2025-08-04 14:25:14 +08:00
parent 559082806b
commit 75d2c668d0
289 changed files with 1449 additions and 1672 deletions
+23 -24
View File
@@ -414,6 +414,29 @@ void ffGenerateGPUJsonResult(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_
}
}
void ffInitGPUOptions(FFGPUOptions* options)
{
ffOptionInitModuleArg(&options->moduleArgs, "󰾲");
options->driverSpecific = false;
options->detectionMethod =
#if defined(__x86_64__) || defined(__i386__)
FF_GPU_DETECTION_METHOD_PCI
#else
FF_GPU_DETECTION_METHOD_AUTO
#endif
;
options->temp = false;
options->hideType = FF_GPU_TYPE_NONE;
options->tempConfig = (FFColorRangeConfig) { 60, 80 };
options->percent = (FFPercentageModuleConfig) { 50, 80, 0 };
}
void ffDestroyGPUOptions(FFGPUOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
FFModuleBaseInfo ffGPUModuleInfo = {
.name = FF_GPU_MODULE_NAME,
.description = "Print GPU names, graphic memory size, type, etc",
@@ -446,27 +469,3 @@ FFModuleBaseInfo ffGPUModuleInfo = {
{"Memory type (Windows only)", "memory-type"},
})),
};
void ffInitGPUOptions(FFGPUOptions* options)
{
options->moduleInfo = ffGPUModuleInfo;
ffOptionInitModuleArg(&options->moduleArgs, "󰾲");
options->driverSpecific = false;
options->detectionMethod =
#if defined(__x86_64__) || defined(__i386__)
FF_GPU_DETECTION_METHOD_PCI
#else
FF_GPU_DETECTION_METHOD_AUTO
#endif
;
options->temp = false;
options->hideType = FF_GPU_TYPE_NONE;
options->tempConfig = (FFColorRangeConfig) { 60, 80 };
options->percent = (FFPercentageModuleConfig) { 50, 80, 0 };
}
void ffDestroyGPUOptions(FFGPUOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
+1 -1
View File
@@ -1,6 +1,6 @@
#pragma once
#include "fastfetch.h"
#include "option.h"
#define FF_GPU_MODULE_NAME "GPU"
+2 -3
View File
@@ -1,7 +1,5 @@
#pragma once
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
#include "common/option.h"
#include "common/percent.h"
@@ -24,7 +22,6 @@ typedef enum __attribute__((__packed__)) FFGPUDetectionMethod
typedef struct FFGPUOptions
{
FFModuleBaseInfo moduleInfo;
FFModuleArgs moduleArgs;
FFGPUType hideType;
@@ -35,3 +32,5 @@ typedef struct FFGPUOptions
FFColorRangeConfig tempConfig;
FFPercentageModuleConfig percent;
} FFGPUOptions;
static_assert(sizeof(FFGPUOptions) <= FF_OPTION_MAX_SIZE, "FFGPUOptions size exceeds maximum allowed size");