add safe_calloc(), safe_calloc_info() functions, use them (optimization, code shortening)

This commit is contained in:
Garux 2020-01-12 18:55:03 +03:00
parent 23a166a4dd
commit 99bae99b36
31 changed files with 122 additions and 260 deletions

View File

@ -96,9 +96,7 @@ void SetLightBytes( int n ){
return;
}
lightBytes = safe_malloc_info( numLightBytes, "SetLightBytes" );
memset( lightBytes, 0, numLightBytes );
lightBytes = safe_calloc_info( numLightBytes, "SetLightBytes" );
}
void SetGridPoints( int n ){
@ -112,9 +110,7 @@ void SetGridPoints( int n ){
return;
}
gridData = safe_malloc_info( numGridPoints * 8, "SetGridPoints" );
memset( gridData, 0, numGridPoints * 8 );
gridData = safe_calloc_info( numGridPoints * 8, "SetGridPoints" );
}
void IncDrawVerts(){
@ -149,12 +145,10 @@ void SetDrawVerts( int n ){
free( drawVerts );
}
numDrawVerts = n;
numDrawVertsBuffer = numDrawVerts;
numDrawVerts =
numDrawVertsBuffer = n;
drawVerts = safe_malloc_info( sizeof( drawVert_t ) * numDrawVertsBuffer, "IncDrawVerts" );
memset( drawVerts, 0, n * sizeof( drawVert_t ) );
drawVerts = safe_calloc_info( sizeof( drawVert_t ) * numDrawVertsBuffer, "IncDrawVerts" );
}
void SetDrawSurfacesBuffer(){
@ -164,9 +158,7 @@ void SetDrawSurfacesBuffer(){
numDrawSurfacesBuffer = MAX_MAP_DRAW_SURFS;
drawSurfaces = safe_malloc_info( sizeof( dsurface_t ) * numDrawSurfacesBuffer, "IncDrawSurfaces" );
memset( drawSurfaces, 0, MAX_MAP_DRAW_SURFS * sizeof( drawVert_t ) );
drawSurfaces = safe_calloc_info( sizeof( dsurface_t ) * numDrawSurfacesBuffer, "IncDrawSurfaces" );
}
void SetDrawSurfaces( int n ){
@ -174,12 +166,10 @@ void SetDrawSurfaces( int n ){
free( drawSurfaces );
}
numDrawSurfaces = n;
numDrawSurfacesBuffer = numDrawSurfaces;
numDrawSurfaces =
numDrawSurfacesBuffer = n;
drawSurfaces = safe_malloc_info( sizeof( dsurface_t ) * numDrawSurfacesBuffer, "IncDrawSurfaces" );
memset( drawSurfaces, 0, n * sizeof( drawVert_t ) );
drawSurfaces = safe_calloc_info( sizeof( dsurface_t ) * numDrawSurfacesBuffer, "IncDrawSurfaces" );
}
void BspFilesCleanup(){
@ -522,10 +512,7 @@ void StripTrailing( char *e ) {
=================
*/
epair_t *ParseEpair( void ) {
epair_t *e;
e = safe_malloc( sizeof( epair_t ) );
memset( e, 0, sizeof( epair_t ) );
epair_t *e = safe_calloc( sizeof( epair_t ) );
if ( strlen( token ) >= MAX_KEY - 1 ) {
Error( "ParseEpar: token too long" );

View File

@ -52,24 +52,34 @@
#ifdef SAFE_MALLOC
void *safe_malloc( size_t size ){
void *p;
p = malloc( size );
void *p = malloc( size );
if ( !p ) {
Error( "safe_malloc failed on allocation of %i bytes", size );
}
return p;
}
void *safe_malloc_info( size_t size, char* info ){
void *p;
p = malloc( size );
void *safe_malloc_info( size_t size, const char* info ){
void *p = malloc( size );
if ( !p ) {
Error( "%s: safe_malloc failed on allocation of %i bytes", info, size );
}
return p;
}
void *safe_calloc( size_t size ){
void *p = calloc( 1, size );
if ( !p ) {
Error( "safe_calloc failed on allocation of %i bytes", size );
}
return p;
}
void *safe_calloc_info( size_t size, const char* info ){
void *p = calloc( 1, size );
if ( !p ) {
Error( "%s: safe_calloc failed on allocation of %i bytes", info, size );
}
return p;
}
#endif
@ -619,8 +629,7 @@ int LoadFileBlock( const char *filename, void **bufferptr ){
if ( nBlock > 0 ) {
nAllocSize += MEM_BLOCKSIZE - nBlock;
}
buffer = safe_malloc( nAllocSize + 1 );
memset( buffer, 0, nAllocSize + 1 );
buffer = safe_calloc( nAllocSize + 1 );
SafeRead( f, buffer, length );
fclose( f );

View File

@ -66,9 +66,14 @@
#define SAFE_MALLOC
#ifdef SAFE_MALLOC
void *safe_malloc( size_t size );
void *safe_malloc_info( size_t size, char* info );
void *safe_malloc_info( size_t size, const char* info );
void *safe_calloc( size_t size );
void *safe_calloc_info( size_t size, const char* info );
#else
#define safe_malloc( a ) malloc( a )
#define safe_malloc( size ) malloc( size )
#define safe_malloc_info( size, info ) malloc( size )
#define safe_calloc( size ) calloc( 1, size )
#define safe_calloc_info( size, info ) calloc( 1, size )
#endif /* SAFE_MALLOC */
// set these before calling CheckParm

View File

@ -255,8 +255,7 @@ void LoadLBM( const char *filename, byte **picture, byte **palette ){
break;
case CMAPID:
cmapbuffer = safe_malloc( 768 );
memset( cmapbuffer, 0, 768 );
cmapbuffer = safe_calloc( 768 );
memcpy( cmapbuffer, LBM_P, chunklength );
break;

View File

@ -358,8 +358,7 @@ int LoadJPGBuff( void *src_buffer, int src_size, unsigned char **pic, int *width
size = cinfo.output_width * cinfo.output_height * 4;
*width = cinfo.output_width;
*height = cinfo.output_height;
*pic = (unsigned char*)( malloc( size + 1 ) );
memset( *pic, 0, size + 1 );
*pic = calloc( 1, size + 1 );
buffer = ( *cinfo.mem->alloc_sarray )( ( j_common_ptr ) & cinfo, JPOOL_IMAGE, row_stride, 1 );

View File

@ -128,7 +128,7 @@ void mdfour_begin( struct mdfour *md ){
static void mdfour_tail( unsigned char *in, int n ){
unsigned char buf[128];
unsigned char buf[128] = {0};
uint32 M[16];
uint32 b;
@ -136,7 +136,6 @@ static void mdfour_tail( unsigned char *in, int n ){
b = m->totalN * 8;
memset( buf, 0, 128 );
if ( n ) {
memcpy( buf, in, n );
}

View File

@ -53,9 +53,6 @@ void pw( winding_t *w ){
=============
*/
winding_t *AllocWinding( int points ){
winding_t *w;
int s;
if ( points >= MAX_POINTS_ON_WINDING ) {
Error( "AllocWinding failed: MAX_POINTS_ON_WINDING exceeded" );
}
@ -68,10 +65,7 @@ winding_t *AllocWinding( int points ){
c_peak_windings = c_active_windings;
}
}
s = sizeof( *w ) + points * sizeof( *w->p );
w = safe_malloc( s );
memset( w, 0, s );
return w;
return safe_calloc( offsetof( winding_t, p[points] ) );
}
/*
@ -80,9 +74,6 @@ winding_t *AllocWinding( int points ){
=============
*/
winding_accu_t *AllocWindingAccu( int points ){
winding_accu_t *w;
int s;
if ( points >= MAX_POINTS_ON_WINDING ) {
Error( "AllocWindingAccu failed: MAX_POINTS_ON_WINDING exceeded" );
}
@ -96,10 +87,7 @@ winding_accu_t *AllocWindingAccu( int points ){
c_peak_windings = c_active_windings;
}
}
s = sizeof( *w ) + points * sizeof( *w->p );
w = safe_malloc( s );
memset( w, 0, s );
return w;
return safe_calloc( offsetof( winding_accu_t, p[points] ) );
}
/*

View File

@ -40,10 +40,7 @@ typedef struct StrList_s
StrList;
static inline StrList* StrList_allocate( size_t strNum ){
StrList* ret;
const size_t size = sizeof( StrList ) + sizeof( ret->s[0] ) * strNum;
ret = safe_malloc( size );
memset( ret, 0, size );
StrList* ret = safe_calloc( offsetof( StrList, s[strNum] ) );
memcpy( ret,
&( StrList const ){ .n = 0, .max = strNum },
sizeof( StrList ) );
@ -198,10 +195,7 @@ typedef struct
StrBuf;
static inline StrBuf* StrBuf_allocate( size_t strLen ){
StrBuf* ret;
const size_t size = sizeof( StrBuf ) + sizeof( ret->s[0] ) * strLen;
ret = safe_malloc( size );
memset( ret, 0, size );
StrBuf* ret = safe_calloc( offsetof( StrBuf, s[strLen] ) );
memcpy( ret,
&( StrBuf const ){ .strlen = 0, .max = strLen },
sizeof( StrBuf ) );

View File

@ -90,18 +90,10 @@ int CountBrushList( brush_t *brushes ){
*/
brush_t *AllocBrush( int numSides ){
brush_t *bb;
size_t c;
c = sizeof( *bb ) + sizeof( *bb->sides ) * numSides;
bb = safe_malloc( c );
memset( bb, 0, c );
if ( numthreads == 1 ) {
numActiveBrushes++;
}
/* return it */
return bb;
return safe_calloc( offsetof( brush_t, sides[numSides] ) );
}
@ -128,7 +120,7 @@ void FreeBrush( brush_t *b ){
}
/* ydnar: overwrite it */
memset( b, 0xFE, offsetof( brush_t, sides ) + sizeof( *b->sides ) * b->numsides );
memset( b, 0xFE, offsetof( brush_t, sides[b->numsides] ) );
*( (unsigned int*) b ) = 0xFEFEFEFE;
/* free it */
@ -819,12 +811,8 @@ void FilterStructuralBrushesIntoTree( entity_t *e, tree_t *tree ) {
================
*/
tree_t *AllocTree( void ){
tree_t *tree;
tree = safe_malloc( sizeof( *tree ) );
memset( tree, 0, sizeof( *tree ) );
tree_t *tree = safe_calloc( sizeof( *tree ) );
ClearBounds( tree->mins, tree->maxs );
return tree;
}
@ -834,12 +822,7 @@ tree_t *AllocTree( void ){
================
*/
node_t *AllocNode( void ){
node_t *node;
node = safe_malloc( sizeof( *node ) );
memset( node, 0, sizeof( *node ) );
return node;
return safe_calloc( sizeof( node_t ) );
}

View File

@ -777,8 +777,7 @@ int BSPMain( int argc, char **argv ){
doingBSP = qtrue;
SetDrawSurfacesBuffer();
mapDrawSurfs = safe_malloc( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
memset( mapDrawSurfs, 0, sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
mapDrawSurfs = safe_calloc( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
numMapDrawSurfs = 0;
tempSource[ 0 ] = '\0';

View File

@ -91,12 +91,10 @@ void SetDrawVerts( int n ){
free( bspDrawVerts );
}
numBSPDrawVerts = n;
numBSPDrawVertsBuffer = numBSPDrawVerts;
numBSPDrawVerts =
numBSPDrawVertsBuffer = n;
bspDrawVerts = safe_malloc_info( sizeof( bspDrawVert_t ) * numBSPDrawVertsBuffer, "IncDrawVerts" );
memset( bspDrawVerts, 0, n * sizeof( bspDrawVert_t ) );
bspDrawVerts = safe_calloc_info( sizeof( bspDrawVert_t ) * numBSPDrawVertsBuffer, "IncDrawVerts" );
}
int numBSPDrawSurfacesBuffer = 0;
@ -107,9 +105,7 @@ void SetDrawSurfacesBuffer(){
numBSPDrawSurfacesBuffer = MAX_MAP_DRAW_SURFS;
bspDrawSurfaces = safe_malloc_info( sizeof( bspDrawSurface_t ) * numBSPDrawSurfacesBuffer, "IncDrawSurfaces" );
memset( bspDrawSurfaces, 0, MAX_MAP_DRAW_SURFS * sizeof( bspDrawSurface_t ) );
bspDrawSurfaces = safe_calloc_info( sizeof( bspDrawSurface_t ) * numBSPDrawSurfacesBuffer, "IncDrawSurfaces" );
}
void SetDrawSurfaces( int n ){
@ -117,12 +113,10 @@ void SetDrawSurfaces( int n ){
free( bspDrawSurfaces );
}
numBSPDrawSurfaces = n;
numBSPDrawSurfacesBuffer = numBSPDrawSurfaces;
numBSPDrawSurfaces =
numBSPDrawSurfacesBuffer = n;
bspDrawSurfaces = safe_malloc_info( sizeof( bspDrawSurface_t ) * numBSPDrawSurfacesBuffer, "IncDrawSurfaces" );
memset( bspDrawSurfaces, 0, n * sizeof( bspDrawSurface_t ) );
bspDrawSurfaces = safe_calloc_info( sizeof( bspDrawSurface_t ) * numBSPDrawSurfacesBuffer, "IncDrawSurfaces" );
}
void BSPFilesCleanup(){
@ -538,12 +532,8 @@ void StripTrailing( char *e ){
*/
epair_t *ParseEPair( void ){
epair_t *e;
/* allocate and clear new epair */
e = safe_malloc( sizeof( epair_t ) );
memset( e, 0, sizeof( epair_t ) );
epair_t *e = safe_calloc( sizeof( epair_t ) );
/* handle key */
if ( strlen( token ) >= ( MAX_KEY - 1 ) ) {

View File

@ -120,8 +120,7 @@ static void AddBrushSidesLump( FILE *file, ibspHeader_t *header ){
/* allocate output buffer */
size = numBSPBrushSides * sizeof( *buffer );
buffer = safe_malloc( size );
memset( buffer, 0, size );
buffer = safe_calloc( size );
/* convert */
in = bspBrushSides;
@ -232,8 +231,7 @@ static void AddDrawSurfacesLump( FILE *file, ibspHeader_t *header ){
/* allocate output buffer */
size = numBSPDrawSurfaces * sizeof( *buffer );
buffer = safe_malloc( size );
memset( buffer, 0, size );
buffer = safe_calloc( size );
/* convert */
in = bspDrawSurfaces;
@ -330,8 +328,7 @@ static void AddDrawVertsLump( FILE *file, ibspHeader_t *header ){
/* allocate output buffer */
size = numBSPDrawVerts * sizeof( *buffer );
buffer = safe_malloc( size );
memset( buffer, 0, size );
buffer = safe_calloc( size );
/* convert */
in = bspDrawVerts;
@ -385,8 +382,7 @@ static void CopyLightGridLumps( ibspHeader_t *header ){
numBSPGridPoints = GetLumpElements( (bspHeader_t*) header, LUMP_LIGHTGRID, sizeof( *in ) );
/* allocate buffer */
bspGridPoints = safe_malloc( numBSPGridPoints * sizeof( *bspGridPoints ) );
memset( bspGridPoints, 0, numBSPGridPoints * sizeof( *bspGridPoints ) );
bspGridPoints = safe_calloc( numBSPGridPoints * sizeof( *bspGridPoints ) );
/* copy */
in = GetLump( (bspHeader_t*) header, LUMP_LIGHTGRID );

View File

@ -96,8 +96,7 @@ static void CopyLightGridLumps( rbspHeader_t *header ){
numBSPGridPoints = GetLumpElements( (bspHeader_t*) header, LUMP_LIGHTARRAY, sizeof( *inArray ) );
/* allocate buffer */
bspGridPoints = safe_malloc( numBSPGridPoints * sizeof( *bspGridPoints ) );
memset( bspGridPoints, 0, numBSPGridPoints * sizeof( *bspGridPoints ) );
bspGridPoints = safe_calloc( numBSPGridPoints * sizeof( *bspGridPoints ) );
/* copy */
inArray = GetLump( (bspHeader_t*) header, LUMP_LIGHTARRAY );

View File

@ -833,8 +833,7 @@ void PseudoCompileBSP( qboolean need_tree ){
int i;
SetDrawSurfacesBuffer();
mapDrawSurfs = safe_malloc( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
memset( mapDrawSurfs, 0, sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
mapDrawSurfs = safe_calloc( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
numMapDrawSurfs = 0;
BeginBSPFile();

View File

@ -620,8 +620,7 @@ static void ProjectDecalOntoWinding( decalProjector_t *dp, mapDrawSurface_t *ds,
ds2->lightmapScale = ds->lightmapScale;
ds2->shadeAngleDegrees = ds->shadeAngleDegrees;
ds2->numVerts = w->numpoints;
ds2->verts = safe_malloc( ds2->numVerts * sizeof( *ds2->verts ) );
memset( ds2->verts, 0, ds2->numVerts * sizeof( *ds2->verts ) );
ds2->verts = safe_calloc( ds2->numVerts * sizeof( *ds2->verts ) );
/* set vertexes */
for ( i = 0; i < ds2->numVerts; i++ )

View File

@ -47,12 +47,7 @@ int c_faceLeafs;
================
*/
face_t *AllocBspFace( void ) {
face_t *f;
f = safe_malloc( sizeof( *f ) );
memset( f, 0, sizeof( *f ) );
return f;
return safe_calloc( sizeof( face_t ) );
}

View File

@ -234,9 +234,6 @@ static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, in
*/
static void ImageInit( void ){
int i;
if ( numImages <= 0 ) {
/* clear images (fixme: this could theoretically leak) */
memset( images, 0, sizeof( images ) );
@ -248,8 +245,7 @@ static void ImageInit( void ){
images[ 0 ].height = 64;
images[ 0 ].refCount = 1;
images[ 0 ].pixels = safe_malloc( 64 * 64 * 4 );
for ( i = 0; i < ( 64 * 64 * 4 ); i++ )
images[ 0 ].pixels[ i ] = 255;
memset( images[ 0 ].pixels, 255, 64 * 64 * 4 );
}
}

View File

@ -107,8 +107,7 @@ static void CreateSunLight( sun_t *sun ){
/* create a light */
numSunLights++;
light = safe_malloc( sizeof( *light ) );
memset( light, 0, sizeof( *light ) );
light = safe_calloc( sizeof( *light ) );
light->next = lights;
lights = light;
@ -254,8 +253,7 @@ void CreateEntityLights( void ){
/* create a light */
numPointLights++;
light = safe_malloc( sizeof( *light ) );
memset( light, 0, sizeof( *light ) );
light = safe_calloc( sizeof( *light ) );
light->next = lights;
lights = light;
@ -592,8 +590,7 @@ void CreateSurfaceLights( void ){
VectorScale( origin, 0.5f, origin );
/* create a light */
light = safe_malloc( sizeof( *light ) );
memset( light, 0, sizeof( *light ) );
light = safe_calloc( sizeof( *light ) );
light->next = lights;
lights = light;
@ -1857,14 +1854,12 @@ void SetupGrid( void ){
numBSPGridPoints = numRawGridPoints;
/* allocate lightgrid */
rawGridPoints = safe_malloc( numRawGridPoints * sizeof( *rawGridPoints ) );
memset( rawGridPoints, 0, numRawGridPoints * sizeof( *rawGridPoints ) );
rawGridPoints = safe_calloc( numRawGridPoints * sizeof( *rawGridPoints ) );
if ( bspGridPoints != NULL ) {
free( bspGridPoints );
}
bspGridPoints = safe_malloc( numBSPGridPoints * sizeof( *bspGridPoints ) );
memset( bspGridPoints, 0, numBSPGridPoints * sizeof( *bspGridPoints ) );
bspGridPoints = safe_calloc( numBSPGridPoints * sizeof( *bspGridPoints ) );
/* clear lightgrid */
for ( i = 0; i < numRawGridPoints; i++ )

View File

@ -538,8 +538,7 @@ static void RadSubdivideDiffuseLight( int lightmapNum, bspDrawSurface_t *ds, raw
}
/* create a light */
light = safe_malloc( sizeof( *light ) );
memset( light, 0, sizeof( *light ) );
light = safe_calloc( sizeof( *light ) );
/* attach it */
ThreadLock();
@ -587,8 +586,7 @@ static void RadSubdivideDiffuseLight( int lightmapNum, bspDrawSurface_t *ds, raw
if ( si->backsplashFraction > 0 ) {
/* allocate a new point light */
splash = safe_malloc( sizeof( *splash ) );
memset( splash, 0, sizeof( *splash ) );
splash = safe_calloc( sizeof( *splash ) );
splash->next = lights;
lights = splash;
@ -617,8 +615,7 @@ static void RadSubdivideDiffuseLight( int lightmapNum, bspDrawSurface_t *ds, raw
//if ( original && si->backsplashFraction > 0 ) {
if ( si->backsplashFraction > 0 && !( si->compileFlags & C_SKY ) ) {
/* allocate a new area light */
splash = safe_malloc( sizeof( *splash ) );
memset( splash, 0, sizeof( *splash ) );
splash = safe_calloc( sizeof( *splash ) );
ThreadLock();
splash->next = lights;
lights = splash;

View File

@ -153,7 +153,7 @@ void ColorToBytes( const float *color, byte *colorBytes, float scale ){
#define EQUAL_NORMAL_EPSILON 0.01
void SmoothNormals( void ){
int i, j, k, f, cs, numVerts, numVotes, fOld, start;
int i, j, k, f, numVerts, numVotes, fOld, start;
float shadeAngle, defaultShadeAngle, maxShadeAngle, dot, testAngle;
bspDrawSurface_t *ds;
shaderInfo_t *si;
@ -165,13 +165,10 @@ void SmoothNormals( void ){
/* allocate shade angle table */
shadeAngles = safe_malloc( numBSPDrawVerts * sizeof( float ) );
memset( shadeAngles, 0, numBSPDrawVerts * sizeof( float ) );
shadeAngles = safe_calloc( numBSPDrawVerts * sizeof( float ) );
/* allocate smoothed table */
cs = ( numBSPDrawVerts / 8 ) + 1;
smoothed = safe_malloc( cs );
memset( smoothed, 0, cs );
smoothed = safe_calloc( ( numBSPDrawVerts / 8 ) + 1 );
/* set default shade angle */
defaultShadeAngle = DEG2RAD( shadeAngleDegrees );
@ -2407,8 +2404,7 @@ void IlluminateRawLightmap( int rawLightmapNum ){
if ( lm->superLuxels[ lightmapNum ] == NULL ) {
/* allocate sampling lightmap storage */
size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float );
lm->superLuxels[ lightmapNum ] = safe_malloc( size );
memset( lm->superLuxels[ lightmapNum ], 0, size );
lm->superLuxels[ lightmapNum ] = safe_calloc( size );
}
/* set style */

View File

@ -468,7 +468,7 @@ qboolean AddPatchToRawLightmap( int num, rawLightmap_t *lm ){
vec3_t delta;
mesh_t src, *subdivided, *mesh;
float sBasis, tBasis, s, t;
float length, widthTable[ MAX_EXPANDED_AXIS ], heightTable[ MAX_EXPANDED_AXIS ];
float length, widthTable[ MAX_EXPANDED_AXIS ] = {0}, heightTable[ MAX_EXPANDED_AXIS ] = {0};
/* patches finish a raw lightmap */
@ -492,8 +492,6 @@ qboolean AddPatchToRawLightmap( int num, rawLightmap_t *lm ){
/* find the longest distance on each row/column */
verts = mesh->verts;
memset( widthTable, 0, sizeof( widthTable ) );
memset( heightTable, 0, sizeof( heightTable ) );
for ( y = 0; y < mesh->height; y++ )
{
for ( x = 0; x < mesh->width; x++ )
@ -827,8 +825,7 @@ qboolean AddSurfaceToRawLightmap( int num, rawLightmap_t *lm ){
/* for planar surfaces, create lightmap vectors for st->xyz conversion */
if ( VectorLength( ds->lightmapVecs[ 2 ] ) || 1 ) { /* ydnar: can't remember what exactly i was thinking here... */
/* allocate space for the vectors */
lm->vecs = safe_malloc( 3 * sizeof( vec3_t ) );
memset( lm->vecs, 0, 3 * sizeof( vec3_t ) );
lm->vecs = safe_calloc( 3 * sizeof( vec3_t ) );
VectorCopy( ds->lightmapVecs[ 2 ], lm->vecs[ 2 ] );
/* project stepped lightmap blocks and subtract to get planevecs */
@ -999,18 +996,15 @@ void SetupSurfaceLightmaps( void ){
/* allocate a list of surface clusters */
numSurfaceClusters = 0;
maxSurfaceClusters = numBSPLeafSurfaces;
surfaceClusters = safe_malloc( maxSurfaceClusters * sizeof( *surfaceClusters ) );
memset( surfaceClusters, 0, maxSurfaceClusters * sizeof( *surfaceClusters ) );
surfaceClusters = safe_calloc( maxSurfaceClusters * sizeof( *surfaceClusters ) );
/* allocate a list for per-surface info */
surfaceInfos = safe_malloc( numBSPDrawSurfaces * sizeof( *surfaceInfos ) );
memset( surfaceInfos, 0, numBSPDrawSurfaces * sizeof( *surfaceInfos ) );
surfaceInfos = safe_calloc( numBSPDrawSurfaces * sizeof( *surfaceInfos ) );
for ( i = 0; i < numBSPDrawSurfaces; i++ )
surfaceInfos[ i ].childSurfaceNum = -1;
/* allocate a list of surface indexes to be sorted */
sortSurfaces = safe_malloc( numBSPDrawSurfaces * sizeof( int ) );
memset( sortSurfaces, 0, numBSPDrawSurfaces * sizeof( int ) );
sortSurfaces = safe_calloc( numBSPDrawSurfaces * sizeof( int ) );
/* walk each model in the bsp */
for ( i = 0; i < numBSPModels; i++ )
@ -1131,14 +1125,12 @@ void SetupSurfaceLightmaps( void ){
/* allocate a list of surfaces that would go into raw lightmaps */
numLightSurfaces = 0;
lightSurfaces = safe_malloc( numSurfsLightmapped * sizeof( int ) );
memset( lightSurfaces, 0, numSurfsLightmapped * sizeof( int ) );
lightSurfaces = safe_calloc( numSurfsLightmapped * sizeof( int ) );
/* allocate a list of raw lightmaps */
numRawSuperLuxels = 0;
numRawLightmaps = 0;
rawLightmaps = safe_malloc( numSurfsLightmapped * sizeof( *rawLightmaps ) );
memset( rawLightmaps, 0, numSurfsLightmapped * sizeof( *rawLightmaps ) );
rawLightmaps = safe_calloc( numSurfsLightmapped * sizeof( *rawLightmaps ) );
/* walk the list of sorted surfaces */
for ( i = 0; i < numBSPDrawSurfaces; i++ )
@ -1227,10 +1219,8 @@ void SetupSurfaceLightmaps( void ){
/* allocate vertex luxel storage */
for ( k = 0; k < MAX_LIGHTMAPS; k++ )
{
vertexLuxels[ k ] = safe_malloc( numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) );
memset( vertexLuxels[ k ], 0, numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) );
radVertexLuxels[ k ] = safe_malloc( numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) );
memset( radVertexLuxels[ k ], 0, numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) );
vertexLuxels[ k ] = safe_calloc( numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) );
radVertexLuxels[ k ] = safe_calloc( numBSPDrawVerts * VERTEX_LUXEL_SIZE * sizeof( float ) );
}
/* emit some stats */
@ -1982,13 +1972,10 @@ static void SetupOutLightmap( rawLightmap_t *lm, outLightmap_t *olm ){
olm->numShaders = 0;
/* allocate buffers */
olm->lightBits = safe_malloc( ( olm->customWidth * olm->customHeight / 8 ) + 8 );
memset( olm->lightBits, 0, ( olm->customWidth * olm->customHeight / 8 ) + 8 );
olm->bspLightBytes = safe_malloc( olm->customWidth * olm->customHeight * 3 );
memset( olm->bspLightBytes, 0, olm->customWidth * olm->customHeight * 3 );
olm->lightBits = safe_calloc( ( olm->customWidth * olm->customHeight / 8 ) + 8 );
olm->bspLightBytes = safe_calloc( olm->customWidth * olm->customHeight * 3 );
if ( deluxemap ) {
olm->bspDirBytes = safe_malloc( olm->customWidth * olm->customHeight * 3 );
memset( olm->bspDirBytes, 0, olm->customWidth * olm->customHeight * 3 );
olm->bspDirBytes = safe_calloc( olm->customWidth * olm->customHeight * 3 );
}
}
@ -2515,8 +2502,8 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){
char dirname[ 1024 ], filename[ 1024 ];
shaderInfo_t *csi;
char lightmapName[ 128 ];
const char *rgbGenValues[ 256 ];
const char *alphaGenValues[ 256 ];
const char *rgbGenValues[ 256 ] = {0};
const char *alphaGenValues[ 256 ] = {0};
/* note it */
@ -2531,8 +2518,6 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){
strcpy( dirname, source );
StripExtension( dirname );
}
memset( rgbGenValues, 0, sizeof( rgbGenValues ) );
memset( alphaGenValues, 0, sizeof( alphaGenValues ) );
/* -----------------------------------------------------------------
average the sampled luxels into the bsp luxels
@ -2564,8 +2549,7 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){
/* allocate bsp luxel storage */
if ( lm->bspLuxels[ lightmapNum ] == NULL ) {
size = lm->w * lm->h * BSP_LUXEL_SIZE * sizeof( float );
lm->bspLuxels[ lightmapNum ] = safe_malloc( size );
memset( lm->bspLuxels[ lightmapNum ], 0, size );
lm->bspLuxels[ lightmapNum ] = safe_calloc( size );
}
/* allocate radiosity lightmap storage */
@ -3181,8 +3165,7 @@ void StoreSurfaceLightmaps( qboolean fastAllocate ){
else
{
numBSPLightBytes = ( numBSPLightmaps * game->lightmapSize * game->lightmapSize * 3 );
bspLightBytes = safe_malloc( numBSPLightBytes );
memset( bspLightBytes, 0, numBSPLightBytes );
bspLightBytes = safe_calloc( numBSPLightBytes );
}
/* walk the list of output lightmaps */

View File

@ -1646,8 +1646,7 @@ void LoadEntityIndexMap( entity_t *e ){
}
/* create a new index map */
im = safe_malloc( sizeof( *im ) );
memset( im, 0, sizeof( *im ) );
im = safe_calloc( sizeof( *im ) );
/* set it up */
im->w = w;

View File

@ -449,12 +449,10 @@ void InsertModel( const char *name, int skin, int frame, m4x4_t transform, remap
/* set particulars */
ds->numVerts = PicoGetSurfaceNumVertexes( surface );
ds->verts = safe_malloc( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
memset( ds->verts, 0, ds->numVerts * sizeof( ds->verts[ 0 ] ) );
ds->verts = safe_calloc( ds->numVerts * sizeof( ds->verts[ 0 ] ) );
ds->numIndexes = PicoGetSurfaceNumIndexes( surface );
ds->indexes = safe_malloc( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
memset( ds->indexes, 0, ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
ds->indexes = safe_calloc( ds->numIndexes * sizeof( ds->indexes[ 0 ] ) );
/* copy vertexes */
for ( i = 0; i < ds->numVerts; i++ )

View File

@ -348,8 +348,7 @@ void ParsePatch( qboolean onlyLights ){
}
/* allocate patch mesh */
pm = safe_malloc( sizeof( *pm ) );
memset( pm, 0, sizeof( *pm ) );
pm = safe_calloc( sizeof( *pm ) );
/* ydnar: add entity/brush numbering */
pm->entityNum = mapEnt->mapEntityNum;
@ -446,8 +445,7 @@ void PatchMapDrawSurfs( entity_t *e ){
if ( !patchCount ) {
return;
}
bordering = safe_malloc( patchCount * patchCount );
memset( bordering, 0, patchCount * patchCount );
bordering = safe_calloc( patchCount * patchCount );
// build the bordering matrix
for ( k = 0 ; k < patchCount ; k++ ) {

View File

@ -53,8 +53,6 @@ int c_boundary_sides;
===========
*/
portal_t *AllocPortal( void ){
portal_t *p;
if ( numthreads == 1 ) {
c_active_portals++;
}
@ -62,10 +60,7 @@ portal_t *AllocPortal( void ){
c_peak_portals = c_active_portals;
}
p = safe_malloc( sizeof( portal_t ) );
memset( p, 0, sizeof( portal_t ) );
return p;
return safe_calloc( sizeof( portal_t ) );
}
void FreePortal( portal_t *p ){

View File

@ -1273,8 +1273,7 @@ static void ParseShaderFile( const char *filename ){
}
/* allocate sun */
sun = safe_malloc( sizeof( *sun ) );
memset( sun, 0, sizeof( *sun ) );
sun = safe_calloc( sizeof( *sun ) );
/* set style */
sun->style = si->lightStyle;
@ -1374,8 +1373,7 @@ static void ParseShaderFile( const char *filename ){
surfaceModel_t *model;
/* allocate new model and attach it */
model = safe_malloc( sizeof( *model ) );
memset( model, 0, sizeof( *model ) );
model = safe_calloc( sizeof( *model ) );
model->next = si->surfaceModel;
si->surfaceModel = model;
@ -1408,8 +1406,7 @@ static void ParseShaderFile( const char *filename ){
/* allocate new foliage struct and attach it */
foliage = safe_malloc( sizeof( *foliage ) );
memset( foliage, 0, sizeof( *foliage ) );
foliage = safe_calloc( sizeof( *foliage ) );
foliage->next = si->foliage;
si->foliage = foliage;
@ -1733,8 +1730,7 @@ static void ParseShaderFile( const char *filename ){
alpha = ( !Q_stricmp( token, "q3map_alphaGen" ) || !Q_stricmp( token, "q3map_alphaMod" ) ) ? 1 : 0;
/* allocate new colormod */
cm = safe_malloc( sizeof( *cm ) );
memset( cm, 0, sizeof( *cm ) );
cm = safe_calloc( sizeof( *cm ) );
/* attach to shader */
if ( si->colorMod == NULL ) {

View File

@ -957,8 +957,7 @@ mapDrawSurface_t *DrawSurfaceForSide( entity_t *e, brush_t *b, side_t *s, windin
ds->sampleSize = b->lightmapSampleSize;
ds->lightmapScale = b->lightmapScale;
ds->numVerts = w->numpoints;
ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
ds->verts = safe_calloc( ds->numVerts * sizeof( *ds->verts ) );
/* compute s/t coordinates from brush primitive texture matrix (compute axis base) */
ComputeAxisBase( mapplanes[ s->planenum ].normal, texX, texY );
@ -3025,8 +3024,7 @@ static void MakeDebugPortalSurfs_r( node_t *node, shaderInfo_t *si ){
VectorCopy( p->plane.normal, ds->lightmapVecs[ 2 ] );
ds->fogNum = -1;
ds->numVerts = w->numpoints;
ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
ds->verts = safe_calloc( ds->numVerts * sizeof( *ds->verts ) );
/* walk the winding */
for ( i = 0; i < ds->numVerts; i++ )
@ -3117,11 +3115,9 @@ void MakeFogHullSurfs( entity_t *e, tree_t *tree, char *shader ){
ds->shaderInfo = si;
ds->fogNum = -1;
ds->numVerts = 8;
ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) );
memset( ds->verts, 0, ds->numVerts * sizeof( *ds->verts ) );
ds->verts = safe_calloc( ds->numVerts * sizeof( *ds->verts ) );
ds->numIndexes = 36;
ds->indexes = safe_malloc( ds->numIndexes * sizeof( *ds->indexes ) );
memset( ds->indexes, 0, ds->numIndexes * sizeof( *ds->indexes ) );
ds->indexes = safe_calloc( ds->numIndexes * sizeof( *ds->indexes ) );
/* set verts */
VectorSet( ds->verts[ 0 ].xyz, fogMins[ 0 ], fogMins[ 1 ], fogMins[ 2 ] );

View File

@ -301,8 +301,7 @@ void Foliage( mapDrawSurface_t *src ){
ds->fogNum = src->fogNum;
/* add a drawvert for every instance */
verts = safe_malloc( ( ds->numVerts + ds->numFoliageInstances ) * sizeof( *verts ) );
memset( verts, 0, ( ds->numVerts + ds->numFoliageInstances ) * sizeof( *verts ) );
verts = safe_calloc( ( ds->numVerts + ds->numFoliageInstances ) * sizeof( *verts ) );
memcpy( verts, ds->verts, ds->numVerts * sizeof( *verts ) );
free( ds->verts );
ds->verts = verts;

View File

@ -638,7 +638,8 @@ void MaxAreaFaceSurface( mapDrawSurface_t *ds ){
*/
void FanFaceSurface( mapDrawSurface_t *ds ){
int i, j, k, a, b, c, color[ MAX_LIGHTMAPS ][ 4 ];
int i, j, k, a, b, c;
int color[ MAX_LIGHTMAPS ][ 4 ] = {0};
bspDrawVert_t *verts, *centroid, *dv;
double iv;
@ -657,7 +658,6 @@ void FanFaceSurface( mapDrawSurface_t *ds ){
/* add up the drawverts to create a centroid */
centroid = &verts[ 0 ];
memset( color, 0, 4 * MAX_LIGHTMAPS * sizeof( int ) );
for ( i = 1, dv = &verts[ 1 ]; i < ( ds->numVerts + 1 ); i++, dv++ )
{
VectorAdd( centroid->xyz, dv->xyz, centroid->xyz );
@ -1180,7 +1180,7 @@ void FixMetaTJunctions( void ){
#define EQUAL_NORMAL_EPSILON 0.01
void SmoothMetaTriangles( void ){
int i, j, k, f, fOld, start, cs, numVerts, numVotes, numSmoothed;
int i, j, k, f, fOld, start, numVerts, numVotes, numSmoothed;
float shadeAngle, defaultShadeAngle, maxShadeAngle, dot, testAngle;
metaTriangle_t *tri;
float *shadeAngles;
@ -1193,13 +1193,10 @@ void SmoothMetaTriangles( void ){
Sys_FPrintf( SYS_VRB, "--- SmoothMetaTriangles ---\n" );
/* allocate shade angle table */
shadeAngles = safe_malloc( numMetaVerts * sizeof( float ) );
memset( shadeAngles, 0, numMetaVerts * sizeof( float ) );
shadeAngles = safe_calloc( numMetaVerts * sizeof( float ) );
/* allocate smoothed table */
cs = ( numMetaVerts / 8 ) + 1;
smoothed = safe_malloc( cs );
memset( smoothed, 0, cs );
smoothed = safe_calloc( ( numMetaVerts / 8 ) + 1 );
/* set default shade angle */
defaultShadeAngle = DEG2RAD( npDegrees );
@ -1705,9 +1702,6 @@ static void MetaTrianglesToSurface( int numPossibles, metaTriangle_t *possibles,
ClearBounds( ds->mins, ds->maxs );
/* clear verts/indexes */
// memset( verts, 0, sizeof( verts ) );
// memset( indexes, 0, sizeof( indexes ) );
//is more correct, but upper works ok too
memset( verts, 0, sizeof( *verts ) * maxSurfaceVerts );
memset( indexes, 0, sizeof( *indexes ) * maxSurfaceIndexes );

View File

@ -58,18 +58,10 @@ void PlaneFromWinding( fixedWinding_t *w, visPlane_t *plane ){
*/
fixedWinding_t *NewFixedWinding( int points ){
fixedWinding_t *w;
int size;
if ( points > MAX_POINTS_ON_WINDING ) {
Error( "NewWinding: %i points", points );
}
size = offsetof( fixedWinding_t, points ) + sizeof( *w->points ) * points;
w = safe_malloc( size );
memset( w, 0, size );
return w;
return safe_calloc( offsetof( fixedWinding_t, points[points] ) );
}
@ -943,11 +935,8 @@ void LoadPortals( char *name ){
portallongs = portalbytes / sizeof( long );
// each file portal is split into two memory portals
portals = safe_malloc( 2 * numportals * sizeof( vportal_t ) );
memset( portals, 0, 2 * numportals * sizeof( vportal_t ) );
leafs = safe_malloc( portalclusters * sizeof( leaf_t ) );
memset( leafs, 0, portalclusters * sizeof( leaf_t ) );
portals = safe_calloc( 2 * numportals * sizeof( vportal_t ) );
leafs = safe_calloc( portalclusters * sizeof( leaf_t ) );
for ( i = 0; i < portalclusters; i++ )
leafs[i].merged = -1;
@ -1043,11 +1032,8 @@ void LoadPortals( char *name ){
}
faces = safe_malloc( 2 * numfaces * sizeof( vportal_t ) );
memset( faces, 0, 2 * numfaces * sizeof( vportal_t ) );
faceleafs = safe_malloc( portalclusters * sizeof( leaf_t ) );
memset( faceleafs, 0, portalclusters * sizeof( leaf_t ) );
faces = safe_calloc( 2 * numfaces * sizeof( vportal_t ) );
faceleafs = safe_calloc( portalclusters * sizeof( leaf_t ) );
for ( i = 0, p = faces; i < numfaces; i++ )
{

View File

@ -1362,8 +1362,7 @@ void CreatePassages( int portalnum ){
continue;
}
passage = (passage_t *) safe_malloc( sizeof( passage_t ) + portalbytes );
memset( passage, 0, sizeof( passage_t ) + portalbytes );
passage = safe_calloc( sizeof( passage_t ) + portalbytes );
numseperators = AddSeperators( portal->winding, target->winding, qfalse, seperators, MAX_SEPERATORS * 2 );
numseperators += AddSeperators( target->winding, portal->winding, qtrue, &seperators[numseperators], MAX_SEPERATORS * 2 - numseperators );
@ -1583,14 +1582,9 @@ void BasePortalVis( int portalnum ){
return;
}
p->portalfront = safe_malloc( portalbytes );
memset( p->portalfront, 0, portalbytes );
p->portalflood = safe_malloc( portalbytes );
memset( p->portalflood, 0, portalbytes );
p->portalvis = safe_malloc( portalbytes );
memset( p->portalvis, 0, portalbytes );
p->portalfront = safe_calloc( portalbytes );
p->portalflood = safe_calloc( portalbytes );
p->portalvis = safe_calloc( portalbytes );
for ( j = 0, tp = portals ; j < numportals * 2 ; j++, tp++ )
{