From 3803bdff5f13dfa8a659200f0379646924d3a0bf Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 24 Jun 2021 15:56:58 +0100 Subject: [PATCH] Allow `bufio.Reader` and `bufio.Writer` to have a configurable `max_consecutive_empty_(reads|writes)` field --- core/bufio/reader.odin | 10 ++++++++-- core/bufio/writer.odin | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/core/bufio/reader.odin b/core/bufio/reader.odin index b26a2769d..27f157630 100644 --- a/core/bufio/reader.odin +++ b/core/bufio/reader.odin @@ -17,6 +17,8 @@ Reader :: struct { last_byte: int, // last byte read, invalid is -1 last_rune_size: int, // size of last rune read, invalid is -1 + + max_consecutive_empty_reads: int, } @@ -25,7 +27,7 @@ DEFAULT_BUF_SIZE :: 4096; @(private) MIN_READ_BUFFER_SIZE :: 16; @(private) -MAX_CONSECUTIVE_EMPTY_READS :: 128; +DEFAULT_MAX_CONSECUTIVE_EMPTY_READS :: 128; reader_init :: proc(b: ^Reader, rd: io.Reader, size: int = DEFAULT_BUF_SIZE, allocator := context.allocator) { size := size; @@ -71,8 +73,12 @@ _reader_read_new_chunk :: proc(b: ^Reader) -> io.Error { return .Buffer_Full; } + if b.max_consecutive_empty_reads <= 0 { + b.max_consecutive_empty_reads = DEFAULT_MAX_CONSECUTIVE_EMPTY_READS; + } + // read new data, and try a limited number of times - for i := MAX_CONSECUTIVE_EMPTY_READS; i > 0; i -= 1 { + for i := b.max_consecutive_empty_reads; i > 0; i -= 1 { n, err := io.read(b.rd, b.buf[b.w:]); if n < 0 { return .Negative_Read; diff --git a/core/bufio/writer.odin b/core/bufio/writer.odin index 28870dcea..cb6b39525 100644 --- a/core/bufio/writer.odin +++ b/core/bufio/writer.odin @@ -15,6 +15,8 @@ Writer :: struct { err: io.Error, + max_consecutive_empty_writes: int, + } writer_init :: proc(b: ^Writer, wr: io.Writer, size: int = DEFAULT_BUF_SIZE, allocator := context.allocator) { @@ -185,16 +187,20 @@ writer_read_from :: proc(b: ^Writer, r: io.Reader) -> (n: i64, err: io.Error) { return n, ferr; } } + if b.max_consecutive_empty_writes <= 0 { + b.max_consecutive_empty_writes = DEFAULT_MAX_CONSECUTIVE_EMPTY_READS; + } + m: int; nr := 0; - for nr < MAX_CONSECUTIVE_EMPTY_READS { + for nr < b.max_consecutive_empty_writes { m, err = io.read(r, b.buf[b.n:]); if m != 0 || err != nil { break; } nr += 1; } - if nr == MAX_CONSECUTIVE_EMPTY_READS { + if nr == b.max_consecutive_empty_writes { return n, .No_Progress; } b.n += m;