iupdate buffer.

This commit is contained in:
Vitor Santos Costa 2013-01-15 17:13:41 +00:00
parent e85e76cd57
commit 5a6f6c09ce
2 changed files with 30 additions and 12 deletions

View File

@ -28,6 +28,7 @@ int
growBuffer(Buffer b, size_t minfree)
{ size_t osz = b->max - b->base, sz = osz;
size_t top = b->top - b->base;
char *new;
if ( b->max - b->top >= (int)minfree )
return TRUE;
@ -37,20 +38,17 @@ growBuffer(Buffer b, size_t minfree)
while( top + minfree > sz )
sz *= 2;
if ( b->base != b->static_buffer )
{ b->base = realloc(b->base, sz);
if ( !b->base )
return FALSE;
} else /* from static buffer */
{ char *new;
if ( !(new = malloc(sz)) )
if ( b->base == b->static_buffer )
{ if ( !(new = malloc(sz)) )
return FALSE;
memcpy(new, b->static_buffer, osz);
b->base = new;
} else
{ if ( !(new = realloc(b->base, sz)) )
return FALSE;
}
b->base = new;
b->top = b->base + top;
b->max = b->base + sz;

View File

@ -68,6 +68,24 @@ int growBuffer(Buffer b, size_t minfree);
(b)->top = (char *)_d; \
} while(0)
#define allocFromBuffer(b, bytes) \
f__allocFromBuffer((Buffer)(b), (bytes))
static inline void*
f__allocFromBuffer(Buffer b, size_t bytes)
{ if ( b->top + bytes <= b->max ||
growBuffer(b, bytes) )
{ void *top = b->top;
b->top += bytes;
return top;
}
return NULL;
}
#define baseBuffer(b, type) ((type *) (b)->base)
#define topBuffer(b, type) ((type *) (b)->top)
#define inBuffer(b, addr) ((char *) (addr) >= (b)->base && \
@ -83,6 +101,8 @@ int growBuffer(Buffer b, size_t minfree);
sizeof((b)->static_buffer))
#define emptyBuffer(b) ((b)->top = (b)->base)
#define isEmptyBuffer(b) ((b)->top == (b)->base)
#define popBuffer(b,type) \
((b)->top -= sizeof(type), *(type*)(b)->top)
#define discardBuffer(b) \
do \