38 #ifndef EFFICIENT_ALIGNMENT
39 #define EFFICIENT_ALIGNMENT 16
48 #define dEFFICIENT_SIZE(x) (((x)+(EFFICIENT_ALIGNMENT-1)) & ~((size_t)(EFFICIENT_ALIGNMENT-1)))
49 #define dEFFICIENT_PTR(p) ((void *)dEFFICIENT_SIZE((size_t)(p)))
50 #define dOFFSET_EFFICIENTLY(p, b) ((void *)((size_t)(p) + dEFFICIENT_SIZE(b)))
55 #define dALLOCA16(n) \
56 dEFFICIENT_PTR(alloca((n)+(EFFICIENT_ALIGNMENT)))
60 #define SIZE_MAX ((size_t)(-1))
63 void dInternalHandleAutoDisabling (
dxWorld *world, dReal stepsize);
64 void dxStepBody (
dxBody *b, dReal h);
70 typedef void *(*alloc_block_fn_t)(
size_t block_size);
71 typedef void *(*shrink_block_fn_t)(
void *block_pointer,
size_t block_current_size,
size_t block_smaller_size);
72 typedef void (*free_block_fn_t)(
void *block_pointer,
size_t block_current_size);
76 Assign(fnAlloc, fnShrink, fnFree);
79 void Assign(alloc_block_fn_t fnAlloc, shrink_block_fn_t fnShrink, free_block_fn_t fnFree)
82 m_fnShrink = fnShrink;
86 alloc_block_fn_t m_fnAlloc;
87 shrink_block_fn_t m_fnShrink;
88 free_block_fn_t m_fnFree;
98 Assign(fReserveFactor, uiReserveMinimum);
101 void Assign(
float fReserveFactor,
unsigned uiReserveMinimum)
103 m_fReserveFactor = fReserveFactor;
104 m_uiReserveMinimum = uiReserveMinimum;
107 float m_fReserveFactor;
108 unsigned m_uiReserveMinimum;
118 #define BUFFER_TO_ARENA_EXTRA (EFFICIENT_ALIGNMENT + dEFFICIENT_SIZE(sizeof(dxWorldProcessMemArena)))
119 static bool IsArenaPossible(
size_t nBufferSize)
121 return SIZE_MAX - BUFFER_TO_ARENA_EXTRA >= nBufferSize;
124 static size_t MakeBufferSize(
size_t nArenaSize)
126 return nArenaSize - BUFFER_TO_ARENA_EXTRA;
129 static size_t MakeArenaSize(
size_t nBufferSize)
131 return BUFFER_TO_ARENA_EXTRA + nBufferSize;
133 #undef BUFFER_TO_ARENA_EXTRA
135 bool IsStructureValid()
const
137 return m_pAllocBegin != NULL && m_pAllocEnd != NULL && m_pAllocBegin <= m_pAllocEnd
138 && (m_pAllocCurrentOrNextArena == NULL || m_pAllocCurrentOrNextArena == m_pAllocBegin)
139 && m_pArenaBegin != NULL && m_pArenaBegin <= m_pAllocBegin;
142 size_t GetMemorySize()
const
144 return (
size_t)m_pAllocEnd - (size_t)m_pAllocBegin;
147 void *SaveState()
const
149 return m_pAllocCurrentOrNextArena;
152 void RestoreState(
void *state)
154 m_pAllocCurrentOrNextArena = state;
159 m_pAllocCurrentOrNextArena = m_pAllocBegin;
162 void *PeekBufferRemainder()
const
164 return m_pAllocCurrentOrNextArena;
167 void *AllocateBlock(
size_t size)
169 void *block = m_pAllocCurrentOrNextArena;
170 m_pAllocCurrentOrNextArena = dOFFSET_EFFICIENTLY(block, size);
171 dIASSERT(m_pAllocCurrentOrNextArena <= m_pAllocEnd);
175 template<
typename ElementType>
176 ElementType *AllocateArray(
size_t count)
178 return (ElementType *)AllocateBlock(count *
sizeof(ElementType));
181 template<
typename ElementType>
182 void ShrinkArray(ElementType *arr,
size_t oldcount,
size_t newcount)
184 dIASSERT(newcount <= oldcount);
185 dIASSERT(dOFFSET_EFFICIENTLY(arr, oldcount *
sizeof(ElementType)) == m_pAllocCurrentOrNextArena);
186 m_pAllocCurrentOrNextArena = dOFFSET_EFFICIENTLY(arr, newcount *
sizeof(ElementType));
196 void SetNextMemArena(
dxWorldProcessMemArena *pArenaInstance) { m_pAllocCurrentOrNextArena = pArenaInstance; }
199 static size_t AdjustArenaSizeForReserveRequirements(
size_t arenareq,
float rsrvfactor,
unsigned rsrvminimum);
202 void *m_pAllocCurrentOrNextArena;
217 void CleanupWorldReferences(
dxWorld *pswWorldInstance);
220 bool EnsureStepperSyncObjectsAreAllocated(
dxWorld *pswWorldInstance);
221 dCallWaitID GetIslandsSteppingWait()
const {
return m_pcwIslandsSteppingWait; }
229 bool ReallocateStepperMemArenas(
dxWorld *world,
unsigned nIslandThreadsCount,
size_t nMemoryRequirement,
247 void LockForAddLimotSerialization();
248 void UnlockForAddLimotSerialization();
249 void LockForStepbodySerialization();
250 void UnlockForStepbodySerialization();
253 enum dxProcessContextMutex
255 dxPCM_STEPPER_ARENA_OBTAIN,
256 dxPCM_STEPPER_ADDLIMOT_SERIALIZE,
257 dxPCM_STEPPER_STEPBODY_SERIALIZE,
262 static const char *
const m_aszContextMutexNames[dxPCM__MAX];
267 dxWorld *m_pswObjectsAllocWorld;
268 dMutexGroupID m_pmgStepperMutexGroup;
269 dCallWaitID m_pcwIslandsSteppingWait;
274 void AssignInfo(
size_t islandcount,
unsigned int const *islandsizes,
dxBody *
const *bodies, dxJoint *
const *joints)
276 m_IslandCount = islandcount;
277 m_pIslandSizes = islandsizes;
282 size_t GetIslandsCount()
const {
return m_IslandCount; }
283 unsigned int const *GetIslandSizes()
const {
return m_pIslandSizes; }
284 dxBody *
const *GetBodiesArray()
const {
return m_pBodies; }
285 dxJoint *
const *GetJointsArray()
const {
return m_pJoints; }
288 size_t m_IslandCount;
289 unsigned int const *m_pIslandSizes;
291 dxJoint *
const *m_pJoints;
298 m_world(world), m_stepSize(stepSize), m_stepperArena(stepperArena), m_finalReleasee(NULL),
299 m_islandBodiesStart(islandBodiesStart), m_islandJointsStart(islandJointsStart), m_islandBodiesCount(0), m_islandJointsCount(0),
300 m_stepperAllowedThreads(stepperAllowedThreads)
304 void AssignIslandSelection(
dxBody *
const *islandBodiesStart, dxJoint *
const *islandJointsStart,
305 unsigned islandBodiesCount,
unsigned islandJointsCount)
307 m_islandBodiesStart = islandBodiesStart;
308 m_islandJointsStart = islandJointsStart;
309 m_islandBodiesCount = islandBodiesCount;
310 m_islandJointsCount = islandJointsCount;
313 dxBody *
const *GetSelectedIslandBodiesEnd()
const {
return m_islandBodiesStart + m_islandBodiesCount; }
314 dxJoint *
const *GetSelectedIslandJointsEnd()
const {
return m_islandJointsStart + m_islandJointsCount; }
316 void AssignStepperCallFinalReleasee(dCallReleaseeID finalReleasee)
318 m_finalReleasee = finalReleasee;
322 dReal
const m_stepSize;
324 dCallReleaseeID m_finalReleasee;
325 dxBody *
const *m_islandBodiesStart;
326 dxJoint *
const *m_islandJointsStart;
327 unsigned m_islandBodiesCount;
328 unsigned m_islandJointsCount;
329 unsigned m_stepperAllowedThreads;
332 #define BEGIN_STATE_SAVE(memarena, state) void *state = memarena->SaveState();
333 #define END_STATE_SAVE(memarena, state) memarena->RestoreState(state)
336 typedef unsigned (*dmaxcallcountestimate_fn_t) (
unsigned activeThreadCount,
unsigned allowedThreadCount);
339 dReal stepSize, dstepper_fn_t stepper, dmaxcallcountestimate_fn_t maxCallCountEstimator);
342 typedef size_t (*dmemestimate_fn_t) (
dxBody *
const *body,
unsigned int nb,
343 dxJoint *
const *_joint,
unsigned int _nj);
346 dReal stepsize, dmemestimate_fn_t stepperestimate);
353 template<
class ClassType>
354 inline ClassType *AllocateOnDemand(ClassType *&pctStorage)
356 ClassType *pctCurrentInstance = pctStorage;
358 if (!pctCurrentInstance)
360 pctCurrentInstance =
new ClassType();
361 pctStorage = pctCurrentInstance;
364 return pctCurrentInstance;
373 dxStepWorkingMemory(): m_uiRefCount(1), m_ppcProcessingContext(NULL), m_priReserveInfo(NULL), m_pmmMemoryManager(NULL) {}
379 delete m_ppcProcessingContext;
380 delete m_priReserveInfo;
381 delete m_pmmMemoryManager;
387 dIASSERT(~m_uiRefCount != 0);
393 dIASSERT(m_uiRefCount != 0);
394 if (--m_uiRefCount == 0)
403 delete m_ppcProcessingContext;
404 m_ppcProcessingContext = NULL;
407 void CleanupWorldReferences(
dxWorld *world)
409 if (m_ppcProcessingContext != NULL)
411 m_ppcProcessingContext->CleanupWorldReferences(world);
416 dxWorldProcessContext *SureGetWorldProcessingContext() {
return AllocateOnDemand(m_ppcProcessingContext); }
420 const dxWorldProcessMemoryReserveInfo *SureGetMemoryReserveInfo()
const {
return m_priReserveInfo ? m_priReserveInfo : &g_WorldProcessDefaultReserveInfo; }
421 void SetMemoryReserveInfo(
float fReserveFactor,
unsigned uiReserveMinimum)
423 if (m_priReserveInfo) { m_priReserveInfo->Assign(fReserveFactor, uiReserveMinimum); }
426 void ResetMemoryReserveInfoToDefault()
428 if (m_priReserveInfo) {
delete m_priReserveInfo; m_priReserveInfo = NULL; }
432 const dxWorldProcessMemoryManager *SureGetMemoryManager()
const {
return m_pmmMemoryManager ? m_pmmMemoryManager : &g_WorldProcessMallocMemoryManager; }
433 void SetMemoryManager(dxWorldProcessMemoryManager::alloc_block_fn_t fnAlloc,
434 dxWorldProcessMemoryManager::shrink_block_fn_t fnShrink,
435 dxWorldProcessMemoryManager::free_block_fn_t fnFree)
437 if (m_pmmMemoryManager) { m_pmmMemoryManager->Assign(fnAlloc, fnShrink, fnFree); }
440 void ResetMemoryManagerToDefault()
442 if (m_pmmMemoryManager) {
delete m_pmmMemoryManager; m_pmmMemoryManager = NULL; }
446 unsigned m_uiRefCount;
Definition: ode/src/objects.h:57
Definition: ode/src/objects.h:161
Definition: ode/src/objects.h:131