diff options
author | Danny van Kooten <dannyvankooten@users.noreply.github.com> | 2020-03-17 15:22:27 +0100 |
---|---|---|
committer | Danny van Kooten <dannyvankooten@users.noreply.github.com> | 2020-03-17 15:22:27 +0100 |
commit | 735d8becf2595c8fee990b55e2d323317f019c8d (patch) | |
tree | b162646504af9b55c57f9a71c5715c729d45fd6d /src | |
parent | 92be46c849a53968e469e544f84820e2fedf6dac (diff) | |
download | unja-735d8becf2595c8fee990b55e2d323317f019c8d.tar.gz unja-735d8becf2595c8fee990b55e2d323317f019c8d.zip |
grow buffer capacity exponentially
Diffstat (limited to 'src')
-rw-r--r-- | src/template.c | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/template.c b/src/template.c index 0529c62..7aa3a75 100644 --- a/src/template.c +++ b/src/template.c @@ -1,5 +1,6 @@ #include <stdio.h> #include <stdlib.h> +#include <err.h> #include "vendor/mpc.h" #include "template.h" @@ -26,9 +27,16 @@ struct buffer { }; void buffer_reserve(struct buffer *buf, int l) { - if (buf->size + l >= buf->cap) { - buf->cap *= 2; - buf->string = realloc(buf->string, buf->size + l); + int req_size = buf->size + l; + if (req_size >= buf->cap) { + while (req_size >= buf->cap) { + buf->cap *= 2; + } + + buf->string = realloc(buf->string, buf->cap * sizeof *buf->string); + if (!buf->string) { + err(EXIT_FAILURE, "out of memory"); + } } } |