From 8875e2dd806515d836d5e1596e99cef7f462e87b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Sun, 21 Jan 2018 01:32:08 +0000 Subject: [PATCH] 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. --- tools/mbspc/mbspc/brushbsp.c | 13 ++-- tools/mbspc/mbspc/l_poly.c | 120 +++++++++++++++++------------------ tools/mbspc/mbspc/l_poly.h | 2 +- tools/mbspc/mbspc/qbsp.h | 2 +- 4 files changed, 68 insertions(+), 69 deletions(-) diff --git a/tools/mbspc/mbspc/brushbsp.c b/tools/mbspc/mbspc/brushbsp.c index 5eecfa15..7a563fa0 100644 --- a/tools/mbspc/mbspc/brushbsp.c +++ b/tools/mbspc/mbspc/brushbsp.c @@ -26,7 +26,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "aas_store.h" #include "aas_cfg.h" -#include #include /* @@ -42,7 +41,7 @@ evaluate split side cost = 0 for all sides for all sides - get + get if side splits side and splitside is on same child cost++; } @@ -425,7 +424,7 @@ bspbrush_t *AllocBrush (int numsides) bspbrush_t *bb; size_t c; - c = offsetof(bspbrush_t, sides[numsides]); + c = sizeof(*bb) + sizeof(*bb->sides) * numsides; bb = GetMemory(c); memset (bb, 0, c); if (numthreads == 1) @@ -487,8 +486,8 @@ bspbrush_t *CopyBrush (bspbrush_t *brush) bspbrush_t *newbrush; size_t size; int i; - - size = offsetof(bspbrush_t, sides[brush->numsides]); + + size = sizeof(*newbrush) + sizeof(*brush->sides) * brush->numsides; newbrush = AllocBrush (brush->numsides); 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; return sides; } //end if - + // general case switch (p->signbits) { @@ -1339,7 +1338,7 @@ void SplitBrush (bspbrush_t *brush, int planenum, // Returns: - // Changes Globals: - //=========================================================================== -void SplitBrushList (bspbrush_t *brushes, +void SplitBrushList (bspbrush_t *brushes, node_t *node, bspbrush_t **front, bspbrush_t **back) { bspbrush_t *brush, *newbrush, *newbrush2; diff --git a/tools/mbspc/mbspc/l_poly.c b/tools/mbspc/mbspc/l_poly.c index 39e89186..25ee4a9d 100644 --- a/tools/mbspc/mbspc/l_poly.c +++ b/tools/mbspc/mbspc/l_poly.c @@ -71,7 +71,7 @@ winding_t *AllocWinding (int points) winding_t *w; int s; - s = sizeof(vec_t)*3*points + sizeof(int); + s = sizeof(*w) + sizeof(*w->p) * points; w = GetMemory(s); memset(w, 0, s); @@ -157,7 +157,7 @@ void RemoveColinearPoints (winding_t *w) if (numthreads == 1) c_removed += 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; vec3_t org, vright, vup; winding_t *w; - + // find the major axis max = -BOGUS_RANGE; @@ -270,48 +270,48 @@ winding_t *BaseWindingForPlane (vec3_t normal, vec_t dist) } if (x==-1) Error ("BaseWindingForPlane: no axis found"); - - VectorCopy (vec3_origin, vup); + + VectorCopy (vec3_origin, vup); switch (x) { case 0: case 1: vup[2] = 1; - break; + break; case 2: vup[0] = 1; - break; + break; } v = DotProduct (vup, normal); VectorMA (vup, -v, normal, vup); VectorNormalize (vup); - + VectorScale (normal, dist, org); - + CrossProduct (vup, normal, vright); - + VectorScale (vup, BOGUS_RANGE, vup); VectorScale (vright, BOGUS_RANGE, vright); // project a really big axis aligned box onto the plane w = AllocWinding (4); - + VectorSubtract (org, vright, w->p[0]); VectorAdd (w->p[0], vup, w->p[0]); - + VectorAdd (org, vright, w->p[1]); VectorAdd (w->p[1], vup, w->p[1]); - + VectorAdd (org, vright, w->p[2]); VectorSubtract (w->p[2], vup, w->p[2]); - + VectorSubtract (org, vright, w->p[3]); VectorSubtract (w->p[3], vup, w->p[3]); - + w->numpoints = 4; - - return w; + + return w; } /* @@ -325,7 +325,7 @@ winding_t *CopyWinding (winding_t *w) winding_t *c; c = AllocWinding (w->numpoints); - size = (int)((winding_t *)0)->p[w->numpoints]; + size = sizeof(*w) + sizeof(*w->p) * w->numpoints; memcpy (c, w, size); return c; } @@ -355,7 +355,7 @@ winding_t *ReverseWinding (winding_t *w) 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 dists[MAX_POINTS_ON_WINDING+4]; @@ -368,7 +368,7 @@ void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist, vec3_t mid; winding_t *f, *b; int maxpts; - + counts[0] = counts[1] = counts[2] = 0; // determine sides for each point @@ -389,7 +389,7 @@ void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist, } sides[i] = sides[0]; dists[i] = dists[0]; - + *front = *back = NULL; if (!counts[0]) @@ -408,11 +408,11 @@ void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist, *front = f = AllocWinding (maxpts); *back = b = AllocWinding (maxpts); - + for (i=0 ; inumpoints ; i++) { p1 = in->p[i]; - + if (sides[i] == SIDE_ON) { VectorCopy (p1, f->p[f->numpoints]); @@ -421,7 +421,7 @@ void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist, b->numpoints++; continue; } - + if (sides[i] == SIDE_FRONT) { 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]) continue; - + // generate a split point p2 = in->p[(i+1)%in->numpoints]; - + dot = dists[i] / (dists[i]-dists[i+1]); for (j=0 ; j<3 ; j++) { // avoid round off error when possible @@ -449,13 +449,13 @@ void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist, else mid[j] = p1[j] + dot*(p2[j]-p1[j]); } - + VectorCopy (mid, f->p[f->numpoints]); f->numpoints++; VectorCopy (mid, b->p[b->numpoints]); b->numpoints++; } - + if (f->numpoints > maxpts || b->numpoints > maxpts) Error ("ClipWinding: points exceeded estimate"); 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]; dists[i] = dists[0]; - + if (!counts[0]) { FreeWinding (in); @@ -517,18 +517,18 @@ void ChopWindingInPlace (winding_t **inout, vec3_t normal, vec_t dist, vec_t eps // of fp grouping errors f = AllocWinding (maxpts); - + for (i=0 ; inumpoints ; i++) { p1 = in->p[i]; - + if (sides[i] == SIDE_ON) { VectorCopy (p1, f->p[f->numpoints]); f->numpoints++; continue; } - + if (sides[i] == SIDE_FRONT) { 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]) continue; - + // generate a split point p2 = in->p[(i+1)%in->numpoints]; - + dot = dists[i] / (dists[i]-dists[i+1]); for (j=0 ; j<3 ; j++) { // 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 mid[j] = p1[j] + dot*(p2[j]-p1[j]); } - + VectorCopy (mid, f->p[f->numpoints]); f->numpoints++; } - + if (f->numpoints > maxpts) Error ("ClipWinding: points exceeded estimate"); if (f->numpoints > MAX_POINTS_ON_WINDING) @@ -603,13 +603,13 @@ void CheckWinding (winding_t *w) if (w->numpoints < 3) Error ("CheckWinding: %i points",w->numpoints); - + area = WindingArea(w); if (area < 1) Error ("CheckWinding: %f area", area); WindingPlane (w, facenormal, &facedist); - + for (i=0 ; inumpoints ; i++) { p1 = w->p[i]; @@ -619,24 +619,24 @@ void CheckWinding (winding_t *w) Error ("CheckWinding: BUGUS_RANGE: %f",p1[j]); j = i+1 == w->numpoints ? 0 : i+1; - + // check the point is on the face plane d = DotProduct (p1, facenormal) - facedist; if (d < -ON_EPSILON || d > ON_EPSILON) Error ("CheckWinding: point off plane"); - + // check the edge isnt degenerate p2 = w->p[j]; VectorSubtract (p2, p1, dir); - + if (VectorLength (dir) < ON_EPSILON) Error ("CheckWinding: degenerate edge"); - + CrossProduct (facenormal, dir, edgenormal); VectorNormalize (edgenormal); edgedist = DotProduct (p1, edgenormal); edgedist += ON_EPSILON; - + // all other points must be on front side for (j=0 ; jnumpoints ; j++) { @@ -715,14 +715,14 @@ winding_t *TryMergeWinding (winding_t *f1, winding_t *f2, vec3_t planenormal) vec3_t normal, delta; vec_t dot; qboolean keep1, keep2; - + // // find a common edge - // + // p1 = p2 = NULL; // stop compiler warning - j = 0; // - + j = 0; // + for (i = 0; i < f1->numpoints; 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) break; } //end for - + if (i == f1->numpoints) 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); CrossProduct (planenormal, delta, normal); VectorNormalize (normal); - + back = f2->p[(j+2)%f2->numpoints]; VectorSubtract (back, p1, delta); dot = DotProduct (delta, normal); if (dot > CONTINUOUS_EPSILON) return NULL; // not a convex polygon keep1 = (qboolean)(dot < -CONTINUOUS_EPSILON); - + back = f1->p[(i+2)%f1->numpoints]; VectorSubtract (back, p2, delta); CrossProduct (planenormal, delta, normal); @@ -780,17 +780,17 @@ winding_t *TryMergeWinding (winding_t *f1, winding_t *f2, vec3_t planenormal) // build the new polygon // newf = AllocWinding (f1->numpoints + f2->numpoints); - + // copy first polygon for (k=(i+1)%f1->numpoints ; k != i ; k=(k+1)%f1->numpoints) { if (k==(i+1)%f1->numpoints && !keep2) continue; - + VectorCopy (f1->p[k], newf->p[newf->numpoints]); newf->numpoints++; } - + // copy second polygon 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); return WE_NOTENOUGHPOINTS; } //end if - + area = WindingArea(w); if (area < 1) { @@ -945,7 +945,7 @@ int WindingError(winding_t *w) } //end if WindingPlane (w, facenormal, &facedist); - + for (i=0 ; inumpoints ; i++) { p1 = w->p[i]; @@ -960,7 +960,7 @@ int WindingError(winding_t *w) } //end for j = i+1 == w->numpoints ? 0 : i+1; - + // check the point is on the face plane d = DotProduct (p1, facenormal) - facedist; if (d < -ON_EPSILON || d > ON_EPSILON) @@ -968,22 +968,22 @@ int WindingError(winding_t *w) sprintf(windingerror, "winding point %d off plane", i); return WE_POINTOFFPLANE; } //end if - + // check the edge isnt degenerate p2 = w->p[j]; VectorSubtract (p2, p1, dir); - + if (VectorLength (dir) < ON_EPSILON) { sprintf(windingerror, "winding degenerate edge %d-%d", i, j); return WE_DEGENERATEEDGE; } //end if - + CrossProduct (facenormal, dir, edgenormal); VectorNormalize (edgenormal); edgedist = DotProduct (p1, edgenormal); edgedist += ON_EPSILON; - + // all other points must be on front side for (j=0 ; jnumpoints ; j++) { diff --git a/tools/mbspc/mbspc/l_poly.h b/tools/mbspc/mbspc/l_poly.h index f3191eb3..21a3bd1c 100644 --- a/tools/mbspc/mbspc/l_poly.h +++ b/tools/mbspc/mbspc/l_poly.h @@ -24,7 +24,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA typedef struct { int numpoints; - vec3_t p[4]; //variable sized + vec3_t p[]; } winding_t; #define MAX_POINTS_ON_WINDING 96 diff --git a/tools/mbspc/mbspc/qbsp.h b/tools/mbspc/mbspc/qbsp.h index 0310835d..e401dd6c 100644 --- a/tools/mbspc/mbspc/qbsp.h +++ b/tools/mbspc/mbspc/qbsp.h @@ -154,7 +154,7 @@ typedef struct bspbrush_s int side, testside; // side of node during construction mapbrush_t *original; int numsides; - side_t sides[6]; // variably sized + side_t sides[]; } bspbrush_t; //sizeof(bspbrush_t) = 44 + numsides * sizeof(side_t) //bsp node typedef struct node_s