aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-17 15:22:27 +0100
committerDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-17 15:22:27 +0100
commit735d8becf2595c8fee990b55e2d323317f019c8d (patch)
treeb162646504af9b55c57f9a71c5715c729d45fd6d
parent92be46c849a53968e469e544f84820e2fedf6dac (diff)
downloadunja-735d8becf2595c8fee990b55e2d323317f019c8d.tar.gz
unja-735d8becf2595c8fee990b55e2d323317f019c8d.zip
grow buffer capacity exponentially
-rw-r--r--src/template.c14
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");
+ }
}
}