Define constructor for buffered writer argument

This commit is contained in:
Yinan Zhang 2020-01-09 16:50:09 -08:00
parent 6d8e616902
commit 40a391408c
4 changed files with 32 additions and 19 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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) {

View File

@ -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);