Skip to content

Commit

Permalink
prefer heap allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
bjia56 committed Jan 18, 2025
1 parent 14f114d commit 25d1ab1
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/linux/intel_gpu_top/intel_name_lookup_shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,24 @@

// Caller must free the returned pointer
char* find_intel_gpu_dir() {
const size_t path_len = 256;

DIR *dir;
struct dirent *entry;
char path[256];
char vendor_path[256];
char* path = (char*)malloc(path_len * sizeof(char));
char* vendor_path = (char*)malloc(path_len * sizeof(char));
char vendor_id[16];

if ((dir = opendir(SYSFS_PATH)) == NULL) {
perror("opendir");
free(path);
free(vendor_path);
return NULL;
}

while ((entry = readdir(dir)) != NULL) {
// Construct the path to the vendor file
snprintf(vendor_path, sizeof(vendor_path), "%s/%s/device/%s", SYSFS_PATH, entry->d_name, VENDOR_FILE);
snprintf(vendor_path, path_len, "%s/%s/device/%s", SYSFS_PATH, entry->d_name, VENDOR_FILE);

// Check if the vendor file exists
if (access(vendor_path, F_OK) != -1) {
Expand All @@ -57,8 +61,9 @@ char* find_intel_gpu_dir() {
fclose(file);
closedir(dir);
// Return the parent directory (i.e., /sys/class/drm/card*)
snprintf(path, sizeof(path), "%s/%s", SYSFS_PATH, entry->d_name);
return strdup(path);
snprintf(path, path_len, "%s/%s", SYSFS_PATH, entry->d_name);
free(vendor_path);
return path;
}
}
fclose(file);
Expand All @@ -67,6 +72,8 @@ char* find_intel_gpu_dir() {
}

closedir(dir);
free(path);
free(vendor_path);
return NULL; // Intel GPU not found
}

Expand Down

0 comments on commit 25d1ab1

Please sign in to comment.