Skip to content

Instantly share code, notes, and snippets.

@terrbear
Created June 1, 2009 18:54
Show Gist options
  • Select an option

  • Save terrbear/121650 to your computer and use it in GitHub Desktop.

Select an option

Save terrbear/121650 to your computer and use it in GitHub Desktop.
static void setDefaultAllocator( TidyBuffer* buf )
{
buf->allocator = &TY_(g_default_allocator);
}
/* Avoid thrashing memory by doubling buffer size
** until larger than requested size.
buf->allocated is bigger than allocSize+1 so that a trailing null byte is
always available.
*/
void TIDY_CALL tidyBufCheckAlloc( TidyBuffer* buf, uint allocSize, uint chunkSize )
{
assert( buf != NULL );
if ( !buf->allocator )
setDefaultAllocator( buf );
if ( 0 == chunkSize )
chunkSize = 256;
if ( allocSize+1 > buf->allocated ) {
byte* bp;
uint allocAmt = chunkSize;
if ( buf->allocated > 0 )
allocAmt = buf->allocated;
while ( allocAmt < allocSize+1 )
allocAmt *= 2;
bp = (byte*)TidyRealloc( buf->allocator, buf->bp, allocAmt );
if ( bp != NULL )
{
TidyClearMemory( bp + buf->allocated, allocAmt - buf->allocated );
buf->bp = bp;
buf->allocated = allocAmt;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment