MCPcopy Create free account
hub / github.com/DanielChappuis/reactphysics3d / allocate

Method allocate

src/components/JointComponents.cpp:43–103  ·  view source on GitHub ↗

Allocate memory for a given number of components

Source from the content-addressed store, hash-verified

41
42// Allocate memory for a given number of components
43void JointComponents::allocate(uint32 nbComponentsToAllocate) {
44
45 assert(nbComponentsToAllocate > mNbAllocatedComponents);
46
47 // Make sure capacity is an integral multiple of alignment
48 nbComponentsToAllocate = std::ceil(nbComponentsToAllocate / float(GLOBAL_ALIGNMENT)) * GLOBAL_ALIGNMENT;
49
50 // Size for the data of a single component (in bytes)
51 const size_t totalSizeBytes = nbComponentsToAllocate * mComponentDataSize + mAlignmentMarginSize;
52
53 // Allocate memory
54 void* newBuffer = mMemoryAllocator.allocate(totalSizeBytes);
55 assert(newBuffer != nullptr);
56 assert(reinterpret_cast<uintptr_t>(newBuffer) % GLOBAL_ALIGNMENT == 0);
57
58 // New pointers to components data
59 Entity* newJointsEntities = static_cast<Entity*>(newBuffer);
60 Entity* newBody1Entities = reinterpret_cast<Entity*>(MemoryAllocator::alignAddress(newJointsEntities + nbComponentsToAllocate, GLOBAL_ALIGNMENT));
61 assert(reinterpret_cast<uintptr_t>(newBody1Entities) % GLOBAL_ALIGNMENT == 0);
62 Entity* newBody2Entities = reinterpret_cast<Entity*>(MemoryAllocator::alignAddress(newBody1Entities + nbComponentsToAllocate, GLOBAL_ALIGNMENT));
63 assert(reinterpret_cast<uintptr_t>(newBody2Entities) % GLOBAL_ALIGNMENT == 0);
64 Joint** newJoints = reinterpret_cast<Joint**>(MemoryAllocator::alignAddress(newBody2Entities + nbComponentsToAllocate, GLOBAL_ALIGNMENT));
65 assert(reinterpret_cast<uintptr_t>(newJoints) % GLOBAL_ALIGNMENT == 0);
66 JointType* newTypes = reinterpret_cast<JointType*>(MemoryAllocator::alignAddress(newJoints + nbComponentsToAllocate, GLOBAL_ALIGNMENT));
67 assert(reinterpret_cast<uintptr_t>(newTypes) % GLOBAL_ALIGNMENT == 0);
68 JointsPositionCorrectionTechnique* newPositionCorrectionTechniques = reinterpret_cast<JointsPositionCorrectionTechnique*>(MemoryAllocator::alignAddress(newTypes + nbComponentsToAllocate, GLOBAL_ALIGNMENT));
69 assert(reinterpret_cast<uintptr_t>(newPositionCorrectionTechniques) % GLOBAL_ALIGNMENT == 0);
70 bool* newIsCollisionEnabled = reinterpret_cast<bool*>(MemoryAllocator::alignAddress(newPositionCorrectionTechniques + nbComponentsToAllocate, GLOBAL_ALIGNMENT));
71 assert(reinterpret_cast<uintptr_t>(newIsCollisionEnabled) % GLOBAL_ALIGNMENT == 0);
72 bool* newIsAlreadyInIsland = reinterpret_cast<bool*>(MemoryAllocator::alignAddress(newIsCollisionEnabled + nbComponentsToAllocate, GLOBAL_ALIGNMENT));
73 assert(reinterpret_cast<uintptr_t>(newIsAlreadyInIsland) % GLOBAL_ALIGNMENT == 0);
74 assert(reinterpret_cast<uintptr_t>(newIsAlreadyInIsland + nbComponentsToAllocate) <= reinterpret_cast<uintptr_t>(newBuffer) + totalSizeBytes);
75
76 // If there was already components before
77 if (mNbComponents > 0) {
78
79 // Copy component data from the previous buffer to the new one
80 memcpy(newJointsEntities, mJointEntities, mNbComponents * sizeof(Entity));
81 memcpy(newBody1Entities, mBody1Entities, mNbComponents * sizeof(Entity));
82 memcpy(newBody2Entities, mBody2Entities, mNbComponents * sizeof(Entity));
83 memcpy(newJoints, mJoints, mNbComponents * sizeof(Joint*));
84 memcpy(newTypes, mTypes, mNbComponents * sizeof(JointType));
85 memcpy(newPositionCorrectionTechniques, mPositionCorrectionTechniques, mNbComponents * sizeof(JointsPositionCorrectionTechnique));
86 memcpy(newIsCollisionEnabled, mIsCollisionEnabled, mNbComponents * sizeof(bool));
87 memcpy(newIsAlreadyInIsland, mIsAlreadyInIsland, mNbComponents * sizeof(bool));
88
89 // Deallocate previous memory
90 mMemoryAllocator.release(mBuffer, mNbAllocatedComponents * mComponentDataSize);
91 }
92
93 mBuffer = newBuffer;
94 mNbAllocatedComponents = nbComponentsToAllocate;
95 mJointEntities = newJointsEntities;
96 mBody1Entities = newBody1Entities;
97 mBody2Entities = newBody2Entities;
98 mJoints = newJoints;
99 mTypes = newTypes;
100 mPositionCorrectionTechniques = newPositionCorrectionTechniques;

Callers

nothing calls this directly

Calls 1

releaseMethod · 0.45

Tested by

no test coverage detected