[PATCH] simplify expand_tcbtab/alloc_tcb

Denys Vlasenko dvlasenk at redhat.com
Tue Jun 21 18:21:26 UTC 2011


Hi,

Sometime ago expand_tcbtab/alloc_tcb were modified
so that they never return failure (they will abort instead).

This opens up a possibility for further simplifications.

This patch is a simple one which gets rid of a few
intermediate variables, simplifies a few expressions,
and uses error_msg_and_die instead of more verbose
fprintf+cleanup+exit sequence.

In alloc_tcp, I use memset to clear entire new tcp.
This not only saves a few bytes of code, but lowers chances
of future bugs where some data "leaks out" into new tcb's
from old ones because we forgot to re-initialize it.

Patch should be fairly safe, but please review anyway.
-- 
vda

diff -d -urpN strace.6/strace.c strace.7/strace.c
--- strace.6/strace.c	2011-06-21 20:12:03.206604267 +0200
+++ strace.7/strace.c	2011-06-21 20:11:27.220751557 +0200
@@ -1252,21 +1252,14 @@ expand_tcbtab(void)
 	   callers have pointers and it would be a pain.
 	   So tcbtab is a table of pointers.  Since we never
 	   free the TCBs, we allocate a single chunk of many.  */
-	struct tcb **newtab = (struct tcb **)
-		realloc(tcbtab, 2 * tcbtabsize * sizeof tcbtab[0]);
-	struct tcb *newtcbs = (struct tcb *) calloc(tcbtabsize,
-						    sizeof *newtcbs);
-	int i;
-	if (newtab == NULL || newtcbs == NULL) {
-		fprintf(stderr, "%s: expand_tcbtab: out of memory\n",
-			progname);
-		cleanup();
-		exit(1);
-	}
-	for (i = tcbtabsize; i < 2 * tcbtabsize; ++i)
-		newtab[i] = &newtcbs[i - tcbtabsize];
+	int i = tcbtabsize;
+	struct tcb *newtcbs = calloc(tcbtabsize, sizeof newtcbs[0]);
 	tcbtabsize *= 2;
-	tcbtab = newtab;
+	tcbtab = realloc(tcbtab, tcbtabsize * sizeof tcbtab[0]);
+	if (tcbtab == NULL || newtcbs == NULL)
+		error_msg_and_die("expand_tcbtab: out of memory");
+	while (i < tcbtabsize)
+		tcbtab[i++] = newtcbs++;
 }
 
 struct tcb *
@@ -1281,17 +1274,10 @@ alloc_tcb(int pid, int command_options_p
 	for (i = 0; i < tcbtabsize; i++) {
 		tcp = tcbtab[i];
 		if ((tcp->flags & TCB_INUSE) == 0) {
+			memset(tcp, 0, sizeof(*tcp));
 			tcp->pid = pid;
-			tcp->parent = NULL;
-			tcp->nchildren = 0;
-#ifdef TCB_CLONE_THREAD
-			tcp->nclone_threads = 0;
-#endif
 			tcp->flags = TCB_INUSE | TCB_STARTUP;
 			tcp->outf = outf; /* Initialise to current out file */
-			tcp->curcol = 0;
-			tcp->stime.tv_sec = 0;
-			tcp->stime.tv_usec = 0;
 			tcp->pfd = -1;
 			nprocs++;
 			if (command_options_parsed)
@@ -1299,9 +1285,7 @@ alloc_tcb(int pid, int command_options_p
 			return tcp;
 		}
 	}
-	fprintf(stderr, "%s: bug in alloc_tcb\n", progname);
-	cleanup();
-	exit(1);
+	error_msg_and_die("bug in alloc_tcb");
 }
 
 #ifdef USE_PROCFS





More information about the Strace-devel mailing list