Skip to content

Commit

Permalink
Add doug_lea_allocator module
Browse files Browse the repository at this point in the history
  • Loading branch information
Drombeys committed Dec 13, 2023
1 parent 805b590 commit d9802d6
Show file tree
Hide file tree
Showing 15 changed files with 7,158 additions and 51 deletions.
6 changes: 3 additions & 3 deletions src/xrCDB/OPC_AABBTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ AABBTree::AABBTree() : mIndices(null), mTotalNbNodes(0)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
AABBTree::~AABBTree()
{
xr_free(mIndices);
CFREE(mIndices);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -347,8 +347,8 @@ bool AABBTree::Build(AABBTreeBuilder* builder)
builder->SetNbInvalidSplits(0);

// Initialize indices. This list will be modified during build.
xr_free(mIndices);
mIndices = xr_alloc<udword>(builder->mNbPrimitives);
CFREE(mIndices);
mIndices = CALLOC(udword,builder->mNbPrimitives);
CHECKALLOC(mIndices);
for(udword i=0;i<builder->mNbPrimitives;i++) mIndices[i] = i;

Expand Down
10 changes: 5 additions & 5 deletions src/xrCDB/OPC_Container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ bool Container::Resize(udword needed)
if(mMaxNbEntries<mCurNbEntries + needed) mMaxNbEntries = mCurNbEntries + needed;

// Get some bytes for _new_ entries
udword* NewEntries = xr_alloc<udword>(mMaxNbEntries);
udword* NewEntries = CALLOC(udword,mMaxNbEntries);
CHECKALLOC(NewEntries);

#ifdef CONTAINER_STATS
Expand All @@ -105,7 +105,7 @@ bool Container::Resize(udword needed)
if(mCurNbEntries) CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));

// Delete old data
xr_free(mEntries);
CFREE(mEntries);

// Assign _new_ pointer
mEntries = NewEntries;
Expand All @@ -132,7 +132,7 @@ bool Container::SetSize(udword nb)
mMaxNbEntries = nb;

// Get some bytes for _new_ entries
mEntries = xr_alloc<udword>(mMaxNbEntries);
mEntries = CALLOC(udword,mMaxNbEntries);
CHECKALLOC(mEntries);

#ifdef CONTAINER_STATS
Expand Down Expand Up @@ -160,7 +160,7 @@ bool Container::Refit()
if(!mMaxNbEntries) return false;

// Get just enough bytes
udword* NewEntries = xr_alloc<udword>(mMaxNbEntries);
udword* NewEntries = CALLOC(udword,mMaxNbEntries);
CHECKALLOC(NewEntries);

#ifdef CONTAINER_STATS
Expand All @@ -172,7 +172,7 @@ bool Container::Refit()
CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));

// Delete old data
xr_free(mEntries);
CFREE(mEntries);

// Assign _new_ pointer
mEntries = NewEntries;
Expand Down
2 changes: 1 addition & 1 deletion src/xrCDB/OPC_Container.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
#ifdef CONTAINER_STATS
mUsedRam-=mMaxNbEntries*sizeof(udword);
#endif
xr_free (mEntries);
CFREE(mEntries);
mCurNbEntries = mMaxNbEntries = 0;
return *this;
}
Expand Down
20 changes: 10 additions & 10 deletions src/xrCDB/OPC_Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ OPCODE_Model::OPCODE_Model() : mSource(null), mTree(null), mNoLeaf(false), mQuan
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
OPCODE_Model::~OPCODE_Model()
{
xr_delete(mSource);
xr_delete(mTree);
CDELETE (mSource);
CDELETE (mTree);
#ifdef __MESHMERIZER_H__ // Collision hulls only supported within ICE !
xr_delete(mHull);
CDELETE (mHull);
#endif // __MESHMERIZER_H__
}

Expand Down Expand Up @@ -205,7 +205,7 @@ bool OPCODE_Model::Build(const OPCODECREATE& create)
// We continue nonetheless....

// 2) Build a generic AABB Tree.
mSource = xr_new<AABBTree>();
mSource = CNEW(AABBTree)();
CHECKALLOC(mSource);

// 2-1) Setup a builder. Our primitives here are triangles from input mesh,
Expand All @@ -224,13 +224,13 @@ bool OPCODE_Model::Build(const OPCODECREATE& create)

if(mNoLeaf)
{
if(mQuantized) mTree = xr_new<AABBQuantizedNoLeafTree>();
else mTree = xr_new<AABBNoLeafTree>();
if(mQuantized) mTree = CNEW(AABBQuantizedNoLeafTree)();
else mTree = CNEW(AABBNoLeafTree)();
}
else
{
if(mQuantized) mTree = xr_new<AABBQuantizedTree>();
else mTree = xr_new<AABBCollisionTree>();
if(mQuantized) mTree = CNEW(AABBQuantizedTree)();
else mTree = CNEW(AABBCollisionTree)();
}

// 3-2) Create optimized tree
Expand All @@ -239,15 +239,15 @@ bool OPCODE_Model::Build(const OPCODECREATE& create)
// 3-3) Delete generic tree if needed
if(!create.KeepOriginal) {
mSource->destroy (&TB) ;
xr_delete (mSource) ;
CDELETE (mSource) ;
}

#ifdef __MESHMERIZER_H__
// 4) Convex hull
if(create.CollisionHull)
{
// Create hull
mHull = xr_new<CollisionHull>();
mHull = CNEW(CollisionHull)();
CHECKALLOC(mHull);

CONVEXHULLCREATE CHC;
Expand Down
24 changes: 12 additions & 12 deletions src/xrCDB/OPC_OptimizedTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ AABBCollisionTree::AABBCollisionTree() : mNodes(null)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
AABBCollisionTree::~AABBCollisionTree()
{
xr_free(mNodes);
CFREE(mNodes);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -234,7 +234,7 @@ bool AABBCollisionTree::Build(AABBTree* tree)

// Get nodes
mNbNodes = NbNodes;
mNodes = xr_alloc<AABBCollisionNode>(mNbNodes);
mNodes = CALLOC(AABBCollisionNode,mNbNodes);
CHECKALLOC (mNodes);
ZeroMemory (mNodes,mNbNodes*sizeof(AABBCollisionNode));

Expand Down Expand Up @@ -267,7 +267,7 @@ AABBNoLeafTree::AABBNoLeafTree() : mNodes(null)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
AABBNoLeafTree::~AABBNoLeafTree()
{
xr_free(mNodes);
CFREE(mNodes);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -288,7 +288,7 @@ bool AABBNoLeafTree::Build(AABBTree* tree)

// Get nodes
mNbNodes = NbTriangles-1;
mNodes = xr_alloc<AABBNoLeafNode>(mNbNodes);
mNodes = CALLOC(AABBNoLeafNode,mNbNodes);
CHECKALLOC (mNodes);
ZeroMemory (mNodes,mNbNodes*sizeof(AABBNoLeafNode));

Expand Down Expand Up @@ -417,7 +417,7 @@ AABBQuantizedTree::AABBQuantizedTree() : mNodes(null)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
AABBQuantizedTree::~AABBQuantizedTree()
{
xr_free(mNodes);
CFREE(mNodes);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -438,7 +438,7 @@ bool AABBQuantizedTree::Build(AABBTree* tree)

// Get nodes
mNbNodes = NbNodes;
AABBCollisionNode* Nodes = xr_alloc<AABBCollisionNode>(mNbNodes);
AABBCollisionNode* Nodes = CALLOC(AABBCollisionNode,mNbNodes);
CHECKALLOC (Nodes);
ZeroMemory (Nodes,mNbNodes*sizeof(AABBCollisionNode));

Expand All @@ -448,7 +448,7 @@ bool AABBQuantizedTree::Build(AABBTree* tree)

// Quantize
{
mNodes = xr_alloc<AABBQuantizedNode>(mNbNodes);
mNodes = CALLOC(AABBQuantizedNode,mNbNodes);
CHECKALLOC (mNodes);
ZeroMemory (mNodes,mNbNodes*sizeof(AABBQuantizedNode));

Expand All @@ -466,7 +466,7 @@ bool AABBQuantizedTree::Build(AABBTree* tree)
REMAP_DATA(mData)
}

xr_free(Nodes);
CFREE(Nodes);
}

#ifdef __ICECORE_H__
Expand Down Expand Up @@ -494,7 +494,7 @@ AABBQuantizedNoLeafTree::AABBQuantizedNoLeafTree() : mNodes(null)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
AABBQuantizedNoLeafTree::~AABBQuantizedNoLeafTree()
{
xr_free(mNodes);
CFREE(mNodes);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -515,7 +515,7 @@ bool AABBQuantizedNoLeafTree::Build(AABBTree* tree)

// Get nodes
mNbNodes = NbTriangles-1;
AABBNoLeafNode* Nodes = xr_alloc<AABBNoLeafNode>(mNbNodes);
AABBNoLeafNode* Nodes = CALLOC(AABBNoLeafNode,mNbNodes);
CHECKALLOC (Nodes);
ZeroMemory (Nodes, mNbNodes*sizeof(AABBNoLeafNode));

Expand All @@ -526,7 +526,7 @@ bool AABBQuantizedNoLeafTree::Build(AABBTree* tree)

// Quantize
{
mNodes = xr_alloc<AABBQuantizedNoLeafNode>(mNbNodes);
mNodes = CALLOC(AABBQuantizedNoLeafNode,mNbNodes);
CHECKALLOC (mNodes);
ZeroMemory (mNodes,mNbNodes*sizeof(AABBQuantizedNoLeafNode));

Expand All @@ -545,7 +545,7 @@ bool AABBQuantizedNoLeafTree::Build(AABBTree* tree)
REMAP_DATA(mData2)
}

xr_free(Nodes);
CFREE(Nodes);
}

#ifdef __ICECORE_H__
Expand Down
6 changes: 3 additions & 3 deletions src/xrCDB/OPC_PlanesCollider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ PlanesCollider::PlanesCollider() :
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PlanesCollider::~PlanesCollider()
{
xr_free(mPlanes);
CFREE(mPlanes);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -144,8 +144,8 @@ BOOL PlanesCollider::InitQuery(PlanesCache& cache, const Plane* planes, udword n
// 2) Compute planes in model space
if(nb_planes>mNbPlanes)
{
xr_free(mPlanes);
mPlanes = xr_alloc<Plane> (nb_planes);
CFREE(mPlanes);
mPlanes = CALLOC(Plane,nb_planes);
}
mNbPlanes = nb_planes;

Expand Down
51 changes: 50 additions & 1 deletion src/xrCDB/StdAfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,59 @@

#ifndef stdafxH
#define stdafxH
#pragma once
//#pragma once

#include "../xrCore/xrCore.h"

#include "../xrCore/doug_lea_allocator.h"

#ifdef USE_ARENA_ALLOCATOR
extern doug_lea_allocator g_collision_allocator;

# define CNEW(type) new ( g_collision_allocator.alloc_impl<type>(1) ) type
# define CDELETE(ptr) cdelete(ptr)
# define CFREE(ptr) g_collision_allocator.free_impl(ptr)
# define CMALLOC(size) g_collision_allocator.malloc_impl(size)
# define CALLOC(type,count) g_collision_allocator.alloc_impl<type>(count)
#else // #ifdef USE_ARENA_ALLOCATOR
# define CNEW(type) new ( xr_alloc<type>(1) ) type
# define CDELETE(ptr) xr_delete(ptr)
# define CFREE(ptr) xr_free(ptr)
# define CMALLOC(size) xr_malloc(size)
# define CALLOC(type,count) xr_alloc<type>(count)
#endif // #ifdef USE_ARENA_ALLOCATOR

template <bool _is_pm, typename T>
struct cspecial_free
{
IC void operator()(T* &ptr)
{
void* _real_ptr = dynamic_cast<void*>(ptr);
ptr->~T ();
CFREE (_real_ptr);
}
};

template <typename T>
struct cspecial_free<false,T>
{
IC void operator()(T* &ptr)
{
ptr->~T ();
CFREE (ptr);
}
};

template <class T>
IC void cdelete (T* &ptr)
{
if (ptr)
{
cspecial_free<std::is_polymorphic<T>::value,T>()(ptr);
ptr = NULL;
}
}

#define ENGINE_API
#include "opcode.h"

Expand Down
32 changes: 19 additions & 13 deletions src/xrCDB/xrCDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

#include "xrCDB.h"

#ifdef USE_ARENA_ALLOCATOR
static const u32 s_arena_size = (128+16)*1024*1024;
static char s_fake_array[s_arena_size];
doug_lea_allocator g_collision_allocator( s_fake_array, s_arena_size, "collision" );
#endif // #ifdef USE_ARENA_ALLOCATOR

namespace Opcode {
# include "OPC_TreeBuilders.h"
} // namespace Opcode
Expand Down Expand Up @@ -46,9 +52,9 @@ MODEL::~MODEL()
{
syncronize (); // maybe model still in building
status = S_INIT;
xr_delete (tree);
xr_free (tris); tris_count = 0;
xr_free (verts); verts_count= 0;
CDELETE (tree);
CFREE (tris); tris_count = 0;
CFREE (verts); verts_count= 0;
}

struct BTHREAD_params
Expand Down Expand Up @@ -100,12 +106,12 @@ void MODEL::build_internal (Fvector* V, int Vcnt, TRI* T, int Tcnt, build_callba
{
// verts
verts_count = Vcnt;
verts = xr_alloc<Fvector> (verts_count);
verts = CALLOC(Fvector,verts_count);
CopyMemory (verts,V,verts_count*sizeof(Fvector));

// tris
tris_count = Tcnt;
tris = xr_alloc<TRI> (tris_count);
tris = CALLOC(TRI,tris_count);
CopyMemory (tris,T,tris_count*sizeof(TRI));

// callback
Expand All @@ -115,10 +121,10 @@ void MODEL::build_internal (Fvector* V, int Vcnt, TRI* T, int Tcnt, build_callba
status = S_BUILD;

// Allocate temporary "OPCODE" tris + convert tris to 'pointer' form
u32* temp_tris = xr_alloc<u32> (tris_count*3);
u32* temp_tris = CALLOC(u32,tris_count*3);
if (0==temp_tris) {
xr_free (verts);
xr_free (tris);
CFREE (verts);
CFREE (tris);
return;
}
u32* temp_ptr = temp_tris;
Expand All @@ -140,16 +146,16 @@ void MODEL::build_internal (Fvector* V, int Vcnt, TRI* T, int Tcnt, build_callba
OPCC.Quantized = false;
// if (Memory.debug_mode) OPCC.KeepOriginal = true;

tree = xr_new<OPCODE_Model> ();
tree = CNEW(OPCODE_Model) ();
if (!tree->Build(OPCC)) {
xr_free (verts);
xr_free (tris);
xr_free (temp_tris);
CFREE (verts);
CFREE (tris);
CFREE (temp_tris);
return;
};

// Free temporary tris
xr_free (temp_tris);
CFREE (temp_tris);
return;
}

Expand Down
Loading

0 comments on commit d9802d6

Please sign in to comment.