From 484dd64d138a1ae977af05193036d3fe46220b2c Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 30 Oct 2014 19:21:30 -0700 Subject: * lib.c (chk_grow_vec): New function. (string_extend): Use chk_grow_vec. (vec_set_length): Avoid subtractions for comparing numbers. * lib.h (chk_grow_vec): Declared. * stream.c (snarf_line, string_out_put_string): Use chk_grow_vec. --- stream.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'stream.c') diff --git a/stream.c b/stream.c index 0d9ff3bb..fa274c51 100644 --- a/stream.c +++ b/stream.c @@ -313,8 +313,8 @@ static wchar_t *snarf_line(struct stdio_handle *h) if (fill >= size) { size_t newsize = size ? size * 2 : min_size; - buf = coerce(wchar_t *, chk_realloc(coerce(mem_t *, buf), - newsize * sizeof *buf)); + buf = coerce(wchar_t *, chk_grow_vec(coerce(mem_t *, buf), + size, newsize, sizeof *buf)); size = newsize; } @@ -325,6 +325,7 @@ static wchar_t *snarf_line(struct stdio_handle *h) buf[fill++] = ch; } + /* Trim to actual size */ if (buf) buf = coerce(wchar_t *, chk_realloc(coerce(mem_t *, buf), fill * sizeof *buf)); @@ -943,8 +944,9 @@ static val string_out_put_string(val stream, val str) } if (so->size != old_size) - so->buf = coerce(wchar_t *, chk_realloc(coerce(mem_t *, so->buf), - so->size * sizeof *so->buf)); + so->buf = coerce(wchar_t *, chk_grow_vec(coerce(mem_t *, so->buf), + old_size, so->size, + sizeof *so->buf)); wmemcpy(so->buf + so->fill, s, len + 1); so->fill += len; return t; @@ -1238,6 +1240,7 @@ val get_string_from_stream(val stream) stream->co.handle = 0; + /* Trim to actual size */ so->buf = coerce(wchar_t *, chk_realloc(coerce(mem_t *, so->buf), (so->fill + 1) * sizeof *so->buf)); out = string_own(so->buf); -- cgit v1.2.3