summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2016-01-31 22:04:00 +0100
committerHiltjo Posthuma <hiltjo@codemadness.org>2016-01-31 22:04:00 +0100
commitfb3e9d05d1cf81783ea49629a2180512f5a60ecf (patch)
treeaf2c0e538cd7af0d8f7cee0725bf213cc73456d8
parentb88aad839e5a541ef24ecc216d3824c5af3b59f6 (diff)
Revert "sfeed: realloc, faster near pow 2 bufsiz"
This reverts commit 5e43bd658e578ced54f6065e95f6efb4892e114c. It is a neat bit trick, but it doesn't matter much in thiscase and it's less readable and possibly less portable.
-rw-r--r--sfeed.c27
1 files changed, 11 insertions, 16 deletions
diff --git a/sfeed.c b/sfeed.c
index b06bff6..a75d832 100644
--- a/sfeed.c
+++ b/sfeed.c
@@ -207,22 +207,15 @@ string_clear(String *s)
static void
string_buffer_realloc(String *s, size_t newlen)
{
- uint32_t v;
- /* check if allocation is necessary, don't shrink buffer,
- * should be more than bufsiz ofcourse. */
- if (newlen <= s->bufsiz)
- return;
+ char *p;
+ size_t alloclen;
- v = (uint32_t)newlen;
- v |= v >> 1;
- v |= v >> 2;
- v |= v >> 4;
- v |= v >> 8;
- v |= v >> 16;
- v++;
- if (!(s->data = realloc(s->data, (size_t)v)))
+ for (alloclen = 64; alloclen <= newlen; alloclen *= 2)
+ ;
+ if (!(p = realloc(s->data, alloclen)))
err(1, "realloc");
- s->bufsiz = (size_t)v;
+ s->bufsiz = alloclen;
+ s->data = p;
}
static void
@@ -230,8 +223,10 @@ string_append(String *s, const char *data, size_t len)
{
if (!len || *data == '\0')
return;
-
- string_buffer_realloc(s, s->len + len + 1);
+ /* check if allocation is necesary, don't shrink buffer,
+ * should be more than bufsiz ofcourse. */
+ if (s->len + len >= s->bufsiz)
+ string_buffer_realloc(s, s->len + len + 1);
memcpy(s->data + s->len, data, len);
s->len += len;
s->data[s->len] = '\0';