Better naming buffered writer

This commit is contained in:
Yinan Zhang 2020-01-23 15:00:01 -08:00
parent c6bfe55857
commit bdc08b5158
6 changed files with 45 additions and 44 deletions

View File

@ -16,21 +16,21 @@ typedef struct {
char *buf; char *buf;
size_t buf_size; size_t buf_size;
size_t buf_end; size_t buf_end;
} buf_write_arg_t; } buf_writer_t;
JEMALLOC_ALWAYS_INLINE void JEMALLOC_ALWAYS_INLINE void
buf_write_init(buf_write_arg_t *arg, void (*write_cb)(void *, const char *), buf_writer_init(buf_writer_t *buf_writer, void (*write_cb)(void *,
void *cbopaque, char *buf, size_t buf_len) { const char *), void *cbopaque, char *buf, size_t buf_len) {
arg->write_cb = write_cb; buf_writer->write_cb = write_cb;
arg->cbopaque = cbopaque; buf_writer->cbopaque = cbopaque;
assert(buf != NULL); assert(buf != NULL);
arg->buf = buf; buf_writer->buf = buf;
assert(buf_len >= 2); assert(buf_len >= 2);
arg->buf_size = buf_len - 1; /* Accommodating '\0' at the end. */ buf_writer->buf_size = buf_len - 1; /* Allowing for '\0' at the end. */
arg->buf_end = 0; buf_writer->buf_end = 0;
} }
void buf_write_flush(buf_write_arg_t *arg); void buf_writer_flush(buf_writer_t *buf_writer);
void buf_write_cb(void *buf_write_arg, const char *s); void buf_writer_cb(void *buf_writer_arg, const char *s);
#endif /* JEMALLOC_INTERNAL_BUF_WRITER_H */ #endif /* JEMALLOC_INTERNAL_BUF_WRITER_H */

View File

@ -6,31 +6,31 @@
#include "jemalloc/internal/malloc_io.h" #include "jemalloc/internal/malloc_io.h"
void void
buf_write_flush(buf_write_arg_t *arg) { buf_writer_flush(buf_writer_t *buf_writer) {
assert(arg->buf_end <= arg->buf_size); assert(buf_writer->buf_end <= buf_writer->buf_size);
arg->buf[arg->buf_end] = '\0'; buf_writer->buf[buf_writer->buf_end] = '\0';
if (arg->write_cb == NULL) { if (buf_writer->write_cb == NULL) {
arg->write_cb = je_malloc_message != NULL ? buf_writer->write_cb = je_malloc_message != NULL ?
je_malloc_message : wrtmessage; je_malloc_message : wrtmessage;
} }
arg->write_cb(arg->cbopaque, arg->buf); buf_writer->write_cb(buf_writer->cbopaque, buf_writer->buf);
arg->buf_end = 0; buf_writer->buf_end = 0;
} }
void void
buf_write_cb(void *buf_write_arg, const char *s) { buf_writer_cb(void *buf_writer_arg, const char *s) {
buf_write_arg_t *arg = (buf_write_arg_t *)buf_write_arg; buf_writer_t *buf_writer = (buf_writer_t *)buf_writer_arg;
size_t i, slen, n, s_remain, buf_remain; 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) { for (i = 0, slen = strlen(s); i < slen; i += n) {
if (arg->buf_end == arg->buf_size) { if (buf_writer->buf_end == buf_writer->buf_size) {
buf_write_flush(arg); buf_writer_flush(buf_writer);
} }
s_remain = slen - i; 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; n = s_remain < buf_remain ? s_remain : buf_remain;
memcpy(arg->buf + arg->buf_end, s + i, n); memcpy(buf_writer->buf + buf_writer->buf_end, s + i, n);
arg->buf_end += n; buf_writer->buf_end += n;
} }
assert(i == slen); assert(i == slen);
} }

View File

@ -3746,11 +3746,11 @@ je_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque,
if (buf == NULL) { if (buf == NULL) {
stats_print(write_cb, cbopaque, opts); stats_print(write_cb, cbopaque, opts);
} else { } else {
buf_write_arg_t buf_arg; buf_writer_t buf_writer;
buf_write_init(&buf_arg, write_cb, cbopaque, buf, buf_writer_init(&buf_writer, write_cb, cbopaque, buf,
STATS_PRINT_BUFSIZE); STATS_PRINT_BUFSIZE);
stats_print(buf_write_cb, &buf_arg, opts); stats_print(buf_writer_cb, &buf_writer, opts);
buf_write_flush(&buf_arg); buf_writer_flush(&buf_writer);
idalloctm(tsdn, buf, NULL, NULL, true, true); idalloctm(tsdn, buf, NULL, NULL, true, true);
} }
} }

View File

@ -632,15 +632,15 @@ prof_log_stop(tsdn_t *tsdn) {
char *buf = (char *)iallocztm(tsdn, PROF_LOG_STOP_BUFSIZE, char *buf = (char *)iallocztm(tsdn, PROF_LOG_STOP_BUFSIZE,
sz_size2index(PROF_LOG_STOP_BUFSIZE), false, NULL, true, sz_size2index(PROF_LOG_STOP_BUFSIZE), false, NULL, true,
arena_get(TSDN_NULL, 0, true), true); arena_get(TSDN_NULL, 0, true), true);
buf_write_arg_t buf_arg; buf_writer_t buf_writer;
if (buf == NULL) { if (buf == NULL) {
emitter_init(&emitter, emitter_output_json_compact, emitter_init(&emitter, emitter_output_json_compact,
prof_emitter_write_cb, &arg); prof_emitter_write_cb, &arg);
} else { } 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); PROF_LOG_STOP_BUFSIZE);
emitter_init(&emitter, emitter_output_json_compact, emitter_init(&emitter, emitter_output_json_compact,
buf_write_cb, &buf_arg); buf_writer_cb, &buf_writer);
} }
emitter_begin(&emitter); emitter_begin(&emitter);
@ -651,7 +651,7 @@ prof_log_stop(tsdn_t *tsdn) {
emitter_end(&emitter); emitter_end(&emitter);
if (buf != NULL) { if (buf != NULL) {
buf_write_flush(&buf_arg); buf_writer_flush(&buf_writer);
idalloctm(tsdn, buf, NULL, NULL, true, true); idalloctm(tsdn, buf, NULL, NULL, true, true);
} }

View File

@ -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, sz_size2index(PROF_RECENT_PRINT_BUFSIZE), false, NULL, true,
arena_get(tsd_tsdn(tsd), 0, false), true); arena_get(tsd_tsdn(tsd), 0, false), true);
emitter_t emitter; emitter_t emitter;
buf_write_arg_t buf_arg; buf_writer_t buf_writer;
if (buf == NULL) { if (buf == NULL) {
emitter_init(&emitter, emitter_output_json_compact, write_cb, emitter_init(&emitter, emitter_output_json_compact, write_cb,
cbopaque); cbopaque);
} else { } else {
buf_write_init(&buf_arg, write_cb, cbopaque, buf, buf_writer_init(&buf_writer, write_cb, cbopaque, buf,
PROF_RECENT_PRINT_BUFSIZE); PROF_RECENT_PRINT_BUFSIZE);
emitter_init(&emitter, emitter_output_json_compact, emitter_init(&emitter, emitter_output_json_compact,
buf_write_cb, &buf_arg); buf_writer_cb, &buf_writer);
} }
emitter_begin(&emitter); emitter_begin(&emitter);
@ -536,7 +536,7 @@ prof_recent_alloc_dump(tsd_t *tsd, void (*write_cb)(void *, const char *),
emitter_end(&emitter); emitter_end(&emitter);
if (buf != NULL) { if (buf != NULL) {
buf_write_flush(&buf_arg); buf_writer_flush(&buf_writer);
idalloctm(tsd_tsdn(tsd), buf, NULL, NULL, true, true); idalloctm(tsd_tsdn(tsd), buf, NULL, NULL, true, true);
} }
} }

View File

@ -22,8 +22,9 @@ TEST_BEGIN(test_buf_write) {
size_t n_unit, remain, i; size_t n_unit, remain, i;
ssize_t unit; ssize_t unit;
uint64_t arg = 4; /* Starting value of random argument. */ uint64_t arg = 4; /* Starting value of random argument. */
buf_write_arg_t test_buf_arg = {test_write_cb, &arg, test_buf, buf_writer_t buf_writer;
TEST_BUF_SIZE - 1, 0}; buf_writer_init(&buf_writer, test_write_cb, &arg, test_buf,
TEST_BUF_SIZE);
memset(s, 'a', UNIT_MAX); memset(s, 'a', UNIT_MAX);
arg_store = arg; arg_store = arg;
@ -35,23 +36,23 @@ TEST_BEGIN(test_buf_write) {
remain = 0; remain = 0;
for (i = 1; i <= n_unit; ++i) { for (i = 1; i <= n_unit; ++i) {
arg = prng_lg_range_u64(&arg, 64); arg = prng_lg_range_u64(&arg, 64);
buf_write_cb(&test_buf_arg, s); buf_writer_cb(&buf_writer, s);
remain += unit; remain += unit;
if (remain > test_buf_arg.buf_size) { if (remain > buf_writer.buf_size) {
/* Flushes should have happened. */ /* Flushes should have happened. */
assert_u64_eq(arg_store, arg, "Call " assert_u64_eq(arg_store, arg, "Call "
"back argument didn't get through"); "back argument didn't get through");
remain %= test_buf_arg.buf_size; remain %= buf_writer.buf_size;
if (remain == 0) { if (remain == 0) {
/* Last flush should be lazy. */ /* 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, assert_zu_eq(test_write_len + remain, i * unit,
"Incorrect length after writing %zu strings" "Incorrect length after writing %zu strings"
" of length %zu", i, unit); " 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, assert_zu_eq(test_write_len, n_unit * unit,
"Incorrect length after flushing at the end of" "Incorrect length after flushing at the end of"
" writing %zu strings of length %zu", n_unit, unit); " writing %zu strings of length %zu", n_unit, unit);