diff options
Diffstat (limited to 'src/bytearray.c')
-rw-r--r-- | src/bytearray.c | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/src/bytearray.c b/src/bytearray.c index 2c6ce4a..861890e 100644 --- a/src/bytearray.c +++ b/src/bytearray.c @@ -21,12 +21,14 @@ #include <string.h> #include "bytearray.h" +#define PAGE_SIZE 4096 + bytearray_t *byte_array_new() { bytearray_t *a = (bytearray_t*)malloc(sizeof(bytearray_t)); - a->data = malloc(256); + a->capacity = PAGE_SIZE * 8; + a->data = malloc(a->capacity); a->len = 0; - a->capacity = 256; return a; } @@ -44,8 +46,10 @@ void byte_array_append(bytearray_t *ba, void *buf, size_t len) if (!ba || !ba->data || (len <= 0)) return; size_t remaining = ba->capacity-ba->len; if (len > remaining) { - ba->data = realloc(ba->data, ba->capacity + (len - remaining)); - ba->capacity += (len - remaining); + size_t needed = len - remaining; + size_t increase = (needed > PAGE_SIZE) ? (needed+(PAGE_SIZE-1)) & (~(PAGE_SIZE-1)) : PAGE_SIZE; + ba->data = realloc(ba->data, ba->capacity + increase); + ba->capacity += increase; } memcpy(((char*)ba->data) + ba->len, buf, len); ba->len += len; |