????MySQL?У??????????Щ????????????????????磺DYNAMIC_STRING??
???????????????????????????????????????????????????????????????????μ???棬????????????MySQL?????DYNAMIC_STRING?????涯?????????????:
????typedef struct st_dynamic_string
????{
????char*str;
????size_tlength?? max_length?? alloc_increment;
????} DYNAMIC_STRING;
??????????????У?str?洢????????????????length?????????????????max_length?????????????????????????????alloc_increment?????????????????????????η????????檔
???????濴??????????????????:
my_bool init_dynamic_string( DYNAMIC_STRING *str?? const char *init_str?? size_t init_alloc?? size_t alloc_increment )
{
size_t length;
DBUG_ENTER( "init_dynamic_string" );
if ( !alloc_increment )
alloc_increment = 128;
length = 1;
if ( init_str && (length = strlen( init_str ) + 1) < init_alloc )
init_alloc = ( (length + alloc_increment - 1) / alloc_increment) * alloc_increment;
if ( !init_alloc )
init_alloc = alloc_increment;
if ( !(str->str = (char *) my_malloc( init_alloc?? MYF( MY_WME ) ) ) )
DBUG_RETURN( TRUE );
str->length = length - 1;
if ( init_str )
memcpy( str->str?? init_str?? length );
str->max_length= init_alloc;
str->alloc_increment= alloc_increment;
DBUG_RETURN( FALSE );
}
????????????????????????????????????????????????????Сinit_alloc???????????????????????ж?????????DYNAMIC_STRING??????????????????????С???????????????????alloc_increment?????????
????length:?????????????
????max_length:???????????
????alloc_increment:?????????′η???????????С.
???????????Щ???????????′????????????????????????????????Щ????ж????????????????????
my_bool dynstr_append_mem( DYNAMIC_STRING *str?? const char *append?? size_t length )
{
char *new_ptr;
if ( str->length + length >= str->max_length ) /* ??????????????????????????????С */
{
/* ???????????alloc_increment ??С????棬??????????????????? */
size_t new_length = (str->length + length + str->alloc_increment) /
str->alloc_increment;
new_length *= str->alloc_increment;
if ( !(new_ptr = (char *) my_realloc( str->str?? new_length?? MYF( MY_WME ) ) ) )
return(TRUE);
str->str= new_ptr;
str->max_length = new_length;
}
/* ???·?????????append??str??? */
memcpy( str->str + str->length?? append?? length );
str->length+= length;                              /* ???????str?μ???? */
str->str[str->length]= 0; /* Safety for C programs */        /* ??????????????’