ref: f148a9d90d6581768c5197dc4e777edc0f6baf0d
parent: 3f23afabb528952a99b751ccc1ea7a7808b070df
author: Ali Gholami Rudi <ali@rudi.ir>
date: Fri May 10 07:18:03 EDT 2013
sbuf: decrease the default sbuf struct buffer size
--- a/sbuf.c
+++ b/sbuf.c
@@ -4,21 +4,22 @@
#include <string.h>
#include "xroff.h"
-#define SBUF_SZ 1024
+#define SBUF_SZ 512
-void sbuf_init(struct sbuf *sbuf)
+static void sbuf_extend(struct sbuf *sbuf, int amount)
{
- sbuf->s = malloc(SBUF_SZ);
- sbuf->sz = SBUF_SZ;
- sbuf->n = 0;
+ char *s = sbuf->s;
+ sbuf->sz = (MAX(1, amount) + SBUF_SZ - 1) & ~(SBUF_SZ - 1);
+ sbuf->s = malloc(sbuf->sz);
+ if (sbuf->n)
+ memcpy(sbuf->s, s, sbuf->n);
+ free(s);
}
-static void sbuf_extend(struct sbuf *sbuf, int amount)
+void sbuf_init(struct sbuf *sbuf)
{
- char *s = sbuf->s;
- sbuf->s = malloc(amount);
- sbuf->sz = amount;
- memcpy(sbuf->s, s, sbuf->n);
+ memset(sbuf, 0, sizeof(*sbuf));
+ sbuf_extend(sbuf, SBUF_SZ);
}
void sbuf_add(struct sbuf *sbuf, int c)