Commit d8e6bbfa authored by Lev Walkin's avatar Lev Walkin

fix memory realloc

parent 52b88e1b
...@@ -279,10 +279,12 @@ static int _el_addbytes(const void *buffer, size_t size, void *el_buf_ptr) { ...@@ -279,10 +279,12 @@ static int _el_addbytes(const void *buffer, size_t size, void *el_buf_ptr) {
struct _el_buffer *el_buf = (struct _el_buffer *)el_buf_ptr; struct _el_buffer *el_buf = (struct _el_buffer *)el_buf_ptr;
if(el_buf->length + size > el_buf->allocated_size) { if(el_buf->length + size > el_buf->allocated_size) {
size_t new_size; size_t new_size = el_buf->allocated_size ? el_buf->allocated_size : 8;
void *p; void *p;
new_size = el_buf->allocated_size ? 2 * el_buf->allocated_size : 16; do {
new_size <<= 2;
} while(el_buf->length + size > new_size);
p = REALLOC(el_buf->buf, new_size); p = REALLOC(el_buf->buf, new_size);
if(p) { if(p) {
......
...@@ -82,15 +82,21 @@ static int ...@@ -82,15 +82,21 @@ static int
encode_dyn_cb(const void *buffer, size_t size, void *key) { encode_dyn_cb(const void *buffer, size_t size, void *key) {
enc_dyn_arg *arg = key; enc_dyn_arg *arg = key;
if(arg->length + size >= arg->allocated) { if(arg->length + size >= arg->allocated) {
size_t new_size = arg->allocated ? arg->allocated : 8;
void *p; void *p;
arg->allocated = arg->allocated ? (arg->allocated << 2) : size;
p = REALLOC(arg->buffer, arg->allocated); do {
new_size <<= 2;
} while(arg->length + size >= new_size);
p = REALLOC(arg->buffer, new_size);
if(!p) { if(!p) {
FREEMEM(arg->buffer); FREEMEM(arg->buffer);
memset(arg, 0, sizeof(*arg)); memset(arg, 0, sizeof(*arg));
return -1; return -1;
} }
arg->buffer = p; arg->buffer = p;
arg->allocated = new_size;
} }
memcpy(((char *)arg->buffer) + arg->length, buffer, size); memcpy(((char *)arg->buffer) + arg->length, buffer, size);
arg->length += size; arg->length += size;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment