From 2b20ed873c5f0c6d81ed29bfb7262782d1c560f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 24 Mar 2025 22:25:56 +0800 Subject: [PATCH] GPU: update JSON result structure; fix segfault and memleaks --- src/detection/gpu/gpu.c | 1 + src/detection/opencl/opencl.c | 1 + src/detection/vulkan/vulkan.c | 1 + src/modules/gpu/gpu.c | 13 +++++++++---- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/detection/gpu/gpu.c b/src/detection/gpu/gpu.c index 1822cb447..bbf83e52d 100644 --- a/src/detection/gpu/gpu.c +++ b/src/detection/gpu/gpu.c @@ -55,6 +55,7 @@ const char* detectByOpenGL(FFlist* gpus) ffStrbufInitMove(&gpu->name, &result.renderer); ffStrbufInit(&gpu->driver); ffStrbufInitF(&gpu->platformApi, "OpenGL %s", result.version.chars); + ffStrbufInit(&gpu->memoryType); gpu->index = FF_GPU_INDEX_UNSET; gpu->temperature = FF_GPU_TEMP_UNSET; gpu->coreCount = FF_GPU_CORE_COUNT_UNSET; diff --git a/src/detection/opencl/opencl.c b/src/detection/opencl/opencl.c index 0c3700023..393202ebe 100644 --- a/src/detection/opencl/opencl.c +++ b/src/detection/opencl/opencl.c @@ -92,6 +92,7 @@ static const char* openCLHandleData(OpenCLData* data, FFOpenCLResult* result) ffStrbufInit(&gpu->vendor); ffStrbufInit(&gpu->driver); ffStrbufInit(&gpu->platformApi); + ffStrbufInit(&gpu->memoryType); gpu->index = FF_GPU_INDEX_UNSET; gpu->temperature = FF_GPU_TEMP_UNSET; gpu->coreCount = FF_GPU_CORE_COUNT_UNSET; diff --git a/src/detection/vulkan/vulkan.c b/src/detection/vulkan/vulkan.c index 3a613ee71..43747d241 100644 --- a/src/detection/vulkan/vulkan.c +++ b/src/detection/vulkan/vulkan.c @@ -220,6 +220,7 @@ static const char* detectVulkan(FFVulkanResult* result) gpu->type = physicalDeviceProperties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU ? FF_GPU_TYPE_DISCRETE : FF_GPU_TYPE_INTEGRATED; ffStrbufInitS(&gpu->vendor, ffGPUGetVendorString(physicalDeviceProperties.properties.vendorID)); ffStrbufInitS(&gpu->driver, driverProperties.driverInfo); + ffStrbufInit(&gpu->memoryType); VkPhysicalDeviceMemoryProperties memoryProperties = {}; ffvkGetPhysicalDeviceMemoryProperties(physicalDevices[i], &memoryProperties); diff --git a/src/modules/gpu/gpu.c b/src/modules/gpu/gpu.c index 77799b367..9049e7afb 100644 --- a/src/modules/gpu/gpu.c +++ b/src/modules/gpu/gpu.c @@ -194,6 +194,7 @@ void ffPrintGPU(FFGPUOptions* options) ffStrbufDestroy(&gpu->name); ffStrbufDestroy(&gpu->driver); ffStrbufDestroy(&gpu->platformApi); + ffStrbufDestroy(&gpu->memoryType); } } @@ -399,9 +400,6 @@ void ffGenerateGPUJsonResult(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_ else yyjson_mut_obj_add_null(doc, dedicatedObj, "used"); - yyjson_mut_obj_add_strbuf(doc, obj, "driver", &gpu->driver); - yyjson_mut_obj_add_strbuf(doc, obj, "name", &gpu->name); - yyjson_mut_val* sharedObj = yyjson_mut_obj_add_obj(doc, memoryObj, "shared"); if (gpu->shared.total != FF_GPU_VMEM_SIZE_UNSET) yyjson_mut_obj_add_uint(doc, sharedObj, "total", gpu->shared.total); @@ -412,7 +410,13 @@ void ffGenerateGPUJsonResult(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_ else yyjson_mut_obj_add_null(doc, sharedObj, "used"); - yyjson_mut_obj_add_strbuf(doc, obj, "memoryType", &gpu->memoryType); + if (gpu->memoryType.length) + yyjson_mut_obj_add_strbuf(doc, memoryObj, "type", &gpu->memoryType); + else + yyjson_mut_obj_add_null(doc, memoryObj, "type"); + + yyjson_mut_obj_add_strbuf(doc, obj, "driver", &gpu->driver); + yyjson_mut_obj_add_strbuf(doc, obj, "name", &gpu->name); if(gpu->temperature == gpu->temperature) //FF_GPU_TEMP_UNSET yyjson_mut_obj_add_real(doc, obj, "temperature", gpu->temperature); @@ -443,6 +447,7 @@ void ffGenerateGPUJsonResult(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_ ffStrbufDestroy(&gpu->name); ffStrbufDestroy(&gpu->driver); ffStrbufDestroy(&gpu->platformApi); + ffStrbufDestroy(&gpu->memoryType); } }