From 0a0c1745b36fe0e0ba41eba409c49b0aac6b350d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 25 Mar 2025 09:19:32 +0800 Subject: [PATCH] GPU (macOS): fix code smell --- src/detection/gpu/gpu_apple.c | 23 ++++++++++++----------- src/detection/gpu/gpu_apple.m | 1 + src/util/apple/cf_helpers.h | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/detection/gpu/gpu_apple.c b/src/detection/gpu/gpu_apple.c index 909fc6a5f..026f9acad 100644 --- a/src/detection/gpu/gpu_apple.c +++ b/src/detection/gpu/gpu_apple.c @@ -18,11 +18,12 @@ static double detectGpuTemp(const FFstrbuf* gpuName) { switch (strtol(gpuName->chars + strlen("Apple M"), NULL, 10)) { + case 0: error = "Invalid Apple Silicon GPU"; break; case 1: error = ffDetectSmcTemps(FF_TEMP_GPU_M1X, &result); break; case 2: error = ffDetectSmcTemps(FF_TEMP_GPU_M2X, &result); break; case 3: error = ffDetectSmcTemps(FF_TEMP_GPU_M3X, &result); break; case 4: error = ffDetectSmcTemps(FF_TEMP_GPU_M4X, &result); break; - default: error = "Unsupported Apple Silicon GPU"; + default: error = "Unsupported Apple Silicon GPU"; break; } } else if (ffStrbufStartsWithS(gpuName, "Intel")) @@ -102,7 +103,7 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) } FFGPUResult* gpu = ffListAdd(gpus); - + gpu->index = FF_GPU_INDEX_UNSET; ffStrbufInit(&gpu->memoryType); gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET; gpu->type = FF_GPU_TYPE_UNKNOWN; @@ -113,7 +114,7 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) ffStrbufInit(&gpu->driver); // Ok for both Apple and Intel ffCfDictGetString(properties, CFSTR("CFBundleIdentifier"), &gpu->driver); - if(ffCfDictGetInt(properties, CFSTR("gpu-core-count"), &gpu->coreCount)) // For Apple + if(ffCfDictGetInt(properties, CFSTR("gpu-core-count"), &gpu->coreCount) != NULL) // For Apple gpu->coreCount = FF_GPU_CORE_COUNT_UNSET; gpu->coreUsage = FF_GPU_CORE_USAGE_UNSET; @@ -132,9 +133,9 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) if (ffCfDictGetInt64(perfStatistics, CFSTR("In use system memory"), (int64_t*) &vramUsed) != NULL) vramTotal = 0; } - else if (ffCfDictGetInt64(perfStatistics, CFSTR("vramUsedBytes"), (int64_t*) &vramTotal) == NULL) + else if (ffCfDictGetInt64(perfStatistics, CFSTR("vramFreeBytes"), (int64_t*) &vramTotal) == NULL) { - if (ffCfDictGetInt64(perfStatistics, CFSTR("vramFreeBytes"), (int64_t*) &vramUsed) == NULL) + if (ffCfDictGetInt64(perfStatistics, CFSTR("vramUsedBytes"), (int64_t*) &vramUsed) == NULL) vramTotal += vramUsed; else vramTotal = 0; @@ -144,15 +145,15 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) ffStrbufInit(&gpu->name); //IOAccelerator returns model / vendor-id properties for Apple Silicon, but not for Intel Iris GPUs. //Still needs testing for AMD's - if(ffCfDictGetString(properties, CFSTR("model"), &gpu->name)) + if(ffCfDictGetString(properties, CFSTR("model"), &gpu->name) != NULL) { CFRelease(properties); + properties = NULL; - io_registry_entry_t parentEntry; - IORegistryEntryGetParentEntry(registryEntry, kIOServicePlane, &parentEntry); - if(IORegistryEntryCreateCFProperties(parentEntry, &properties, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess) + FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t parentEntry = 0; + if(IORegistryEntryGetParentEntry(registryEntry, kIOServicePlane, &parentEntry) != kIOReturnSuccess || + IORegistryEntryCreateCFProperties(parentEntry, &properties, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess) { - IOObjectRelease(parentEntry); IOObjectRelease(registryEntry); continue; } @@ -180,7 +181,7 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) gpu->shared.total = vramTotal; gpu->shared.used = vramUsed; } - else + else if (gpu->type == FF_GPU_TYPE_DISCRETE) { gpu->dedicated.total = vramTotal; gpu->dedicated.used = vramUsed; diff --git a/src/detection/gpu/gpu_apple.m b/src/detection/gpu/gpu_apple.m index 943bd16c5..7adfde58a 100644 --- a/src/detection/gpu/gpu_apple.m +++ b/src/detection/gpu/gpu_apple.m @@ -29,6 +29,7 @@ const char* ffGpuDetectDriverVersion(FFlist* gpus) ffStrbufAppendS(&x->driver, version.UTF8String); } } + return NULL; } return "Unsupported macOS version"; } diff --git a/src/util/apple/cf_helpers.h b/src/util/apple/cf_helpers.h index c509bd574..5f6cb291c 100644 --- a/src/util/apple/cf_helpers.h +++ b/src/util/apple/cf_helpers.h @@ -28,7 +28,7 @@ static inline void cfReleaseWrapper(void* type) #define FF_CFTYPE_AUTO_RELEASE __attribute__((__cleanup__(cfReleaseWrapper))) -static inline void wrapIoObjectRelease(io_service_t* service) +static inline void wrapIoObjectRelease(io_object_t* service) { assert(service); if (*service)