Make buffered writer an independent module

This commit is contained in:
Yinan Zhang
2020-01-09 16:36:09 -08:00
parent 6b6b4709b3
commit 6d8e616902
13 changed files with 76 additions and 57 deletions

View File

@@ -0,0 +1,24 @@
#ifndef JEMALLOC_INTERNAL_BUF_WRITER_H
#define JEMALLOC_INTERNAL_BUF_WRITER_H
/*
* Note: when using the buffered writer, cbopaque is passed to write_cb only
* when the buffer is flushed. It would make a difference if cbopaque points
* to something that's changing for each write_cb call, or something that
* affects write_cb in a way dependent on the content of the output string.
* However, the most typical usage case in practice is that cbopaque points to
* some "option like" content for the write_cb, so it doesn't matter.
*/
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_end;
} buf_write_arg_t;
void buf_write_flush(buf_write_arg_t *arg);
void buf_write_cb(void *buf_write_arg, const char *s);
#endif /* JEMALLOC_INTERNAL_BUF_WRITER_H */

View File

@@ -40,6 +40,7 @@
*/
#define MALLOC_PRINTF_BUFSIZE 4096
void wrtmessage(void *cbopaque, const char *s);
int buferror(int err, char *buf, size_t buflen);
uintmax_t malloc_strtoumax(const char *restrict nptr, char **restrict endptr,
int base);
@@ -99,29 +100,4 @@ malloc_read_fd(int fd, void *buf, size_t count) {
return (ssize_t)result;
}
/******************************************************************************/
/*
* The rest is buffered writing utility.
*
* The only difference when using the buffered writer is that cbopaque is
* passed to write_cb only when the buffer is flushed. It would make a
* difference if cbopaque points to something that's changing for each write_cb
* call, or something that affects write_cb in a way dependent on the content
* of the output string. However, the most typical usage case in practice is
* that cbopaque points to some "option like" content for the write_cb, so it
* doesn't matter.
*/
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_end;
} buf_write_arg_t;
void buf_write_flush(buf_write_arg_t *arg);
void buf_write_cb(void *buf_write_arg, const char *s);
#endif /* JEMALLOC_INTERNAL_MALLOC_IO_H */