]> git.mdlowis.com Git - projs/tide.git/commitdiff
resize buffer on file load for efficiency
authorMichael D. Lowis <mike@mdlowis.com>
Wed, 3 May 2017 01:01:00 +0000 (21:01 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Wed, 3 May 2017 01:01:00 +0000 (21:01 -0400)
lib/buf.c

index 7164974a9e5b2cabbac9b09a7ba7eb0e60f36b4a..e5afc32924f11ae75cec47422dcd4afa44136541 100644 (file)
--- 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) {