diff --git a/common/bspfile.cc b/common/bspfile.cc index 7d87ead4..8f53712a 100644 --- a/common/bspfile.cc +++ b/common/bspfile.cc @@ -1191,7 +1191,7 @@ static void SwapBSPFile(bspdata_t *bspdata, swaptype_t swap) static dmodelh2_t *BSPQ1toH2_Models(const dmodelq1_t *dmodelsq1, const int nummodels) { const dmodelq1_t *in = dmodelsq1; - dmodelh2_t *out = static_cast(calloc(nummodels, sizeof(dmodelh2_t))); + dmodelh2_t *out = new dmodelh2_t[nummodels]; int i, j; for (i = 0; i < nummodels; i++) { @@ -1214,7 +1214,7 @@ static dmodelh2_t *BSPQ1toH2_Models(const dmodelq1_t *dmodelsq1, const int nummo static dmodelq1_t *BSPH2toQ1_Models(const dmodelh2_t *dmodelsh2, int nummodels) { const dmodelh2_t *in = dmodelsh2; - dmodelq1_t *out = static_cast(calloc(nummodels, sizeof(dmodelq1_t))); + dmodelq1_t *out = new dmodelq1_t[nummodels]; int i, j; for (i = 0; i < nummodels; i++) { @@ -1238,7 +1238,7 @@ static mleaf_t *BSP29toM_Leafs(const bsp29_dleaf_t *dleafs29, int numleafs) mleaf_t *newdata, *mleaf; int i, j; - newdata = mleaf = (mleaf_t *)calloc(numleafs, sizeof(*mleaf)); + newdata = mleaf = new mleaf_t[numleafs]; for (i = 0; i < numleafs; i++, dleaf29++, mleaf++) { mleaf->contents = dleaf29->contents; @@ -1315,7 +1315,7 @@ static bsp29_dleaf_t *MBSPto29_Leafs(const mleaf_t *mleafs, int numleafs) bsp29_dleaf_t *newdata, *dleaf29; int i, j; - newdata = dleaf29 = (bsp29_dleaf_t *)calloc(numleafs, sizeof(*dleaf29)); + newdata = dleaf29 = new bsp29_dleaf_t[numleafs]; for (i = 0; i < numleafs; i++, mleaf++, dleaf29++) { dleaf29->contents = mleaf->contents; @@ -1339,7 +1339,7 @@ static gtexinfo_t *BSP29toM_Texinfo(const texinfo_t *texinfos, int numtexinfo) gtexinfo_t *newdata, *mtexinfo; int i, j, k; - newdata = mtexinfo = (gtexinfo_t *)calloc(numtexinfo, sizeof(*mtexinfo)); + newdata = mtexinfo = new gtexinfo_t[numtexinfo]; for (i = 0; i < numtexinfo; i++, texinfo29++, mtexinfo++) { for (j = 0; j < 2; j++) @@ -1358,7 +1358,7 @@ static texinfo_t *MBSPto29_Texinfo(const gtexinfo_t *mtexinfos, int numtexinfo) texinfo_t *newdata, *texinfo29; int i, j, k; - newdata = texinfo29 = (texinfo_t *)calloc(numtexinfo, sizeof(*texinfo29)); + newdata = texinfo29 = new texinfo_t[numtexinfo]; for (i = 0; i < numtexinfo; i++, texinfo29++, mtexinfo++) { for (j = 0; j < 2; j++) @@ -1383,7 +1383,7 @@ static dmodelh2_t *Q2BSPtoM_Models(const q2_dmodel_t *dmodelsq2, int nummodels) dmodelh2_t *newdata, *dmodelh2; int i, j; - newdata = dmodelh2 = (dmodelh2_t *)calloc(nummodels, sizeof(*dmodelh2)); + newdata = dmodelh2 = new dmodelh2_t[nummodels]; for (i = 0; i < nummodels; i++, dmodelq2++, dmodelh2++) { for (j = 0; j < 3; j++) { @@ -1402,7 +1402,6 @@ static dmodelh2_t *Q2BSPtoM_Models(const q2_dmodel_t *dmodelsq2, int nummodels) static uint8_t *Q2BSPtoM_CopyVisData(const dvis_t *dvisq2, int vissize, int *outvissize, mleaf_t *leafs, int numleafs) { - if (!*outvissize) { return nullptr; } @@ -1425,7 +1424,7 @@ static uint8_t *Q2BSPtoM_CopyVisData(const dvis_t *dvisq2, int vissize, int *out // cut off the PHS and header *outvissize -= header_offset + ((*outvissize - header_offset) - phs_start); - uint8_t *vis = (uint8_t *)calloc(1, *outvissize); + uint8_t *vis = new uint8_t[*outvissize]; memcpy(vis, ((uint8_t *)dvisq2) + pvs_start, *outvissize); return vis; } @@ -1436,7 +1435,7 @@ static q2_dmodel_t *MBSPtoQ2_Models(const dmodelh2_t *dmodelsh2, int nummodels) q2_dmodel_t *newdata, *dmodelq2; int i, j; - newdata = dmodelq2 = (q2_dmodel_t *)calloc(nummodels, sizeof(*dmodelq2)); + newdata = dmodelq2 = new q2_dmodel_t[nummodels]; for (i = 0; i < nummodels; i++, dmodelh2++, dmodelq2++) { for (j = 0; j < 3; j++) { @@ -1466,10 +1465,11 @@ static std::vector CalcPHS( const int32_t leafbytes = (portalclusters + 7) >> 3; const int32_t leaflongs = leafbytes / sizeof(long); std::vector compressed_phs; - uint8_t *uncompressed = (uint8_t *)calloc(1, leafbytes); - uint8_t *uncompressed_2 = (uint8_t *)calloc(1, leafbytes); - uint8_t *compressed = (uint8_t *)calloc(1, leafbytes * 2); - uint8_t *uncompressed_orig = (uint8_t *)calloc(1, leafbytes); + // FIXME: should this use alloca? + uint8_t *uncompressed = new uint8_t[leafbytes]; + uint8_t *uncompressed_2 = new uint8_t[leafbytes]; + uint8_t *compressed = new uint8_t[leafbytes * 2]; + uint8_t *uncompressed_orig = new uint8_t[leafbytes]; printf("Building PHS...\n"); @@ -1516,10 +1516,10 @@ static std::vector CalcPHS( compressed_phs.insert(compressed_phs.end(), compressed, compressed + j); } - free(uncompressed); - free(uncompressed_2); - free(compressed); - free(uncompressed_orig); + delete[] uncompressed; + delete[] uncompressed_2; + delete[] compressed; + delete[] uncompressed_orig; printf("Average clusters hearable: %i\n", count / portalclusters); @@ -1581,7 +1581,7 @@ static mleaf_t *Q2BSPtoM_Leafs(const q2_dleaf_t *dleafsq2, int numleafs) mleaf_t *newdata, *mleaf; int i, j; - newdata = mleaf = (mleaf_t *)calloc(numleafs, sizeof(*mleaf)); + newdata = mleaf = new mleaf_t[numleafs]; for (i = 0; i < numleafs; i++, dleafq2++, mleaf++) { mleaf->contents = dleafq2->contents; @@ -1608,7 +1608,7 @@ static mleaf_t *Q2BSP_QBSPtoM_Leafs(const q2_dleaf_qbism_t *dleafsq2, int numlea mleaf_t *newdata, *mleaf; int i, j; - newdata = mleaf = (mleaf_t *)calloc(numleafs, sizeof(*mleaf)); + newdata = mleaf = new mleaf_t[numleafs]; for (i = 0; i < numleafs; i++, dleafq2++, mleaf++) { mleaf->contents = dleafq2->contents; @@ -1635,7 +1635,7 @@ static q2_dleaf_t *MBSPtoQ2_Leafs(const mleaf_t *mleafs, int numleafs) q2_dleaf_t *newdata, *dleafq2; int i, j; - newdata = dleafq2 = (q2_dleaf_t *)calloc(numleafs, sizeof(*dleafq2)); + newdata = dleafq2 = new q2_dleaf_t[numleafs]; for (i = 0; i < numleafs; i++, mleaf++, dleafq2++) { dleafq2->contents = mleaf->contents; @@ -1662,7 +1662,7 @@ static q2_dleaf_qbism_t *MBSPtoQ2_Qbism_Leafs(const mleaf_t *mleafs, int numleaf q2_dleaf_qbism_t *newdata, *dleafq2; int i, j; - newdata = dleafq2 = (q2_dleaf_qbism_t *)calloc(numleafs, sizeof(*dleafq2)); + newdata = dleafq2 = new q2_dleaf_qbism_t[numleafs]; for (i = 0; i < numleafs; i++, mleaf++, dleafq2++) { dleafq2->contents = mleaf->contents; @@ -1689,7 +1689,7 @@ static bsp2_dnode_t *Q2BSPto2_Nodes(const q2_dnode_t *dnodesq2, int numnodes) bsp2_dnode_t *newdata, *dnode2; int i, j; - newdata = dnode2 = static_cast(malloc(numnodes * sizeof(*dnode2))); + newdata = dnode2 = new bsp2_dnode_t[numnodes]; for (i = 0; i < numnodes; i++, dnodeq2++, dnode2++) { dnode2->planenum = dnodeq2->planenum; @@ -1712,7 +1712,7 @@ static q2_dnode_t *BSP2toQ2_Nodes(const bsp2_dnode_t *dnodes2, int numnodes) q2_dnode_t *newdata, *dnodeq2; int i, j; - newdata = dnodeq2 = static_cast(malloc(numnodes * sizeof(*dnodeq2))); + newdata = dnodeq2 = new q2_dnode_t[numnodes]; for (i = 0; i < numnodes; i++, dnode2++, dnodeq2++) { dnodeq2->planenum = dnode2->planenum; @@ -1735,7 +1735,7 @@ static bsp2_dface_t *Q2BSPto2_Faces(const q2_dface_t *dfacesq2, int numfaces) bsp2_dface_t *newdata, *dface2; int i, j; - newdata = dface2 = static_cast(malloc(numfaces * sizeof(*dface2))); + newdata = dface2 = new bsp2_dface_t[numfaces]; for (i = 0; i < numfaces; i++, dfaceq2++, dface2++) { dface2->planenum = dfaceq2->planenum; @@ -1757,7 +1757,7 @@ static bsp2_dface_t *Q2BSP_QBSPto2_Faces(const q2_dface_qbism_t *dfacesq2, int n bsp2_dface_t *newdata, *dface2; int i, j; - newdata = dface2 = static_cast(malloc(numfaces * sizeof(*dface2))); + newdata = dface2 = new bsp2_dface_t[numfaces]; for (i = 0; i < numfaces; i++, dfaceq2++, dface2++) { dface2->planenum = dfaceq2->planenum; @@ -1779,7 +1779,7 @@ static q2_dface_t *BSP2toQ2_Faces(const bsp2_dface_t *dfaces2, int numfaces) q2_dface_t *newdata, *dfaceq2; int i, j; - newdata = dfaceq2 = static_cast(malloc(numfaces * sizeof(*dfaceq2))); + newdata = dfaceq2 = new q2_dface_t[numfaces]; for (i = 0; i < numfaces; i++, dface2++, dfaceq2++) { dfaceq2->planenum = dface2->planenum; @@ -1801,7 +1801,7 @@ static q2_dface_qbism_t *BSP2toQ2_Qbism_Faces(const bsp2_dface_t *dfaces2, int n q2_dface_qbism_t *newdata, *dfaceq2; int i, j; - newdata = dfaceq2 = static_cast(malloc(numfaces * sizeof(*dfaceq2))); + newdata = dfaceq2 = new q2_dface_qbism_t[numfaces]; for (i = 0; i < numfaces; i++, dface2++, dfaceq2++) { dfaceq2->planenum = dface2->planenum; @@ -1823,7 +1823,7 @@ static gtexinfo_t *Q2BSPtoM_Texinfo(const q2_texinfo_t *dtexinfosq2, int numtexi gtexinfo_t *newdata, *dtexinfo2; int i, j, k; - newdata = dtexinfo2 = static_cast(malloc(numtexinfos * sizeof(*dtexinfo2))); + newdata = dtexinfo2 = new gtexinfo_t[numtexinfos]; for (i = 0; i < numtexinfos; i++, dtexinfoq2++, dtexinfo2++) { for (j = 0; j < 2; j++) @@ -1844,7 +1844,7 @@ static q2_texinfo_t *MBSPtoQ2_Texinfo(const gtexinfo_t *dtexinfos2, int numtexin q2_texinfo_t *newdata, *dtexinfoq2; int i, j, k; - newdata = dtexinfoq2 = static_cast(malloc(numtexinfos * sizeof(*dtexinfoq2))); + newdata = dtexinfoq2 = new q2_texinfo_t[numtexinfos]; for (i = 0; i < numtexinfos; i++, dtexinfo2++, dtexinfoq2++) { for (j = 0; j < 2; j++) @@ -1871,7 +1871,7 @@ static mleaf_t *BSP2rmqtoM_Leafs(const bsp2rmq_dleaf_t *dleafs2rmq, int numleafs mleaf_t *newdata, *mleaf; int i, j; - newdata = mleaf = (mleaf_t *)calloc(numleafs, sizeof(*mleaf)); + newdata = mleaf = new mleaf_t[numleafs]; for (i = 0; i < numleafs; i++, dleaf2rmq++, mleaf++) { mleaf->contents = dleaf2rmq->contents; @@ -1895,7 +1895,7 @@ static bsp2rmq_dleaf_t *MBSPto2rmq_Leafs(const mleaf_t *mleafs, int numleafs) bsp2rmq_dleaf_t *newdata, *dleaf2rmq; int i, j; - newdata = dleaf2rmq = (bsp2rmq_dleaf_t *)calloc(numleafs, sizeof(*dleaf2rmq)); + newdata = dleaf2rmq = new bsp2rmq_dleaf_t[numleafs]; for (i = 0; i < numleafs; i++, mleaf++, dleaf2rmq++) { dleaf2rmq->contents = mleaf->contents; @@ -1925,7 +1925,7 @@ static mleaf_t *BSP2toM_Leafs(const bsp2_dleaf_t *dleafs2, int numleafs) mleaf_t *newdata, *mleaf; int i, j; - newdata = mleaf = (mleaf_t *)calloc(numleafs, sizeof(*mleaf)); + newdata = mleaf = new mleaf_t[numleafs]; for (i = 0; i < numleafs; i++, dleaf2++, mleaf++) { mleaf->contents = dleaf2->contents; @@ -1949,7 +1949,7 @@ static bsp2_dleaf_t *MBSPto2_Leafs(const mleaf_t *mleafs, int numleafs) bsp2_dleaf_t *newdata, *dleaf2; int i, j; - newdata = dleaf2 = (bsp2_dleaf_t *)calloc(numleafs, sizeof(*dleaf2)); + newdata = dleaf2 = new bsp2_dleaf_t[numleafs]; for (i = 0; i < numleafs; i++, mleaf++, dleaf2++) { dleaf2->contents = mleaf->contents; @@ -1979,7 +1979,7 @@ static bsp2_dnode_t *BSP29to2_Nodes(const bsp29_dnode_t *dnodes29, int numnodes) bsp2_dnode_t *newdata, *dnode2; int i, j; - newdata = dnode2 = static_cast(malloc(numnodes * sizeof(*dnode2))); + newdata = dnode2 = new bsp2_dnode_t[numnodes]; for (i = 0; i < numnodes; i++, dnode29++, dnode2++) { dnode2->planenum = dnode29->planenum; @@ -2026,7 +2026,7 @@ static bsp29_dnode_t *BSP2to29_Nodes(const bsp2_dnode_t *dnodes2, int numnodes) bsp29_dnode_t *newdata, *dnode29; int i, j; - newdata = dnode29 = static_cast(malloc(numnodes * sizeof(*dnode29))); + newdata = dnode29 = new bsp29_dnode_t[numnodes]; for (i = 0; i < numnodes; i++, dnode2++, dnode29++) { dnode29->planenum = dnode2->planenum; @@ -2049,7 +2049,7 @@ static bsp2_dface_t *BSP29to2_Faces(const bsp29_dface_t *dfaces29, int numfaces) bsp2_dface_t *newdata, *dface2; int i, j; - newdata = dface2 = static_cast(malloc(numfaces * sizeof(*dface2))); + newdata = dface2 = new bsp2_dface_t[numfaces]; for (i = 0; i < numfaces; i++, dface29++, dface2++) { dface2->planenum = dface29->planenum; @@ -2093,7 +2093,7 @@ static bsp29_dface_t *BSP2to29_Faces(const bsp2_dface_t *dfaces2, int numfaces) bsp29_dface_t *newdata, *dface29; int i, j; - newdata = dface29 = static_cast(malloc(numfaces * sizeof(*dface29))); + newdata = dface29 = new bsp29_dface_t[numfaces]; for (i = 0; i < numfaces; i++, dface2++, dface29++) { dface29->planenum = dface2->planenum; @@ -2115,7 +2115,7 @@ static bsp2_dclipnode_t *BSP29to2_Clipnodes(const bsp29_dclipnode_t *dclipnodes2 bsp2_dclipnode_t *newdata, *dclipnode2; int i, j; - newdata = dclipnode2 = static_cast(malloc(numclipnodes * sizeof(*dclipnode2))); + newdata = dclipnode2 = new bsp2_dclipnode_t[numclipnodes]; for (i = 0; i < numclipnodes; i++, dclipnode29++, dclipnode2++) { dclipnode2->planenum = dclipnode29->planenum; @@ -2152,7 +2152,7 @@ static bsp29_dclipnode_t *BSP2to29_Clipnodes(const bsp2_dclipnode_t *dclipnodes2 bsp29_dclipnode_t *newdata, *dclipnode29; int i, j; - newdata = dclipnode29 = static_cast(malloc(numclipnodes * sizeof(*dclipnode29))); + newdata = dclipnode29 = new bsp29_dclipnode_t[numclipnodes]; for (i = 0; i < numclipnodes; i++, dclipnode2++, dclipnode29++) { dclipnode29->planenum = dclipnode2->planenum; @@ -2172,7 +2172,7 @@ static bsp2_dedge_t *BSP29to2_Edges(const bsp29_dedge_t *dedges29, int numedges) bsp2_dedge_t *newdata, *dedge2; int i; - newdata = dedge2 = static_cast(malloc(numedges * sizeof(*dedge2))); + newdata = dedge2 = new bsp2_dedge_t[numedges]; for (i = 0; i < numedges; i++, dedge29++, dedge2++) { dedge2->v[0] = dedge29->v[0]; @@ -2201,7 +2201,7 @@ static bsp29_dedge_t *BSP2to29_Edges(const bsp2_dedge_t *dedges2, int numedges) bsp29_dedge_t *newdata, *dedge29; int i; - newdata = dedge29 = static_cast(malloc(numedges * sizeof(*dedge29))); + newdata = dedge29 = new bsp29_dedge_t[numedges]; for (i = 0; i < numedges; i++, dedge2++, dedge29++) { dedge29->v[0] = dedge2->v[0]; @@ -2217,7 +2217,7 @@ static uint32_t *BSP29to2_Marksurfaces(const uint16_t *dmarksurfaces29, int numm uint32_t *newdata, *dmarksurface2; int i; - newdata = dmarksurface2 = static_cast(malloc(nummarksurfaces * sizeof(*dmarksurface2))); + newdata = dmarksurface2 = new uint32_t[nummarksurfaces]; for (i = 0; i < nummarksurfaces; i++, dmarksurface29++, dmarksurface2++) *dmarksurface2 = *dmarksurface29; @@ -2244,7 +2244,7 @@ static uint16_t *BSP2to29_Marksurfaces(const uint32_t *dmarksurfaces2, int numma uint16_t *newdata, *dmarksurface29; int i; - newdata = dmarksurface29 = static_cast(malloc(nummarksurfaces * sizeof(*dmarksurface29))); + newdata = dmarksurface29 = new uint16_t[nummarksurfaces]; for (i = 0; i < nummarksurfaces; i++, dmarksurface2++, dmarksurface29++) *dmarksurface29 = *dmarksurface2; @@ -2264,7 +2264,7 @@ static bsp2_dnode_t *BSP2rmqto2_Nodes(const bsp2rmq_dnode_t *dnodes2rmq, int num bsp2_dnode_t *newdata, *dnode2; int i, j; - newdata = dnode2 = static_cast(malloc(numnodes * sizeof(*dnode2))); + newdata = dnode2 = new bsp2_dnode_t[numnodes]; for (i = 0; i < numnodes; i++, dnode2rmq++, dnode2++) { dnode2->planenum = dnode2rmq->planenum; @@ -2287,7 +2287,7 @@ static bsp2rmq_dnode_t *BSP2to2rmq_Nodes(const bsp2_dnode_t *dnodes2, int numnod bsp2rmq_dnode_t *newdata, *dnode2rmq; int i, j; - newdata = dnode2rmq = static_cast(malloc(numnodes * sizeof(*dnode2rmq))); + newdata = dnode2rmq = new bsp2rmq_dnode_t[numnodes]; for (i = 0; i < numnodes; i++, dnode2++, dnode2rmq++) { dnode2rmq->planenum = dnode2->planenum; @@ -2310,129 +2310,130 @@ static bsp2rmq_dnode_t *BSP2to2rmq_Nodes(const bsp2_dnode_t *dnodes2, int numnod * ========================================================================= */ -static void *CopyArray(const void *in, int numelems, size_t elemsize) +template +inline T *CopyArray(const T *in, int numelems) { - void *out = (void *)calloc(numelems, elemsize); - memcpy(out, in, numelems * elemsize); + T *out = new T[numelems]; + + if constexpr(std::is_trivially_copyable_v) + memcpy(out, in, sizeof(T) * numelems); + else + for (int i = 0; i < numelems; i++) + out[i] = in[i]; + + return out; +} + +// TODO: if T and F are scalar values, overflow should be checked. +template> +inline T *CopyArray(const F *in, int numelems) +{ + T *out = new T[numelems]; + + for (int i = 0; i < numelems; i++) + out[i] = static_cast(in[i]); + return out; } static dmodelh2_t *H2_CopyModels(const dmodelh2_t *dmodels, int nummodels) { - return (dmodelh2_t *)CopyArray(dmodels, nummodels, sizeof(*dmodels)); + return CopyArray(dmodels, nummodels); } static uint8_t *BSP29_CopyVisData(const uint8_t *dvisdata, int visdatasize) { - return (uint8_t *)CopyArray(dvisdata, visdatasize, 1); + return CopyArray(dvisdata, visdatasize); } static uint8_t *BSP29_CopyLightData(const uint8_t *dlightdata, int lightdatasize) { - return (uint8_t *)CopyArray(dlightdata, lightdatasize, 1); + return CopyArray(dlightdata, lightdatasize); } static dmiptexlump_t *BSP29_CopyTexData(const dmiptexlump_t *dtexdata, int texdatasize) { - return (dmiptexlump_t *)CopyArray(dtexdata, texdatasize, 1); + return CopyArray(dtexdata, texdatasize); } static char *BSP29_CopyEntData(const char *dentdata, int entdatasize) { - return (char *)CopyArray(dentdata, entdatasize, 1); + return CopyArray(dentdata, entdatasize); } static dplane_t *BSP29_CopyPlanes(const dplane_t *dplanes, int numplanes) { - return (dplane_t *)CopyArray(dplanes, numplanes, sizeof(*dplanes)); + return CopyArray(dplanes, numplanes); } static dvertex_t *BSP29_CopyVertexes(const dvertex_t *dvertexes, int numvertexes) { - return (dvertex_t *)CopyArray(dvertexes, numvertexes, sizeof(*dvertexes)); + return CopyArray(dvertexes, numvertexes); } static texinfo_t *BSP29_CopyTexinfo(const texinfo_t *texinfo, int numtexinfo) { - return (texinfo_t *)CopyArray(texinfo, numtexinfo, sizeof(*texinfo)); + return CopyArray(texinfo, numtexinfo); } static int32_t *BSP29_CopySurfedges(const int32_t *surfedges, int numsurfedges) { - return (int32_t *)CopyArray(surfedges, numsurfedges, sizeof(*surfedges)); + return CopyArray(surfedges, numsurfedges); } static bsp2_dface_t *BSP2_CopyFaces(const bsp2_dface_t *dfaces, int numfaces) { - return (bsp2_dface_t *)CopyArray(dfaces, numfaces, sizeof(*dfaces)); + return CopyArray(dfaces, numfaces); } static bsp2_dclipnode_t *BSP2_CopyClipnodes(const bsp2_dclipnode_t *dclipnodes, int numclipnodes) { - return (bsp2_dclipnode_t *)CopyArray(dclipnodes, numclipnodes, sizeof(*dclipnodes)); + return CopyArray(dclipnodes, numclipnodes); } static bsp2_dedge_t *BSP2_CopyEdges(const bsp2_dedge_t *dedges, int numedges) { - return (bsp2_dedge_t *)CopyArray(dedges, numedges, sizeof(*dedges)); + return CopyArray(dedges, numedges); } static uint32_t *BSP2_CopyMarksurfaces(const uint32_t *marksurfaces, int nummarksurfaces) { - return (uint32_t *)CopyArray(marksurfaces, nummarksurfaces, sizeof(*marksurfaces)); + return CopyArray(marksurfaces, nummarksurfaces); } static bsp2_dnode_t *BSP2_CopyNodes(const bsp2_dnode_t *dnodes, int numnodes) { - return (bsp2_dnode_t *)CopyArray(dnodes, numnodes, sizeof(*dnodes)); + return CopyArray(dnodes, numnodes); } static uint32_t *Q2BSPtoM_CopyLeafBrushes(const uint16_t *leafbrushes, int count) { - const uint16_t *leafbrush = leafbrushes; - uint32_t *newdata, *leafbrushes2; - int i; - - newdata = leafbrushes2 = static_cast(malloc(count * sizeof(*leafbrushes2))); - - for (i = 0; i < count; i++, leafbrush++, leafbrushes2++) - *leafbrushes2 = *leafbrush; - - return newdata; + return CopyArray(leafbrushes, count); } static uint16_t *MBSPtoQ2_CopyLeafBrushes(const uint32_t *leafbrushes, int count) { - const uint32_t *leafbrush = leafbrushes; - uint16_t *newdata, *leafbrushes2; - int i; - - newdata = leafbrushes2 = static_cast(malloc(count * sizeof(*leafbrushes2))); - - for (i = 0; i < count; i++, leafbrush++, leafbrushes2++) - *leafbrushes2 = *leafbrush; - - return newdata; + return CopyArray(leafbrushes, count); } static uint32_t *Q2BSP_Qbism_CopyLeafBrushes(const uint32_t *leafbrushes, int count) { - return (uint32_t *)CopyArray(leafbrushes, count, sizeof(*leafbrushes)); + return CopyArray(leafbrushes, count); } static darea_t *Q2BSP_CopyAreas(const darea_t *areas, int count) { - return (darea_t *)CopyArray(areas, count, sizeof(*areas)); + return CopyArray(areas, count); } static dareaportal_t *Q2BSP_CopyAreaPortals(const dareaportal_t *areaportals, int count) { - return (dareaportal_t *)CopyArray(areaportals, count, sizeof(*areaportals)); + return CopyArray(areaportals, count); } static dbrush_t *Q2BSP_CopyBrushes(const dbrush_t *brushes, int count) { - return (dbrush_t *)CopyArray(brushes, count, sizeof(*brushes)); + return CopyArray(brushes, count); } static q2_dbrushside_qbism_t *Q2BSPtoM_CopyBrushSides(const dbrushside_t *dbrushsides, int count) @@ -2441,7 +2442,7 @@ static q2_dbrushside_qbism_t *Q2BSPtoM_CopyBrushSides(const dbrushside_t *dbrush q2_dbrushside_qbism_t *newdata, *brushsides2; int i; - newdata = brushsides2 = static_cast(malloc(count * sizeof(*brushsides2))); + newdata = brushsides2 = new q2_dbrushside_qbism_t[count]; for (i = 0; i < count; i++, brushside++, brushsides2++) { brushsides2->planenum = brushside->planenum; @@ -2453,7 +2454,7 @@ static q2_dbrushside_qbism_t *Q2BSPtoM_CopyBrushSides(const dbrushside_t *dbrush static q2_dbrushside_qbism_t *Q2BSP_Qbism_CopyBrushSides(const q2_dbrushside_qbism_t *brushsides, int count) { - return (q2_dbrushside_qbism_t *)CopyArray(brushsides, count, sizeof(*brushsides)); + return CopyArray(brushsides, count); } static dbrushside_t *MBSPtoQ2_CopyBrushSides(const q2_dbrushside_qbism_t *dbrushsides, int count) @@ -2462,7 +2463,7 @@ static dbrushside_t *MBSPtoQ2_CopyBrushSides(const q2_dbrushside_qbism_t *dbrush dbrushside_t *newdata, *brushsides2; int i; - newdata = brushsides2 = static_cast(malloc(count * sizeof(*brushsides2))); + newdata = brushsides2 = new dbrushside_t[count]; for (i = 0; i < count; i++, brushside++, brushsides2++) { brushsides2->planenum = brushside->planenum; @@ -2480,136 +2481,136 @@ static dbrushside_t *MBSPtoQ2_CopyBrushSides(const q2_dbrushside_qbism_t *dbrush static void FreeBSP29(bsp29_t *bsp) { - free(bsp->dmodels_q); - free(bsp->dmodels_h2); - free(bsp->dvisdata); - free(bsp->dlightdata); - free(bsp->dtexdata); - free(bsp->dentdata); - free(bsp->dleafs); - free(bsp->dplanes); - free(bsp->dvertexes); - free(bsp->dnodes); - free(bsp->texinfo); - free(bsp->dfaces); - free(bsp->dclipnodes); - free(bsp->dedges); - free(bsp->dmarksurfaces); - free(bsp->dsurfedges); + delete[] bsp->dmodels_q; + delete[] bsp->dmodels_h2; + delete[] bsp->dvisdata; + delete[] bsp->dlightdata; + delete[] bsp->dtexdata; + delete[] bsp->dentdata; + delete[] bsp->dleafs; + delete[] bsp->dplanes; + delete[] bsp->dvertexes; + delete[] bsp->dnodes; + delete[] bsp->texinfo; + delete[] bsp->dfaces; + delete[] bsp->dclipnodes; + delete[] bsp->dedges; + delete[] bsp->dmarksurfaces; + delete[] bsp->dsurfedges; memset(bsp, 0, sizeof(*bsp)); } static void FreeBSP2RMQ(bsp2rmq_t *bsp) { - free(bsp->dmodels_q); - free(bsp->dmodels_h2); - free(bsp->dvisdata); - free(bsp->dlightdata); - free(bsp->dtexdata); - free(bsp->dentdata); - free(bsp->dleafs); - free(bsp->dplanes); - free(bsp->dvertexes); - free(bsp->dnodes); - free(bsp->texinfo); - free(bsp->dfaces); - free(bsp->dclipnodes); - free(bsp->dedges); - free(bsp->dmarksurfaces); - free(bsp->dsurfedges); + delete[] bsp->dmodels_q; + delete[] bsp->dmodels_h2; + delete[] bsp->dvisdata; + delete[] bsp->dlightdata; + delete[] bsp->dtexdata; + delete[] bsp->dentdata; + delete[] bsp->dleafs; + delete[] bsp->dplanes; + delete[] bsp->dvertexes; + delete[] bsp->dnodes; + delete[] bsp->texinfo; + delete[] bsp->dfaces; + delete[] bsp->dclipnodes; + delete[] bsp->dedges; + delete[] bsp->dmarksurfaces; + delete[] bsp->dsurfedges; memset(bsp, 0, sizeof(*bsp)); } static void FreeBSP2(bsp2_t *bsp) { - free(bsp->dmodels_q); - free(bsp->dmodels_h2); - free(bsp->dvisdata); - free(bsp->dlightdata); - free(bsp->dtexdata); - free(bsp->dentdata); - free(bsp->dleafs); - free(bsp->dplanes); - free(bsp->dvertexes); - free(bsp->dnodes); - free(bsp->texinfo); - free(bsp->dfaces); - free(bsp->dclipnodes); - free(bsp->dedges); - free(bsp->dmarksurfaces); - free(bsp->dsurfedges); + delete[] bsp->dmodels_q; + delete[] bsp->dmodels_h2; + delete[] bsp->dvisdata; + delete[] bsp->dlightdata; + delete[] bsp->dtexdata; + delete[] bsp->dentdata; + delete[] bsp->dleafs; + delete[] bsp->dplanes; + delete[] bsp->dvertexes; + delete[] bsp->dnodes; + delete[] bsp->texinfo; + delete[] bsp->dfaces; + delete[] bsp->dclipnodes; + delete[] bsp->dedges; + delete[] bsp->dmarksurfaces; + delete[] bsp->dsurfedges; memset(bsp, 0, sizeof(*bsp)); } static void FreeQ2BSP(q2bsp_t *bsp) { - free(bsp->dmodels); - free(bsp->dvis); - free(bsp->dlightdata); - free(bsp->dentdata); - free(bsp->dleafs); - free(bsp->dplanes); - free(bsp->dvertexes); - free(bsp->dnodes); - free(bsp->texinfo); - free(bsp->dfaces); - free(bsp->dedges); - free(bsp->dleaffaces); - free(bsp->dleafbrushes); - free(bsp->dsurfedges); - free(bsp->dareas); - free(bsp->dareaportals); - free(bsp->dbrushes); - free(bsp->dbrushsides); + delete[] bsp->dmodels; + delete[] bsp->dvis; + delete[] bsp->dlightdata; + delete[] bsp->dentdata; + delete[] bsp->dleafs; + delete[] bsp->dplanes; + delete[] bsp->dvertexes; + delete[] bsp->dnodes; + delete[] bsp->texinfo; + delete[] bsp->dfaces; + delete[] bsp->dedges; + delete[] bsp->dleaffaces; + delete[] bsp->dleafbrushes; + delete[] bsp->dsurfedges; + delete[] bsp->dareas; + delete[] bsp->dareaportals; + delete[] bsp->dbrushes; + delete[] bsp->dbrushsides; memset(bsp, 0, sizeof(*bsp)); } static void FreeQ2BSP_QBSP(q2bsp_qbism_t *bsp) { - free(bsp->dmodels); - free(bsp->dvis); - free(bsp->dlightdata); - free(bsp->dentdata); - free(bsp->dleafs); - free(bsp->dplanes); - free(bsp->dvertexes); - free(bsp->dnodes); - free(bsp->texinfo); - free(bsp->dfaces); - free(bsp->dedges); - free(bsp->dleaffaces); - free(bsp->dleafbrushes); - free(bsp->dsurfedges); - free(bsp->dareas); - free(bsp->dareaportals); - free(bsp->dbrushes); - free(bsp->dbrushsides); + delete[] bsp->dmodels; + delete[] bsp->dvis; + delete[] bsp->dlightdata; + delete[] bsp->dentdata; + delete[] bsp->dleafs; + delete[] bsp->dplanes; + delete[] bsp->dvertexes; + delete[] bsp->dnodes; + delete[] bsp->texinfo; + delete[] bsp->dfaces; + delete[] bsp->dedges; + delete[] bsp->dleaffaces; + delete[] bsp->dleafbrushes; + delete[] bsp->dsurfedges; + delete[] bsp->dareas; + delete[] bsp->dareaportals; + delete[] bsp->dbrushes; + delete[] bsp->dbrushsides; memset(bsp, 0, sizeof(*bsp)); } static void FreeMBSP(mbsp_t *bsp) { - free(bsp->dmodels); - free(bsp->dvisdata); - free(bsp->dlightdata); - free(bsp->dtexdata); - free(bsp->drgbatexdata); // mxd - free(bsp->dentdata); - free(bsp->dleafs); - free(bsp->dplanes); - free(bsp->dvertexes); - free(bsp->dnodes); - free(bsp->texinfo); - free(bsp->dfaces); - free(bsp->dclipnodes); - free(bsp->dedges); - free(bsp->dleaffaces); - free(bsp->dleafbrushes); - free(bsp->dsurfedges); - free(bsp->dareas); - free(bsp->dareaportals); - free(bsp->dbrushes); - free(bsp->dbrushsides); + delete[] bsp->dmodels; + delete[] bsp->dvisdata; + delete[] bsp->dlightdata; + delete[] bsp->dtexdata; + delete[] bsp->drgbatexdata; // mxd + delete[] bsp->dentdata; + delete[] bsp->dleafs; + delete[] bsp->dplanes; + delete[] bsp->dvertexes; + delete[] bsp->dnodes; + delete[] bsp->texinfo; + delete[] bsp->dfaces; + delete[] bsp->dclipnodes; + delete[] bsp->dedges; + delete[] bsp->dleaffaces; + delete[] bsp->dleafbrushes; + delete[] bsp->dsurfedges; + delete[] bsp->dareas; + delete[] bsp->dareaportals; + delete[] bsp->dbrushes; + delete[] bsp->dbrushsides; memset(bsp, 0, sizeof(*bsp)); } @@ -3413,34 +3414,36 @@ static const lumpspec_t *LumpspecsForVersion(const bspversion_t *version) return lumpspec; } -static int CopyLump(const void *header, const bspversion_t *version, const lump_t *lumps, int lumpnum, void *destptr) +template +static int CopyLump(const void *header, const bspversion_t *version, const lump_t *lumps, int lumpnum, T **bufferptr) { const lumpspec_t *lumpspecs = LumpspecsForVersion(version); const lumpspec_t *lumpspec = &lumpspecs[lumpnum]; - uint8_t **bufferptr = static_cast(destptr); - uint8_t *buffer = *bufferptr; - int length; - int ofs; + int length = lumps[lumpnum].filelen; + int ofs = lumps[lumpnum].fileofs; - length = lumps[lumpnum].filelen; - ofs = lumps[lumpnum].fileofs; + if (*bufferptr) + delete[] *bufferptr; - if (buffer) - free(buffer); + if (sizeof(T) != lumpspec->size || length % lumpspec->size) + Error("%s: odd %s lump size", __func__, lumpspec->name); - { - if (length % lumpspec->size) - Error("%s: odd %s lump size", __func__, lumpspec->name); + T *buffer; - buffer = *bufferptr = static_cast(malloc(length + 1)); - if (!buffer) - Error("%s: allocation of %i bytes failed.", __func__, length); + if constexpr(std::is_same_v) + buffer = *bufferptr = new T[length + 1]; + else + buffer = *bufferptr = new T[length]; - memcpy(buffer, (const uint8_t *)header + ofs, length); + if (!buffer) + Error("%s: allocation of %i bytes failed.", __func__, length); + + memcpy(buffer, (const uint8_t *)header + ofs, length); + + if constexpr(std::is_same_v) buffer[length] = 0; /* In case of corrupt entity lump */ - return length / lumpspec->size; - } + return length / lumpspec->size; } void BSPX_AddLump(bspdata_t *bspdata, const char *xname, const void *xdata, size_t xsize) @@ -3452,7 +3455,7 @@ void BSPX_AddLump(bspdata_t *bspdata, const char *xname, const void *xdata, size e = *link; if (!strcmp(e->lumpname, xname)) { *link = e->next; - free(e); + delete e; break; } else link = &(*link)->next; @@ -3464,15 +3467,14 @@ void BSPX_AddLump(bspdata_t *bspdata, const char *xname, const void *xdata, size break; } if (!e) { - e = static_cast(malloc(sizeof(*e))); - memset(e, 0, sizeof(*e)); + e = new bspxentry_t { }; strncpy(e->lumpname, xname, sizeof(e->lumpname)); e->next = bspdata->bspxentries; bspdata->bspxentries = e; } // ericw -- make a copy - uint8_t *xdata_copy = (uint8_t *)malloc(xsize); + uint8_t *xdata_copy = new uint8_t[xsize]; memcpy(xdata_copy, xdata, xsize); e->lumpdata = xdata_copy; @@ -3590,11 +3592,11 @@ void LoadBSPFile(char *filename, bspdata_t *bspdata) bsp->numareaportals = CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_AREAPORTALS, &bsp->dareaportals); - bsp->visdatasize = CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_VISIBILITY, &bsp->dvis); + bsp->visdatasize = CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_VISIBILITY, &bsp->dvisdata); bsp->lightdatasize = CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_LIGHTING, &bsp->dlightdata); bsp->entdatasize = CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_ENTITIES, &bsp->dentdata); - CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_POP, &bsp->dpop); + //CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_POP, &bsp->dpop); } else if (bspdata->version == &bspver_qbism) { q2_dheader_t *header = (q2_dheader_t *)file_data; q2bsp_qbism_t *bsp = &bspdata->data.q2bsp_qbism; @@ -3619,11 +3621,11 @@ void LoadBSPFile(char *filename, bspdata_t *bspdata) bsp->numareaportals = CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_AREAPORTALS, &bsp->dareaportals); - bsp->visdatasize = CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_VISIBILITY, &bsp->dvis); + bsp->visdatasize = CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_VISIBILITY, &bsp->dvisdata); bsp->lightdatasize = CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_LIGHTING, &bsp->dlightdata); bsp->entdatasize = CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_ENTITIES, &bsp->dentdata); - CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_POP, &bsp->dpop); + //CopyLump(header, bspdata->version, header->lumps, Q2_LUMP_POP, &bsp->dpop); } else if (bspdata->version == &bspver_q1 || bspdata->version == &bspver_h2 || bspdata->version == &bspver_hl) { dheader_t *header = (dheader_t *)file_data; bsp29_t *bsp = &bspdata->data.bsp29; @@ -3731,7 +3733,7 @@ void LoadBSPFile(char *filename, bspdata_t *bspdata) while (xlumps-- > 0) { uint32_t ofs = LittleLong(xlump[xlumps].fileofs); uint32_t len = LittleLong(xlump[xlumps].filelen); - void *lumpdata = malloc(len); + void *lumpdata = new uint8_t[len]; memcpy(lumpdata, (const uint8_t *)header + ofs, len); BSPX_AddLump(bspdata, xlump[xlumps].lumpname, lumpdata, len); } @@ -3742,7 +3744,7 @@ void LoadBSPFile(char *filename, bspdata_t *bspdata) } /* everything has been copied out */ - free(file_data); + delete[] file_data; /* swap everything */ SwapBSPFile(bspdata, TO_CPU); diff --git a/common/bsputils.cc b/common/bsputils.cc index 0bea0431..88657f6f 100644 --- a/common/bsputils.cc +++ b/common/bsputils.cc @@ -340,7 +340,7 @@ static const bsp2_dface_t *BSP_FindFaceAtPoint_r( // Next test if it's within the boundaries of the face plane_t *edgeplanes = Face_AllocInwardFacingEdgePlanes(bsp, face); const bool insideFace = EdgePlanes_PointInside(face, edgeplanes, point); - free(edgeplanes); + delete[] edgeplanes; // Found a match? if (insideFace) { @@ -370,7 +370,7 @@ const bsp2_dface_t *BSP_FindFaceAtPoint_InWorld(const mbsp_t *bsp, const vec3_t plane_t *Face_AllocInwardFacingEdgePlanes(const mbsp_t *bsp, const bsp2_dface_t *face) { - plane_t *out = (plane_t *)calloc(face->numedges, sizeof(plane_t)); + plane_t *out = new plane_t[face->numedges]; const plane_t faceplane = Face_Plane(bsp, face); for (int i = 0; i < face->numedges; i++) { diff --git a/common/cmdlib.cc b/common/cmdlib.cc index 5dd3e681..bb667403 100644 --- a/common/cmdlib.cc +++ b/common/cmdlib.cc @@ -268,9 +268,7 @@ char *ExpandPathAndArchive(char *path) char *copystring(const char *s) { - char *b; - - b = static_cast(malloc(strlen(s) + 1)); + char *b = new char[strlen(s) + 1]; strcpy(b, s); return b; } @@ -619,7 +617,7 @@ int LoadFilePak(char *filename, void *destptr) header.tableofs = LittleLong(header.tableofs); if (!strncmp(header.magic, "PACK", 4)) { - pakfile_t *files = static_cast(malloc(header.numfiles * sizeof(*files))); + pakfile_t *files = new pakfile_t[header.numfiles]; // printf("%s: %u files\n", pakfilename, header.numfiles); fseek(file, header.tableofs, SEEK_SET); SafeRead(file, files, header.numfiles * sizeof(*files)); @@ -627,13 +625,13 @@ int LoadFilePak(char *filename, void *destptr) for (i = 0; i < header.numfiles; i++) { if (!strcmp(files[i].name, innerfile)) { fseek(file, files[i].offset, SEEK_SET); - *bufferptr = static_cast(malloc(files[i].length + 1)); + *bufferptr = new uint8_t[files[i].length + 1]; SafeRead(file, *bufferptr, files[i].length); length = files[i].length; break; } } - free(files); + delete[] files; } fclose(file); @@ -656,7 +654,7 @@ int LoadFilePak(char *filename, void *destptr) file = SafeOpenRead(filename); length = Sys_filelength(file); - buffer = *bufferptr = static_cast(malloc(length + 1)); + buffer = *bufferptr = new uint8_t[length + 1]; if (!buffer) Error("%s: allocation of %i bytes failed.", __func__, length); @@ -681,7 +679,7 @@ int LoadFile(const char *filename, void *destptr) file = SafeOpenRead(filename); length = Sys_filelength(file); - buffer = *bufferptr = static_cast(malloc(length + 1)); + buffer = *bufferptr = new uint8_t[length + 1]; if (!buffer) Error("%s: allocation of %i bytes failed.", __func__, length); @@ -1077,5 +1075,5 @@ void Q_CopyFile(const char *from, char *to) length = LoadFile(from, &buffer); CreatePath(to); SaveFile(to, buffer, length); - free(buffer); + delete[] buffer; } diff --git a/common/polylib.cc b/common/polylib.cc index 40086100..8e85fe37 100644 --- a/common/polylib.cc +++ b/common/polylib.cc @@ -337,9 +337,9 @@ polylib::winding_t *polylib::ChopWinding(winding_t *in, vec3_t normal, vec_t dis winding_t *f, *b; ClipWinding(in, normal, dist, &f, &b); - free(in); + delete in; if (b) - free(b); + delete b; return f; } @@ -442,7 +442,7 @@ void polylib::DiceWinding(winding_t *w, vec_t subdiv, save_winding_fn_t save_fn, split[i] = 1; dist = subdiv * (1 + floor((mins[i] + 1) / subdiv)); ClipWinding(w, split, dist, &o1, &o2); - free(w); + delete w; // // create a new patch @@ -491,9 +491,9 @@ polylib::winding_edges_t *polylib::AllocWindingEdges(const winding_t *w) plane_t p; WindingPlane(w, p.normal, &p.dist); - winding_edges_t *result = (winding_edges_t *)calloc(1, sizeof(winding_edges_t)); + winding_edges_t *result = new winding_edges_t; result->numedges = w->numpoints; - result->planes = (plane_t *)calloc(w->numpoints, sizeof(plane_t)); + result->planes = new plane_t[w->numpoints]; for (int i = 0; i < w->numpoints; i++) { plane_t *dest = &result->planes[i]; @@ -514,8 +514,8 @@ polylib::winding_edges_t *polylib::AllocWindingEdges(const winding_t *w) void polylib::FreeWindingEdges(winding_edges_t *wi) { - free(wi->planes); - free(wi); + delete[] wi->planes; + delete wi; } bool polylib::PointInWindingEdges(const winding_edges_t *wi, const vec3_t point) diff --git a/include/common/bspfile.hh b/include/common/bspfile.hh index a3f0c07f..9727af79 100644 --- a/include/common/bspfile.hh +++ b/include/common/bspfile.hh @@ -883,7 +883,10 @@ struct q2bsp_t q2_dmodel_t *dmodels; int visdatasize; - dvis_t *dvis; + union { + dvis_t *dvis; + uint8_t *dvisdata; + }; int lightdatasize; uint8_t *dlightdata; @@ -942,7 +945,10 @@ struct q2bsp_qbism_t q2_dmodel_t *dmodels; int visdatasize; - dvis_t *dvis; + union { + dvis_t *dvis; + uint8_t *dvisdata; + }; int lightdatasize; uint8_t *dlightdata; diff --git a/include/light/light.hh b/include/light/light.hh index 7438c5d1..886f213a 100644 --- a/include/light/light.hh +++ b/include/light/light.hh @@ -98,7 +98,7 @@ class lightmap_t { public: int style; - lightsample_t *samples; // malloc'ed array of numpoints //FIXME: this is stupid, we shouldn't need to allocate + lightsample_t *samples; // new'ed array of numpoints //FIXME: this is stupid, we shouldn't need to allocate // extra data here for -extra4 }; @@ -130,16 +130,16 @@ struct lightsurf_t vec3_t midpoint; int numpoints; - vec3_t *points; // malloc'ed array of numpoints - vec3_t *normals; // malloc'ed array of numpoints - bool *occluded; // malloc'ed array of numpoints - int *realfacenums; // malloc'ed array of numpoints + vec3_t *points; // new'ed array of numpoints + vec3_t *normals; // new'ed array of numpoints + bool *occluded; // new'ed array of numpoints + int *realfacenums; // new'ed array of numpoints /* raw ambient occlusion amount per sample point, 0-1, where 1 is fully occluded. dirtgain/dirtscale are not applied yet */ - vec_t *occlusion; // malloc'ed array of numpoints + vec_t *occlusion; // new'ed array of numpoints /* for sphere culling */ vec3_t origin; diff --git a/light/entities.cc b/light/entities.cc index 93782711..a9ee4bbf 100644 --- a/light/entities.cc +++ b/light/entities.cc @@ -1240,14 +1240,14 @@ void WriteEntitiesToString(const globalconfig_t &cfg, mbsp_t *bsp) std::string entdata = EntData_Write(entdicts); if (bsp->dentdata) - free(bsp->dentdata); + delete[] bsp->dentdata; /* FIXME - why are we printing this here? */ logprint("%i switchable light styles (%d max)\n", static_cast(lightstyleForTargetname.size()), MAX_SWITCHABLE_STYLES - cfg.compilerstyle_start.intValue()); bsp->entdatasize = entdata.size() + 1; // +1 for a null byte at the end - bsp->dentdata = (char *)calloc(bsp->entdatasize, 1); + bsp->dentdata = new char[bsp->entdatasize]; if (!bsp->dentdata) Error("%s: allocation of %d bytes failed\n", __func__, bsp->entdatasize); diff --git a/light/imglib.cc b/light/imglib.cc index 207b6fee..641b3602 100644 --- a/light/imglib.cc +++ b/light/imglib.cc @@ -214,7 +214,7 @@ qboolean LoadPCX(const char *filename, uint8_t **pixels, uint8_t **palette, int } if (palette) { - *palette = static_cast(malloc(768)); + *palette = new uint8_t[768]; memcpy(*palette, reinterpret_cast(pcx) + len - 768, 768); } @@ -227,7 +227,7 @@ qboolean LoadPCX(const char *filename, uint8_t **pixels, uint8_t **palette, int return true; // No target array specified, so skip reading pixels const int numbytes = (pcx->ymax + 1) * (pcx->xmax + 1); // mxd - uint8_t *out = static_cast(malloc(numbytes)); + uint8_t *out = new uint8_t[numbytes]; if (!out) { logprint("LoadPCX: Failed to load '%s'. Couldn't allocate %i bytes of memory.\n", filename, numbytes); return false; // mxd @@ -258,7 +258,7 @@ qboolean LoadPCX(const char *filename, uint8_t **pixels, uint8_t **palette, int return false; // mxd } - free(pcx); + delete[] pcx; return true; } @@ -346,7 +346,7 @@ qboolean LoadTGA(const char *filename, uint8_t **pixels, int *width, int *height if (height) *height = rows; - uint8_t *targa_rgba = static_cast(malloc(numPixels * 4)); + uint8_t *targa_rgba = new uint8_t[numPixels * 4]; *pixels = targa_rgba; if (targa_header.id_length != 0) @@ -502,7 +502,7 @@ qboolean LoadWAL(const char *filename, uint8_t **pixels, int *width, int *height if (height) *height = h; - uint8_t *out = static_cast(malloc(numpixels)); + uint8_t *out = new uint8_t[numpixels]; if (!out) { logprint("LoadWAL: Failed to load '%s'. Couldn't allocate %i bytes of memory.\n", filename, numpixels); return false; @@ -518,7 +518,7 @@ qboolean LoadWAL(const char *filename, uint8_t **pixels, int *width, int *height out[i * 4 + 3] = (palindex == 255 ? 0 : 255); // Last palette index is transparent color } - free(mt); + delete[] mt; return true; } @@ -549,9 +549,11 @@ static void WriteRGBATextureData( } } + free(miplmp); + // Step 2: write rgba_miptex_t and palette bytes to uint8_t array uint8_t *texdata, *texdatastart; - texdata = texdatastart = static_cast(malloc(totalsize)); + texdata = texdatastart = new uint8_t[totalsize]; memcpy(texdata, miplmp, headersize); texdata += headersize; @@ -674,7 +676,7 @@ LoadTextures(mbsp_t *bsp) } // Create rgba_miptex_t... - rgba_miptex_t *tex = static_cast(malloc(miptexsize)); + rgba_miptex_t *tex = new rgba_miptex_t; strcpy(tex->name, pair.first.c_str()); tex->width = width; tex->height = height; @@ -730,7 +732,7 @@ ConvertTextures(mbsp_t *bsp) miptex_t *miptex = (miptex_t *)((uint8_t *)bsp->dtexdata + ofs); // Create rgba_miptex_t... - rgba_miptex_t *tex = static_cast(malloc(miptexsize)); + rgba_miptex_t *tex = new rgba_miptex_t; strcpy(tex->name, miptex->name); tex->width = miptex->width; tex->height = miptex->height; @@ -741,7 +743,7 @@ ConvertTextures(mbsp_t *bsp) // Convert to RGBA const int numpalpixels = tex->width * tex->height; - uint8_t *pixels = static_cast(malloc(numpalpixels * 4)); // RGBA + uint8_t *pixels = new uint8_t[numpalpixels * 4]; // RGBA const uint8_t *data = reinterpret_cast(miptex) + miptex->offsets[0]; for (int c = 0; c < numpalpixels; c++) { diff --git a/light/light.cc b/light/light.cc index a61fe73a..2de0026f 100644 --- a/light/light.cc +++ b/light/light.cc @@ -381,26 +381,26 @@ static void LightWorld(bspdata_t *bspdata, qboolean forcedscale) logprint("--- LightWorld ---\n"); mbsp_t *const bsp = &bspdata->data.mbsp; - free(filebase); - free(lit_filebase); - free(lux_filebase); + delete[] filebase; + delete[] lit_filebase; + delete[] lux_filebase; /* greyscale data stored in a separate buffer */ - filebase = (uint8_t *)calloc(MAX_MAP_LIGHTING, 1); + filebase = new uint8_t[MAX_MAP_LIGHTING] { }; if (!filebase) Error("%s: allocation of %i bytes failed.", __func__, MAX_MAP_LIGHTING); file_p = 0; file_end = MAX_MAP_LIGHTING; /* litfile data stored in a separate buffer */ - lit_filebase = (uint8_t *)calloc(MAX_MAP_LIGHTING * 3, 1); + lit_filebase = new uint8_t[MAX_MAP_LIGHTING * 3] { }; if (!lit_filebase) Error("%s: allocation of %i bytes failed.", __func__, MAX_MAP_LIGHTING * 3); lit_file_p = 0; lit_file_end = (MAX_MAP_LIGHTING * 3); /* lux data stored in a separate buffer */ - lux_filebase = (uint8_t *)calloc(MAX_MAP_LIGHTING * 3, 1); + lux_filebase = new uint8_t[MAX_MAP_LIGHTING * 3] { }; if (!lux_filebase) Error("%s: allocation of %i bytes failed.", __func__, MAX_MAP_LIGHTING * 3); lux_file_p = 0; @@ -411,10 +411,10 @@ static void LightWorld(bspdata_t *bspdata, qboolean forcedscale) const unsigned char *lmshift_lump = (const unsigned char *)BSPX_GetLump(bspdata, "LMSHIFT", NULL); if (!lmshift_lump && write_litfile != ~0) - faces_sup = NULL; // no scales, no lit2 + faces_sup = nullptr; // no scales, no lit2 else { // we have scales or lit2 output. yay... - faces_sup = (facesup_t *)malloc(sizeof(*faces_sup) * bsp->numfaces); - memset(faces_sup, 0, sizeof(*faces_sup) * bsp->numfaces); + faces_sup = new facesup_t[bsp->numfaces] { }; + if (lmshift_lump) { for (int i = 0; i < bsp->numfaces; i++) faces_sup[i].lmscale = 1 << lmshift_lump[i]; @@ -460,14 +460,14 @@ static void LightWorld(bspdata_t *bspdata, qboolean forcedscale) // Transfer greyscale lightmap (or color lightmap for Q2/HL) to the bsp and update lightdatasize if (!litonly) { - free(bsp->dlightdata); + delete[] bsp->dlightdata; if (bsp->loadversion->game->has_rgb_lightmap) { bsp->lightdatasize = lit_file_p; - bsp->dlightdata = (uint8_t *)malloc(bsp->lightdatasize); + bsp->dlightdata = new uint8_t[bsp->lightdatasize]; memcpy(bsp->dlightdata, lit_filebase, bsp->lightdatasize); } else { bsp->lightdatasize = file_p; - bsp->dlightdata = (uint8_t *)malloc(bsp->lightdatasize); + bsp->dlightdata = new uint8_t[bsp->lightdatasize]; memcpy(bsp->dlightdata, filebase, bsp->lightdatasize); } } else { @@ -476,8 +476,8 @@ static void LightWorld(bspdata_t *bspdata, qboolean forcedscale) logprint("lightdatasize: %i\n", bsp->lightdatasize); if (faces_sup) { - uint8_t *styles = (uint8_t *)malloc(sizeof(*styles) * 4 * bsp->numfaces); - int32_t *offsets = (int32_t *)malloc(sizeof(*offsets) * bsp->numfaces); + uint8_t *styles = new uint8_t[4 * bsp->numfaces]; + int32_t *offsets = new int32_t[bsp->numfaces]; for (int i = 0; i < bsp->numfaces; i++) { offsets[i] = faces_sup[i].lightofs; for (int j = 0; j < MAXLIGHTMAPS; j++) @@ -497,7 +497,7 @@ static void LoadExtendedTexinfoFlags(const char *sourcefilename, const mbsp_t *b char filename[1024]; // always create the zero'ed array - extended_texinfo_flags = static_cast(calloc(bsp->numtexinfo, sizeof(surfflags_t))); + extended_texinfo_flags = new surfflags_t[bsp->numtexinfo] { }; strcpy(filename, sourcefilename); StripExtension(filename); diff --git a/light/litfile.cc b/light/litfile.cc index 1218f8dc..a6937333 100644 --- a/light/litfile.cc +++ b/light/litfile.cc @@ -46,10 +46,10 @@ void WriteLitFile(const mbsp_t *bsp, facesup_t *facesup, const char *filename, i SafeWrite(litfile, &header.v1, sizeof(header.v1)); if (version == 2) { unsigned int i, j; - unsigned int *offsets = (unsigned int *)malloc(bsp->numfaces * sizeof(*offsets)); - unsigned short *extents = (unsigned short *)malloc(2 * bsp->numfaces * sizeof(*extents)); - unsigned char *styles = (unsigned char *)malloc(4 * bsp->numfaces * sizeof(*styles)); - unsigned char *shifts = (unsigned char *)malloc(bsp->numfaces * sizeof(*shifts)); + unsigned int *offsets = new unsigned int[bsp->numfaces]; + unsigned short *extents = new unsigned short[2 * bsp->numfaces]; + unsigned char *styles = new unsigned char[4 * bsp->numfaces]; + unsigned char *shifts = new unsigned char[bsp->numfaces]; for (i = 0; i < bsp->numfaces; i++) { offsets[i] = LittleLong(facesup[i].lightofs); styles[i * 4 + 0] = LittleShort(facesup[i].styles[0]); diff --git a/light/ltface.cc b/light/ltface.cc index 86997cd7..cd2f4a4f 100644 --- a/light/ltface.cc +++ b/light/ltface.cc @@ -696,10 +696,10 @@ static void CalcPoints( /* Allocate surf->points */ surf->numpoints = surf->width * surf->height; - surf->points = (vec3_t *)calloc(surf->numpoints, sizeof(vec3_t)); - surf->normals = (vec3_t *)calloc(surf->numpoints, sizeof(vec3_t)); - surf->occluded = (bool *)calloc(surf->numpoints, sizeof(bool)); - surf->realfacenums = (int *)calloc(surf->numpoints, sizeof(int)); + surf->points = new vec3_t[surf->numpoints]; + surf->normals = new vec3_t[surf->numpoints]; + surf->occluded = new bool[surf->numpoints]; + surf->realfacenums = new int[surf->numpoints]; const auto points = GLM_FacePoints(bsp, face); const auto edgeplanes = GLM_MakeInwardFacingEdgePlanes(points); @@ -844,7 +844,7 @@ static bool Lightsurf_Init(const modelinfo_t *modelinfo, const bsp2_dface_t *fac VectorAdd(lightsurf->maxs, modelinfo->offset, lightsurf->maxs); /* Allocate occlusion array */ - lightsurf->occlusion = (float *)calloc(lightsurf->numpoints, sizeof(float)); + lightsurf->occlusion = new float[lightsurf->numpoints] { }; lightsurf->intersection_stream = MakeIntersectionRayStream(lightsurf->numpoints); lightsurf->occlusion_stream = MakeOcclusionRayStream(lightsurf->numpoints); @@ -855,7 +855,7 @@ static void Lightmap_AllocOrClear(lightmap_t *lightmap, const lightsurf_t *light { if (lightmap->samples == NULL) { /* first use of this lightmap, allocate the storage for it. */ - lightmap->samples = (lightsample_t *)calloc(lightsurf->numpoints, sizeof(lightsample_t)); + lightmap->samples = new lightsample_t[lightsurf->numpoints] { }; } else { /* clear only the data that is going to be merged to it. there's no point clearing more */ memset(lightmap->samples, 0, sizeof(*lightmap->samples) * lightsurf->numpoints); @@ -2531,8 +2531,8 @@ static void LightFace_CalculateDirt(lightsurf_t *lightsurf) // batch implementation: - vec3_t *myUps = (vec3_t *)calloc(lightsurf->numpoints, sizeof(vec3_t)); - vec3_t *myRts = (vec3_t *)calloc(lightsurf->numpoints, sizeof(vec3_t)); + vec3_t *myUps = new vec3_t[lightsurf->numpoints]; + vec3_t *myRts = new vec3_t[lightsurf->numpoints]; // init for (int i = 0; i < lightsurf->numpoints; i++) { @@ -2584,8 +2584,8 @@ static void LightFace_CalculateDirt(lightsurf_t *lightsurf) lightsurf->occlusion[i] = 1 - (avgHitdist / cfg.dirtDepth.floatValue()); } - free(myUps); - free(myRts); + delete[] myUps; + delete[] myRts; } // clamps negative values. applies gamma and rangescale. clamps values over 255 @@ -3233,14 +3233,14 @@ static void WriteSingleLightmap(const mbsp_t *bsp, const bsp2_dface_t *face, con static void LightFaceShutdown(lightsurf_t *lightsurf) { for (auto &lm : lightsurf->lightmapsByStyle) { - free(lm.samples); + delete[] lm.samples; } - free(lightsurf->points); - free(lightsurf->normals); - free(lightsurf->occlusion); - free(lightsurf->occluded); - free(lightsurf->realfacenums); + delete[] lightsurf->points; + delete[] lightsurf->normals; + delete[] lightsurf->occlusion; + delete[] lightsurf->occluded; + delete[] lightsurf->realfacenums; delete lightsurf->occlusion_stream; delete lightsurf->intersection_stream; diff --git a/light/trace.cc b/light/trace.cc index bc1a2c34..40d2775d 100644 --- a/light/trace.cc +++ b/light/trace.cc @@ -256,7 +256,7 @@ static bool Model_HasFence(const mbsp_t *bsp, const dmodel_t *model) static void MakeFenceInfo(const mbsp_t *bsp) { - fence_dmodels = (bool *)calloc(bsp->nummodels, sizeof(bool)); + fence_dmodels = new bool[bsp->nummodels]; for (int i = 0; i < bsp->nummodels; i++) { fence_dmodels[i] = Model_HasFence(bsp, &bsp->dmodels[i]); } @@ -265,11 +265,11 @@ static void MakeFenceInfo(const mbsp_t *bsp) static void BSP_MakeTnodes(const mbsp_t *bsp) { bsp_static = bsp; - tnodes = (tnode_t *)malloc(bsp->numnodes * sizeof(tnode_t)); + tnodes = new tnode_t[bsp->numnodes]; for (int i = 0; i < bsp->nummodels; i++) MakeTnodes_r(bsp->dmodels[i].headnode[0], bsp); - faceinfos = (faceinfo_t *)malloc(bsp->numfaces * sizeof(faceinfo_t)); + faceinfos = new faceinfo_t[bsp->numfaces]; for (int i = 0; i < bsp->numfaces; i++) MakeFaceInfo(bsp, BSP_GetFace(bsp, i), &faceinfos[i]); diff --git a/light/trace_embree.cc b/light/trace_embree.cc index a0c01146..284f1361 100644 --- a/light/trace_embree.cc +++ b/light/trace_embree.cc @@ -897,9 +897,8 @@ public: public: raystream_embree_common_t(int maxRays) - : _rays_maxdist{new float[maxRays]}, _point_indices{new int[maxRays]}, _ray_colors{static_cast( - calloc(maxRays, sizeof(vec3_t)))}, - _ray_normalcontribs{static_cast(calloc(maxRays, sizeof(vec3_t)))}, + : _rays_maxdist{new float[maxRays]}, _point_indices{new int[maxRays]}, _ray_colors{new vec3_t[maxRays] { }}, + _ray_normalcontribs{new vec3_t[maxRays] { }}, _ray_dynamic_styles{new int[maxRays]}, _numrays{0}, _maxrays{maxRays} { } @@ -910,8 +909,8 @@ public: { delete[] _rays_maxdist; delete[] _point_indices; - free(_ray_colors); - free(_ray_normalcontribs); + delete[] _ray_colors; + delete[] _ray_normalcontribs; delete[] _ray_dynamic_styles; } diff --git a/qbsp/brush.cc b/qbsp/brush.cc index 8196f1be..5ddb7a45 100644 --- a/qbsp/brush.cc +++ b/qbsp/brush.cc @@ -415,7 +415,7 @@ static face_t *CreateBrushFaces(const mapentity_t *src, hullbrush_t *hullbrush, continue; // overconstrained plane // this face is a keeper - f = (face_t *)AllocMem(OTHER, sizeof(face_t), true); + f = new face_t { }; f->planenum = PLANENUM_LEAF; f->w.numpoints = w->numpoints; if (f->w.numpoints > MAXEDGES) @@ -548,7 +548,7 @@ FreeBrush void FreeBrush(brush_t *brush) { FreeBrushFaces(brush->faces); - free(brush); + delete brush; } /* @@ -1052,7 +1052,7 @@ brush_t *LoadBrush(const mapentity_t *src, const mapbrush_t *mapbrush, const con } // create the brush - brush = (brush_t *)AllocMem(OTHER, sizeof(brush_t), true); + brush = new brush_t { }; brush->contents = contents; brush->faces = facelist; @@ -1492,9 +1492,7 @@ int BrushMostlyOnSide(const brush_t *brush, const vec3_t planenormal, vec_t plan face_t *CopyFace(const face_t *face) { - face_t *newface = (face_t *)AllocMem(OTHER, sizeof(face_t), true); - - memcpy(newface, face, sizeof(face_t)); + face_t *newface = new face_t(*face); // clear stuff that shouldn't be copied. newface->original = nullptr; @@ -1516,9 +1514,7 @@ Duplicates the brush, the sides, and the windings */ brush_t *CopyBrush(const brush_t *brush) { - brush_t *newbrush = (brush_t *)AllocMem(OTHER, sizeof(brush_t), true); - - memcpy(newbrush, brush, sizeof(brush_t)); + brush_t *newbrush = new brush_t(*brush); newbrush->next = nullptr; newbrush->faces = nullptr; @@ -1685,7 +1681,7 @@ void SplitBrush(const brush_t *brush, int planenum, int planeside, brush_t **fro // first, make two empty brushes (for the front and back side of the plane) for (int i = 0; i < 2; i++) { - b[i] = (brush_t *)AllocMem(OTHER, sizeof(brush_t), true); + b[i] = new brush_t { }; // memcpy( b[i], brush, sizeof( brush_t ) ); // NOTE: brush copying diff --git a/qbsp/csg4.cc b/qbsp/csg4.cc index 87b916f8..f4234ff1 100644 --- a/qbsp/csg4.cc +++ b/qbsp/csg4.cc @@ -70,9 +70,7 @@ MergeFace. */ face_t *NewFaceFromFace(face_t *in) { - face_t *newf; - - newf = (face_t *)AllocMem(OTHER, sizeof(face_t), true); + face_t *newf = new face_t { }; newf->planenum = in->planenum; newf->texinfo = in->texinfo; @@ -203,7 +201,7 @@ void SplitFace(face_t *in, const qbsp_plane_t *split, face_t **front, face_t **b Error("Internal error: numpoints > MAXEDGES (%s)", __func__); /* free the original face now that it is represented by the fragments */ - free(in); + delete in; } /* @@ -393,7 +391,7 @@ static void FreeFaces(face_t *face) while (face) { next = face->next; - free(face); + delete face; face = next; } } @@ -489,7 +487,7 @@ surface_t *BuildSurfaces(const std::map &planefaces) continue; /* create a new surface to hold the faces on this plane */ - surface_t *surf = (surface_t *)AllocMem(OTHER, sizeof(surface_t), true); + surface_t *surf = new surface_t { }; surf->planenum = entry->first; surf->next = surfaces; surfaces = surf; @@ -518,7 +516,7 @@ static face_t *CopyBrushFaces(const brush_t *brush) facelist = NULL; for (face = brush->faces; face; face = face->next) { brushfaces++; - newface = (face_t *)AllocMem(OTHER, sizeof(face_t), true); + newface = new face_t { }; *newface = *face; newface->contents[0] = options.target_game->create_empty_contents(); newface->contents[1] = brush->contents; diff --git a/qbsp/file.cc b/qbsp/file.cc index a40f6353..15aacfc8 100644 --- a/qbsp/file.cc +++ b/qbsp/file.cc @@ -47,8 +47,8 @@ size_t LoadFile(const char *filename, void *bufptr, bool nofail) len = ftell(f); fseek(f, 0, SEEK_SET); - *buf = (char *)AllocMem(OTHER, len + 1, false); - ((char *)*buf)[len] = 0; + *buf = new char[len + 1]; + (*buf)[len] = 0; if (fread(*buf, 1, len, f) != len) Error("Failure reading from file"); diff --git a/qbsp/map.cc b/qbsp/map.cc index e0556423..e4e949ab 100644 --- a/qbsp/map.cc +++ b/qbsp/map.cc @@ -483,9 +483,7 @@ static surfflags_t SurfFlagsForEntity(const mtexinfo_t &texinfo, const mapentity static void ParseEpair(parser_t *parser, mapentity_t *entity) { - epair_t *epair; - - epair = (epair_t *)AllocMem(OTHER, sizeof(epair_t), true); + epair_t *epair = new epair_t { }; epair->next = entity->epairs; entity->epairs = epair; @@ -1966,7 +1964,7 @@ mapentity_t LoadExternalMap(const char *filename) Message(msgStat, "LoadExternalMap: '%s': Loaded %d mapbrushes.\n", filename, dest.nummapbrushes); - free(buf); + delete[] buf; return dest; } @@ -1995,7 +1993,7 @@ void LoadMapFile(void) assert(map.entities.back().numbrushes == 0); map.entities.pop_back(); - free(buf); + delete[] buf; // Print out warnings for entities if (!(rgfStartSpots & info_player_start)) @@ -2240,11 +2238,12 @@ void SetKeyValue(mapentity_t *entity, const char *key, const char *value) for (ep = entity->epairs; ep; ep = ep->next) if (!Q_strcasecmp(ep->key, key)) { - free(ep->value); /* FIXME */ + delete[] ep->value; /* FIXME */ ep->value = copystring(value); return; } - ep = (epair_t *)AllocMem(OTHER, sizeof(epair_t), true); + + ep = new epair_t { }; ep->next = entity->epairs; entity->epairs = ep; ep->key = copystring(key); diff --git a/qbsp/merge.cc b/qbsp/merge.cc index 7701d431..9152b794 100644 --- a/qbsp/merge.cc +++ b/qbsp/merge.cc @@ -175,7 +175,7 @@ face_t *MergeFaceToList(face_t *face, face_t *list) #endif newf = TryMerge(face, f); if (newf) { - free(face); + delete face; f->w.numpoints = -1; // merged out, remove later face = newf; f = list; @@ -201,7 +201,7 @@ face_t *FreeMergeListScraps(face_t *merged) for (; merged; merged = next) { next = merged->next; if (merged->w.numpoints == -1) - free(merged); + delete merged; else { merged->next = head; head = merged; diff --git a/qbsp/portals.cc b/qbsp/portals.cc index c60bf96e..31ef1947 100644 --- a/qbsp/portals.cc +++ b/qbsp/portals.cc @@ -368,7 +368,7 @@ static void MakeHeadnodePortals(const mapentity_t *entity, node_t *node) for (j = 0; j < 2; j++) { n = j * 3 + i; - p = (portal_t *)AllocMem(OTHER, sizeof(portal_t), true); + p = new portal_t { }; portals[n] = p; pl = &bplanes[n]; @@ -530,7 +530,7 @@ static void CutNodePortals_r(node_t *node, portal_state_t *state) * create the new portal by taking the full plane winding for the cutting * plane and clipping it by all of the planes from the other portals */ - new_portal = (portal_t *)AllocMem(OTHER, sizeof(portal_t), true); + new_portal = new portal_t { }; new_portal->planenum = node->planenum; winding = BaseWindingForPlane(plane); @@ -598,7 +598,7 @@ static void CutNodePortals_r(node_t *node, portal_state_t *state) } /* the winding is split */ - new_portal = (portal_t *)AllocMem(OTHER, sizeof(portal_t), true); + new_portal = new portal_t { }; *new_portal = *portal; new_portal->winding = backwinding; free(portal->winding); @@ -673,7 +673,7 @@ void FreeAllPortals(node_t *node) RemovePortalFromNode(p, p->nodes[0]); RemovePortalFromNode(p, p->nodes[1]); free(p->winding); - free(p); + delete p; } node->portals = NULL; } diff --git a/qbsp/qbsp.cc b/qbsp/qbsp.cc index 5bf1815a..630199ad 100644 --- a/qbsp/qbsp.cc +++ b/qbsp/qbsp.cc @@ -775,14 +775,14 @@ void EnsureTexturesLoaded() if (wadstring[0]) Message(msgWarning, warnNoValidWads); /* Try the default wad name */ - defaultwad = (char *)AllocMem(OTHER, strlen(options.szMapName) + 5, false); + defaultwad = new char[strlen(options.szMapName) + 5]; strcpy(defaultwad, options.szMapName); StripExtension(defaultwad); DefaultExtension(defaultwad, ".wad"); WADList_Init(defaultwad); if (wadlist.size()) Message(msgLiteral, "Using default WAD: %s\n", defaultwad); - free(defaultwad); + delete[] defaultwad; } } @@ -1156,7 +1156,7 @@ static void InitQBSP(int argc, const char **argv) Message(msgLiteral, "Loading options from qbsp.ini\n"); ParseOptions(szBuf); - free(szBuf); + delete[] szBuf; } // Concatenate command line args @@ -1166,7 +1166,7 @@ static void InitQBSP(int argc, const char **argv) if (argv[i][0] != '-') length += 2; /* quotes */ } - szBuf = (char *)AllocMem(OTHER, length, true); + szBuf = new char[length] { }; for (i = 1; i < argc; i++) { /* Quote filenames for the parsing function */ if (argv[i][0] != '-') @@ -1179,7 +1179,7 @@ static void InitQBSP(int argc, const char **argv) } szBuf[length - 1] = 0; ParseOptions(szBuf); - free(szBuf); + delete[] szBuf; if (options.szMapName[0] == 0) PrintOptions(); diff --git a/qbsp/solidbsp.cc b/qbsp/solidbsp.cc index 6a74a46f..1672c6ed 100644 --- a/qbsp/solidbsp.cc +++ b/qbsp/solidbsp.cc @@ -58,7 +58,7 @@ void ConvertNodeToLeaf(node_t *node, const contentflags_t &contents) node->planenum = PLANENUM_LEAF; node->contents = contents; - node->markfaces = (face_t **)AllocMem(OTHER, sizeof(face_t *), true); + node->markfaces = new face_t *[1] { }; Q_assert(node->markfaces[0] == nullptr); } @@ -566,7 +566,7 @@ static void DividePlane(surface_t *in, const qbsp_plane_t *split, surface_t **fr in->onnode = true; // divide the facets to the front and back sides - surface_t *newsurf = (surface_t *)AllocMem(OTHER, sizeof(surface_t), true); + surface_t *newsurf = new surface_t { }; *newsurf = *in; // Prepend each face in facet list to either in or newsurf lists @@ -590,12 +590,12 @@ static void DividePlane(surface_t *in, const qbsp_plane_t *split, surface_t **fr if (in->faces) *front = in; else - free(in); + delete in; if (newsurf->faces) *back = newsurf; else - free(newsurf); + delete newsurf; return; } @@ -642,8 +642,7 @@ static void DividePlane(surface_t *in, const qbsp_plane_t *split, surface_t **fr } // stuff got split, so allocate one new plane and reuse in - surface_t *newsurf = (surface_t *)AllocMem(OTHER, sizeof(surface_t), true); - *newsurf = *in; + surface_t *newsurf = new surface_t(*in); newsurf->faces = backlist; *back = newsurf; @@ -767,7 +766,7 @@ static void LinkConvexFaces(surface_t *planelist, node_t *leafnode) // write the list of the original faces to the leaf's markfaces // free surf and the surf->faces list. leaffaces += count; - leafnode->markfaces = (face_t **)AllocMem(OTHER, sizeof(face_t *) * (count + 1), true); + leafnode->markfaces = new face_t *[count + 1] { }; int i = 0; surface_t *pnext; @@ -778,9 +777,9 @@ static void LinkConvexFaces(surface_t *planelist, node_t *leafnode) next = f->next; leafnode->markfaces[i] = f->original; i++; - free(f); + delete f; } - free(surf); + delete surf; } leafnode->markfaces[i] = NULL; // sentinal } @@ -814,8 +813,7 @@ static face_t *LinkNodeFaces(surface_t *surface) // copy for (face_t *f = surface->faces; f; f = f->next) { nodefaces++; - face_t *newf = (face_t *)AllocMem(OTHER, sizeof(face_t), true); - *newf = *f; + face_t *newf = new face_t(*f); f->original = newf; newf->next = list; list = newf; @@ -847,8 +845,8 @@ static void PartitionSurfaces(surface_t *surfaces, node_t *node) Message(msgPercent, splitnodes.load(), csgmergefaces); node->faces = LinkNodeFaces(split); - node->children[0] = (node_t *)AllocMem(OTHER, sizeof(node_t), true); - node->children[1] = (node_t *)AllocMem(OTHER, sizeof(node_t), true); + node->children[0] = new node_t { }; + node->children[1] = new node_t { }; node->planenum = split->planenum; node->detail_separator = split->detail_separator; @@ -907,26 +905,26 @@ node_t *SolidBSP(const mapentity_t *entity, surface_t *surfhead, bool midsplit) * collision hull for the engine. Probably could be done a little * smarter, but this works. */ - node_t *headnode = (node_t *)AllocMem(OTHER, sizeof(node_t), true); + node_t *headnode = new node_t { }; for (int i = 0; i < 3; i++) { headnode->mins[i] = entity->mins[i] - SIDESPACE; headnode->maxs[i] = entity->maxs[i] + SIDESPACE; } - headnode->children[0] = (node_t *)AllocMem(OTHER, sizeof(node_t), true); + headnode->children[0] = new node_t { }; headnode->children[0]->planenum = PLANENUM_LEAF; headnode->children[0]->contents = options.target_game->create_empty_contents(); - headnode->children[0]->markfaces = (face_t **)AllocMem(OTHER, sizeof(face_t *), true); - headnode->children[1] = (node_t *)AllocMem(OTHER, sizeof(node_t), true); + headnode->children[0]->markfaces = new face_t *[1] { }; + headnode->children[1] = new node_t { }; headnode->children[1]->planenum = PLANENUM_LEAF; headnode->children[1]->contents = options.target_game->create_empty_contents(); - headnode->children[1]->markfaces = (face_t **)AllocMem(OTHER, sizeof(face_t *), true); + headnode->children[1]->markfaces = new face_t *[1] { }; return headnode; } Message(msgProgress, "SolidBSP"); - node_t *headnode = (node_t *)AllocMem(OTHER, sizeof(node_t), true); + node_t *headnode = new node_t { }; usemidsplit = midsplit; // calculate a bounding box for the entire model diff --git a/qbsp/surfaces.cc b/qbsp/surfaces.cc index 0c73bf00..e0af846e 100644 --- a/qbsp/surfaces.cc +++ b/qbsp/surfaces.cc @@ -138,7 +138,7 @@ static void GatherNodeFaces_r(node_t *node, std::map &planefaces) for (f = node->faces; f; f = next) { next = f->next; if (!f->w.numpoints) { // face was removed outside - free(f); + delete f; } else { f->next = planefaces[f->planenum]; planefaces[f->planenum] = f; @@ -147,7 +147,7 @@ static void GatherNodeFaces_r(node_t *node, std::map &planefaces) GatherNodeFaces_r(node->children[0], planefaces); GatherNodeFaces_r(node->children[1], planefaces); } - free(node); + delete node; } /* @@ -323,7 +323,7 @@ FindFaceEdges */ static void FindFaceEdges(mapentity_t *entity, face_t *face) { - int i, memsize; + int i; if (map.mtexinfos.at(face->texinfo).flags.extended & (TEX_EXFLAG_SKIP | TEX_EXFLAG_HINT)) return; @@ -332,8 +332,7 @@ static void FindFaceEdges(mapentity_t *entity, face_t *face) if (face->w.numpoints > MAXEDGES) Error("Internal error: face->numpoints > MAXEDGES (%s)", __func__); - memsize = face->w.numpoints * sizeof(face->edges[0]); - face->edges = (int *)AllocMem(OTHER, memsize, true); + face->edges = new int[face->w.numpoints] { }; for (i = 0; i < face->w.numpoints; i++) { const vec_t *p1 = face->w.points[i]; const vec_t *p2 = face->w.points[(i + 1) % face->w.numpoints]; @@ -399,7 +398,7 @@ static void EmitFace(mapentity_t *entity, face_t *face) for (i = 0; i < face->w.numpoints; i++) { map.exported_surfedges.push_back(face->edges[i]); } - free(face->edges); + delete[] face->edges; out->numedges = static_cast(map.exported_surfedges.size()) - out->firstedge; } diff --git a/qbsp/test_qbsp.cc b/qbsp/test_qbsp.cc index 9cac9bc4..b7ca885f 100644 --- a/qbsp/test_qbsp.cc +++ b/qbsp/test_qbsp.cc @@ -277,8 +277,8 @@ TEST(qbsp, SplitBrush) checkCube(back); FreeBrush(brush); - free(front); - free(back); + delete front; + delete back; } TEST(qbsp, SplitBrushOnSide) diff --git a/qbsp/tjunc.cc b/qbsp/tjunc.cc index ee52b0cc..2e61def8 100644 --- a/qbsp/tjunc.cc +++ b/qbsp/tjunc.cc @@ -456,8 +456,8 @@ void TJunc(const mapentity_t *entity, node_t *headnode) cWEdges = cWVerts; cWVerts *= 2; - pWVerts = (wvert_t *)AllocMem(OTHER, cWVerts * sizeof(wvert_t), true); - pWEdges = (wedge_t *)AllocMem(OTHER, cWEdges * sizeof(wedge_t), true); + pWVerts = new wvert_t[cWVerts] { }; + pWEdges = new wedge_t[cWEdges] { }; /* * identify all points on common edges @@ -489,8 +489,8 @@ void TJunc(const mapentity_t *entity, node_t *headnode) free(superface); - free(pWVerts); - free(pWEdges); + delete[] pWVerts; + delete[] pWEdges; Message(msgStat, "%8d edges added by tjunctions", tjuncs); Message(msgStat, "%8d faces added by tjunctions", tjuncfaces); diff --git a/qbsp/writebsp.cc b/qbsp/writebsp.cc index 52b97467..fb92f849 100644 --- a/qbsp/writebsp.cc +++ b/qbsp/writebsp.cc @@ -117,7 +117,7 @@ static int ExportClipNodes(mapentity_t *entity, node_t *node) // FIXME: free more stuff? if (node->planenum == PLANENUM_LEAF) { int contents = node->contents.native; - free(node); + delete node; return contents; } @@ -136,11 +136,10 @@ static int ExportClipNodes(mapentity_t *entity, node_t *node) for (face = node->faces; face; face = next) { next = face->next; - memset(face, 0, sizeof(face_t)); - free(face); + delete face; } - free(node); + delete node; return nodenum; } @@ -377,9 +376,8 @@ static void WriteExtendedTexinfoFlags(void) template static void CopyVector(const std::vector &vec, int *elementCountOut, C **arrayCopyOut) { - const size_t numBytes = sizeof(C) * vec.size(); - void *data = (void *)malloc(numBytes); - memcpy(data, vec.data(), numBytes); + C *data = new C[vec.size()]; + memcpy(data, vec.data(), sizeof(C) * vec.size()); *elementCountOut = vec.size(); *arrayCopyOut = (C *)data; @@ -388,7 +386,7 @@ static void CopyVector(const std::vector &vec, int *elementCountOut, C **arra static void CopyString(const std::string &string, bool addNullTermination, int *elementCountOut, void **arrayCopyOut) { const size_t numBytes = addNullTermination ? string.size() + 1 : string.size(); - void *data = malloc(numBytes); + void *data = new uint8_t[numBytes]; memcpy(data, string.data(), numBytes); // std::string::data() has null termination, so it's safe to copy it *elementCountOut = numBytes; @@ -433,10 +431,10 @@ static void WriteBSPFile() // FIXME: temp bspdata.data.mbsp.numareaportals = 1; - bspdata.data.mbsp.dareaportals = (dareaportal_t *)calloc(bspdata.data.mbsp.numareaportals, sizeof(dareaportal_t)); + bspdata.data.mbsp.dareaportals = new dareaportal_t[bspdata.data.mbsp.numareaportals]; bspdata.data.mbsp.numareas = 2; - bspdata.data.mbsp.dareas = (darea_t *)calloc(bspdata.data.mbsp.numareas, sizeof(darea_t)); + bspdata.data.mbsp.dareas = new darea_t[bspdata.data.mbsp.numareas]; bspdata.data.mbsp.dareas[1].firstareaportal = 1; if (!ConvertBSPFormat(&bspdata, options.target_version)) { const bspversion_t *highLimitsFormat = nullptr; diff --git a/vis/flow.cc b/vis/flow.cc index bfa3901b..39f0f199 100644 --- a/vis/flow.cc +++ b/vis/flow.cc @@ -426,7 +426,7 @@ static void *BasePortalThread(void *dummy) float d; uint8_t *portalsee; - portalsee = static_cast(malloc(sizeof(*portalsee) * numportals * 2)); + portalsee = new uint8_t[numportals * 2]; if (!portalsee) Error("%s: Out of Memory", __func__); @@ -488,7 +488,7 @@ static void *BasePortalThread(void *dummy) SimpleFlood(p, p->leaf, portalsee); } - free(portalsee); + delete[] portalsee; return NULL; } diff --git a/vis/state.cc b/vis/state.cc index 6faba751..ee030565 100644 --- a/vis/state.cc +++ b/vis/state.cc @@ -154,8 +154,8 @@ void SaveVisState(void) SafeWrite(outfile, &state, sizeof(state)); /* Allocate memory for compressed bitstrings */ - might = static_cast(malloc((portalleafs + 7) >> 3)); - vis = static_cast(malloc((portalleafs + 7) >> 3)); + might = new uint8_t[(portalleafs + 7) >> 3]; + vis = new uint8_t[(portalleafs + 7) >> 3]; for (i = 0, p = portals; i < numportals * 2; i++, p++) { might_len = CompressBits(might, p->mightsee); @@ -176,8 +176,8 @@ void SaveVisState(void) SafeWrite(outfile, vis, vis_len); } - free(might); - free(vis); + delete[] might; + delete[] vis; err = fclose(outfile); if (err) @@ -244,7 +244,7 @@ qboolean LoadVisState(void) starttime -= state.time_elapsed; numbytes = (portalleafs + 7) >> 3; - compressed = static_cast(malloc(numbytes)); + compressed = new uint8_t[numbytes]; /* Update the portal information */ for (i = 0, p = portals; i < numportals * 2; i++, p++) { @@ -282,7 +282,7 @@ qboolean LoadVisState(void) p->status = pstat_none; } - free(compressed); + delete[] compressed; fclose(infile); return true; diff --git a/vis/vis.cc b/vis/vis.cc index dc4e0a78..98282f40 100644 --- a/vis/vis.cc +++ b/vis/vis.cc @@ -579,7 +579,7 @@ static void LeafFlow(int leafnum, mleaf_t *dleaf, const mbsp_t *bsp) totalvis += numvis; /* Allocate for worst case where RLE might grow the data (unlikely) */ - compressed = static_cast(malloc(portalleafs * 2 / 8)); + compressed = new uint8_t[(portalleafs * 2) / 8]; len = CompressRow(outbuffer, (portalleafs + 7) >> 3, compressed); dest = vismap_p; @@ -592,7 +592,7 @@ static void LeafFlow(int leafnum, mleaf_t *dleaf, const mbsp_t *bsp) dleaf->visofs = dest - vismap; memcpy(dest, compressed, len); - free(compressed); + delete[] compressed; } static void ClusterFlow(int clusternum, leafbits_t *buffer, const mbsp_t *bsp) @@ -666,10 +666,10 @@ static void ClusterFlow(int clusternum, leafbits_t *buffer, const mbsp_t *bsp) /* Allocate for worst case where RLE might grow the data (unlikely) */ if (bsp->loadversion->game->id == GAME_QUAKE_II) { - compressed = static_cast(malloc(portalleafs * 2 / 8)); + compressed = new uint8_t[(portalleafs * 2) / 8]; len = CompressRow(outbuffer, (portalleafs + 7) >> 3, compressed); } else { - compressed = static_cast(malloc(portalleafs_real * 2 / 8)); + compressed = new uint8_t[(portalleafs_real * 2) / 8]; len = CompressRow(outbuffer, (portalleafs_real + 7) >> 3, compressed); } @@ -683,7 +683,7 @@ static void ClusterFlow(int clusternum, leafbits_t *buffer, const mbsp_t *bsp) leaf->visofs = dest - vismap; memcpy(dest, compressed, len); - free(compressed); + delete[] compressed; } /* @@ -749,10 +749,8 @@ void CalcVis(const mbsp_t *bsp) for (i = 0; i < portalleafs; i++) LeafFlow(i, &bsp->dleafs[i + 1], bsp); } else { - leafbits_t *buffer; - logprint("Expanding clusters...\n"); - buffer = static_cast(malloc(LeafbitsSize(portalleafs))); + leafbits_t *buffer = static_cast(malloc(LeafbitsSize(portalleafs))); for (i = 0; i < portalleafs; i++) { memset(buffer, 0, LeafbitsSize(portalleafs)); ClusterFlow(i, buffer, bsp); @@ -893,7 +891,7 @@ sep_t *Findpassages(winding_t *source, winding_t *pass) // count_sep++; - sep = static_cast(malloc(sizeof(*sep))); + sep = new sep_t; sep->next = list; list = sep; sep->plane = plane; @@ -942,11 +940,11 @@ void CalcPassages(void) if (!sep) { // Error ("No seperating planes found in portal pair"); count_sep++; - sep = static_cast(malloc(sizeof(*sep))); + sep = new sep_t; sep->next = NULL; sep->plane = p1->plane; } - passages = static_cast(malloc(sizeof(*passages))); + passages = new passage_t; passages->planes = sep; passages->from = p1->leaf; passages->to = p2->leaf; @@ -1058,11 +1056,9 @@ void LoadPortals(char *name, mbsp_t *bsp) leafbytes_real = ((portalleafs_real + 63) & ~63) >> 3; // each file portal is split into two memory portals - portals = static_cast(malloc(2 * numportals * sizeof(portal_t))); - memset(portals, 0, 2 * numportals * sizeof(portal_t)); + portals = new portal_t[numportals * 2] { }; - leafs = static_cast(malloc(portalleafs * sizeof(leaf_t))); - memset(leafs, 0, portalleafs * sizeof(leaf_t)); + leafs = new leaf_t[portalleafs] { }; if (bsp->loadversion->game->id == GAME_QUAKE_II) { originalvismapsize = portalleafs * ((portalleafs + 7) / 8); @@ -1071,7 +1067,7 @@ void LoadPortals(char *name, mbsp_t *bsp) } // FIXME - more intelligent allocation? - bsp->dvisdata = static_cast(malloc(MAX_MAP_VISIBILITY)); + bsp->dvisdata = new uint8_t[MAX_MAP_VISIBILITY]; if (!bsp->dvisdata) Error("%s: dvisdata allocation failed (%i bytes)", __func__, MAX_MAP_VISIBILITY); memset(bsp->dvisdata, 0, MAX_MAP_VISIBILITY); @@ -1141,13 +1137,13 @@ void LoadPortals(char *name, mbsp_t *bsp) /* Load the cluster expansion map if needed */ if (bsp->loadversion->game->id == GAME_QUAKE_II) { - clustermap = static_cast(malloc(portalleafs_real * sizeof(int))); + clustermap = new int[portalleafs_real]; for (int32_t i = 0; i < bsp->numleafs; i++) { clustermap[i] = bsp->dleafs[i + 1].cluster; } } else if (portalleafs != portalleafs_real) { - clustermap = static_cast(malloc(portalleafs_real * sizeof(int))); + clustermap = new int[portalleafs_real]; if (!strcmp(magic, PORTALFILE2)) { for (i = 0; i < portalleafs; i++) { while (1) { @@ -1293,9 +1289,9 @@ int main(int argc, char **argv) DefaultExtension(statetmpfile, ".vi0"); if (bsp->loadversion->game->id != GAME_QUAKE_II) { - uncompressed = static_cast(calloc(portalleafs, leafbytes_real)); + uncompressed = new uint8_t[portalleafs * leafbytes_real]; } else { - uncompressed_q2 = static_cast(calloc(portalleafs, leafbytes)); + uncompressed_q2 = new uint8_t[portalleafs * leafbytes]; } // CalcPassages ();