Change winding_t and bspbrush_t to use flexible array members rather than size-1 arrays.

The arrays were always meant to be variably sized, and objects are only ever allocated dynamically. Object size computations are simplified with this change.

Flexible arrays were introduced in C99, so this change means that we will require a C99-conforming compiler henceforth.
This commit is contained in:
Thomas Köppe 2018-01-21 01:32:08 +00:00 committed by Garux
parent 5064c1f163
commit 8875e2dd80
4 changed files with 68 additions and 69 deletions

View File

@ -26,7 +26,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "aas_store.h" #include "aas_store.h"
#include "aas_cfg.h" #include "aas_cfg.h"
#include <stddef.h>
#include <assert.h> #include <assert.h>
/* /*
@ -42,7 +41,7 @@ evaluate split side
cost = 0 cost = 0
for all sides for all sides
for all sides for all sides
get get
if side splits side and splitside is on same child if side splits side and splitside is on same child
cost++; cost++;
} }
@ -425,7 +424,7 @@ bspbrush_t *AllocBrush (int numsides)
bspbrush_t *bb; bspbrush_t *bb;
size_t c; size_t c;
c = offsetof(bspbrush_t, sides[numsides]); c = sizeof(*bb) + sizeof(*bb->sides) * numsides;
bb = GetMemory(c); bb = GetMemory(c);
memset (bb, 0, c); memset (bb, 0, c);
if (numthreads == 1) if (numthreads == 1)
@ -487,8 +486,8 @@ bspbrush_t *CopyBrush (bspbrush_t *brush)
bspbrush_t *newbrush; bspbrush_t *newbrush;
size_t size; size_t size;
int i; int i;
size = offsetof(bspbrush_t, sides[brush->numsides]); size = sizeof(*newbrush) + sizeof(*brush->sides) * brush->numsides;
newbrush = AllocBrush (brush->numsides); newbrush = AllocBrush (brush->numsides);
memcpy (newbrush, brush, size); memcpy (newbrush, brush, size);
@ -590,7 +589,7 @@ int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, plane_t *p)
if (emins[p->type] < p->dist-PLANESIDE_EPSILON) sides |= PSIDE_BACK; if (emins[p->type] < p->dist-PLANESIDE_EPSILON) sides |= PSIDE_BACK;
return sides; return sides;
} //end if } //end if
// general case // general case
switch (p->signbits) switch (p->signbits)
{ {
@ -1339,7 +1338,7 @@ void SplitBrush (bspbrush_t *brush, int planenum,
// Returns: - // Returns: -
// Changes Globals: - // Changes Globals: -
//=========================================================================== //===========================================================================
void SplitBrushList (bspbrush_t *brushes, void SplitBrushList (bspbrush_t *brushes,
node_t *node, bspbrush_t **front, bspbrush_t **back) node_t *node, bspbrush_t **front, bspbrush_t **back)
{ {
bspbrush_t *brush, *newbrush, *newbrush2; bspbrush_t *brush, *newbrush, *newbrush2;

View File

@ -71,7 +71,7 @@ winding_t *AllocWinding (int points)
winding_t *w; winding_t *w;
int s; int s;
s = sizeof(vec_t)*3*points + sizeof(int); s = sizeof(*w) + sizeof(*w->p) * points;
w = GetMemory(s); w = GetMemory(s);
memset(w, 0, s); memset(w, 0, s);
@ -157,7 +157,7 @@ void RemoveColinearPoints (winding_t *w)
if (numthreads == 1) if (numthreads == 1)
c_removed += w->numpoints - nump; c_removed += w->numpoints - nump;
w->numpoints = nump; w->numpoints = nump;
memcpy (w->p, p, nump*sizeof(p[0])); memcpy (w->p, p, nump * sizeof(*w->p));
} }
/* /*
@ -254,7 +254,7 @@ winding_t *BaseWindingForPlane (vec3_t normal, vec_t dist)
vec_t max, v; vec_t max, v;
vec3_t org, vright, vup; vec3_t org, vright, vup;
winding_t *w; winding_t *w;
// find the major axis // find the major axis
max = -BOGUS_RANGE; max = -BOGUS_RANGE;
@ -270,48 +270,48 @@ winding_t *BaseWindingForPlane (vec3_t normal, vec_t dist)
} }
if (x==-1) if (x==-1)
Error ("BaseWindingForPlane: no axis found"); Error ("BaseWindingForPlane: no axis found");
VectorCopy (vec3_origin, vup); VectorCopy (vec3_origin, vup);
switch (x) switch (x)
{ {
case 0: case 0:
case 1: case 1:
vup[2] = 1; vup[2] = 1;
break; break;
case 2: case 2:
vup[0] = 1; vup[0] = 1;
break; break;
} }
v = DotProduct (vup, normal); v = DotProduct (vup, normal);
VectorMA (vup, -v, normal, vup); VectorMA (vup, -v, normal, vup);
VectorNormalize (vup); VectorNormalize (vup);
VectorScale (normal, dist, org); VectorScale (normal, dist, org);
CrossProduct (vup, normal, vright); CrossProduct (vup, normal, vright);
VectorScale (vup, BOGUS_RANGE, vup); VectorScale (vup, BOGUS_RANGE, vup);
VectorScale (vright, BOGUS_RANGE, vright); VectorScale (vright, BOGUS_RANGE, vright);
// project a really big axis aligned box onto the plane // project a really big axis aligned box onto the plane
w = AllocWinding (4); w = AllocWinding (4);
VectorSubtract (org, vright, w->p[0]); VectorSubtract (org, vright, w->p[0]);
VectorAdd (w->p[0], vup, w->p[0]); VectorAdd (w->p[0], vup, w->p[0]);
VectorAdd (org, vright, w->p[1]); VectorAdd (org, vright, w->p[1]);
VectorAdd (w->p[1], vup, w->p[1]); VectorAdd (w->p[1], vup, w->p[1]);
VectorAdd (org, vright, w->p[2]); VectorAdd (org, vright, w->p[2]);
VectorSubtract (w->p[2], vup, w->p[2]); VectorSubtract (w->p[2], vup, w->p[2]);
VectorSubtract (org, vright, w->p[3]); VectorSubtract (org, vright, w->p[3]);
VectorSubtract (w->p[3], vup, w->p[3]); VectorSubtract (w->p[3], vup, w->p[3]);
w->numpoints = 4; w->numpoints = 4;
return w; return w;
} }
/* /*
@ -325,7 +325,7 @@ winding_t *CopyWinding (winding_t *w)
winding_t *c; winding_t *c;
c = AllocWinding (w->numpoints); c = AllocWinding (w->numpoints);
size = (int)((winding_t *)0)->p[w->numpoints]; size = sizeof(*w) + sizeof(*w->p) * w->numpoints;
memcpy (c, w, size); memcpy (c, w, size);
return c; return c;
} }
@ -355,7 +355,7 @@ winding_t *ReverseWinding (winding_t *w)
ClipWindingEpsilon ClipWindingEpsilon
============= =============
*/ */
void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist, void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist,
vec_t epsilon, winding_t **front, winding_t **back) vec_t epsilon, winding_t **front, winding_t **back)
{ {
vec_t dists[MAX_POINTS_ON_WINDING+4]; vec_t dists[MAX_POINTS_ON_WINDING+4];
@ -368,7 +368,7 @@ void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist,
vec3_t mid; vec3_t mid;
winding_t *f, *b; winding_t *f, *b;
int maxpts; int maxpts;
counts[0] = counts[1] = counts[2] = 0; counts[0] = counts[1] = counts[2] = 0;
// determine sides for each point // determine sides for each point
@ -389,7 +389,7 @@ void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist,
} }
sides[i] = sides[0]; sides[i] = sides[0];
dists[i] = dists[0]; dists[i] = dists[0];
*front = *back = NULL; *front = *back = NULL;
if (!counts[0]) if (!counts[0])
@ -408,11 +408,11 @@ void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist,
*front = f = AllocWinding (maxpts); *front = f = AllocWinding (maxpts);
*back = b = AllocWinding (maxpts); *back = b = AllocWinding (maxpts);
for (i=0 ; i<in->numpoints ; i++) for (i=0 ; i<in->numpoints ; i++)
{ {
p1 = in->p[i]; p1 = in->p[i];
if (sides[i] == SIDE_ON) if (sides[i] == SIDE_ON)
{ {
VectorCopy (p1, f->p[f->numpoints]); VectorCopy (p1, f->p[f->numpoints]);
@ -421,7 +421,7 @@ void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist,
b->numpoints++; b->numpoints++;
continue; continue;
} }
if (sides[i] == SIDE_FRONT) if (sides[i] == SIDE_FRONT)
{ {
VectorCopy (p1, f->p[f->numpoints]); VectorCopy (p1, f->p[f->numpoints]);
@ -435,10 +435,10 @@ void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist,
if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i]) if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i])
continue; continue;
// generate a split point // generate a split point
p2 = in->p[(i+1)%in->numpoints]; p2 = in->p[(i+1)%in->numpoints];
dot = dists[i] / (dists[i]-dists[i+1]); dot = dists[i] / (dists[i]-dists[i+1]);
for (j=0 ; j<3 ; j++) for (j=0 ; j<3 ; j++)
{ // avoid round off error when possible { // avoid round off error when possible
@ -449,13 +449,13 @@ void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist,
else else
mid[j] = p1[j] + dot*(p2[j]-p1[j]); mid[j] = p1[j] + dot*(p2[j]-p1[j]);
} }
VectorCopy (mid, f->p[f->numpoints]); VectorCopy (mid, f->p[f->numpoints]);
f->numpoints++; f->numpoints++;
VectorCopy (mid, b->p[b->numpoints]); VectorCopy (mid, b->p[b->numpoints]);
b->numpoints++; b->numpoints++;
} }
if (f->numpoints > maxpts || b->numpoints > maxpts) if (f->numpoints > maxpts || b->numpoints > maxpts)
Error ("ClipWinding: points exceeded estimate"); Error ("ClipWinding: points exceeded estimate");
if (f->numpoints > MAX_POINTS_ON_WINDING || b->numpoints > MAX_POINTS_ON_WINDING) if (f->numpoints > MAX_POINTS_ON_WINDING || b->numpoints > MAX_POINTS_ON_WINDING)
@ -503,7 +503,7 @@ void ChopWindingInPlace (winding_t **inout, vec3_t normal, vec_t dist, vec_t eps
} }
sides[i] = sides[0]; sides[i] = sides[0];
dists[i] = dists[0]; dists[i] = dists[0];
if (!counts[0]) if (!counts[0])
{ {
FreeWinding (in); FreeWinding (in);
@ -517,18 +517,18 @@ void ChopWindingInPlace (winding_t **inout, vec3_t normal, vec_t dist, vec_t eps
// of fp grouping errors // of fp grouping errors
f = AllocWinding (maxpts); f = AllocWinding (maxpts);
for (i=0 ; i<in->numpoints ; i++) for (i=0 ; i<in->numpoints ; i++)
{ {
p1 = in->p[i]; p1 = in->p[i];
if (sides[i] == SIDE_ON) if (sides[i] == SIDE_ON)
{ {
VectorCopy (p1, f->p[f->numpoints]); VectorCopy (p1, f->p[f->numpoints]);
f->numpoints++; f->numpoints++;
continue; continue;
} }
if (sides[i] == SIDE_FRONT) if (sides[i] == SIDE_FRONT)
{ {
VectorCopy (p1, f->p[f->numpoints]); VectorCopy (p1, f->p[f->numpoints]);
@ -537,10 +537,10 @@ void ChopWindingInPlace (winding_t **inout, vec3_t normal, vec_t dist, vec_t eps
if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i]) if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i])
continue; continue;
// generate a split point // generate a split point
p2 = in->p[(i+1)%in->numpoints]; p2 = in->p[(i+1)%in->numpoints];
dot = dists[i] / (dists[i]-dists[i+1]); dot = dists[i] / (dists[i]-dists[i+1]);
for (j=0 ; j<3 ; j++) for (j=0 ; j<3 ; j++)
{ // avoid round off error when possible { // avoid round off error when possible
@ -551,11 +551,11 @@ void ChopWindingInPlace (winding_t **inout, vec3_t normal, vec_t dist, vec_t eps
else else
mid[j] = p1[j] + dot*(p2[j]-p1[j]); mid[j] = p1[j] + dot*(p2[j]-p1[j]);
} }
VectorCopy (mid, f->p[f->numpoints]); VectorCopy (mid, f->p[f->numpoints]);
f->numpoints++; f->numpoints++;
} }
if (f->numpoints > maxpts) if (f->numpoints > maxpts)
Error ("ClipWinding: points exceeded estimate"); Error ("ClipWinding: points exceeded estimate");
if (f->numpoints > MAX_POINTS_ON_WINDING) if (f->numpoints > MAX_POINTS_ON_WINDING)
@ -603,13 +603,13 @@ void CheckWinding (winding_t *w)
if (w->numpoints < 3) if (w->numpoints < 3)
Error ("CheckWinding: %i points",w->numpoints); Error ("CheckWinding: %i points",w->numpoints);
area = WindingArea(w); area = WindingArea(w);
if (area < 1) if (area < 1)
Error ("CheckWinding: %f area", area); Error ("CheckWinding: %f area", area);
WindingPlane (w, facenormal, &facedist); WindingPlane (w, facenormal, &facedist);
for (i=0 ; i<w->numpoints ; i++) for (i=0 ; i<w->numpoints ; i++)
{ {
p1 = w->p[i]; p1 = w->p[i];
@ -619,24 +619,24 @@ void CheckWinding (winding_t *w)
Error ("CheckWinding: BUGUS_RANGE: %f",p1[j]); Error ("CheckWinding: BUGUS_RANGE: %f",p1[j]);
j = i+1 == w->numpoints ? 0 : i+1; j = i+1 == w->numpoints ? 0 : i+1;
// check the point is on the face plane // check the point is on the face plane
d = DotProduct (p1, facenormal) - facedist; d = DotProduct (p1, facenormal) - facedist;
if (d < -ON_EPSILON || d > ON_EPSILON) if (d < -ON_EPSILON || d > ON_EPSILON)
Error ("CheckWinding: point off plane"); Error ("CheckWinding: point off plane");
// check the edge isnt degenerate // check the edge isnt degenerate
p2 = w->p[j]; p2 = w->p[j];
VectorSubtract (p2, p1, dir); VectorSubtract (p2, p1, dir);
if (VectorLength (dir) < ON_EPSILON) if (VectorLength (dir) < ON_EPSILON)
Error ("CheckWinding: degenerate edge"); Error ("CheckWinding: degenerate edge");
CrossProduct (facenormal, dir, edgenormal); CrossProduct (facenormal, dir, edgenormal);
VectorNormalize (edgenormal); VectorNormalize (edgenormal);
edgedist = DotProduct (p1, edgenormal); edgedist = DotProduct (p1, edgenormal);
edgedist += ON_EPSILON; edgedist += ON_EPSILON;
// all other points must be on front side // all other points must be on front side
for (j=0 ; j<w->numpoints ; j++) for (j=0 ; j<w->numpoints ; j++)
{ {
@ -715,14 +715,14 @@ winding_t *TryMergeWinding (winding_t *f1, winding_t *f2, vec3_t planenormal)
vec3_t normal, delta; vec3_t normal, delta;
vec_t dot; vec_t dot;
qboolean keep1, keep2; qboolean keep1, keep2;
// //
// find a common edge // find a common edge
// //
p1 = p2 = NULL; // stop compiler warning p1 = p2 = NULL; // stop compiler warning
j = 0; // j = 0; //
for (i = 0; i < f1->numpoints; i++) for (i = 0; i < f1->numpoints; i++)
{ {
p1 = f1->p[i]; p1 = f1->p[i];
@ -744,7 +744,7 @@ winding_t *TryMergeWinding (winding_t *f1, winding_t *f2, vec3_t planenormal)
if (j < f2->numpoints) if (j < f2->numpoints)
break; break;
} //end for } //end for
if (i == f1->numpoints) if (i == f1->numpoints)
return NULL; // no matching edges return NULL; // no matching edges
@ -756,14 +756,14 @@ winding_t *TryMergeWinding (winding_t *f1, winding_t *f2, vec3_t planenormal)
VectorSubtract (p1, back, delta); VectorSubtract (p1, back, delta);
CrossProduct (planenormal, delta, normal); CrossProduct (planenormal, delta, normal);
VectorNormalize (normal); VectorNormalize (normal);
back = f2->p[(j+2)%f2->numpoints]; back = f2->p[(j+2)%f2->numpoints];
VectorSubtract (back, p1, delta); VectorSubtract (back, p1, delta);
dot = DotProduct (delta, normal); dot = DotProduct (delta, normal);
if (dot > CONTINUOUS_EPSILON) if (dot > CONTINUOUS_EPSILON)
return NULL; // not a convex polygon return NULL; // not a convex polygon
keep1 = (qboolean)(dot < -CONTINUOUS_EPSILON); keep1 = (qboolean)(dot < -CONTINUOUS_EPSILON);
back = f1->p[(i+2)%f1->numpoints]; back = f1->p[(i+2)%f1->numpoints];
VectorSubtract (back, p2, delta); VectorSubtract (back, p2, delta);
CrossProduct (planenormal, delta, normal); CrossProduct (planenormal, delta, normal);
@ -780,17 +780,17 @@ winding_t *TryMergeWinding (winding_t *f1, winding_t *f2, vec3_t planenormal)
// build the new polygon // build the new polygon
// //
newf = AllocWinding (f1->numpoints + f2->numpoints); newf = AllocWinding (f1->numpoints + f2->numpoints);
// copy first polygon // copy first polygon
for (k=(i+1)%f1->numpoints ; k != i ; k=(k+1)%f1->numpoints) for (k=(i+1)%f1->numpoints ; k != i ; k=(k+1)%f1->numpoints)
{ {
if (k==(i+1)%f1->numpoints && !keep2) if (k==(i+1)%f1->numpoints && !keep2)
continue; continue;
VectorCopy (f1->p[k], newf->p[newf->numpoints]); VectorCopy (f1->p[k], newf->p[newf->numpoints]);
newf->numpoints++; newf->numpoints++;
} }
// copy second polygon // copy second polygon
for (l= (j+1)%f2->numpoints ; l != j ; l=(l+1)%f2->numpoints) for (l= (j+1)%f2->numpoints ; l != j ; l=(l+1)%f2->numpoints)
{ {
@ -936,7 +936,7 @@ int WindingError(winding_t *w)
sprintf(windingerror, "winding %i points", w->numpoints); sprintf(windingerror, "winding %i points", w->numpoints);
return WE_NOTENOUGHPOINTS; return WE_NOTENOUGHPOINTS;
} //end if } //end if
area = WindingArea(w); area = WindingArea(w);
if (area < 1) if (area < 1)
{ {
@ -945,7 +945,7 @@ int WindingError(winding_t *w)
} //end if } //end if
WindingPlane (w, facenormal, &facedist); WindingPlane (w, facenormal, &facedist);
for (i=0 ; i<w->numpoints ; i++) for (i=0 ; i<w->numpoints ; i++)
{ {
p1 = w->p[i]; p1 = w->p[i];
@ -960,7 +960,7 @@ int WindingError(winding_t *w)
} //end for } //end for
j = i+1 == w->numpoints ? 0 : i+1; j = i+1 == w->numpoints ? 0 : i+1;
// check the point is on the face plane // check the point is on the face plane
d = DotProduct (p1, facenormal) - facedist; d = DotProduct (p1, facenormal) - facedist;
if (d < -ON_EPSILON || d > ON_EPSILON) if (d < -ON_EPSILON || d > ON_EPSILON)
@ -968,22 +968,22 @@ int WindingError(winding_t *w)
sprintf(windingerror, "winding point %d off plane", i); sprintf(windingerror, "winding point %d off plane", i);
return WE_POINTOFFPLANE; return WE_POINTOFFPLANE;
} //end if } //end if
// check the edge isnt degenerate // check the edge isnt degenerate
p2 = w->p[j]; p2 = w->p[j];
VectorSubtract (p2, p1, dir); VectorSubtract (p2, p1, dir);
if (VectorLength (dir) < ON_EPSILON) if (VectorLength (dir) < ON_EPSILON)
{ {
sprintf(windingerror, "winding degenerate edge %d-%d", i, j); sprintf(windingerror, "winding degenerate edge %d-%d", i, j);
return WE_DEGENERATEEDGE; return WE_DEGENERATEEDGE;
} //end if } //end if
CrossProduct (facenormal, dir, edgenormal); CrossProduct (facenormal, dir, edgenormal);
VectorNormalize (edgenormal); VectorNormalize (edgenormal);
edgedist = DotProduct (p1, edgenormal); edgedist = DotProduct (p1, edgenormal);
edgedist += ON_EPSILON; edgedist += ON_EPSILON;
// all other points must be on front side // all other points must be on front side
for (j=0 ; j<w->numpoints ; j++) for (j=0 ; j<w->numpoints ; j++)
{ {

View File

@ -24,7 +24,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
typedef struct typedef struct
{ {
int numpoints; int numpoints;
vec3_t p[4]; //variable sized vec3_t p[];
} winding_t; } winding_t;
#define MAX_POINTS_ON_WINDING 96 #define MAX_POINTS_ON_WINDING 96

View File

@ -154,7 +154,7 @@ typedef struct bspbrush_s
int side, testside; // side of node during construction int side, testside; // side of node during construction
mapbrush_t *original; mapbrush_t *original;
int numsides; int numsides;
side_t sides[6]; // variably sized side_t sides[];
} bspbrush_t; //sizeof(bspbrush_t) = 44 + numsides * sizeof(side_t) } bspbrush_t; //sizeof(bspbrush_t) = 44 + numsides * sizeof(side_t)
//bsp node //bsp node
typedef struct node_s typedef struct node_s