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; } } }