libowfat/stralloc/stralloc_ready.c

21 lines
731 B
C
Raw Normal View History

2001-02-02 17:54:47 +00:00
#include "stralloc.h"
#include <stdlib.h>
/* stralloc_ready makes sure that sa has enough space allocated to hold
* len bytes: If sa is not allocated, stralloc_ready allocates at least
* len bytes of space, and returns 1. If sa is already allocated, but
* not enough to hold len bytes, stralloc_ready allocates at least len
* bytes of space, copies the old string into the new space, frees the
* old space, and returns 1. Note that this changes sa.s. */
2006-11-07 17:56:05 +00:00
int stralloc_ready(stralloc *sa,size_t len) {
2007-10-17 16:23:01 +00:00
register size_t wanted=len+(len>>3)+30; /* heuristic from djb */
2001-02-02 17:54:47 +00:00
if (!sa->s || sa->a<len) {
register char* tmp;
if (!(tmp=realloc(sa->s,wanted)))
return 0;
sa->a=wanted;
sa->s=tmp;
}
return 1;
}