optimize copystring(), use it

This commit is contained in:
Garux 2020-01-10 20:43:15 +03:00
parent d9bad45a3d
commit d320ac3a4b
5 changed files with 25 additions and 47 deletions

View File

@ -238,11 +238,9 @@ char *ExpandPath( const char *path ){
return full;
}
char *copystring( const char *s ){
char *b;
b = safe_malloc( strlen( s ) + 1 );
strcpy( b, s );
return b;
char *copystring( const char *src ){
const size_t size = strlen( src ) + 1;
return memcpy( safe_malloc( size ), src, size );
}

View File

@ -155,7 +155,7 @@ char *COM_Parse( char *data );
extern char com_token[1024];
extern qboolean com_eof;
char *copystring( const char *s );
char *copystring( const char *src ); // version of strdup() with safe_malloc()
void CRC_Init( unsigned short *crcvalue );

View File

@ -242,10 +242,8 @@ static void ImageInit( void ){
memset( images, 0, sizeof( images ) );
/* generate *bogus image */
images[ 0 ].name = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
strcpy( images[ 0 ].name, DEFAULT_IMAGE );
images[ 0 ].filename = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
strcpy( images[ 0 ].filename, DEFAULT_IMAGE );
images[ 0 ].name = copystring( DEFAULT_IMAGE );
images[ 0 ].filename = copystring( DEFAULT_IMAGE );
images[ 0 ].width = 64;
images[ 0 ].height = 64;
images[ 0 ].refCount = 1;
@ -375,8 +373,7 @@ image_t *ImageLoad( const char *filename ){
}
/* set it up */
image->name = safe_malloc( strlen( name ) + 1 );
strcpy( image->name, name );
image->name = copystring( name );
/* attempt to load tga */
StripExtension( name );
@ -447,8 +444,7 @@ image_t *ImageLoad( const char *filename ){
}
/* set filename */
image->filename = safe_malloc( strlen( name ) + 1 );
strcpy( image->filename, name );
image->filename = copystring( name );
/* set count */
image->refCount = 1;

View File

@ -260,10 +260,10 @@ void AddBasePath( char *path ){
}
/* add it to the list */
basePaths[ numBasePaths ] = safe_malloc( strlen( path ) + 1 );
strcpy( basePaths[ numBasePaths ], path );
basePaths[ numBasePaths ] = copystring( path );
FixDOSName( basePaths[ numBasePaths ] );
if ( EnginePath[0] == '\0' ) strcpy( EnginePath, basePaths[ numBasePaths ] );
if ( EnginePath[0] == '\0' )
strcpy( EnginePath, basePaths[ numBasePaths ] );
numBasePaths++;
}
@ -317,8 +317,7 @@ void AddHomeBasePath( char *path ){
basePaths[ i + 1 ] = basePaths[ i ];
/* add it to the list */
basePaths[ 0 ] = safe_malloc( strlen( temp ) + 1 );
strcpy( basePaths[ 0 ], temp );
basePaths[ 0 ] = copystring( temp );
FixDOSName( basePaths[ 0 ] );
numBasePaths++;
}
@ -339,8 +338,7 @@ void AddGamePath( char *path ){
}
/* add it to the list */
gamePaths[ numGamePaths ] = safe_malloc( strlen( path ) + 1 );
strcpy( gamePaths[ numGamePaths ], path );
gamePaths[ numGamePaths ] = copystring( path );
FixDOSName( gamePaths[ numGamePaths ] );
numGamePaths++;
@ -370,8 +368,7 @@ void AddPakPath( char *path ){
}
/* add it to the list */
pakPaths[ numPakPaths ] = safe_malloc( strlen( path ) + 1 );
strcpy( pakPaths[ numPakPaths ], path );
pakPaths[ numPakPaths ] = copystring( path );
FixDOSName( pakPaths[ numPakPaths ] );
numPakPaths++;
}

View File

@ -561,8 +561,7 @@ shaderInfo_t *CustomShader( shaderInfo_t *si, char *find, char *replace ){
csi->custom = qtrue;
/* store new shader text */
csi->shaderText = safe_malloc( strlen( shaderText ) + 1 );
strcpy( csi->shaderText, shaderText ); /* LEAK! */
csi->shaderText = copystring( shaderText ); /* LEAK! */
/* return it */
return csi;
@ -980,8 +979,7 @@ static void ParseShaderFile( const char *filename ){
/* copy shader text to the shaderinfo */
if ( si != NULL && shaderText[ 0 ] != '\0' ) {
strcat( shaderText, "\n" );
si->shaderText = safe_malloc( strlen( shaderText ) + 1 );
strcpy( si->shaderText, shaderText );
si->shaderText = copystring( shaderText );
//% if( VectorLength( si->vecs[ 0 ] ) )
//% Sys_Printf( "%s\n", shaderText );
}
@ -1169,8 +1167,7 @@ static void ParseShaderFile( const char *filename ){
else if ( !Q_stricmp( token, "damageShader" ) ) {
GetTokenAppend( shaderText, qfalse );
if ( token[ 0 ] != '\0' ) {
si->damageShader = safe_malloc( strlen( token ) + 1 );
strcpy( si->damageShader, token );
si->damageShader = copystring( token );
}
GetTokenAppend( shaderText, qfalse ); /* don't do anything with health */
}
@ -1610,8 +1607,7 @@ static void ParseShaderFile( const char *filename ){
else if ( !Q_stricmp( token, "q3map_flare" ) || !Q_stricmp( token, "q3map_flareShader" ) ) {
GetTokenAppend( shaderText, qfalse );
if ( token[ 0 ] != '\0' ) {
si->flareShader = safe_malloc( strlen( token ) + 1 );
strcpy( si->flareShader, token );
si->flareShader = copystring( token );
}
}
@ -1619,8 +1615,7 @@ static void ParseShaderFile( const char *filename ){
else if ( !Q_stricmp( token, "q3map_backShader" ) ) {
GetTokenAppend( shaderText, qfalse );
if ( token[ 0 ] != '\0' ) {
si->backShader = safe_malloc( strlen( token ) + 1 );
strcpy( si->backShader, token );
si->backShader = copystring( token );
}
}
@ -1628,8 +1623,7 @@ static void ParseShaderFile( const char *filename ){
else if ( !Q_stricmp( token, "q3map_cloneShader" ) ) {
GetTokenAppend( shaderText, qfalse );
if ( token[ 0 ] != '\0' ) {
si->cloneShader = safe_malloc( strlen( token ) + 1 );
strcpy( si->cloneShader, token );
si->cloneShader = copystring( token );
}
}
@ -1637,8 +1631,7 @@ static void ParseShaderFile( const char *filename ){
else if ( !Q_stricmp( token, "q3map_remapShader" ) ) {
GetTokenAppend( shaderText, qfalse );
if ( token[ 0 ] != '\0' ) {
si->remapShader = safe_malloc( strlen( token ) + 1 );
strcpy( si->remapShader, token );
si->remapShader = copystring( token );
}
}
@ -1646,9 +1639,7 @@ static void ParseShaderFile( const char *filename ){
else if ( !Q_stricmp( token, "q3map_deprecateShader" ) ) {
GetTokenAppend( shaderText, qfalse );
if ( token[ 0 ] != '\0' ) {
si->deprecateShader = safe_malloc( strlen( token ) + 1 );
strcpy( si->deprecateShader, token );
si->deprecateShader = copystring( token );
}
}
@ -2031,8 +2022,7 @@ static void ParseCustomInfoParms( void ){
break;
}
custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
custSurfaceParms[ numCustSurfaceParms ].name = copystring( token );
GetToken( qfalse );
sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].contentFlags );
numCustSurfaceParms++;
@ -2057,8 +2047,7 @@ static void ParseCustomInfoParms( void ){
break;
}
custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
custSurfaceParms[ numCustSurfaceParms ].name = copystring( token );
GetToken( qfalse );
sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].surfaceFlags );
numCustSurfaceParms++;
@ -2122,9 +2111,7 @@ void LoadShaderInfo( void ){
/* new shader file */
if ( j == numShaderFiles ) {
shaderFiles[ numShaderFiles ] = safe_malloc( MAX_OS_PATH );
strcpy( shaderFiles[ numShaderFiles ], token );
numShaderFiles++;
shaderFiles[ numShaderFiles++ ] = copystring( token );
}
}
}