summaryrefslogtreecommitdiffstats
path: root/src/bytearray.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bytearray.c')
-rw-r--r--src/bytearray.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/src/bytearray.c b/src/bytearray.c
index 861890e..fff5089 100644
--- a/src/bytearray.c
+++ b/src/bytearray.c
@@ -41,15 +41,20 @@ void byte_array_free(bytearray_t *ba)
41 free(ba); 41 free(ba);
42} 42}
43 43
44void byte_array_grow(bytearray_t *ba, size_t amount)
45{
46 size_t increase = (amount > PAGE_SIZE) ? (amount+(PAGE_SIZE-1)) & (~(PAGE_SIZE-1)) : PAGE_SIZE;
47 ba->data = realloc(ba->data, ba->capacity + increase);
48 ba->capacity += increase;
49}
50
44void byte_array_append(bytearray_t *ba, void *buf, size_t len) 51void byte_array_append(bytearray_t *ba, void *buf, size_t len)
45{ 52{
46 if (!ba || !ba->data || (len <= 0)) return; 53 if (!ba || !ba->data || (len <= 0)) return;
47 size_t remaining = ba->capacity-ba->len; 54 size_t remaining = ba->capacity-ba->len;
48 if (len > remaining) { 55 if (len > remaining) {
49 size_t needed = len - remaining; 56 size_t needed = len - remaining;
50 size_t increase = (needed > PAGE_SIZE) ? (needed+(PAGE_SIZE-1)) & (~(PAGE_SIZE-1)) : PAGE_SIZE; 57 byte_array_grow(ba, needed);
51 ba->data = realloc(ba->data, ba->capacity + increase);
52 ba->capacity += increase;
53 } 58 }
54 memcpy(((char*)ba->data) + ba->len, buf, len); 59 memcpy(((char*)ba->data) + ba->len, buf, len);
55 ba->len += len; 60 ba->len += len;