Created
June 1, 2009 18:54
-
-
Save terrbear/121650 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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