From 40a391408c6edbabac4e408c1cdfdda64c0cd356 Mon Sep 17 00:00:00 2001 From: Yinan Zhang Date: Thu, 9 Jan 2020 16:50:09 -0800 Subject: [PATCH] Define constructor for buffered writer argument --- include/jemalloc/internal/buf_writer.h | 12 +++++++++++- src/jemalloc.c | 17 +++++++++-------- src/prof_log.c | 17 +++++++++-------- src/prof_recent.c | 5 +++-- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/include/jemalloc/internal/buf_writer.h b/include/jemalloc/internal/buf_writer.h index 013bbf5d..1acda9a4 100644 --- a/include/jemalloc/internal/buf_writer.h +++ b/include/jemalloc/internal/buf_writer.h @@ -14,10 +14,20 @@ typedef struct { void (*write_cb)(void *, const char *); void *cbopaque; char *buf; - size_t buf_size; /* must be one less than the capacity of buf array */ + size_t buf_size; size_t buf_end; } buf_write_arg_t; +JEMALLOC_ALWAYS_INLINE void +buf_write_init(buf_write_arg_t *arg, void (*write_cb)(void *, const char *), + void *cbopaque, char *buf, size_t buf_len) { + arg->write_cb = write_cb; + arg->cbopaque = cbopaque; + arg->buf = buf; + arg->buf_size = buf_len - 1; /* Accommodating '\0' at the end. */ + arg->buf_end = 0; +} + void buf_write_flush(buf_write_arg_t *arg); void buf_write_cb(void *buf_write_arg, const char *s); diff --git a/src/jemalloc.c b/src/jemalloc.c index e54c49b0..5503fd00 100644 --- a/src/jemalloc.c +++ b/src/jemalloc.c @@ -3694,14 +3694,15 @@ je_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque, if (config_debug) { stats_print(write_cb, cbopaque, opts); } else { - char *stats_print_buf = (char *)iallocztm(tsdn, - STATS_PRINT_BUFSIZE, sz_size2index(STATS_PRINT_BUFSIZE), - false, NULL, true, arena_get(TSDN_NULL, 0, true), true); - buf_write_arg_t stats_print_buf_arg = {write_cb, cbopaque, - stats_print_buf, STATS_PRINT_BUFSIZE - 1, 0}; - stats_print(buf_write_cb, &stats_print_buf_arg, opts); - buf_write_flush(&stats_print_buf_arg); - idalloctm(tsdn, stats_print_buf, NULL, NULL, true, true); + char *buf = (char *)iallocztm(tsdn, STATS_PRINT_BUFSIZE, + sz_size2index(STATS_PRINT_BUFSIZE), false, NULL, true, + arena_get(TSDN_NULL, 0, true), true); + buf_write_arg_t buf_arg; + buf_write_init(&buf_arg, write_cb, cbopaque, buf, + STATS_PRINT_BUFSIZE); + stats_print(buf_write_cb, &buf_arg, opts); + buf_write_flush(&buf_arg); + idalloctm(tsdn, buf, NULL, NULL, true, true); } check_entry_exit_locking(tsdn); diff --git a/src/prof_log.c b/src/prof_log.c index d0b798de..9495cf7a 100644 --- a/src/prof_log.c +++ b/src/prof_log.c @@ -629,15 +629,16 @@ prof_log_stop(tsdn_t *tsdn) { struct prof_emitter_cb_arg_s arg; arg.fd = fd; - char *prof_log_stop_buf = (char *)iallocztm(tsdn, - PROF_LOG_STOP_BUFSIZE, sz_size2index(PROF_LOG_STOP_BUFSIZE), - false, NULL, true, arena_get(TSDN_NULL, 0, true), true); - buf_write_arg_t prof_log_stop_buf_arg = {prof_emitter_write_cb, &arg, - prof_log_stop_buf, PROF_LOG_STOP_BUFSIZE - 1, 0}; + char *buf = (char *)iallocztm(tsdn, PROF_LOG_STOP_BUFSIZE, + sz_size2index(PROF_LOG_STOP_BUFSIZE), false, NULL, true, + arena_get(TSDN_NULL, 0, true), true); + buf_write_arg_t buf_arg; + buf_write_init(&buf_arg, prof_emitter_write_cb, &arg, buf, + PROF_LOG_STOP_BUFSIZE); /* Emit to json. */ emitter_init(&emitter, emitter_output_json_compact, buf_write_cb, - &prof_log_stop_buf_arg); + &buf_arg); emitter_begin(&emitter); prof_log_emit_metadata(&emitter); @@ -646,8 +647,8 @@ prof_log_stop(tsdn_t *tsdn) { prof_log_emit_allocs(tsd, &emitter); emitter_end(&emitter); - buf_write_flush(&prof_log_stop_buf_arg); - idalloctm(tsdn, prof_log_stop_buf, NULL, NULL, true, true); + buf_write_flush(&buf_arg); + idalloctm(tsdn, buf, NULL, NULL, true, true); /* Reset global state. */ if (log_tables_initialized) { diff --git a/src/prof_recent.c b/src/prof_recent.c index a1f71ea1..ed4170e0 100644 --- a/src/prof_recent.c +++ b/src/prof_recent.c @@ -462,8 +462,9 @@ prof_recent_alloc_dump(tsd_t *tsd, void (*write_cb)(void *, const char *), char *buf = (char *)iallocztm(tsd_tsdn(tsd), PROF_RECENT_PRINT_BUFSIZE, sz_size2index(PROF_RECENT_PRINT_BUFSIZE), false, NULL, true, arena_get(tsd_tsdn(tsd), 0, false), true); - buf_write_arg_t buf_arg = {write_cb, cbopaque, buf, - PROF_RECENT_PRINT_BUFSIZE - 1, 0}; + buf_write_arg_t buf_arg; + buf_write_init(&buf_arg, write_cb, cbopaque, buf, + PROF_RECENT_PRINT_BUFSIZE); emitter_t emitter; emitter_init(&emitter, emitter_output_json_compact, buf_write_cb, &buf_arg);