Custom allocator for Recast.

This commit is contained in:
Mikko Mononen
2010-07-09 12:55:14 +00:00
parent e5cca88cd1
commit cd3a351f40
20 changed files with 2472 additions and 434 deletions
+39 -34
View File
@@ -23,7 +23,7 @@
#include "Recast.h"
#include "RecastLog.h"
#include "RecastTimer.h"
#include "RecastAlloc.h"
struct rcEdge
{
@@ -39,15 +39,18 @@ static bool buildMeshAdjacency(unsigned short* polys, const int npolys,
// http://www.terathon.com/code/edges.php
int maxEdgeCount = npolys*vertsPerPoly;
unsigned short* firstEdge = new unsigned short[nverts + maxEdgeCount];
unsigned short* firstEdge = (unsigned short*)rcAlloc(sizeof(unsigned short)*(nverts + maxEdgeCount), RC_ALLOC_TEMP);
if (!firstEdge)
return false;
unsigned short* nextEdge = firstEdge + nverts;
int edgeCount = 0;
rcEdge* edges = new rcEdge[maxEdgeCount];
rcEdge* edges = (rcEdge*)rcAlloc(sizeof(rcEdge)*maxEdgeCount, RC_ALLOC_TEMP);
if (!edges)
{
rcFree(firstEdge);
return false;
}
for (int i = 0; i < nverts; i++)
firstEdge[i] = RC_MESH_NULL_IDX;
@@ -112,8 +115,8 @@ static bool buildMeshAdjacency(unsigned short* polys, const int npolys,
}
}
delete [] firstEdge;
delete [] edges;
rcFree(firstEdge);
rcFree(edges);
return true;
}
@@ -518,7 +521,7 @@ static int removeVertex(rcPolyMesh& mesh, const unsigned short rem, const int ma
return -1;
int nedges = 0;
rcScopedDelete<int> edges = new int[numRemovedVerts*nvp*4];
rcScopedDelete<int> edges = (int*)rcAlloc(sizeof(int)*numRemovedVerts*nvp*4, RC_ALLOC_TEMP);
if (!edges)
{
if (rcGetLog())
@@ -527,7 +530,7 @@ static int removeVertex(rcPolyMesh& mesh, const unsigned short rem, const int ma
}
int nhole = 0;
rcScopedDelete<int> hole = new int[numRemovedVerts*nvp];
rcScopedDelete<int> hole = (int*)rcAlloc(sizeof(int)*numRemovedVerts*nvp, RC_ALLOC_TEMP);
if (!hole)
{
if (rcGetLog())
@@ -536,7 +539,7 @@ static int removeVertex(rcPolyMesh& mesh, const unsigned short rem, const int ma
}
int nhreg = 0;
rcScopedDelete<int> hreg = new int[numRemovedVerts*nvp];
rcScopedDelete<int> hreg = (int*)rcAlloc(sizeof(int)*numRemovedVerts*nvp, RC_ALLOC_TEMP);
if (!hreg)
{
if (rcGetLog())
@@ -545,7 +548,7 @@ static int removeVertex(rcPolyMesh& mesh, const unsigned short rem, const int ma
}
int nharea = 0;
rcScopedDelete<int> harea = new int[numRemovedVerts*nvp];
rcScopedDelete<int> harea = (int*)rcAlloc(sizeof(int)*numRemovedVerts*nvp, RC_ALLOC_TEMP);
if (!harea)
{
if (rcGetLog())
@@ -662,7 +665,7 @@ static int removeVertex(rcPolyMesh& mesh, const unsigned short rem, const int ma
break;
}
rcScopedDelete<int> tris = new int[nhole*3];
rcScopedDelete<int> tris = (int*)rcAlloc(sizeof(int)*nhole*3, RC_ALLOC_TEMP);
if (!tris)
{
if (rcGetLog())
@@ -670,7 +673,7 @@ static int removeVertex(rcPolyMesh& mesh, const unsigned short rem, const int ma
return 0;
}
rcScopedDelete<int> tverts = new int[nhole*4];
rcScopedDelete<int> tverts = (int*)rcAlloc(sizeof(int)*nhole*4, RC_ALLOC_TEMP);
if (!tverts)
{
if (rcGetLog())
@@ -678,7 +681,7 @@ static int removeVertex(rcPolyMesh& mesh, const unsigned short rem, const int ma
return 0;
}
rcScopedDelete<int> thole = new int[nhole];
rcScopedDelete<int> thole = (int*)rcAlloc(sizeof(int)*nhole, RC_ALLOC_TEMP);
if (!tverts)
{
if (rcGetLog())
@@ -707,21 +710,21 @@ static int removeVertex(rcPolyMesh& mesh, const unsigned short rem, const int ma
}
// Merge the hole triangles back to polygons.
rcScopedDelete<unsigned short> polys = new unsigned short[(ntris+1)*nvp];
rcScopedDelete<unsigned short> polys = (unsigned short*)rcAlloc(sizeof(unsigned short)*(ntris+1)*nvp, RC_ALLOC_TEMP);
if (!polys)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "removeVertex: Out of memory 'polys' (%d).", (ntris+1)*nvp);
return 0;
}
rcScopedDelete<unsigned short> pregs = new unsigned short[ntris];
rcScopedDelete<unsigned short> pregs = (unsigned short*)rcAlloc(sizeof(unsigned short)*ntris, RC_ALLOC_TEMP);
if (!pregs)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "removeVertex: Out of memory 'pregs' (%d).", ntris);
return 0;
}
rcScopedDelete<unsigned char> pareas = new unsigned char[ntris];
rcScopedDelete<unsigned char> pareas = (unsigned char*)rcAlloc(sizeof(unsigned char)*ntris, RC_ALLOC_TEMP);
if (!pregs)
{
if (rcGetLog())
@@ -848,7 +851,7 @@ bool rcBuildPolyMesh(rcContourSet& cset, int nvp, rcPolyMesh& mesh)
return false;
}
rcScopedDelete<unsigned char> vflags = new unsigned char[maxVertices];
rcScopedDelete<unsigned char> vflags = (unsigned char*)rcAlloc(sizeof(unsigned char)*maxVertices, RC_ALLOC_TEMP);
if (!vflags)
{
if (rcGetLog())
@@ -857,28 +860,28 @@ bool rcBuildPolyMesh(rcContourSet& cset, int nvp, rcPolyMesh& mesh)
}
memset(vflags, 0, maxVertices);
mesh.verts = new unsigned short[maxVertices*3];
mesh.verts = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxVertices*3, RC_ALLOC_PERM);
if (!mesh.verts)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.verts' (%d).", maxVertices);
return false;
}
mesh.polys = new unsigned short[maxTris*nvp*2*2];
mesh.polys = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxTris*nvp*2*2, RC_ALLOC_PERM);
if (!mesh.polys)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.polys' (%d).", maxTris*nvp*2);
return false;
}
mesh.regs = new unsigned short[maxTris];
mesh.regs = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxTris, RC_ALLOC_PERM);
if (!mesh.regs)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.regs' (%d).", maxTris);
return false;
}
mesh.areas = new unsigned char[maxTris];
mesh.areas = (unsigned char*)rcAlloc(sizeof(unsigned char)*maxTris, RC_ALLOC_PERM);
if (!mesh.areas)
{
if (rcGetLog())
@@ -896,7 +899,7 @@ bool rcBuildPolyMesh(rcContourSet& cset, int nvp, rcPolyMesh& mesh)
memset(mesh.regs, 0, sizeof(unsigned short)*maxTris);
memset(mesh.areas, 0, sizeof(unsigned char)*maxTris);
rcScopedDelete<int> nextVert = new int[maxVertices];
rcScopedDelete<int> nextVert = (int*)rcAlloc(sizeof(int)*maxVertices, RC_ALLOC_TEMP);
if (!nextVert)
{
if (rcGetLog())
@@ -905,7 +908,7 @@ bool rcBuildPolyMesh(rcContourSet& cset, int nvp, rcPolyMesh& mesh)
}
memset(nextVert, 0, sizeof(int)*maxVertices);
rcScopedDelete<int> firstVert = new int[VERTEX_BUCKET_COUNT];
rcScopedDelete<int> firstVert = (int*)rcAlloc(sizeof(int)*VERTEX_BUCKET_COUNT, RC_ALLOC_TEMP);
if (!firstVert)
{
if (rcGetLog())
@@ -915,21 +918,21 @@ bool rcBuildPolyMesh(rcContourSet& cset, int nvp, rcPolyMesh& mesh)
for (int i = 0; i < VERTEX_BUCKET_COUNT; ++i)
firstVert[i] = -1;
rcScopedDelete<int> indices = new int[maxVertsPerCont];
rcScopedDelete<int> indices = (int*)rcAlloc(sizeof(int)*maxVertsPerCont, RC_ALLOC_TEMP);
if (!indices)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'indices' (%d).", maxVertsPerCont);
return false;
}
rcScopedDelete<int> tris = new int[maxVertsPerCont*3];
rcScopedDelete<int> tris = (int*)rcAlloc(sizeof(int)*maxVertsPerCont*3, RC_ALLOC_TEMP);
if (!tris)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'tris' (%d).", maxVertsPerCont*3);
return false;
}
rcScopedDelete<unsigned short> polys = new unsigned short[(maxVertsPerCont+1)*nvp];
rcScopedDelete<unsigned short> polys = (unsigned short*)rcAlloc(sizeof(unsigned short)*(maxVertsPerCont+1)*nvp, RC_ALLOC_TEMP);
if (!polys)
{
if (rcGetLog())
@@ -964,6 +967,8 @@ bool rcBuildPolyMesh(rcContourSet& cset, int nvp, rcPolyMesh& mesh)
printf("\t\t%d,%d,%d,%d,\n", v[0], v[1], v[2], v[3]);
}
printf("\t};\n\tconst int nverts = sizeof(verts)/(sizeof(int)*4);\n");*/
if (rcGetLog())
rcGetLog()->log(RC_LOG_WARNING, "rcBuildPolyMesh: Bad triangulation Contour %d.", i);
ntris = -ntris;
}
@@ -1100,7 +1105,7 @@ bool rcBuildPolyMesh(rcContourSet& cset, int nvp, rcPolyMesh& mesh)
}
// Just allocate the mesh flags array. The user is resposible to fill it.
mesh.flags = new unsigned short[mesh.npolys];
mesh.flags = (unsigned short*)rcAlloc(sizeof(unsigned short)*mesh.npolys, RC_ALLOC_PERM);
if (!mesh.flags)
{
if (rcGetLog())
@@ -1145,7 +1150,7 @@ bool rcMergePolyMeshes(rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh)
}
mesh.nverts = 0;
mesh.verts = new unsigned short[maxVerts*3];
mesh.verts = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxVerts*3, RC_ALLOC_PERM);
if (!mesh.verts)
{
if (rcGetLog())
@@ -1154,7 +1159,7 @@ bool rcMergePolyMeshes(rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh)
}
mesh.npolys = 0;
mesh.polys = new unsigned short[maxPolys*2*mesh.nvp];
mesh.polys = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxPolys*2*mesh.nvp, RC_ALLOC_PERM);
if (!mesh.polys)
{
if (rcGetLog())
@@ -1163,7 +1168,7 @@ bool rcMergePolyMeshes(rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh)
}
memset(mesh.polys, 0xff, sizeof(unsigned short)*maxPolys*2*mesh.nvp);
mesh.regs = new unsigned short[maxPolys];
mesh.regs = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxPolys, RC_ALLOC_PERM);
if (!mesh.regs)
{
if (rcGetLog())
@@ -1172,7 +1177,7 @@ bool rcMergePolyMeshes(rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh)
}
memset(mesh.regs, 0, sizeof(unsigned short)*maxPolys);
mesh.areas = new unsigned char[maxPolys];
mesh.areas = (unsigned char*)rcAlloc(sizeof(unsigned char)*maxPolys, RC_ALLOC_PERM);
if (!mesh.areas)
{
if (rcGetLog())
@@ -1181,7 +1186,7 @@ bool rcMergePolyMeshes(rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh)
}
memset(mesh.areas, 0, sizeof(unsigned char)*maxPolys);
mesh.flags = new unsigned short[maxPolys];
mesh.flags = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxPolys, RC_ALLOC_PERM);
if (!mesh.flags)
{
if (rcGetLog())
@@ -1190,7 +1195,7 @@ bool rcMergePolyMeshes(rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh)
}
memset(mesh.flags, 0, sizeof(unsigned short)*maxPolys);
rcScopedDelete<int> nextVert = new int[maxVerts];
rcScopedDelete<int> nextVert = (int*)rcAlloc(sizeof(int)*maxVerts, RC_ALLOC_TEMP);
if (!nextVert)
{
if (rcGetLog())
@@ -1199,7 +1204,7 @@ bool rcMergePolyMeshes(rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh)
}
memset(nextVert, 0, sizeof(int)*maxVerts);
rcScopedDelete<int> firstVert = new int[VERTEX_BUCKET_COUNT];
rcScopedDelete<int> firstVert = (int*)rcAlloc(sizeof(int)*VERTEX_BUCKET_COUNT, RC_ALLOC_TEMP);
if (!firstVert)
{
if (rcGetLog())
@@ -1209,7 +1214,7 @@ bool rcMergePolyMeshes(rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh)
for (int i = 0; i < VERTEX_BUCKET_COUNT; ++i)
firstVert[i] = -1;
rcScopedDelete<unsigned short> vremap = new unsigned short[maxVertsPerMesh];
rcScopedDelete<unsigned short> vremap = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxVertsPerMesh, RC_ALLOC_PERM);
if (!vremap)
{
if (rcGetLog())