From b0c6047765944609d1eec479898547d792419f39 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 8 Aug 2021 14:15:26 -0400 Subject: Factor out duplicated ByteSource buffer allocation code --- src/byte_source.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/byte_source.c b/src/byte_source.c index c0cb6413..567beee8 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -60,6 +60,18 @@ serd_byte_source_page(SerdByteSource* const source) return SERD_SUCCESS; } +static void +serd_byte_source_init_buffer(SerdByteSource* const source) +{ + if (source->page_size > 1) { + source->file_buf = (uint8_t*)serd_allocate_buffer(source->page_size); + source->read_buf = source->file_buf; + memset(source->file_buf, '\0', source->page_size); + } else { + source->read_buf = &source->read_byte; + } +} + SerdByteSource* serd_byte_source_new_function(const SerdReadFunc read_func, const SerdStreamErrorFunc error_func, @@ -92,13 +104,7 @@ serd_byte_source_new_function(const SerdReadFunc read_func, source->caret.line = 1u; source->caret.col = 1u; - if (page_size > 1) { - source->file_buf = (uint8_t*)serd_allocate_buffer(page_size); - source->read_buf = source->file_buf; - memset(source->file_buf, '\0', page_size); - } else { - source->read_buf = &source->read_byte; - } + serd_byte_source_init_buffer(source); return source; } @@ -145,13 +151,7 @@ serd_byte_source_new_filename(const char* const path, const size_t page_size) source->caret.line = 1u; source->caret.col = 1u; - if (page_size > 1) { - source->file_buf = (uint8_t*)serd_allocate_buffer(page_size); - source->read_buf = source->file_buf; - memset(source->file_buf, '\0', page_size); - } else { - source->read_buf = &source->read_byte; - } + serd_byte_source_init_buffer(source); #if USE_POSIX_FADVISE && USE_FILENO posix_fadvise(fileno(fd), 0, 0, POSIX_FADV_SEQUENTIAL); -- cgit v1.2.1