summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 3cd9777)
raw | patch | inline | side by side (parent: 3cd9777)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 17 Sep 2014 09:22:54 +0000 (11:22 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Wed, 17 Sep 2014 09:22:54 +0000 (11:22 +0200) |
That'll make sure that lots of small writes don't result in lots of memory
reallocations.
reallocations.
src/utils/strbuf.c | patch | blob | history |
diff --git a/src/utils/strbuf.c b/src/utils/strbuf.c
index ecc05315ef897f9d84a415ccecb9b6f77e89e9bd..d8a95ac42f60a4003d9cdf21924cbfbf2a5ccee1 100644 (file)
--- a/src/utils/strbuf.c
+++ b/src/utils/strbuf.c
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#include "sysdb.h"
#include "utils/strbuf.h"
#include <assert.h>
static int
strbuf_resize(sdb_strbuf_t *buf, size_t new_size)
{
+ size_t tmp_size;
char *tmp;
if (new_size <= buf->pos)
return -1;
+ tmp_size = SDB_MAX(buf->size, buf->min_size);
+ if (! tmp_size)
+ tmp_size = 64;
+ while (tmp_size < new_size)
+ tmp_size *= 2;
+
tmp = realloc(buf->string, new_size);
if (! tmp)
return -1;
assert((buf->size == 0) || (buf->string[buf->pos] == '\0'));
- if (buf->pos + n + 1 >= buf->size) {
- size_t newsize = buf->size * 2;
-
- if (! newsize)
- newsize = 64;
- while (buf->pos + n + 1 >= newsize)
- newsize *= 2;
-
- if (strbuf_resize(buf, newsize))
+ if (buf->pos + n + 1 > buf->size) {
+ if (strbuf_resize(buf, buf->pos + n + 1))
return -1;
}