Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/gen Don't make assert use stdio; saves >100K on an ...



details:   https://anonhg.NetBSD.org/src/rev/b0d352397534
branches:  trunk
changeset: 823944:b0d352397534
user:      christos <christos%NetBSD.org@localhost>
date:      Mon May 15 16:09:09 2017 +0000

description:
Don't make assert use stdio; saves >100K on an empty static binary.

diffstat:

 lib/libc/gen/assert.c |  53 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 34 insertions(+), 19 deletions(-)

diffs (87 lines):

diff -r dd1977ace5d1 -r b0d352397534 lib/libc/gen/assert.c
--- a/lib/libc/gen/assert.c     Mon May 15 13:38:34 2017 +0000
+++ b/lib/libc/gen/assert.c     Mon May 15 16:09:09 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: assert.c,v 1.17 2012/06/25 22:32:43 abs Exp $  */
+/*     $NetBSD: assert.c,v 1.18 2017/05/15 16:09:09 christos Exp $     */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)assert.c   8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: assert.c,v 1.17 2012/06/25 22:32:43 abs Exp $");
+__RCSID("$NetBSD: assert.c,v 1.18 2017/05/15 16:09:09 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -44,19 +44,31 @@
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <syslog.h>
+#include "extern.h"
+
+static int
+fmtassert(char *buf, size_t len, const char *file, int line,
+    const char *function, const char *failedexpr)
+{
+       return snprintf_ss(buf, len,
+           "assertion \"%s\" failed: file \"%s\", line %d%s%s%s\n",
+           failedexpr, file, line,
+           function ? ", function \"" : "",
+           function ? function : "",
+           function ? "\"" : "");
+}
 
 void
 __assert13(const char *file, int line, const char *function,
     const char *failedexpr)
 {
-
-       (void)fprintf(stderr,
-           "assertion \"%s\" failed: file \"%s\", line %d%s%s%s\n",
-           failedexpr, file, line,
-           function ? ", function \"" : "",
-           function ? function : "",
-           function ? "\"" : "");
+       char buf[1024];
+       int l = fmtassert(buf, sizeof(buf), file, line, function, failedexpr);
+       if (l < 0)
+               abort();
+       (void)write(STDERR_FILENO, buf, (size_t)l);
        abort();
        /* NOTREACHED */
 }
@@ -113,16 +125,19 @@
                }
        }
 
-       snprintf(buf, sizeof(buf),
-           "assertion \"%s\" failed: file \"%s\", line %d%s%s%s",
-           failedexpr, file, line,
-           function ? ", function \"" : "",
-           function ? function : "",
-           function ? "\"" : "");
-       if (diagassert_flags & DIAGASSERT_STDERR)
-               (void)fprintf(stderr, "%s: %s\n", getprogname(), buf);
-       if (diagassert_flags & DIAGASSERT_SYSLOG)
-               syslog(LOG_DEBUG | LOG_USER, "%s", buf);
+       fmtassert(buf, sizeof(buf), file, line, function, failedexpr);
+       if (diagassert_flags & DIAGASSERT_STDERR) {
+               char ebuf[1024];
+               int l = snprintf_ss(ebuf, sizeof(ebuf), "%s: %s\n",
+                   getprogname(), buf);
+               if (l == -1)
+                       abort();
+               (void)write(STDERR_FILENO, ebuf, (size_t)l);
+       }
+       if (diagassert_flags & DIAGASSERT_SYSLOG) {
+               struct syslog_data sdata = SYSLOG_DATA_INIT;
+               syslog_ss(LOG_DEBUG | LOG_USER, &sdata, "%s", buf);
+       }
        if (diagassert_flags & DIAGASSERT_ABORT)
                abort();
 }



Home | Main Index | Thread Index | Old Index