|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org> |
|
|
|
|
* Copyright (c) 2015,2017 Con Kolivas <kernel@kolivas.org> |
|
|
|
|
* |
|
|
|
|
* Jansson is free software; you can redistribute it and/or modify |
|
|
|
|
* it under the terms of the MIT license. See LICENSE for details. |
|
|
|
@ -11,10 +12,11 @@
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h> |
|
|
|
|
#include <string.h> |
|
|
|
|
#include <unistd.h> |
|
|
|
|
#include "jansson_private.h" |
|
|
|
|
#include "strbuffer.h" |
|
|
|
|
|
|
|
|
|
#define STRBUFFER_MIN_SIZE 16 |
|
|
|
|
#define STRBUFFER_MIN_SIZE 4096 |
|
|
|
|
#define STRBUFFER_FACTOR 2 |
|
|
|
|
#define STRBUFFER_SIZE_MAX ((size_t)-1) |
|
|
|
|
|
|
|
|
@ -67,8 +69,10 @@ int strbuffer_append_byte(strbuffer_t *strbuff, char byte)
|
|
|
|
|
|
|
|
|
|
int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size) |
|
|
|
|
{ |
|
|
|
|
if(size >= strbuff->size - strbuff->length) |
|
|
|
|
/* Leave room for EOL and NULL bytes */ |
|
|
|
|
if(size + 2 > strbuff->size - strbuff->length) |
|
|
|
|
{ |
|
|
|
|
int backoff = 1; |
|
|
|
|
size_t new_size; |
|
|
|
|
char *new_value; |
|
|
|
|
|
|
|
|
@ -81,13 +85,14 @@ int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size)
|
|
|
|
|
new_size = max(strbuff->size * STRBUFFER_FACTOR, |
|
|
|
|
strbuff->length + size + 1); |
|
|
|
|
|
|
|
|
|
new_value = jsonp_malloc(new_size); |
|
|
|
|
if(!new_value) |
|
|
|
|
return -1; |
|
|
|
|
|
|
|
|
|
memcpy(new_value, strbuff->value, strbuff->length); |
|
|
|
|
while (42) { |
|
|
|
|
new_value = realloc(strbuff->value, new_size); |
|
|
|
|
if (new_value) |
|
|
|
|
break; |
|
|
|
|
usleep(backoff * 1000); |
|
|
|
|
backoff <<= 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
jsonp_free(strbuff->value); |
|
|
|
|
strbuff->value = new_value; |
|
|
|
|
strbuff->size = new_size; |
|
|
|
|
} |
|
|
|
|