use realloc() instead of safe_malloc() + free()
add AUTOEXPAND_BY_REALLOC_ADD macro, use it
This commit is contained in:
parent
e4eae18f5f
commit
ad05e553b9
|
|
@ -135,8 +135,6 @@ traceNode_t *traceNodes = NULL;
|
||||||
|
|
||||||
static int AddTraceInfo( traceInfo_t *ti ){
|
static int AddTraceInfo( traceInfo_t *ti ){
|
||||||
int num;
|
int num;
|
||||||
void *temp;
|
|
||||||
|
|
||||||
|
|
||||||
/* find an existing info */
|
/* find an existing info */
|
||||||
for ( num = firstTraceInfo; num < numTraceInfos; num++ )
|
for ( num = firstTraceInfo; num < numTraceInfos; num++ )
|
||||||
|
|
@ -150,16 +148,7 @@ static int AddTraceInfo( traceInfo_t *ti ){
|
||||||
}
|
}
|
||||||
|
|
||||||
/* enough space? */
|
/* enough space? */
|
||||||
if ( numTraceInfos >= maxTraceInfos ) {
|
AUTOEXPAND_BY_REALLOC_ADD( traceInfos, numTraceInfos, maxTraceInfos, GROW_TRACE_INFOS );
|
||||||
/* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* add the info */
|
/* add the info */
|
||||||
memcpy( &traceInfos[ num ], ti, sizeof( *traceInfos ) );
|
memcpy( &traceInfos[ num ], ti, sizeof( *traceInfos ) );
|
||||||
|
|
@ -179,20 +168,8 @@ static int AddTraceInfo( traceInfo_t *ti ){
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int AllocTraceNode( void ){
|
static int AllocTraceNode( void ){
|
||||||
traceNode_t *temp;
|
|
||||||
|
|
||||||
|
|
||||||
/* enough space? */
|
/* enough space? */
|
||||||
if ( numTraceNodes >= maxTraceNodes ) {
|
AUTOEXPAND_BY_REALLOC_ADD( traceNodes, numTraceNodes, maxTraceNodes, GROW_TRACE_NODES );
|
||||||
/* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* add the node */
|
/* add the node */
|
||||||
memset( &traceNodes[ numTraceNodes ], 0, sizeof( traceNode_t ) );
|
memset( &traceNodes[ numTraceNodes ], 0, sizeof( traceNode_t ) );
|
||||||
|
|
@ -216,8 +193,6 @@ static int AllocTraceNode( void ){
|
||||||
|
|
||||||
static int AddTraceWinding( traceWinding_t *tw ){
|
static int AddTraceWinding( traceWinding_t *tw ){
|
||||||
int num;
|
int num;
|
||||||
void *temp;
|
|
||||||
|
|
||||||
|
|
||||||
/* check for a dead winding */
|
/* check for a dead winding */
|
||||||
if ( deadWinding >= 0 && deadWinding < numTraceWindings ) {
|
if ( deadWinding >= 0 && deadWinding < numTraceWindings ) {
|
||||||
|
|
@ -229,16 +204,7 @@ static int AddTraceWinding( traceWinding_t *tw ){
|
||||||
num = numTraceWindings;
|
num = numTraceWindings;
|
||||||
|
|
||||||
/* enough space? */
|
/* enough space? */
|
||||||
if ( numTraceWindings >= maxTraceWindings ) {
|
AUTOEXPAND_BY_REALLOC_ADD( traceWindings, numTraceWindings, maxTraceWindings, GROW_TRACE_WINDINGS );
|
||||||
/* 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* add the winding */
|
/* add the winding */
|
||||||
|
|
@ -261,8 +227,6 @@ static int AddTraceWinding( traceWinding_t *tw ){
|
||||||
|
|
||||||
static int AddTraceTriangle( traceTriangle_t *tt ){
|
static int AddTraceTriangle( traceTriangle_t *tt ){
|
||||||
int num;
|
int num;
|
||||||
void *temp;
|
|
||||||
|
|
||||||
|
|
||||||
/* check for a dead triangle */
|
/* check for a dead triangle */
|
||||||
if ( deadTriangle >= 0 && deadTriangle < numTraceTriangles ) {
|
if ( deadTriangle >= 0 && deadTriangle < numTraceTriangles ) {
|
||||||
|
|
@ -274,16 +238,7 @@ static int AddTraceTriangle( traceTriangle_t *tt ){
|
||||||
num = numTraceTriangles;
|
num = numTraceTriangles;
|
||||||
|
|
||||||
/* enough space? */
|
/* enough space? */
|
||||||
if ( numTraceTriangles >= maxTraceTriangles ) {
|
AUTOEXPAND_BY_REALLOC_ADD( traceTriangles, numTraceTriangles, maxTraceTriangles, GROW_TRACE_TRIANGLES );
|
||||||
/* 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* find vectors for two edges sharing the first vert */
|
/* 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 ){
|
static int AddItemToTraceNode( traceNode_t *node, int num ){
|
||||||
void *temp;
|
|
||||||
|
|
||||||
|
|
||||||
/* dummy check */
|
/* dummy check */
|
||||||
if ( num < 0 ) {
|
if ( num < 0 ) {
|
||||||
return -1;
|
return -1;
|
||||||
|
|
@ -329,12 +281,10 @@ static int AddItemToTraceNode( traceNode_t *node, int num ){
|
||||||
if ( node->maxItems <= 0 ) {
|
if ( node->maxItems <= 0 ) {
|
||||||
node->maxItems = GROW_NODE_ITEMS;
|
node->maxItems = GROW_NODE_ITEMS;
|
||||||
}
|
}
|
||||||
temp = safe_malloc( node->maxItems * sizeof( *node->items ) );
|
node->items = realloc( node->items, node->maxItems * sizeof( *node->items ) );
|
||||||
if ( node->items != NULL ) {
|
if ( !node->items ) {
|
||||||
memcpy( temp, node->items, node->numItems * sizeof( *node->items ) );
|
Error( "node->items out of memory" );
|
||||||
free( node->items );
|
|
||||||
}
|
}
|
||||||
node->items = (int*) temp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* add the poly */
|
/* add the poly */
|
||||||
|
|
|
||||||
|
|
@ -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_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_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 )
|
#define Image_sRGBFloatFromLinearFloat( c ) ( ( ( c ) < 0.0031308f ) ? ( c ) * 12.92f : 1.055f * (float)pow( ( c ), 1.0f / 2.4f ) - 0.055f )
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -72,23 +72,11 @@ surfaceExtra_t seDefault = { NULL, NULL, -1, 0, WORLDSPAWN_CAST_SHADOWS, WORLDSP
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static surfaceExtra_t *AllocSurfaceExtra( void ){
|
static surfaceExtra_t *AllocSurfaceExtra( void ){
|
||||||
surfaceExtra_t *se;
|
|
||||||
|
|
||||||
|
|
||||||
/* enough space? */
|
/* enough space? */
|
||||||
if ( numSurfaceExtras >= maxSurfaceExtras ) {
|
AUTOEXPAND_BY_REALLOC_ADD( surfaceExtras, numSurfaceExtras, maxSurfaceExtras, GROW_SURFACE_EXTRAS );
|
||||||
/* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* add another */
|
/* add another */
|
||||||
se = &surfaceExtras[ numSurfaceExtras ];
|
surfaceExtra_t *se = &surfaceExtras[ numSurfaceExtras ];
|
||||||
numSurfaceExtras++;
|
numSurfaceExtras++;
|
||||||
memcpy( se, &seDefault, sizeof( surfaceExtra_t ) );
|
memcpy( se, &seDefault, sizeof( surfaceExtra_t ) );
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -81,8 +81,7 @@ void ClearMetaTriangles( void ){
|
||||||
|
|
||||||
static int FindMetaVertex( bspDrawVert_t *src ){
|
static int FindMetaVertex( bspDrawVert_t *src ){
|
||||||
int i;
|
int i;
|
||||||
bspDrawVert_t *v, *temp;
|
bspDrawVert_t *v;
|
||||||
|
|
||||||
|
|
||||||
/* try to find an existing drawvert */
|
/* try to find an existing drawvert */
|
||||||
for ( i = firstSearchMetaVert, v = &metaVerts[ i ]; i < numMetaVerts; i++, v++ )
|
for ( i = firstSearchMetaVert, v = &metaVerts[ i ]; i < numMetaVerts; i++, v++ )
|
||||||
|
|
@ -93,16 +92,7 @@ static int FindMetaVertex( bspDrawVert_t *src ){
|
||||||
}
|
}
|
||||||
|
|
||||||
/* enough space? */
|
/* enough space? */
|
||||||
if ( numMetaVerts >= maxMetaVerts ) {
|
AUTOEXPAND_BY_REALLOC_ADD( metaVerts, numMetaVerts, maxMetaVerts, GROW_META_VERTS );
|
||||||
/* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* add the triangle */
|
/* add the triangle */
|
||||||
memcpy( &metaVerts[ numMetaVerts ], src, sizeof( bspDrawVert_t ) );
|
memcpy( &metaVerts[ numMetaVerts ], src, sizeof( bspDrawVert_t ) );
|
||||||
|
|
@ -120,20 +110,8 @@ static int FindMetaVertex( bspDrawVert_t *src ){
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int AddMetaTriangle( void ){
|
static int AddMetaTriangle( void ){
|
||||||
metaTriangle_t *temp;
|
|
||||||
|
|
||||||
|
|
||||||
/* enough space? */
|
/* enough space? */
|
||||||
if ( numMetaTriangles >= maxMetaTriangles ) {
|
AUTOEXPAND_BY_REALLOC_ADD( metaTriangles, numMetaTriangles, maxMetaTriangles, GROW_META_TRIANGLES );
|
||||||
/* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* increment and return */
|
/* increment and return */
|
||||||
numMetaTriangles++;
|
numMetaTriangles++;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user