diff --git a/tools/quake3/q3map2/light_trace.c b/tools/quake3/q3map2/light_trace.c index 52debdd9..aeda4dd5 100644 --- a/tools/quake3/q3map2/light_trace.c +++ b/tools/quake3/q3map2/light_trace.c @@ -135,8 +135,6 @@ traceNode_t *traceNodes = NULL; static int AddTraceInfo( traceInfo_t *ti ){ int num; - void *temp; - /* find an existing info */ for ( num = firstTraceInfo; num < numTraceInfos; num++ ) @@ -150,16 +148,7 @@ static int AddTraceInfo( traceInfo_t *ti ){ } /* enough space? */ - if ( numTraceInfos >= maxTraceInfos ) { - /* allocate more room */ - maxTraceInfos += GROW_TRACE_INFOS; - temp = safe_malloc( maxTraceInfos * sizeof( *traceInfos ) ); - if ( traceInfos != NULL ) { - memcpy( temp, traceInfos, numTraceInfos * sizeof( *traceInfos ) ); - free( traceInfos ); - } - traceInfos = (traceInfo_t*) temp; - } + AUTOEXPAND_BY_REALLOC_ADD( traceInfos, numTraceInfos, maxTraceInfos, GROW_TRACE_INFOS ); /* add the info */ memcpy( &traceInfos[ num ], ti, sizeof( *traceInfos ) ); @@ -179,20 +168,8 @@ static int AddTraceInfo( traceInfo_t *ti ){ */ static int AllocTraceNode( void ){ - traceNode_t *temp; - - /* enough space? */ - if ( numTraceNodes >= maxTraceNodes ) { - /* reallocate more room */ - maxTraceNodes += GROW_TRACE_NODES; - temp = safe_malloc( maxTraceNodes * sizeof( traceNode_t ) ); - if ( traceNodes != NULL ) { - memcpy( temp, traceNodes, numTraceNodes * sizeof( traceNode_t ) ); - free( traceNodes ); - } - traceNodes = temp; - } + AUTOEXPAND_BY_REALLOC_ADD( traceNodes, numTraceNodes, maxTraceNodes, GROW_TRACE_NODES ); /* add the node */ memset( &traceNodes[ numTraceNodes ], 0, sizeof( traceNode_t ) ); @@ -216,8 +193,6 @@ static int AllocTraceNode( void ){ static int AddTraceWinding( traceWinding_t *tw ){ int num; - void *temp; - /* check for a dead winding */ if ( deadWinding >= 0 && deadWinding < numTraceWindings ) { @@ -229,16 +204,7 @@ static int AddTraceWinding( traceWinding_t *tw ){ num = numTraceWindings; /* enough space? */ - if ( numTraceWindings >= maxTraceWindings ) { - /* allocate more room */ - maxTraceWindings += GROW_TRACE_WINDINGS; - temp = safe_malloc( maxTraceWindings * sizeof( *traceWindings ) ); - if ( traceWindings != NULL ) { - memcpy( temp, traceWindings, numTraceWindings * sizeof( *traceWindings ) ); - free( traceWindings ); - } - traceWindings = (traceWinding_t*) temp; - } + AUTOEXPAND_BY_REALLOC_ADD( traceWindings, numTraceWindings, maxTraceWindings, GROW_TRACE_WINDINGS ); } /* add the winding */ @@ -261,8 +227,6 @@ static int AddTraceWinding( traceWinding_t *tw ){ static int AddTraceTriangle( traceTriangle_t *tt ){ int num; - void *temp; - /* check for a dead triangle */ if ( deadTriangle >= 0 && deadTriangle < numTraceTriangles ) { @@ -274,16 +238,7 @@ static int AddTraceTriangle( traceTriangle_t *tt ){ num = numTraceTriangles; /* enough space? */ - if ( numTraceTriangles >= maxTraceTriangles ) { - /* allocate more room */ - maxTraceTriangles += GROW_TRACE_TRIANGLES; - temp = safe_malloc( maxTraceTriangles * sizeof( *traceTriangles ) ); - if ( traceTriangles != NULL ) { - memcpy( temp, traceTriangles, numTraceTriangles * sizeof( *traceTriangles ) ); - free( traceTriangles ); - } - traceTriangles = (traceTriangle_t*) temp; - } + AUTOEXPAND_BY_REALLOC_ADD( traceTriangles, numTraceTriangles, maxTraceTriangles, GROW_TRACE_TRIANGLES ); } /* find vectors for two edges sharing the first vert */ @@ -309,9 +264,6 @@ static int AddTraceTriangle( traceTriangle_t *tt ){ */ static int AddItemToTraceNode( traceNode_t *node, int num ){ - void *temp; - - /* dummy check */ if ( num < 0 ) { return -1; @@ -329,12 +281,10 @@ static int AddItemToTraceNode( traceNode_t *node, int num ){ if ( node->maxItems <= 0 ) { node->maxItems = GROW_NODE_ITEMS; } - temp = safe_malloc( node->maxItems * sizeof( *node->items ) ); - if ( node->items != NULL ) { - memcpy( temp, node->items, node->numItems * sizeof( *node->items ) ); - free( node->items ); + node->items = realloc( node->items, node->maxItems * sizeof( *node->items ) ); + if ( !node->items ) { + Error( "node->items out of memory" ); } - node->items = (int*) temp; } /* add the poly */ diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index 253c883b..98dafe52 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -2576,6 +2576,19 @@ Q_EXTERN bspAdvertisement_t bspAds[ MAX_MAP_ADVERTISEMENTS ]; #define AUTOEXPAND_BY_REALLOC_BSP( suffix, def ) AUTOEXPAND_BY_REALLOC( bsp ## suffix, numBSP ## suffix, allocatedBSP ## suffix, def ) +#define AUTOEXPAND_BY_REALLOC_ADD( ptr, used, allocated, add ) \ + do \ + { \ + if ( used >= allocated ) \ + { \ + allocated += add; \ + ptr = realloc( ptr, sizeof( *ptr ) * allocated ); \ + if ( !ptr ) { \ + Error( # ptr " out of memory" ); } \ + } \ + } \ + while ( 0 ) + #define Image_LinearFloatFromsRGBFloat( c ) ( ( ( c ) <= 0.04045f ) ? ( c ) * ( 1.0f / 12.92f ) : (float)pow( ( ( c ) + 0.055f ) * ( 1.0f / 1.055f ), 2.4f ) ) #define Image_sRGBFloatFromLinearFloat( c ) ( ( ( c ) < 0.0031308f ) ? ( c ) * 12.92f : 1.055f * (float)pow( ( c ), 1.0f / 2.4f ) - 0.055f ) diff --git a/tools/quake3/q3map2/surface_extra.c b/tools/quake3/q3map2/surface_extra.c index 4f9eaf13..9227fad7 100644 --- a/tools/quake3/q3map2/surface_extra.c +++ b/tools/quake3/q3map2/surface_extra.c @@ -72,23 +72,11 @@ surfaceExtra_t seDefault = { NULL, NULL, -1, 0, WORLDSPAWN_CAST_SHADOWS, WORLDSP */ static surfaceExtra_t *AllocSurfaceExtra( void ){ - surfaceExtra_t *se; - - /* enough space? */ - if ( numSurfaceExtras >= maxSurfaceExtras ) { - /* reallocate more room */ - maxSurfaceExtras += GROW_SURFACE_EXTRAS; - se = safe_malloc( maxSurfaceExtras * sizeof( surfaceExtra_t ) ); - if ( surfaceExtras != NULL ) { - memcpy( se, surfaceExtras, numSurfaceExtras * sizeof( surfaceExtra_t ) ); - free( surfaceExtras ); - } - surfaceExtras = se; - } + AUTOEXPAND_BY_REALLOC_ADD( surfaceExtras, numSurfaceExtras, maxSurfaceExtras, GROW_SURFACE_EXTRAS ); /* add another */ - se = &surfaceExtras[ numSurfaceExtras ]; + surfaceExtra_t *se = &surfaceExtras[ numSurfaceExtras ]; numSurfaceExtras++; memcpy( se, &seDefault, sizeof( surfaceExtra_t ) ); diff --git a/tools/quake3/q3map2/surface_meta.c b/tools/quake3/q3map2/surface_meta.c index c3ef39df..689a869e 100644 --- a/tools/quake3/q3map2/surface_meta.c +++ b/tools/quake3/q3map2/surface_meta.c @@ -81,8 +81,7 @@ void ClearMetaTriangles( void ){ static int FindMetaVertex( bspDrawVert_t *src ){ int i; - bspDrawVert_t *v, *temp; - + bspDrawVert_t *v; /* try to find an existing drawvert */ for ( i = firstSearchMetaVert, v = &metaVerts[ i ]; i < numMetaVerts; i++, v++ ) @@ -93,16 +92,7 @@ static int FindMetaVertex( bspDrawVert_t *src ){ } /* enough space? */ - if ( numMetaVerts >= maxMetaVerts ) { - /* reallocate more room */ - maxMetaVerts += GROW_META_VERTS; - temp = safe_malloc( maxMetaVerts * sizeof( bspDrawVert_t ) ); - if ( metaVerts != NULL ) { - memcpy( temp, metaVerts, numMetaVerts * sizeof( bspDrawVert_t ) ); - free( metaVerts ); - } - metaVerts = temp; - } + AUTOEXPAND_BY_REALLOC_ADD( metaVerts, numMetaVerts, maxMetaVerts, GROW_META_VERTS ); /* add the triangle */ memcpy( &metaVerts[ numMetaVerts ], src, sizeof( bspDrawVert_t ) ); @@ -120,20 +110,8 @@ static int FindMetaVertex( bspDrawVert_t *src ){ */ static int AddMetaTriangle( void ){ - metaTriangle_t *temp; - - /* enough space? */ - if ( numMetaTriangles >= maxMetaTriangles ) { - /* reallocate more room */ - maxMetaTriangles += GROW_META_TRIANGLES; - temp = safe_malloc( maxMetaTriangles * sizeof( metaTriangle_t ) ); - if ( metaTriangles != NULL ) { - memcpy( temp, metaTriangles, numMetaTriangles * sizeof( metaTriangle_t ) ); - free( metaTriangles ); - } - metaTriangles = temp; - } + AUTOEXPAND_BY_REALLOC_ADD( metaTriangles, numMetaTriangles, maxMetaTriangles, GROW_META_TRIANGLES ); /* increment and return */ numMetaTriangles++;