Module Name:    src
Committed By:   rillig
Date:           Tue Aug  3 17:20:02 UTC 2021

Modified Files:
        src/usr.bin/xlint/common: mem.c

Log Message:
lint: make memory management code easier to read

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/usr.bin/xlint/common/mem.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/xlint/common/mem.c
diff -u src/usr.bin/xlint/common/mem.c:1.15 src/usr.bin/xlint/common/mem.c:1.16
--- src/usr.bin/xlint/common/mem.c:1.15	Sun Aug  1 18:13:53 2021
+++ src/usr.bin/xlint/common/mem.c	Tue Aug  3 17:20:02 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: mem.c,v 1.15 2021/08/01 18:13:53 rillig Exp $	*/
+/*	$NetBSD: mem.c,v 1.16 2021/08/03 17:20:02 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: mem.c,v 1.15 2021/08/01 18:13:53 rillig Exp $");
+__RCSID("$NetBSD: mem.c,v 1.16 2021/08/03 17:20:02 rillig Exp $");
 #endif
 
 #include <stdarg.h>
@@ -46,53 +46,41 @@ __RCSID("$NetBSD: mem.c,v 1.15 2021/08/0
 
 #include "lint.h"
 
-static void __attribute__((noreturn))
-nomem(void)
+static void *
+not_null(void *ptr)
 {
 
-	errx(1, "virtual memory exhausted");
+	if (ptr == NULL)
+		errx(1, "virtual memory exhausted");
+	return ptr;
 }
 
 void *
 xmalloc(size_t s)
 {
-	void	*p;
 
-	if ((p = malloc(s)) == NULL)
-		nomem();
-	return p;
+	return not_null(malloc(s));
 }
 
 void *
 xcalloc(size_t n, size_t s)
 {
-	void	*p;
 
-	if ((p = calloc(n, s)) == NULL)
-		nomem();
-	return p;
+	return not_null(calloc(n, s));
 }
 
 void *
 xrealloc(void *p, size_t s)
 {
-	void *n;
 
-	if ((n = realloc(p, s)) == NULL) {
-		free(p);
-		nomem();
-	}
-	return n;
+	return not_null(realloc(p, s));
 }
 
 char *
 xstrdup(const char *s)
 {
-	char	*s2;
 
-	if ((s2 = strdup(s)) == NULL)
-		nomem();
-	return s2;
+	return not_null(strdup(s));
 }
 
 char *
@@ -106,6 +94,6 @@ xasprintf(const char *fmt, ...)
 	e = vasprintf(&str, fmt, ap);
 	va_end(ap);
 	if (e < 0)
-		nomem();
+		not_null(NULL);
 	return str;
 }

Reply via email to