From d5ba4851bd8c05ac5a43e302506a1ed67f6be7e7 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 6 Sep 2017 09:07:50 -0400 Subject: Add buf_t API helpers for using buffers to construct outputs. --- src/common/buffers.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/common/buffers.h') diff --git a/src/common/buffers.h b/src/common/buffers.h index 5a52b2a81c..b05c5b13d4 100644 --- a/src/common/buffers.h +++ b/src/common/buffers.h @@ -43,6 +43,11 @@ int buf_flush_to_socket(buf_t *buf, tor_socket_t s, size_t sz, size_t *buf_flushlen); int buf_add(buf_t *buf, const char *string, size_t string_len); +void buf_add_string(buf_t *buf, const char *string); +void buf_add_printf(buf_t *buf, const char *format, ...) + CHECK_PRINTF(2, 3); +void buf_add_vprintf(buf_t *buf, const char *format, va_list args) + CHECK_PRINTF(2, 0); int buf_add_compress(buf_t *buf, struct tor_compress_state_t *state, const char *data, size_t data_len, int done); int buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen); @@ -62,6 +67,7 @@ void buf_assert_ok(buf_t *buf); int buf_find_string_offset(const buf_t *buf, const char *s, size_t n); void buf_pullup(buf_t *buf, size_t bytes, const char **head_out, size_t *len_out); +char *buf_extract(buf_t *buf, size_t *sz_out); #ifdef BUFFERS_PRIVATE #ifdef TOR_UNIT_TESTS -- cgit v1.2.3-54-g00ecf From 095e15f8ac8216cd945b85c159b461d8cd0697a3 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 6 Sep 2017 09:30:50 -0400 Subject: Add a zero-copy buffer move implementation. --- src/common/buffers.c | 22 ++++++++++++++++++++++ src/common/buffers.h | 1 + 2 files changed, 23 insertions(+) (limited to 'src/common/buffers.h') diff --git a/src/common/buffers.c b/src/common/buffers.c index d7c4e4d8c3..d1725cf1a1 100644 --- a/src/common/buffers.c +++ b/src/common/buffers.c @@ -838,6 +838,28 @@ buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen) return (int)cp; } +/** Moves all data from buf_in to buf_out, without copying. + */ +void +buf_move_all(buf_t *buf_out, buf_t *buf_in) +{ + tor_assert(buf_out); + if (!buf_in) + return; + + if (buf_out->head == NULL) { + buf_out->head = buf_in->head; + buf_out->tail = buf_in->tail; + } else { + buf_out->tail->next = buf_in->head; + buf_out->tail = buf_in->tail; + } + + buf_out->datalen += buf_in->datalen; + buf_in->head = buf_in->tail = NULL; + buf_in->datalen = 0; +} + /** Internal structure: represents a position in a buffer. */ typedef struct buf_pos_t { const chunk_t *chunk; /**< Which chunk are we pointing to? */ diff --git a/src/common/buffers.h b/src/common/buffers.h index b05c5b13d4..56ba7c77ba 100644 --- a/src/common/buffers.h +++ b/src/common/buffers.h @@ -51,6 +51,7 @@ void buf_add_vprintf(buf_t *buf, const char *format, va_list args) int buf_add_compress(buf_t *buf, struct tor_compress_state_t *state, const char *data, size_t data_len, int done); int buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen); +void buf_move_all(buf_t *buf_out, buf_t *buf_in); void buf_peek(const buf_t *buf, char *string, size_t string_len); void buf_drain(buf_t *buf, size_t n); int buf_get_bytes(buf_t *buf, char *string, size_t string_len); -- cgit v1.2.3-54-g00ecf