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; return full;
} }
char *copystring( const char *s ){ char *copystring( const char *src ){
char *b; const size_t size = strlen( src ) + 1;
b = safe_malloc( strlen( s ) + 1 ); return memcpy( safe_malloc( size ), src, size );
strcpy( b, s );
return b;
} }

View File

@ -155,7 +155,7 @@ char *COM_Parse( char *data );
extern char com_token[1024]; extern char com_token[1024];
extern qboolean com_eof; 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 ); void CRC_Init( unsigned short *crcvalue );

View File

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

View File

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

View File

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