diff --git a/include/jemalloc/internal/buf_writer.h b/include/jemalloc/internal/buf_writer.h index 1acda9a4..60bd0108 100644 --- a/include/jemalloc/internal/buf_writer.h +++ b/include/jemalloc/internal/buf_writer.h @@ -23,7 +23,9 @@ 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; + assert(buf != NULL); arg->buf = buf; + assert(buf_len >= 2); arg->buf_size = buf_len - 1; /* Accommodating '\0' at the end. */ arg->buf_end = 0; } diff --git a/src/jemalloc.c b/src/jemalloc.c index 64550613..218e04a1 100644 --- a/src/jemalloc.c +++ b/src/jemalloc.c @@ -3716,12 +3716,16 @@ je_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque, 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); + if (buf == NULL) { + stats_print(write_cb, cbopaque, opts); + } else { + 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 9495cf7a..a04c8e40 100644 --- a/src/prof_log.c +++ b/src/prof_log.c @@ -633,12 +633,15 @@ prof_log_stop(tsdn_t *tsdn) { 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, - &buf_arg); + if (buf == NULL) { + emitter_init(&emitter, emitter_output_json_compact, + prof_emitter_write_cb, &arg); + } else { + buf_write_init(&buf_arg, prof_emitter_write_cb, &arg, buf, + PROF_LOG_STOP_BUFSIZE); + emitter_init(&emitter, emitter_output_json_compact, + buf_write_cb, &buf_arg); + } emitter_begin(&emitter); prof_log_emit_metadata(&emitter); @@ -647,8 +650,10 @@ prof_log_stop(tsdn_t *tsdn) { prof_log_emit_allocs(tsd, &emitter); emitter_end(&emitter); - buf_write_flush(&buf_arg); - idalloctm(tsdn, buf, NULL, NULL, true, true); + if (buf != NULL) { + 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 a53f82c7..66a9b406 100644 --- a/src/prof_recent.c +++ b/src/prof_recent.c @@ -465,12 +465,17 @@ 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; - 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); + buf_write_arg_t buf_arg; + if (buf == NULL) { + emitter_init(&emitter, emitter_output_json_compact, write_cb, + cbopaque); + } else { + buf_write_init(&buf_arg, write_cb, cbopaque, buf, + PROF_RECENT_PRINT_BUFSIZE); + emitter_init(&emitter, emitter_output_json_compact, + buf_write_cb, &buf_arg); + } emitter_begin(&emitter); malloc_mutex_lock(tsd_tsdn(tsd), &prof_recent_alloc_mtx); @@ -530,8 +535,10 @@ prof_recent_alloc_dump(tsd_t *tsd, void (*write_cb)(void *, const char *), malloc_mutex_unlock(tsd_tsdn(tsd), &prof_recent_alloc_mtx); emitter_end(&emitter); - buf_write_flush(&buf_arg); - idalloctm(tsd_tsdn(tsd), buf, NULL, NULL, true, true); + if (buf != NULL) { + buf_write_flush(&buf_arg); + idalloctm(tsd_tsdn(tsd), buf, NULL, NULL, true, true); + } } #undef PROF_RECENT_PRINT_BUFSIZE