mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 10:52:08 +02:00
Top: removes realloc retries
This commit is contained in:
@@ -10,33 +10,25 @@ const char* ffTopGetProcessSnapshot(FFlist* snapshots, FFTopTypes) {
|
||||
size_t length;
|
||||
|
||||
if (sysctl(request, ARRAY_SIZE(request), nullptr, &length, nullptr, 0) != 0) {
|
||||
return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_ALL}) failed";
|
||||
return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_ALL, nullptr}) failed";
|
||||
}
|
||||
|
||||
// The process table may change between the two sysctl calls; retry with a larger buffer.
|
||||
FF_AUTO_FREE struct kinfo_proc* processes = nullptr;
|
||||
for (int attempts = 0;; ++attempts) {
|
||||
length += length / 8 + sizeof(struct kinfo_proc);
|
||||
struct kinfo_proc* newProcesses = (struct kinfo_proc*) realloc(processes, length);
|
||||
if (!newProcesses) {
|
||||
return "realloc(struct kinfo_proc[]) failed";
|
||||
}
|
||||
processes = newProcesses;
|
||||
if (sysctl(request, ARRAY_SIZE(request), processes, &length, nullptr, 0) == 0) {
|
||||
break;
|
||||
}
|
||||
if ((errno != ENOMEM && errno != EINVAL) || attempts >= 4) {
|
||||
return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_ALL}) failed";
|
||||
}
|
||||
length += length / 8 + sizeof(struct kinfo_proc);
|
||||
FF_AUTO_FREE struct kinfo_proc* processes = malloc(length);
|
||||
|
||||
if (sysctl(request, ARRAY_SIZE(request), processes, &length, nullptr, 0) != 0) {
|
||||
return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_ALL, processes}) failed";
|
||||
}
|
||||
|
||||
uint32_t count = (uint32_t) (length / sizeof(struct kinfo_proc));
|
||||
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
const struct kinfo_proc* proc = &processes[i];
|
||||
pid_t pid = proc->kp_proc.p_pid;
|
||||
if (pid <= 0) {
|
||||
if (proc->kp_proc.p_flag & P_SYSTEM) {
|
||||
continue;
|
||||
}
|
||||
pid_t pid = proc->kp_proc.p_pid;
|
||||
|
||||
struct rusage_info_v2 rusage;
|
||||
if (proc_pid_rusage(pid, RUSAGE_INFO_V2, (rusage_info_t*) &rusage) != 0) {
|
||||
|
||||
@@ -15,20 +15,10 @@ const char* ffTopGetProcessSnapshot(FFlist* snapshots, FFTopTypes) {
|
||||
}
|
||||
|
||||
// The process table may change between the two sysctl calls; retry with a larger buffer.
|
||||
FF_AUTO_FREE struct kinfo_proc* processes = nullptr;
|
||||
for (int attempts = 0;; ++attempts) {
|
||||
length += length / 8 + sizeof(struct kinfo_proc);
|
||||
struct kinfo_proc* newProcesses = (struct kinfo_proc*) realloc(processes, length);
|
||||
if (!newProcesses) {
|
||||
return "realloc(struct kinfo_proc[]) failed";
|
||||
}
|
||||
processes = newProcesses;
|
||||
if (sysctl(request, ARRAY_SIZE(request), processes, &length, nullptr, 0) == 0) {
|
||||
break;
|
||||
}
|
||||
if ((errno != ENOMEM && errno != EINVAL) || attempts >= 4) {
|
||||
return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_PROC}) failed";
|
||||
}
|
||||
length += length / 8 + sizeof(struct kinfo_proc);
|
||||
FF_AUTO_FREE struct kinfo_proc* processes = malloc(length);
|
||||
if (sysctl(request, ARRAY_SIZE(request), processes, &length, nullptr, 0) != 0) {
|
||||
return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_PROC}) failed";
|
||||
}
|
||||
uint32_t count = (uint32_t) (length / sizeof(struct kinfo_proc));
|
||||
|
||||
|
||||
@@ -11,24 +11,15 @@ const char* ffTopGetProcessSnapshot(FFlist* snapshots, FFTopTypes) {
|
||||
size_t length;
|
||||
|
||||
if (sysctl(request, ARRAY_SIZE(request), nullptr, &length, nullptr, 0) != 0) {
|
||||
return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_ALL}) failed";
|
||||
return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_ALL, nullptr}) failed";
|
||||
}
|
||||
|
||||
// The process table may change between the two sysctl calls; retry with a larger buffer.
|
||||
FF_AUTO_FREE struct kinfo_proc* processes = nullptr;
|
||||
for (int attempts = 0;; ++attempts) {
|
||||
length += length / 8 + sizeof(struct kinfo_proc);
|
||||
struct kinfo_proc* newProcesses = (struct kinfo_proc*) realloc(processes, length);
|
||||
if (!newProcesses) {
|
||||
return "realloc(struct kinfo_proc[]) failed";
|
||||
}
|
||||
processes = newProcesses;
|
||||
if (sysctl(request, ARRAY_SIZE(request), processes, &length, nullptr, 0) == 0) {
|
||||
break;
|
||||
}
|
||||
if ((errno != ENOMEM && errno != EINVAL) || attempts >= 4) {
|
||||
return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_ALL}) failed";
|
||||
}
|
||||
length += length / 8 + sizeof(struct kinfo_proc);
|
||||
|
||||
FF_AUTO_FREE struct kinfo_proc* processes = malloc(length);
|
||||
if (sysctl(request, ARRAY_SIZE(request), processes, &length, nullptr, 0) != 0) {
|
||||
return "sysctl({CTL_KERN, KERN_PROC, KERN_PROC_ALL, processes}) failed";
|
||||
}
|
||||
uint32_t count = (uint32_t) (length / sizeof(struct kinfo_proc));
|
||||
|
||||
|
||||
@@ -11,24 +11,15 @@ const char* ffTopGetProcessSnapshot(FFlist* snapshots, FFTopTypes) {
|
||||
size_t length;
|
||||
|
||||
if (sysctl(request, ARRAY_SIZE(request), nullptr, &length, nullptr, 0) != 0) {
|
||||
return "sysctl({CTL_KERN, KERN_PROC2, KERN_PROC_ALL}) failed";
|
||||
return "sysctl({CTL_KERN, KERN_PROC2, KERN_PROC_ALL, nullptr}) failed";
|
||||
}
|
||||
|
||||
// The process table may change between the two sysctl calls; retry with a larger buffer.
|
||||
FF_AUTO_FREE struct kinfo_proc2* processes = nullptr;
|
||||
for (int attempts = 0;; ++attempts) {
|
||||
length += length / 8 + sizeof(struct kinfo_proc2);
|
||||
struct kinfo_proc2* newProcesses = (struct kinfo_proc2*) realloc(processes, length);
|
||||
if (!newProcesses) {
|
||||
return "realloc(struct kinfo_proc2[]) failed";
|
||||
}
|
||||
processes = newProcesses;
|
||||
if (sysctl(request, ARRAY_SIZE(request), processes, &length, nullptr, 0) == 0) {
|
||||
break;
|
||||
}
|
||||
if ((errno != ENOMEM && errno != EINVAL) || attempts >= 4) {
|
||||
return "sysctl({CTL_KERN, KERN_PROC2, KERN_PROC_ALL}) failed";
|
||||
}
|
||||
length += length / 8 + sizeof(struct kinfo_proc2);
|
||||
|
||||
FF_AUTO_FREE struct kinfo_proc2* processes = malloc(length);
|
||||
if (sysctl(request, ARRAY_SIZE(request), processes, &length, nullptr, 0) != 0) {
|
||||
return "sysctl({CTL_KERN, KERN_PROC2, KERN_PROC_ALL, processes}) failed";
|
||||
}
|
||||
uint32_t count = (uint32_t) (length / sizeof(struct kinfo_proc2));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user