From bdc08b51581d422189e32ee87724e668f0fa5ef2 Mon Sep 17 00:00:00 2001 From: Yinan Zhang Date: Thu, 23 Jan 2020 15:00:01 -0800 Subject: [PATCH] Better naming buffered writer --- include/jemalloc/internal/buf_writer.h | 20 ++++++++--------- src/buf_writer.c | 30 +++++++++++++------------- src/jemalloc.c | 8 +++---- src/prof_log.c | 8 +++---- src/prof_recent.c | 8 +++---- test/unit/buf_writer.c | 15 +++++++------ 6 files changed, 45 insertions(+), 44 deletions(-) diff --git a/include/jemalloc/internal/buf_writer.h b/include/jemalloc/internal/buf_writer.h index 60bd0108..b2644a86 100644 --- a/include/jemalloc/internal/buf_writer.h +++ b/include/jemalloc/internal/buf_writer.h @@ -16,21 +16,21 @@ typedef struct { char *buf; size_t buf_size; size_t buf_end; -} buf_write_arg_t; +} buf_writer_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; +buf_writer_init(buf_writer_t *buf_writer, void (*write_cb)(void *, + const char *), void *cbopaque, char *buf, size_t buf_len) { + buf_writer->write_cb = write_cb; + buf_writer->cbopaque = cbopaque; assert(buf != NULL); - arg->buf = buf; + buf_writer->buf = buf; assert(buf_len >= 2); - arg->buf_size = buf_len - 1; /* Accommodating '\0' at the end. */ - arg->buf_end = 0; + buf_writer->buf_size = buf_len - 1; /* Allowing for '\0' at the end. */ + buf_writer->buf_end = 0; } -void buf_write_flush(buf_write_arg_t *arg); -void buf_write_cb(void *buf_write_arg, const char *s); +void buf_writer_flush(buf_writer_t *buf_writer); +void buf_writer_cb(void *buf_writer_arg, const char *s); #endif /* JEMALLOC_INTERNAL_BUF_WRITER_H */ diff --git a/src/buf_writer.c b/src/buf_writer.c index 41065946..aed7d4a8 100644 --- a/src/buf_writer.c +++ b/src/buf_writer.c @@ -6,31 +6,31 @@ #include "jemalloc/internal/malloc_io.h" void -buf_write_flush(buf_write_arg_t *arg) { - assert(arg->buf_end <= arg->buf_size); - arg->buf[arg->buf_end] = '\0'; - if (arg->write_cb == NULL) { - arg->write_cb = je_malloc_message != NULL ? +buf_writer_flush(buf_writer_t *buf_writer) { + assert(buf_writer->buf_end <= buf_writer->buf_size); + buf_writer->buf[buf_writer->buf_end] = '\0'; + if (buf_writer->write_cb == NULL) { + buf_writer->write_cb = je_malloc_message != NULL ? je_malloc_message : wrtmessage; } - arg->write_cb(arg->cbopaque, arg->buf); - arg->buf_end = 0; + buf_writer->write_cb(buf_writer->cbopaque, buf_writer->buf); + buf_writer->buf_end = 0; } void -buf_write_cb(void *buf_write_arg, const char *s) { - buf_write_arg_t *arg = (buf_write_arg_t *)buf_write_arg; +buf_writer_cb(void *buf_writer_arg, const char *s) { + buf_writer_t *buf_writer = (buf_writer_t *)buf_writer_arg; size_t i, slen, n, s_remain, buf_remain; - assert(arg->buf_end <= arg->buf_size); + assert(buf_writer->buf_end <= buf_writer->buf_size); for (i = 0, slen = strlen(s); i < slen; i += n) { - if (arg->buf_end == arg->buf_size) { - buf_write_flush(arg); + if (buf_writer->buf_end == buf_writer->buf_size) { + buf_writer_flush(buf_writer); } s_remain = slen - i; - buf_remain = arg->buf_size - arg->buf_end; + buf_remain = buf_writer->buf_size - buf_writer->buf_end; n = s_remain < buf_remain ? s_remain : buf_remain; - memcpy(arg->buf + arg->buf_end, s + i, n); - arg->buf_end += n; + memcpy(buf_writer->buf + buf_writer->buf_end, s + i, n); + buf_writer->buf_end += n; } assert(i == slen); } diff --git a/src/jemalloc.c b/src/jemalloc.c index 190b3a2a..35c490be 100644 --- a/src/jemalloc.c +++ b/src/jemalloc.c @@ -3746,11 +3746,11 @@ je_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque, 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, + buf_writer_t buf_writer; + buf_writer_init(&buf_writer, write_cb, cbopaque, buf, STATS_PRINT_BUFSIZE); - stats_print(buf_write_cb, &buf_arg, opts); - buf_write_flush(&buf_arg); + stats_print(buf_writer_cb, &buf_writer, opts); + buf_writer_flush(&buf_writer); idalloctm(tsdn, buf, NULL, NULL, true, true); } } diff --git a/src/prof_log.c b/src/prof_log.c index a04c8e40..95cf246d 100644 --- a/src/prof_log.c +++ b/src/prof_log.c @@ -632,15 +632,15 @@ prof_log_stop(tsdn_t *tsdn) { 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_writer_t buf_writer; 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, + buf_writer_init(&buf_writer, prof_emitter_write_cb, &arg, buf, PROF_LOG_STOP_BUFSIZE); emitter_init(&emitter, emitter_output_json_compact, - buf_write_cb, &buf_arg); + buf_writer_cb, &buf_writer); } emitter_begin(&emitter); @@ -651,7 +651,7 @@ prof_log_stop(tsdn_t *tsdn) { emitter_end(&emitter); if (buf != NULL) { - buf_write_flush(&buf_arg); + buf_writer_flush(&buf_writer); idalloctm(tsdn, buf, NULL, NULL, true, true); } diff --git a/src/prof_recent.c b/src/prof_recent.c index 66a9b406..dde029ce 100644 --- a/src/prof_recent.c +++ b/src/prof_recent.c @@ -466,15 +466,15 @@ prof_recent_alloc_dump(tsd_t *tsd, void (*write_cb)(void *, const char *), sz_size2index(PROF_RECENT_PRINT_BUFSIZE), false, NULL, true, arena_get(tsd_tsdn(tsd), 0, false), true); emitter_t emitter; - buf_write_arg_t buf_arg; + buf_writer_t buf_writer; if (buf == NULL) { emitter_init(&emitter, emitter_output_json_compact, write_cb, cbopaque); } else { - buf_write_init(&buf_arg, write_cb, cbopaque, buf, + buf_writer_init(&buf_writer, write_cb, cbopaque, buf, PROF_RECENT_PRINT_BUFSIZE); emitter_init(&emitter, emitter_output_json_compact, - buf_write_cb, &buf_arg); + buf_writer_cb, &buf_writer); } emitter_begin(&emitter); @@ -536,7 +536,7 @@ prof_recent_alloc_dump(tsd_t *tsd, void (*write_cb)(void *, const char *), emitter_end(&emitter); if (buf != NULL) { - buf_write_flush(&buf_arg); + buf_writer_flush(&buf_writer); idalloctm(tsd_tsdn(tsd), buf, NULL, NULL, true, true); } } diff --git a/test/unit/buf_writer.c b/test/unit/buf_writer.c index bbdb657e..63fd0c67 100644 --- a/test/unit/buf_writer.c +++ b/test/unit/buf_writer.c @@ -22,8 +22,9 @@ TEST_BEGIN(test_buf_write) { size_t n_unit, remain, i; ssize_t unit; uint64_t arg = 4; /* Starting value of random argument. */ - buf_write_arg_t test_buf_arg = {test_write_cb, &arg, test_buf, - TEST_BUF_SIZE - 1, 0}; + buf_writer_t buf_writer; + buf_writer_init(&buf_writer, test_write_cb, &arg, test_buf, + TEST_BUF_SIZE); memset(s, 'a', UNIT_MAX); arg_store = arg; @@ -35,23 +36,23 @@ TEST_BEGIN(test_buf_write) { remain = 0; for (i = 1; i <= n_unit; ++i) { arg = prng_lg_range_u64(&arg, 64); - buf_write_cb(&test_buf_arg, s); + buf_writer_cb(&buf_writer, s); remain += unit; - if (remain > test_buf_arg.buf_size) { + if (remain > buf_writer.buf_size) { /* Flushes should have happened. */ assert_u64_eq(arg_store, arg, "Call " "back argument didn't get through"); - remain %= test_buf_arg.buf_size; + remain %= buf_writer.buf_size; if (remain == 0) { /* Last flush should be lazy. */ - remain += test_buf_arg.buf_size; + remain += buf_writer.buf_size; } } assert_zu_eq(test_write_len + remain, i * unit, "Incorrect length after writing %zu strings" " of length %zu", i, unit); } - buf_write_flush(&test_buf_arg); + buf_writer_flush(&buf_writer); assert_zu_eq(test_write_len, n_unit * unit, "Incorrect length after flushing at the end of" " writing %zu strings of length %zu", n_unit, unit);