From 36240b8016a627c32cf2984678033698577d8a42 Mon Sep 17 00:00:00 2001 From: divverent Date: Tue, 10 Feb 2009 18:37:56 +0000 Subject: [PATCH] Some limits removed by dynamically expanding the arrays using realloc: -#define MAX_MAP_MODELS 0x400 -#define MAX_MAP_BRUSHES 0x10000 -#define MAX_MAP_SHADERS 0x800 //% 0x400 /* ydnar */ -#define MAX_MAP_NODES 0x20000 -#define MAX_MAP_BRUSHSIDES 0x100000 //% 0x20000 /* ydnar */ -#define MAX_MAP_LEAFFACES 0x100000 //% 0x20000 /* ydnar */ -#define MAX_MAP_LEAFBRUSHES 0x40000 git-svn-id: svn://svn.icculus.org/netradiant/trunk@185 61c419a2-8eb2-4b30-bcec-8cead039b335 --- tools/quake3/q3map2/bspfile_abstract.c | 9 +++++ tools/quake3/q3map2/bspfile_ibsp.c | 16 ++++---- tools/quake3/q3map2/bspfile_rbsp.c | 12 +++--- tools/quake3/q3map2/lightmaps.c | 9 ++--- tools/quake3/q3map2/q3map2.h | 51 +++++++++++++++++++------- tools/quake3/q3map2/writebsp.c | 24 +++++------- 6 files changed, 74 insertions(+), 47 deletions(-) diff --git a/tools/quake3/q3map2/bspfile_abstract.c b/tools/quake3/q3map2/bspfile_abstract.c index e2904186..ef43a2ac 100644 --- a/tools/quake3/q3map2/bspfile_abstract.c +++ b/tools/quake3/q3map2/bspfile_abstract.c @@ -329,6 +329,15 @@ int CopyLump( bspHeader_t *header, int lump, void *dest, int size ) return length / size; } +int CopyLump_Allocate( bspHeader_t *header, int lump, void **dest, int size, int *allocationVariable ) +{ + int length, offset; + + /* get lump length and offset */ + *allocationVariable = header->lumps[ lump ].length / size; + *dest = realloc(*dest, size * *allocationVariable); + return CopyLump(header, lump, *dest, size); +} /* diff --git a/tools/quake3/q3map2/bspfile_ibsp.c b/tools/quake3/q3map2/bspfile_ibsp.c index 26fc56dc..7874b922 100644 --- a/tools/quake3/q3map2/bspfile_ibsp.c +++ b/tools/quake3/q3map2/bspfile_ibsp.c @@ -101,14 +101,14 @@ static void CopyBrushSidesLump( ibspHeader_t *header ) /* copy */ in = GetLump( (bspHeader_t*) header, LUMP_BRUSHSIDES ); - out = bspBrushSides; for( i = 0; i < numBSPBrushSides; i++ ) { + AUTOEXPAND_BY_REALLOC(bspBrushSides, i, allocatedBSPBrushSides, 1024); + out = &bspBrushSides[i]; out->planeNum = in->planeNum; out->shaderNum = in->shaderNum; out->surfaceNum = -1; in++; - out++; } } @@ -477,21 +477,21 @@ void LoadIBSPFile( const char *filename ) Error( "%s is version %d, not %d", filename, header->version, game->bspVersion ); /* load/convert lumps */ - numBSPShaders = CopyLump( (bspHeader_t*) header, LUMP_SHADERS, bspShaders, sizeof( bspShader_t ) ); + numBSPShaders = CopyLump_Allocate( (bspHeader_t*) header, LUMP_SHADERS, (void **) &bspShaders, sizeof( bspShader_t ), &allocatedBSPShaders ); - numBSPModels = CopyLump( (bspHeader_t*) header, LUMP_MODELS, bspModels, sizeof( bspModel_t ) ); + numBSPModels = CopyLump_Allocate( (bspHeader_t*) header, LUMP_MODELS, (void **) &bspModels, sizeof( bspModel_t ), &allocatedBSPModels ); numBSPPlanes = CopyLump( (bspHeader_t*) header, LUMP_PLANES, bspPlanes, sizeof( bspPlane_t ) ); numBSPLeafs = CopyLump( (bspHeader_t*) header, LUMP_LEAFS, bspLeafs, sizeof( bspLeaf_t ) ); - numBSPNodes = CopyLump( (bspHeader_t*) header, LUMP_NODES, bspNodes, sizeof( bspNode_t ) ); + numBSPNodes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_NODES, (void **) &bspNodes, sizeof( bspNode_t ), &allocatedBSPNodes ); - numBSPLeafSurfaces = CopyLump( (bspHeader_t*) header, LUMP_LEAFSURFACES, bspLeafSurfaces, sizeof( bspLeafSurfaces[ 0 ] ) ); + numBSPLeafSurfaces = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFSURFACES, (void **) &bspLeafSurfaces, sizeof( bspLeafSurfaces[ 0 ] ), &allocatedBSPLeafSurfaces ); - numBSPLeafBrushes = CopyLump( (bspHeader_t*) header, LUMP_LEAFBRUSHES, bspLeafBrushes, sizeof( bspLeafBrushes[ 0 ] ) ); + numBSPLeafBrushes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFBRUSHES, (void **) &bspLeafBrushes, sizeof( bspLeafBrushes[ 0 ] ), &allocatedBSPLeafBrushes ); - numBSPBrushes = CopyLump( (bspHeader_t*) header, LUMP_BRUSHES, bspBrushes, sizeof( bspBrush_t ) ); + numBSPBrushes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_BRUSHES, (void **) &bspBrushes, sizeof( bspBrush_t ), &allocatedBSPLeafBrushes ); CopyBrushSidesLump( header ); diff --git a/tools/quake3/q3map2/bspfile_rbsp.c b/tools/quake3/q3map2/bspfile_rbsp.c index 74d77ae9..c31da09e 100644 --- a/tools/quake3/q3map2/bspfile_rbsp.c +++ b/tools/quake3/q3map2/bspfile_rbsp.c @@ -229,21 +229,21 @@ void LoadRBSPFile( const char *filename ) Error( "%s is version %d, not %d", filename, header->version, game->bspVersion ); /* load/convert lumps */ - numBSPShaders = CopyLump( (bspHeader_t*) header, LUMP_SHADERS, bspShaders, sizeof( bspShader_t ) ); + numBSPShaders = CopyLump_Allocate( (bspHeader_t*) header, LUMP_SHADERS, (void **) &bspShaders, sizeof( bspShader_t ), &allocatedBSPShaders ); - numBSPModels = CopyLump( (bspHeader_t*) header, LUMP_MODELS, bspModels, sizeof( bspModel_t ) ); + numBSPModels = CopyLump_Allocate( (bspHeader_t*) header, LUMP_MODELS, (void **) &bspModels, sizeof( bspModel_t ), &allocatedBSPModels ); numBSPPlanes = CopyLump( (bspHeader_t*) header, LUMP_PLANES, bspPlanes, sizeof( bspPlane_t ) ); numBSPLeafs = CopyLump( (bspHeader_t*) header, LUMP_LEAFS, bspLeafs, sizeof( bspLeaf_t ) ); - numBSPNodes = CopyLump( (bspHeader_t*) header, LUMP_NODES, bspNodes, sizeof( bspNode_t ) ); + numBSPNodes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_NODES, (void **) &bspNodes, sizeof( bspNode_t ), &allocatedBSPNodes ); - numBSPLeafSurfaces = CopyLump( (bspHeader_t*) header, LUMP_LEAFSURFACES, bspLeafSurfaces, sizeof( bspLeafSurfaces[ 0 ] ) ); + numBSPLeafSurfaces = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFSURFACES, (void **) &bspLeafSurfaces, sizeof( bspLeafSurfaces[ 0 ] ), &allocatedBSPLeafSurfaces ); - numBSPLeafBrushes = CopyLump( (bspHeader_t*) header, LUMP_LEAFBRUSHES, bspLeafBrushes, sizeof( bspLeafBrushes[ 0 ] ) ); + numBSPLeafBrushes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFBRUSHES, (void **) &bspLeafBrushes, sizeof( bspLeafBrushes[ 0 ] ), &allocatedBSPLeafBrushes ); - numBSPBrushes = CopyLump( (bspHeader_t*) header, LUMP_BRUSHES, bspBrushes, sizeof( bspBrush_t ) ); + numBSPBrushes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_BRUSHES, &bspBrushes, sizeof( bspBrush_t ), &allocatedBSPLeafBrushes ); numBSPBrushSides = CopyLump( (bspHeader_t*) header, LUMP_BRUSHSIDES, bspBrushSides, sizeof( bspBrushSide_t ) ); diff --git a/tools/quake3/q3map2/lightmaps.c b/tools/quake3/q3map2/lightmaps.c index 62977ddf..94607170 100644 --- a/tools/quake3/q3map2/lightmaps.c +++ b/tools/quake3/q3map2/lightmaps.c @@ -30,7 +30,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ int numSortShaders; -mapDrawSurface_t *surfsOnShader[ MAX_MAP_SHADERS ]; +mapDrawSurface_t **surfsOnShader; +int allocatedSurfsOnShader; int allocated[ LIGHTMAP_WIDTH ]; @@ -416,8 +417,7 @@ void AllocateLightmaps( entity_t *e ) /* new shader */ if( j == numSortShaders ) { - if( numSortShaders >= MAX_MAP_SHADERS ) - Error( "MAX_MAP_SHADERS" ); + EXPAND_BY_REALLOC(surfsOnShader, numSortShaders, allocatedSurfsOnShader, 1024); surfsOnShader[ j ] = ds; ds->nextOnShader = NULL; numSortShaders++; @@ -457,8 +457,7 @@ void AllocateLightmaps( entity_t *e ) /* new shader */ if( j == numSortShaders ) { - if( numSortShaders >= MAX_MAP_SHADERS ) - Error( "MAX_MAP_SHADERS" ); + EXPAND_BY_REALLOC(surfsOnShader, numSortShaders, allocatedSurfsOnShader, 1024); surfsOnShader[ j ] = ds; ds->nextOnShader = NULL; numSortShaders++; diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index 2ff8e713..4e5c0eea 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -305,20 +305,13 @@ abstracted bsp file #define MAX_LIGHTMAP_SHADERS 256 /* ok to increase these at the expense of more memory */ -#define MAX_MAP_MODELS 0x400 -#define MAX_MAP_BRUSHES 0x10000 #define MAX_MAP_ENTITIES 0x1000 //% 0x800 /* ydnar */ #define MAX_MAP_ENTSTRING 0x80000 //% 0x40000 /* ydnar */ -#define MAX_MAP_SHADERS 0x800 //% 0x400 /* ydnar */ #define MAX_MAP_AREAS 0x100 /* MAX_MAP_AREA_BYTES in q_shared must match! */ #define MAX_MAP_FOGS 30 //& 0x100 /* RBSP (32 - world fog - goggles) */ #define MAX_MAP_PLANES 0x200000 //% 0x20000 /* ydnar for md */ -#define MAX_MAP_NODES 0x20000 -#define MAX_MAP_BRUSHSIDES 0x100000 //% 0x20000 /* ydnar */ #define MAX_MAP_LEAFS 0x20000 -#define MAX_MAP_LEAFFACES 0x100000 //% 0x20000 /* ydnar */ -#define MAX_MAP_LEAFBRUSHES 0x40000 #define MAX_MAP_PORTALS 0x20000 #define MAX_MAP_LIGHTING 0x800000 #define MAX_MAP_LIGHTGRID 0x100000 //% 0x800000 /* ydnar: set to points, not bytes */ @@ -1797,6 +1790,7 @@ void SwapBlock( int *block, int size ); int GetLumpElements( bspHeader_t *header, int lump, int size ); void *GetLump( bspHeader_t *header, int lump ); int CopyLump( bspHeader_t *header, int lump, void *dest, int size ); +int CopyLump_Allocate( bspHeader_t *header, int lump, void **dest, int size, int *allocationVariable ); void AddLump( FILE *file, bspHeader_t *header, int lumpNum, const void *data, int length ); void LoadBSPFile( const char *filename ); @@ -2308,10 +2302,12 @@ Q_EXTERN int numBSPEntities Q_ASSIGN( 0 ); Q_EXTERN entity_t entities[ MAX_MAP_ENTITIES ]; Q_EXTERN int numBSPModels Q_ASSIGN( 0 ); -Q_EXTERN bspModel_t bspModels[ MAX_MAP_MODELS ]; +Q_EXTERN int allocatedBSPModels Q_ASSIGN( 0 ); +Q_EXTERN bspModel_t* bspModels Q_ASSIGN(NULL); Q_EXTERN int numBSPShaders Q_ASSIGN( 0 ); -Q_EXTERN bspShader_t bspShaders[ MAX_MAP_MODELS ]; +Q_EXTERN int allocatedBSPShaders Q_ASSIGN( 0 ); +Q_EXTERN bspShader_t* bspShaders Q_ASSIGN(0); Q_EXTERN int bspEntDataSize Q_ASSIGN( 0 ); Q_EXTERN char bspEntData[ MAX_MAP_ENTSTRING ]; @@ -2323,19 +2319,24 @@ Q_EXTERN int numBSPPlanes Q_ASSIGN( 0 ); Q_EXTERN bspPlane_t bspPlanes[ MAX_MAP_PLANES ]; Q_EXTERN int numBSPNodes Q_ASSIGN( 0 ); -Q_EXTERN bspNode_t bspNodes[ MAX_MAP_NODES ]; +Q_EXTERN int allocatedBSPNodes Q_ASSIGN( 0 ); +Q_EXTERN bspNode_t* bspNodes Q_ASSIGN(NULL); Q_EXTERN int numBSPLeafSurfaces Q_ASSIGN( 0 ); -Q_EXTERN int bspLeafSurfaces[ MAX_MAP_LEAFFACES ]; +Q_EXTERN int allocatedBSPLeafSurfaces Q_ASSIGN( 0 ); +Q_EXTERN int* bspLeafSurfaces Q_ASSIGN(NULL); Q_EXTERN int numBSPLeafBrushes Q_ASSIGN( 0 ); -Q_EXTERN int bspLeafBrushes[ MAX_MAP_LEAFBRUSHES ]; +Q_EXTERN int allocatedBSPLeafBrushes Q_ASSIGN( 0 ); +Q_EXTERN int* bspLeafBrushes Q_ASSIGN(0); Q_EXTERN int numBSPBrushes Q_ASSIGN( 0 ); -Q_EXTERN bspBrush_t bspBrushes[ MAX_MAP_BRUSHES ]; +Q_EXTERN int allocatedBSPBrushes Q_ASSIGN( 0 ); +Q_EXTERN bspBrush_t* bspBrushes Q_ASSIGN(NULL); Q_EXTERN int numBSPBrushSides Q_ASSIGN( 0 ); -Q_EXTERN bspBrushSide_t bspBrushSides[ MAX_MAP_BRUSHSIDES ]; +Q_EXTERN int allocatedBSPBrushSides Q_ASSIGN( 0 ); +Q_EXTERN bspBrushSide_t* bspBrushSides Q_ASSIGN(NULL); Q_EXTERN int numBSPLightBytes Q_ASSIGN( 0 ); Q_EXTERN byte *bspLightBytes Q_ASSIGN( NULL ); @@ -2364,5 +2365,27 @@ Q_EXTERN bspFog_t bspFogs[ MAX_MAP_FOGS ]; Q_EXTERN int numBSPAds Q_ASSIGN( 0 ); Q_EXTERN bspAdvertisement_t bspAds[ MAX_MAP_ADVERTISEMENTS ]; +#define AUTOEXPAND_BY_REALLOC(ptr, reqitem, allocated, def) \ + do \ + { \ + if(reqitem >= allocated) \ + { \ + if(allocated == 0) \ + allocated = def; \ + while(reqitem >= allocated && allocated) \ + allocated *= 2; \ + if(allocated > 2147483647 / sizeof(*ptr)) \ + { \ + Error(#ptr " over 2 GB"); \ + } \ + ptr = realloc(ptr, sizeof(*ptr) * allocated); \ + if(!ptr) \ + Error(#ptr " out of memory"); \ + } \ + } \ + while(0) + +#define AUTOEXPAND_BY_REALLOC_BSP(suffix, def) AUTOEXPAND_BY_REALLOC(bsp##suffix, numBSP##suffix, allocatedBSP##suffix, def) + /* end marker */ #endif diff --git a/tools/quake3/q3map2/writebsp.c b/tools/quake3/q3map2/writebsp.c index 4cdd3a99..70a382dd 100644 --- a/tools/quake3/q3map2/writebsp.c +++ b/tools/quake3/q3map2/writebsp.c @@ -66,13 +66,15 @@ int EmitShader( const char *shader, int *contentFlags, int *surfaceFlags ) if( !Q_stricmp( shader, bspShaders[ i ].shader ) ) return i; } + + // i == numBSPShaders /* get shaderinfo */ si = ShaderInfoForShader( shader ); /* emit a new shader */ - if( i == MAX_MAP_SHADERS ) - Error( "MAX_MAP_SHADERS" ); + AUTOEXPAND_BY_REALLOC_BSP(Shaders, 1024); + numBSPShaders++; strcpy( bspShaders[ i ].shader, shader ); bspShaders[ i ].surfaceFlags = si->surfaceFlags; @@ -165,8 +167,7 @@ void EmitLeaf( node_t *node ) //% if( b->guard != 0xDEADBEEF ) //% Sys_Printf( "Brush %6d: 0x%08X Guard: 0x%08X Next: 0x%08X Original: 0x%08X Sides: %d\n", b->brushNum, b, b, b->next, b->original, b->numsides ); - if( numBSPLeafBrushes >= MAX_MAP_LEAFBRUSHES ) - Error( "MAX_MAP_LEAFBRUSHES" ); + AUTOEXPAND_BY_REALLOC_BSP(LeafBrushes, 1024); bspLeafBrushes[ numBSPLeafBrushes ] = b->original->outputNum; numBSPLeafBrushes++; } @@ -181,8 +182,7 @@ void EmitLeaf( node_t *node ) leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces; for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef ) { - if( numBSPLeafSurfaces >= MAX_MAP_LEAFFACES ) - Error( "MAX_MAP_LEAFFACES" ); + AUTOEXPAND_BY_REALLOC_BSP(LeafSurfaces, 1024); bspLeafSurfaces[ numBSPLeafSurfaces ] = dsr->outputNum; numBSPLeafSurfaces++; } @@ -210,8 +210,7 @@ int EmitDrawNode_r( node_t *node ) } /* emit a node */ - if( numBSPNodes == MAX_MAP_NODES ) - Error( "MAX_MAP_NODES" ); + AUTOEXPAND_BY_REALLOC_BSP(Nodes, 1024); n = &bspNodes[ numBSPNodes ]; numBSPNodes++; @@ -441,8 +440,7 @@ void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes ) for( b = brushes; b != NULL; b = b->next ) { /* check limits */ - if( numBSPBrushes == MAX_MAP_BRUSHES ) - Error( "MAX_MAP_BRUSHES (%d)", numBSPBrushes ); + AUTOEXPAND_BY_REALLOC_BSP(Brushes, 1024); /* get bsp brush */ b->outputNum = numBSPBrushes; @@ -462,8 +460,7 @@ void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes ) b->sides[ j ].outputNum = -1; /* check count */ - if( numBSPBrushSides == MAX_MAP_BRUSHSIDES ) - Error( "MAX_MAP_BRUSHSIDES "); + AUTOEXPAND_BY_REALLOC_BSP(BrushSides, 1024); /* emit side */ b->sides[ j ].outputNum = numBSPBrushSides; @@ -553,8 +550,7 @@ void BeginModel( void ) /* test limits */ - if( numBSPModels == MAX_MAP_MODELS ) - Error( "MAX_MAP_MODELS" ); + AUTOEXPAND_BY_REALLOC_BSP(Models, 256); /* get model and entity */ mod = &bspModels[ numBSPModels ]; -- 2.39.2