(sn_append_char): don't write any terminating zero.
(as_reserve): don't loop. better heuristic for how much space to realloc. (vasnprintf): simplify initializing to one. git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@4082 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
@@ -80,7 +80,6 @@ static int
|
|||||||
sn_append_char (struct state *state, char c)
|
sn_append_char (struct state *state, char c)
|
||||||
{
|
{
|
||||||
if (sn_reserve (state, 1)) {
|
if (sn_reserve (state, 1)) {
|
||||||
*state->s++ = '\0';
|
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
*state->s++ = c;
|
*state->s++ = c;
|
||||||
@@ -91,17 +90,16 @@ sn_append_char (struct state *state, char c)
|
|||||||
static int
|
static int
|
||||||
as_reserve (struct state *state, size_t n)
|
as_reserve (struct state *state, size_t n)
|
||||||
{
|
{
|
||||||
while (state->s + n > state->theend) {
|
if (state->s + n > state->theend) {
|
||||||
int off = state->s - state->str;
|
int off = state->s - state->str;
|
||||||
char *tmp;
|
char *tmp;
|
||||||
|
|
||||||
if (state->max_sz && state->sz >= state->max_sz)
|
if (state->max_sz && state->sz >= state->max_sz)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
state->sz = max(state->sz * 2, state->sz + n);
|
||||||
if (state->max_sz)
|
if (state->max_sz)
|
||||||
state->sz = min(state->max_sz, state->sz*2);
|
state->sz = min(state->sz, state->max_sz);
|
||||||
else
|
|
||||||
state->sz *= 2;
|
|
||||||
tmp = realloc (state->str, state->sz);
|
tmp = realloc (state->str, state->sz);
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
@@ -560,9 +558,6 @@ vasnprintf (char **ret, size_t max_sz, const char *format, va_list args)
|
|||||||
struct state state;
|
struct state state;
|
||||||
|
|
||||||
state.max_sz = max_sz;
|
state.max_sz = max_sz;
|
||||||
if (max_sz)
|
|
||||||
state.sz = min(1, max_sz);
|
|
||||||
else
|
|
||||||
state.sz = 1;
|
state.sz = 1;
|
||||||
state.str = malloc(state.sz);
|
state.str = malloc(state.sz);
|
||||||
if (state.str == NULL) {
|
if (state.str == NULL) {
|
||||||
|
Reference in New Issue
Block a user