In the pvwatchfd_s structure, use booleans "displayable" and "unused" instead of having values of watch_pid=0 for unused and watch_fd=-1 for not displayable, so that each item only means one thing.

This commit is contained in:
Andrew Wood
2025-09-26 23:01:07 +01:00
parent c61c589378
commit 7fdb6716e1
3 changed files with 29 additions and 16 deletions
+3 -1
View File
@@ -514,7 +514,9 @@ struct pvwatchfd_s {
off_t position; /* position last seen at */
struct timespec start_time; /* time we started watching the fd */
pid_t watch_pid; /* PID to watch */
int watch_fd; /* fd to watch, -1 = not displayed */
int watch_fd; /* fd to watch */
bool displayable; /* false if not displayable */
bool unused; /* true if free for re-use */
};
typedef struct pvwatchfd_s *pvwatchfd_t;
+15 -7
View File
@@ -618,6 +618,8 @@ int pv_watchfd_loop(pvstate_t state)
memset(&info, 0, sizeof(info));
info.watch_pid = state->watchfd.pid[0];
info.watch_fd = state->watchfd.fd[0];
info.displayable = true;
info.unused = false;
pv_reset_watchfd(&info);
rc = pv_watchfd_info(state, &info, false);
if (0 != rc) {
@@ -900,7 +902,7 @@ int pv_watchpid_loop(pvstate_t state)
state->control.height = new_height;
for (idx = 0; NULL != info_array && idx < array_length; idx++) {
if (info_array[idx].watch_fd < 0)
if (!info_array[idx].displayable)
continue;
pv_watchpid_setname(state, &(info_array[idx]));
info_array[idx].flags.reparse_display = 1;
@@ -934,15 +936,20 @@ int pv_watchpid_loop(pvstate_t state)
if (idx < 0)
continue;
if (info_array[idx].watch_fd < 0) {
if (info_array[idx].unused) {
debug("%s %d: %s", "fd", fd, "unused array entry - skipping");
continue;
}
if (!info_array[idx].displayable) {
/*
* Non-displayable fd - just remove if
* changed
*/
if (pv_watchfd_changed(&(info_array[idx]))) {
fd_to_idx[fd] = -1;
info_array[idx].watch_pid = 0;
info_array[idx].watch_fd = -1;
info_array[idx].unused = true;
info_array[idx].displayable = false;
pv_freecontents_watchfd(&(info_array[idx]));
debug("%s %d: %s", "fd", fd, "removing");
}
@@ -962,8 +969,8 @@ int pv_watchpid_loop(pvstate_t state)
if (position_now < 0) {
fd_to_idx[fd] = -1;
info_array[idx].watch_pid = 0;
info_array[idx].watch_fd = -1;
info_array[idx].unused = true;
info_array[idx].displayable = false;
pv_freecontents_watchfd(&(info_array[idx]));
debug("%s %d: %s", "fd", fd, "removing");
continue;
@@ -1069,7 +1076,8 @@ int pv_watchpid_loop(pvstate_t state)
*/
for (idx = 0; NULL != info_array && idx < array_length; idx++) {
pv_freecontents_watchfd(&(info_array[idx]));
info_array[idx].watch_fd = -1;
info_array[idx].unused = true;
info_array[idx].displayable = false;
}
if (NULL != info_array)
+11 -8
View File
@@ -353,6 +353,8 @@ static bool extend_info_array(int *array_length_ptr, pvwatchfd_t * info_array_pt
return false;
}
new_info_array[array_length - 1].unused = true;
debug("%s", "extended info array");
*info_array_ptr = new_info_array;
@@ -466,12 +468,11 @@ int pv_watchpid_scanfds(pvstate_t state,
}
/*
* See if there's an empty slot we can re-use. An empty slot
* has a watch_pid of 0.
* See if there's an empty slot we can re-use.
*/
use_idx = -1;
for (check_idx = 0; check_idx < array_length; check_idx++) {
if (info_array[check_idx].watch_pid == 0) {
if (info_array[check_idx].unused) {
use_idx = check_idx;
break;
}
@@ -498,6 +499,8 @@ int pv_watchpid_scanfds(pvstate_t state,
pv_reset_watchfd(&(info_array[use_idx]));
info_array[use_idx].watch_pid = watch_pid;
info_array[use_idx].watch_fd = fd;
info_array[use_idx].unused = false;
info_array[use_idx].displayable = true;
/*
* Set the average rate window so that a new history buffer
@@ -520,20 +523,20 @@ int pv_watchpid_scanfds(pvstate_t state,
if ((rc != 0) && (rc != 4)) {
debug("%s %d: %s: %d", "fd", fd, "lookup failed - marking slot for re-use", use_idx);
pv_freecontents_watchfd(&(info_array[use_idx]));
info_array[use_idx].watch_pid = 0;
info_array[use_idx].watch_fd = -1;
info_array[use_idx].unused = true;
info_array[use_idx].displayable = false;
continue;
}
fd_to_idx[fd] = use_idx;
/*
* Not displayable - set fd to -1 so the main loop doesn't
* show it.
* Not displayable - mark it as such so the main loop
* doesn't show it.
*/
if (rc != 0) {
debug("%s %d: %s", "fd", fd, "marking as not displayable");
info_array[use_idx].watch_fd = -1;
info_array[use_idx].displayable = false;
}
/* Set the info display_name appropriately. */