From 39582471e8572185bdcff6437f18210858fa374c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 21 Apr 2023 22:41:32 +0800 Subject: [PATCH] Global: major FFlist related code refactor --- src/detection/battery/battery_apple.c | 3 +-- src/detection/cpu/cpu_apple.c | 3 +-- src/detection/disk/disk_linux.c | 3 +-- src/detection/gpu/gpu_apple.c | 3 +-- src/modules/bluetooth/bluetooth.c | 5 +---- src/modules/brightness/brightness.c | 4 ++-- src/modules/gamepad/gamepad.c | 4 ++-- src/modules/localip/localip.c | 3 +-- src/modules/sound/sound.c | 7 +++---- src/modules/users.c | 3 +-- src/modules/wifi.c | 3 +-- src/util/FFlist.c | 8 -------- src/util/FFlist.h | 24 +++++++++++++++++++++--- tests/list.c | 7 +++++++ 14 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/detection/battery/battery_apple.c b/src/detection/battery/battery_apple.c index 8a4214180..af49b9a69 100644 --- a/src/detection/battery/battery_apple.c +++ b/src/detection/battery/battery_apple.c @@ -7,8 +7,7 @@ static double detectBatteryTemp() { - FF_LIST_AUTO_DESTROY temps; - ffListInit(&temps, sizeof(FFTempValue)); + FF_LIST_AUTO_DESTROY temps = ffListCreate(sizeof(FFTempValue)); ffDetectCoreTemps(FF_TEMP_BATTERY, &temps); diff --git a/src/detection/cpu/cpu_apple.c b/src/detection/cpu/cpu_apple.c index 7f3f3598f..fb53b60d2 100644 --- a/src/detection/cpu/cpu_apple.c +++ b/src/detection/cpu/cpu_apple.c @@ -15,8 +15,7 @@ static double getFrequency(const char* propName) static double detectCpuTemp(const FFstrbuf* cpuName) { - FF_LIST_AUTO_DESTROY temps; - ffListInit(&temps, sizeof(FFTempValue)); + FF_LIST_AUTO_DESTROY temps = ffListCreate(sizeof(FFTempValue)); if(ffStrbufStartsWithS(cpuName, "Apple M1")) ffDetectCoreTemps(FF_TEMP_CPU_M1X, &temps); diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index 2be87af66..210417ef4 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -201,8 +201,7 @@ void ffDetectDisksImpl(FFDiskResult* disks) return; } - FF_LIST_AUTO_DESTROY devices; - ffListInit(&devices, sizeof(FFstrbuf)); + FF_LIST_AUTO_DESTROY devices = ffListCreate(sizeof(FFstrbuf)); char* line = NULL; size_t len = 0; diff --git a/src/detection/gpu/gpu_apple.c b/src/detection/gpu/gpu_apple.c index 937b835f6..2245692b5 100644 --- a/src/detection/gpu/gpu_apple.c +++ b/src/detection/gpu/gpu_apple.c @@ -9,8 +9,7 @@ static double detectGpuTemp(const FFstrbuf* gpuName) { - FF_LIST_AUTO_DESTROY temps; - ffListInit(&temps, sizeof(FFTempValue)); + FF_LIST_AUTO_DESTROY temps = ffListCreate(sizeof(FFTempValue)); if(ffStrbufStartsWithS(gpuName, "Apple M1")) ffDetectCoreTemps(FF_TEMP_GPU_M1X, &temps); diff --git a/src/modules/bluetooth/bluetooth.c b/src/modules/bluetooth/bluetooth.c index 1b33db8bf..2318336b5 100644 --- a/src/modules/bluetooth/bluetooth.c +++ b/src/modules/bluetooth/bluetooth.c @@ -37,8 +37,7 @@ void ffPrintBluetooth(FFinstance* instance, FFBluetoothOptions* options) return; } - FFlist filtered; - ffListInit(&filtered, sizeof(FFBluetoothDevice*)); + FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFBluetoothDevice*)); FF_LIST_FOR_EACH(FFBluetoothDevice, device, bluetooth->devices) { @@ -59,8 +58,6 @@ void ffPrintBluetooth(FFinstance* instance, FFBluetoothOptions* options) uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1); printDevice(instance, options, *(FFBluetoothDevice**)ffListGet(&filtered, i), index); } - - ffListDestroy(&filtered); } void ffInitBluetoothOptions(FFBluetoothOptions* options) diff --git a/src/modules/brightness/brightness.c b/src/modules/brightness/brightness.c index 71176b922..34ae9f8f1 100644 --- a/src/modules/brightness/brightness.c +++ b/src/modules/brightness/brightness.c @@ -7,8 +7,8 @@ void ffPrintBrightness(FFinstance* instance, FFBrightnessOptions* options) { - FF_LIST_AUTO_DESTROY result; - ffListInit(&result, sizeof(FFBrightnessResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFBrightnessResult)); + const char* error = ffDetectBrightness(&result); if(error) diff --git a/src/modules/gamepad/gamepad.c b/src/modules/gamepad/gamepad.c index a0cd64f27..6d4b37c53 100644 --- a/src/modules/gamepad/gamepad.c +++ b/src/modules/gamepad/gamepad.c @@ -22,8 +22,8 @@ static void printDevice(FFinstance* instance, FFGamepadOptions* options, const F void ffPrintGamepad(FFinstance* instance, FFGamepadOptions* options) { - FF_LIST_AUTO_DESTROY result; - ffListInit(&result, sizeof(FFGamepadDevice)); + FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFGamepadDevice)); + const char* error = ffDetectGamepad(instance, &result); if(error) diff --git a/src/modules/localip/localip.c b/src/modules/localip/localip.c index 92ec3fe21..9b27da159 100644 --- a/src/modules/localip/localip.c +++ b/src/modules/localip/localip.c @@ -55,8 +55,7 @@ static void printIp(FFLocalIpResult* ip) void ffPrintLocalIp(FFinstance* instance, FFLocalIpOptions* options) { - FF_LIST_AUTO_DESTROY results; - ffListInit(&results, sizeof(FFLocalIpResult)); + FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFLocalIpResult)); const char* error = ffDetectLocalIps(options, &results); diff --git a/src/modules/sound/sound.c b/src/modules/sound/sound.c index 678c47b5d..9f5e1c5a4 100644 --- a/src/modules/sound/sound.c +++ b/src/modules/sound/sound.c @@ -37,8 +37,8 @@ static void printDevice(FFinstance* instance, FFSoundOptions* options, const FFS void ffPrintSound(FFinstance* instance, FFSoundOptions* options) { - FF_LIST_AUTO_DESTROY result; - ffListInit(&result, sizeof(FFSoundDevice)); + FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFSoundDevice)); + const char* error = ffDetectSound(instance, &result); if(error) @@ -47,8 +47,7 @@ void ffPrintSound(FFinstance* instance, FFSoundOptions* options) return; } - FF_LIST_AUTO_DESTROY filtered; - ffListInit(&filtered, sizeof(FFSoundDevice*)); + FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFSoundDevice*)); FF_LIST_FOR_EACH(FFSoundDevice, device, result) { diff --git a/src/modules/users.c b/src/modules/users.c index 7fd27c36e..87503d56b 100644 --- a/src/modules/users.c +++ b/src/modules/users.c @@ -7,8 +7,7 @@ void ffPrintUsers(FFinstance* instance) { - FF_LIST_AUTO_DESTROY users; - ffListInit(&users, sizeof(FFstrbuf)); + FF_LIST_AUTO_DESTROY users = ffListCreate(sizeof(FFstrbuf)); FF_STRBUF_AUTO_DESTROY error = ffStrbufCreate(); diff --git a/src/modules/wifi.c b/src/modules/wifi.c index 06c3768fc..d78d750b4 100644 --- a/src/modules/wifi.c +++ b/src/modules/wifi.c @@ -7,8 +7,7 @@ void ffPrintWifi(FFinstance* instance) { - FF_LIST_AUTO_DESTROY result; - ffListInit(&result, sizeof(FFWifiResult)); + FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFWifiResult)); const char* error = ffDetectWifi(instance, &result); if(error) diff --git a/src/util/FFlist.c b/src/util/FFlist.c index bd643014a..59f5a8b56 100644 --- a/src/util/FFlist.c +++ b/src/util/FFlist.c @@ -55,11 +55,3 @@ bool ffListPop(FFlist* list, void* result) --list->length; return result; } - -void ffListDestroy(FFlist* list) -{ - //Avoid free-after-use. These 3 assignments are cheap so don't remove them - list->capacity = list->length = 0; - free(list->data); - list->data = NULL; -} diff --git a/src/util/FFlist.h b/src/util/FFlist.h index 58612e347..eccd30b55 100644 --- a/src/util/FFlist.h +++ b/src/util/FFlist.h @@ -31,12 +31,20 @@ bool ffListShift(FFlist* list, void* result); // Removes the last element, and copy its value to `*result` bool ffListPop(FFlist* list, void* result); -void ffListDestroy(FFlist* list); - static inline void ffListInit(FFlist* list, uint32_t elementSize) { assert(elementSize > 0); - ffListInitA(list, elementSize, 0); + list->elementSize = elementSize; + list->capacity = 0; + list->length = 0; + list->data = NULL; +} + +static inline FFlist ffListCreate(uint32_t elementSize) +{ + FFlist result; + ffListInit(&result, elementSize); + return result; } static inline void* ffListGet(const FFlist* list, uint32_t index) @@ -50,6 +58,16 @@ static inline void ffListSort(FFlist* list, int(*compar)(const void*, const void qsort(list->data, list->length, list->elementSize, compar); } +static inline void ffListDestroy(FFlist* list) +{ + if (!list->data) return; + + //Avoid free-after-use. These 3 assignments are cheap so don't remove them + list->capacity = list->length = 0; + free(list->data); + list->data = NULL; +} + #define FF_LIST_FOR_EACH(itemType, itemVarName, listVar) \ assert(sizeof(itemType) == (listVar).elementSize); \ for(itemType* itemVarName = (itemType*)(listVar).data; \ diff --git a/tests/list.c b/tests/list.c index db4db544a..4ba879097 100644 --- a/tests/list.c +++ b/tests/list.c @@ -108,6 +108,13 @@ int main(void) VERIFY(list.capacity == 0); VERIFY(list.length == 0); + { + FF_LIST_AUTO_DESTROY test = ffListCreate(1); + VERIFY(test.elementSize = 1); + VERIFY(test.capacity == 0); + VERIFY(test.length == 0); + } + //Success puts("\033[32mAll tests passed!"FASTFETCH_TEXT_MODIFIER_RESET); }