[CRIU] [PATCH 2/3] files-reg: Simplify have_seen_dead_pid

Cyrill Gorcunov gorcunov at openvz.org
Tue Sep 23 10:00:24 PDT 2014


We've a special helper xrealloc_safe for reallocs.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 files-reg.c | 28 ++++++++--------------------
 1 file changed, 8 insertions(+), 20 deletions(-)

diff --git a/files-reg.c b/files-reg.c
index 84121f151faa..6eb51f9915f3 100644
--- a/files-reg.c
+++ b/files-reg.c
@@ -554,29 +554,17 @@ static int have_seen_dead_pid(pid_t pid)
 {
 	static pid_t *dead_pids = NULL;
 	static int n_dead_pids = 0;
+	size_t i;
 
-	if (dead_pids) {
-		int i;
-		void *m;
-
-		for (i = 0; i < n_dead_pids; i++)
-			if (dead_pids[i] == pid)
-				return 1;
-
-		m = realloc(dead_pids, sizeof(*dead_pids) * (n_dead_pids + 1));
-		if (!m)
-			return -1;
-
-		dead_pids = m;
-		dead_pids[n_dead_pids++] = pid;
-	} else {
-		dead_pids = malloc(sizeof(*dead_pids));
-		if (!dead_pids)
-			return -1;
-		*dead_pids = pid;
-		n_dead_pids++;
+	for (i = 0; i < n_dead_pids; i++) {
+		if (dead_pids[i] == pid)
+			return 1;
 	}
 
+	if (xrealloc_safe(&dead_pids, sizeof(*dead_pids) * (n_dead_pids + 1)))
+		return -1;
+	dead_pids[n_dead_pids++] = pid;
+
 	return 0;
 }
 
-- 
1.9.3



More information about the CRIU mailing list