From 0b4f130791728cf3ffd7240f54c6e14f942c876f Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 26 Nov 2022 11:23:29 +0000 Subject: [PATCH] Remove get_fmt_buf function --- src/common.c | 30 ------------------ src/common.h | 3 -- src/make.c | 2 +- src/rt/model.c | 82 ++++++++++++++++++++++++++++++++++---------------- src/util.c | 26 ---------------- src/util.h | 2 -- 6 files changed, 57 insertions(+), 88 deletions(-) diff --git a/src/common.c b/src/common.c index 78565e9f..19d2be0a 100644 --- a/src/common.c +++ b/src/common.c @@ -743,36 +743,6 @@ type_t array_aggregate_type(type_t array, int from_dim) } } -int fmt_time_r(char *buf, size_t len, uint64_t t) -{ - static const struct { - uint64_t time; - const char *unit; - } units[] = { - { UINT64_C(1), "fs" }, - { UINT64_C(1000), "ps" }, - { UINT64_C(1000000), "ns" }, - { UINT64_C(1000000000), "us" }, - { UINT64_C(1000000000000), "ms" }, - { 0, NULL } - }; - - int u = 0; - while (units[u + 1].unit && (t % units[u + 1].time == 0)) - ++u; - - return checked_sprintf(buf, len, "%"PRIu64"%s", - t / units[u].time, units[u].unit); -} - -const char *fmt_time(uint64_t t) -{ - static const int BUF_SZ = 64; - char *buf = get_fmt_buf(BUF_SZ); - fmt_time_r(buf, BUF_SZ, t); - return buf; -} - unsigned bits_for_range(int64_t low, int64_t high) { assert(low <= high); diff --git a/src/common.h b/src/common.h index 56ace168..bbb5eb5b 100644 --- a/src/common.h +++ b/src/common.h @@ -85,9 +85,6 @@ type_t get_type_or_null(tree_t t); type_t subtype_for_string(tree_t str, type_t base); tree_t change_ref(tree_t name, tree_t new); -int fmt_time_r(char *buf, size_t len, uint64_t t); -const char *fmt_time(uint64_t t); - #define MAX_CONSTRAINTS 8 int pack_constraints(type_t type, tree_t out[MAX_CONSTRAINTS]); diff --git a/src/make.c b/src/make.c index 5f9bbd2e..8e310e06 100644 --- a/src/make.c +++ b/src/make.c @@ -99,7 +99,7 @@ static lib_t make_get_lib(ident_t name) static const char *make_product(tree_t t, make_product_t product) { - char *buf = get_fmt_buf(PATH_MAX); + static char buf[PATH_MAX]; ident_t name = tree_ident(t); lib_t lib = make_get_lib(name); diff --git a/src/rt/model.c b/src/rt/model.c index ed42cc5e..5d92df22 100644 --- a/src/rt/model.c +++ b/src/rt/model.c @@ -138,16 +138,26 @@ static void async_update_driver(void *context, void *arg); static void async_update_driving(void *context, void *arg); static void async_disconnect(void *context, void *arg); -static int fmt_now(rt_model_t *m, char *buf, size_t len) -{ - if (m->iteration < 0) - return checked_sprintf(buf, len, "(init)"); - else { - char *p = buf; - p += fmt_time_r(p, buf + len - p, m->now); - p += checked_sprintf(p, buf + len - p, "+%d", m->iteration); - return p - buf; - } +static int fmt_time_r(char *buf, size_t len, uint64_t t) +{ + static const struct { + uint64_t time; + const char *unit; + } units[] = { + { UINT64_C(1), "fs" }, + { UINT64_C(1000), "ps" }, + { UINT64_C(1000000), "ns" }, + { UINT64_C(1000000000), "us" }, + { UINT64_C(1000000000000), "ms" }, + { 0, NULL } + }; + + int u = 0; + while (units[u + 1].unit && (t % units[u + 1].time == 0)) + ++u; + + return checked_sprintf(buf, len, "%"PRIu64"%s", + t / units[u].time, units[u].unit); } __attribute__((format(printf, 2, 3))) @@ -156,14 +166,17 @@ static void __model_trace(rt_model_t *m, const char *fmt, ...) va_list ap; va_start(ap, fmt); - char buf[64]; - fmt_now(m, buf, sizeof(buf)); - static nvc_lock_t lock = 0; { SCOPED_LOCK(lock); - fprintf(stderr, "TRACE %s: ", buf); + if (m->iteration < 0) + fprintf(stderr, "TRACE (init): "); + else { + char buf[64]; + fmt_time_r(buf, sizeof(buf), m->now); + fprintf(stderr, "TRACE %s+%d: ", buf, m->iteration); + } vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); fflush(stderr); @@ -172,14 +185,28 @@ static void __model_trace(rt_model_t *m, const char *fmt, ...) va_end(ap); } +static const char *trace_time(uint64_t value) +{ + static __thread char buf[2][32]; + static __thread int which = 0; + + which ^= 1; + fmt_time_r(buf[which], 32, value); + return buf[which]; +} + static void model_diag_cb(diag_t *d, void *arg) { rt_model_t *m = arg; - char tmbuf[64]; - fmt_now(m, tmbuf, sizeof(tmbuf)); + if (m->iteration < 0) + diag_printf(d, "(init): "); + else { + char tmbuf[64]; + fmt_time_r(tmbuf, sizeof(tmbuf), m->now); - diag_printf(d, "%s: ", tmbuf); + diag_printf(d, "%s+%d: ", tmbuf, m->iteration); + } } static void __model_entry(rt_model_t *m, rt_model_t **save) @@ -2058,7 +2085,7 @@ static void sched_driver(rt_model_t *m, rt_nexus_t *nexus, uint64_t after, if (unlikely(reject > after)) jit_msg(NULL, DIAG_FATAL, "signal %s pulse reject limit %s is greater " "than delay %s", istr(tree_ident(nexus->signal->where)), - fmt_time(reject), fmt_time(after)); + trace_time(reject), trace_time(after)); waveform_t *w = alloc_waveform(); w->when = m->now + after; @@ -2662,13 +2689,16 @@ void model_interrupt(rt_model_t *m) { model_stop(m); + char tmbuf[32]; + fmt_time_r(tmbuf, sizeof(tmbuf), m->now); + if (active_proc != NULL) jit_msg(NULL, DIAG_FATAL, "interrupted in process %s at %s+%d", - istr(active_proc->name), fmt_time(m->now), m->iteration); + istr(active_proc->name), tmbuf, m->iteration); else { diag_t *d = diag_new(DIAG_FATAL, NULL); - diag_printf(d, "interrupted at %s+%d", fmt_time(m->now), m->iteration); + diag_printf(d, "interrupted at %s+%d", tmbuf, m->iteration); diag_emit(d); jit_set_exit_status(m->jit, EXIT_FAILURE); @@ -2748,7 +2778,7 @@ int x_current_delta(void) void x_sched_process(int64_t delay) { - TRACE("_sched_process delay=%s", fmt_time(delay)); + TRACE("_sched_process delay=%s", trace_time(delay)); deltaq_insert_proc(get_model(), delay, active_proc); } @@ -2758,8 +2788,8 @@ void x_sched_waveform_s(sig_shared_t *ss, uint32_t offset, uint64_t scalar, rt_signal_t *s = container_of(ss, rt_signal_t, shared); TRACE("_sched_waveform_s %s+%d value=%"PRIi64" after=%s reject=%s", - istr(tree_ident(s->where)), offset, scalar, fmt_time(after), - fmt_time(reject)); + istr(tree_ident(s->where)), offset, scalar, trace_time(after), + trace_time(reject)); check_postponed(after); @@ -2779,7 +2809,7 @@ void x_sched_waveform(sig_shared_t *ss, uint32_t offset, void *values, TRACE("_sched_waveform %s+%d value=%s count=%d after=%s reject=%s", istr(tree_ident(s->where)), offset, fmt_values(values, count), - count, fmt_time(after), fmt_time(reject)); + count, trace_time(after), trace_time(reject)); check_postponed(after); @@ -3192,8 +3222,8 @@ void x_disconnect(sig_shared_t *ss, uint32_t offset, int32_t count, rt_signal_t *s = container_of(ss, rt_signal_t, shared); TRACE("_disconnect %s+%d len=%d after=%s reject=%s", - istr(tree_ident(s->where)), offset, count, fmt_time(after), - fmt_time(reject)); + istr(tree_ident(s->where)), offset, count, trace_time(after), + trace_time(reject)); check_postponed(after); diff --git a/src/util.c b/src/util.c index 2319f8cb..a03941ff 100644 --- a/src/util.c +++ b/src/util.c @@ -977,32 +977,6 @@ int terminal_width(void) return term_width; } -char *get_fmt_buf(size_t len) -{ - // This is a bit of a kludge but keeping a sufficient number - // of static buffers allows us to use format functions multiple - // times in printf - static char *buf_set[MAX_FMT_BUFS]; - static size_t buflen[MAX_FMT_BUFS]; - static int next_buf = 0; - - char **bufp = &buf_set[next_buf]; - size_t *blenp = &buflen[next_buf]; - next_buf = (next_buf + 1) % MAX_FMT_BUFS; - - if (*bufp == NULL) { - *bufp = xmalloc(len); - *blenp = len; - } - - while (len > *blenp) { - *blenp *= 2; - *bufp = xrealloc(*bufp, *blenp); - } - - return *bufp; -} - const char *ordinal_str(int n) { switch (n) { diff --git a/src/util.h b/src/util.h index f569c086..a74dc9f5 100644 --- a/src/util.h +++ b/src/util.h @@ -180,8 +180,6 @@ void term_init(void); bool color_terminal(void); int terminal_width(void); -char *get_fmt_buf(size_t len); - const char *ordinal_str(int n); int checked_sprintf(char *buf, int len, const char *fmt, ...) -- 2.39.2