char *buf; /* start of buffer */
char *ptr; /* next place to write */
char *eptr; /* end of buffer */
- int dyna; /* true if buffer is malloc'ed */
+ int stack_allocated; // false if buffer is malloc'ed
} agxbuf;
/* agxbinit:
static inline void agxbinit(agxbuf *xb, unsigned int hint, char *init) {
if (init != NULL) {
xb->buf = init;
- xb->dyna = 0;
+ xb->stack_allocated = 1;
} else {
if (hint == 0) {
hint = BUFSIZ;
}
- xb->dyna = 1;
+ xb->stack_allocated = 0;
xb->buf = (char *)gv_calloc(hint, sizeof(char));
}
xb->eptr = xb->buf + hint;
* Free any malloced resources.
*/
static inline void agxbfree(agxbuf *xb) {
- if (xb->dyna)
+ if (!xb->stack_allocated)
free(xb->buf);
}
if (size + ssz > nsize)
nsize = size + ssz;
cnt = (size_t)(xb->ptr - xb->buf);
- if (xb->dyna) {
+ if (!xb->stack_allocated) {
nbuf = (char *)gv_recalloc(xb->buf, size, nsize, sizeof(char));
} else {
nbuf = (char *)gv_calloc(nsize, sizeof(char));
memcpy(nbuf, xb->buf, cnt);
- xb->dyna = 1;
+ xb->stack_allocated = 0;
}
xb->buf = nbuf;
xb->ptr = xb->buf + cnt;
// terminate the existing string
agxbputc(xb, '\0');
- if (!xb->dyna) {
+ if (xb->stack_allocated) {
// the buffer is not dynamically allocated, so we need to copy its contents
// to heap memory
// reset xb to a state where it is usable
xb->buf = xb->ptr = xb->eptr = NULL;
- xb->dyna = 1;
+ xb->stack_allocated = 0;
return buf;
}