[PATCH] Introduce xmalloc, memory allocator with die_out_of_memory()

Masatake YAMATO yamato at redhat.com
Fri Mar 20 05:53:39 UTC 2015


In strace following code sentences are frequently used:

   var = malloc(fdsize);
    if (!var)
       die_out_of_memory();

This patch introduces xmalloc and friends which simplify
above sentences like:

   var = xmalloc(fdsize);

Here friends are xcalloc and xrealloc.

* defs.h: (xmalloc, xcalloc, xrealloc): New declarations.
* strace.c (xmalloc, xcalloc, xrealloc): New functions.

* vsprintf.c (strace_vfprintf): Use xmalloc.
* util.c (printstr): Use xmalloc.
* unwind.c (unwind_tcb_init): Use xmalloc.
(build_mmap_cache): Use xmalloc and xrealloc.
(get_symbol_name): Use xrealloc.
* syscall.c (reallocate_qual): Use xrealloc.
* strace.c (expand_tcbtab): Use xcalloc and xrealloc.
(init): Use xcalloc and xmalloc.
* pathtrace.c (storepath): Use xrealloc.
(pathtrace_match): Use xmalloc.
* dirent.c (sys_getdents): Use xmalloc.
(sys_getdents64): Use xmalloc.
* desc.c (decode_select): Use xmalloc.
* count.c (count_syscall): Use xcalloc.
(call_summary_pers): Use xcalloc.

Signed-off-by: Masatake YAMATO <yamato at redhat.com>
---
 count.c     | 11 +++--------
 defs.h      |  7 +++++++
 desc.c      | 11 +++--------
 dirent.c    |  8 ++------
 pathtrace.c |  8 ++------
 strace.c    | 46 ++++++++++++++++++++++++++++++----------------
 syscall.c   |  4 +---
 unwind.c    | 24 ++++++------------------
 util.c      |  8 ++------
 vsprintf.c  |  4 +---
 10 files changed, 57 insertions(+), 74 deletions(-)

diff --git a/count.c b/count.c
index 25921b2..1232d3d 100644
--- a/count.c
+++ b/count.c
@@ -58,11 +58,8 @@ count_syscall(struct tcb *tcp, const struct timeval *syscall_exiting_tv)
 	if (!SCNO_IN_RANGE(scno))
 		return;
 
-	if (!counts) {
-		counts = calloc(nsyscalls, sizeof(*counts));
-		if (!counts)
-			die_out_of_memory();
-	}
+	if (!counts)
+		counts = xcalloc(nsyscalls, sizeof(*counts));
 	cc = &counts[scno];
 
 	cc->calls++;
@@ -171,9 +168,7 @@ call_summary_pers(FILE *outf)
 	fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
 		dashes, dashes, dashes, dashes, dashes, dashes);
 
-	sorted_count = calloc(sizeof(int), nsyscalls);
-	if (!sorted_count)
-		die_out_of_memory();
+	sorted_count = xcalloc(sizeof(int), nsyscalls);
 	call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
 	if (overhead.tv_sec == -1) {
 		tv_mul(&overhead, &shortest, 8);
diff --git a/defs.h b/defs.h
index dad4fe8..afc1247 100644
--- a/defs.h
+++ b/defs.h
@@ -398,6 +398,13 @@ void error_msg_and_die(const char *fmt, ...) __attribute__ ((noreturn, format(pr
 void perror_msg_and_die(const char *fmt, ...) __attribute__ ((noreturn, format(printf, 1, 2)));
 void die_out_of_memory(void) __attribute__ ((noreturn));
 
+/*
+ * Memory allocator + die_out_of_memory
+ */
+void *xmalloc(size_t size);
+void *xcalloc(size_t nmmeb, size_t size);
+void *xrealloc(void *ptr, size_t size);
+
 #if USE_CUSTOM_PRINTF
 /*
  * See comment in vsprintf.c for allowed formats.
diff --git a/desc.c b/desc.c
index 24de51d..fafb4ec 100644
--- a/desc.c
+++ b/desc.c
@@ -338,11 +338,8 @@ decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
 	if (entering(tcp)) {
 		tprintf("%d", (int) args[0]);
 
-		if (verbose(tcp) && fdsize > 0) {
-			fds = malloc(fdsize);
-			if (!fds)
-				die_out_of_memory();
-		}
+		if (verbose(tcp) && fdsize > 0)
+			fds = xmalloc(fdsize);
 		for (i = 0; i < 3; i++) {
 			arg = args[i+1];
 			if (arg == 0) {
@@ -387,9 +384,7 @@ decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
 			return RVAL_STR;
 		}
 
-		fds = malloc(fdsize);
-		if (!fds)
-			die_out_of_memory();
+		fds = xmalloc(fdsize);
 
 		outptr = outstr;
 		sep = "";
diff --git a/dirent.c b/dirent.c
index d30e0d2..2b6acb6 100644
--- a/dirent.c
+++ b/dirent.c
@@ -83,9 +83,7 @@ sys_getdents(struct tcb *tcp)
 		len = tcp->u_rval;
 
 	if (len) {
-		buf = malloc(len);
-		if (!buf)
-			die_out_of_memory();
+		buf = xmalloc(len);
 		if (umoven(tcp, tcp->u_arg[1], len, buf) < 0) {
 			tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
 			free(buf);
@@ -167,9 +165,7 @@ sys_getdents64(struct tcb *tcp)
 		len = tcp->u_rval;
 
 	if (len) {
-		buf = malloc(len);
-		if (!buf)
-			die_out_of_memory();
+		buf = xmalloc(len);
 		if (umoven(tcp, tcp->u_arg[1], len, buf) < 0) {
 			tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
 			free(buf);
diff --git a/pathtrace.c b/pathtrace.c
index 28fc7c9..a70e8b0 100644
--- a/pathtrace.c
+++ b/pathtrace.c
@@ -91,9 +91,7 @@ storepath(const char *path)
 		return; /* already in table */
 
 	i = num_selected++;
-	paths_selected = realloc(paths_selected, num_selected * sizeof(paths_selected[0]));
-	if (!paths_selected)
-		die_out_of_memory();
+	paths_selected = xrealloc(paths_selected, num_selected * sizeof(paths_selected[0]));
 	paths_selected[i] = path;
 }
 
@@ -287,9 +285,7 @@ pathtrace_match(struct tcb *tcp)
 		if (nfds > 1024*1024)
 			nfds = 1024*1024;
 		fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
-		fds = malloc(fdsize);
-		if (!fds)
-			die_out_of_memory();
+		fds = xmalloc(fdsize);
 
 		for (i = 1; i <= 3; ++i) {
 			if (args[i] == 0)
diff --git a/strace.c b/strace.c
index b714255..c766da2 100644
--- a/strace.c
+++ b/strace.c
@@ -333,6 +333,30 @@ void die_out_of_memory(void)
 	error_msg_and_die("Out of memory");
 }
 
+void *xmalloc(size_t size)
+{
+	void *r = malloc(size);
+	if (!r)
+		die_out_of_memory();
+	return r;
+}
+
+void *xcalloc(size_t nmmeb, size_t size)
+{
+	void *r = calloc(nmmeb, size);
+	if (!r)
+		die_out_of_memory();
+	return r;
+}
+
+void *xrealloc(void *ptr, size_t size)
+{
+	void *r= realloc(ptr, size);
+	if (!r)
+		die_out_of_memory();
+	return r;
+}
+
 static void
 error_opt_arg(int opt, const char *arg)
 {
@@ -676,10 +700,8 @@ expand_tcbtab(void)
 	   So tcbtab is a table of pointers.  Since we never
 	   free the TCBs, we allocate a single chunk of many.  */
 	unsigned int i = tcbtabsize;
-	struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0]));
-	struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
-	if (!newtab || !newtcbs)
-		die_out_of_memory();
+	struct tcb *newtcbs = xcalloc(tcbtabsize, sizeof(newtcbs[0]));
+	struct tcb **newtab = xrealloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
 	tcbtabsize *= 2;
 	tcbtab = newtab;
 	while (i < tcbtabsize)
@@ -1444,12 +1466,8 @@ init(int argc, char *argv[])
 
 	/* Allocate the initial tcbtab.  */
 	tcbtabsize = argc;	/* Surely enough for all -p args.  */
-	tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
-	if (!tcbtab)
-		die_out_of_memory();
-	tcp = calloc(tcbtabsize, sizeof(*tcp));
-	if (!tcp)
-		die_out_of_memory();
+	tcbtab = xcalloc(tcbtabsize, sizeof(tcbtab[0]));
+	tcp = xcalloc(tcbtabsize, sizeof(*tcp));
 	for (tcbi = 0; tcbi < tcbtabsize; tcbi++)
 		tcbtab[tcbi] = tcp++;
 
@@ -1595,9 +1613,7 @@ init(int argc, char *argv[])
 	argv += optind;
 	/* argc -= optind; - no need, argc is not used below */
 
-	acolumn_spaces = malloc(acolumn + 1);
-	if (!acolumn_spaces)
-		die_out_of_memory();
+	acolumn_spaces = xmalloc(acolumn + 1);
 	memset(acolumn_spaces, ' ', acolumn);
 	acolumn_spaces[acolumn] = '\0';
 
@@ -1690,9 +1706,7 @@ init(int argc, char *argv[])
 	}
 
 	if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
-		char *buf = malloc(BUFSIZ);
-		if (!buf)
-			die_out_of_memory();
+		char *buf = xmalloc(BUFSIZ);
 		setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
 	}
 	if (outfname && argv[0]) {
diff --git a/syscall.c b/syscall.c
index b37f43a..d7c8ef4 100644
--- a/syscall.c
+++ b/syscall.c
@@ -381,9 +381,7 @@ reallocate_qual(const unsigned int n)
 	unsigned p;
 	qualbits_t *qp;
 	for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
-		qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
-		if (!qp)
-			die_out_of_memory();
+		qp = qual_vec[p] = xrealloc(qual_vec[p], n * sizeof(qualbits_t));
 		memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
 	}
 	num_quals = n;
diff --git a/unwind.c b/unwind.c
index 6f422a1..711bede 100644
--- a/unwind.c
+++ b/unwind.c
@@ -107,9 +107,7 @@ unwind_tcb_init(struct tcb *tcp)
 	if (!tcp->libunwind_ui)
 		die_out_of_memory();
 
-	tcp->queue = malloc(sizeof(*tcp->queue));
-	if (!tcp->queue)
-		die_out_of_memory();
+	tcp->queue = xmalloc(sizeof(*tcp->queue));
 	tcp->queue->head = NULL;
 	tcp->queue->tail = NULL;
 }
@@ -152,9 +150,7 @@ build_mmap_cache(struct tcb* tcp)
 		return;
 	}
 
-	cache_head = calloc(cur_array_size, sizeof(*cache_head));
-	if (!cache_head)
-		die_out_of_memory();
+	cache_head = xcalloc(cur_array_size, sizeof(*cache_head));
 
 	while (fgets(buffer, sizeof(buffer), fp) != NULL) {
 		struct mmap_cache_t *entry;
@@ -197,10 +193,8 @@ build_mmap_cache(struct tcb* tcp)
 
 		if (tcp->mmap_cache_size >= cur_array_size) {
 			cur_array_size *= 2;
-			cache_head = realloc(cache_head,
+			cache_head = xrealloc(cache_head,
 					     cur_array_size * sizeof(*cache_head));
-			if (!cache_head)
-				die_out_of_memory();
 		}
 
 		entry = &cache_head[tcp->mmap_cache_size];
@@ -291,9 +285,7 @@ get_symbol_name(unw_cursor_t *cursor, char **name,
 			break;
 		}
 		*size *= 2;
-		*name = realloc(*name, *size);
-		if (!*name)
-			die_out_of_memory();
+		*name = xrealloc(*name, *size);
 	}
 }
 
@@ -372,9 +364,7 @@ stacktrace_walk(struct tcb *tcp,
 	if (tcp->mmap_cache_size == 0)
 		error_msg_and_die("bug: mmap_cache is empty");
 
-	symbol_name = malloc(symbol_name_size);
-	if (!symbol_name)
-		die_out_of_memory();
+	symbol_name = xmalloc(symbol_name_size);
 
 	if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0)
 		perror_msg_and_die("Can't initiate libunwind");
@@ -490,9 +480,7 @@ queue_put(struct queue_t *queue,
 {
 	struct call_t *call;
 
-	call = malloc(sizeof(*call));
-	if (!call)
-		die_out_of_memory();
+	call = xmalloc(sizeof(*call));
 
 	call->output_line = sprint_call_or_error(binary_filename,
 						 symbol_name,
diff --git a/util.c b/util.c
index 02c5b9a..9ff3909 100644
--- a/util.c
+++ b/util.c
@@ -763,12 +763,8 @@ printstr(struct tcb *tcp, long addr, long len)
 
 		if (outstr_size / 4 != max_strlen)
 			die_out_of_memory();
-		str = malloc(max_strlen + 1);
-		if (!str)
-			die_out_of_memory();
-		outstr = malloc(outstr_size);
-		if (!outstr)
-			die_out_of_memory();
+		str = xmalloc(max_strlen + 1);
+		outstr = xmalloc(outstr_size);
 	}
 
 	size = max_strlen;
diff --git a/vsprintf.c b/vsprintf.c
index 0125e72..aae40db 100644
--- a/vsprintf.c
+++ b/vsprintf.c
@@ -776,9 +776,7 @@ int strace_vfprintf(FILE *fp, const char *fmt, va_list args)
 	if (len >= buflen) {
 		buflen = len + 256;
 		free(buf);
-		buf = malloc(buflen);
-		if (!buf)
-			die_out_of_memory();
+		buf = xmalloc(buflen);
 		/*len =*/ kernel_vsnprintf(buf, buflen, fmt, args);
 	}
 
-- 
2.1.0





More information about the Strace-devel mailing list