From: Michael D. Lowis Date: Wed, 3 May 2017 01:01:00 +0000 (-0400) Subject: resize buffer on file load for efficiency X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=8b892ead3aaf17bb9b144b8ebba8d82d9faee082;p=projs%2Ftide.git resize buffer on file load for efficiency --- diff --git a/lib/buf.c b/lib/buf.c index 7164974..e5afc32 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -194,6 +194,12 @@ void buf_init(Buf* buf) { assert(buf->bufstart); } +static size_t next_size(size_t curr) { + int size = 1; + while(size < curr) + size = (size << 1); +} + unsigned buf_load(Buf* buf, char* path) { /* process the file path and address */ if (path && path[0] == '.' && path[1] == '/') @@ -211,6 +217,7 @@ unsigned buf_load(Buf* buf, char* path) { die("Unsupported character set"); /* read the file contents into the buffer */ + buf_resize(buf, next_size(file.len)); for (size_t i = 0; i < file.len;) { Rune r; if (buf->charset == BINARY) {