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

Method allocate

src/memory/SingleFrameAllocator.cpp:59–91  ·  view source on GitHub ↗

Allocate memory of a given size (in bytes) and return a pointer to the allocated memory. Allocated memory must be 16-bytes aligned.

Source from the content-addressed store, hash-verified

57// Allocate memory of a given size (in bytes) and return a pointer to the
58// allocated memory. Allocated memory must be 16-bytes aligned.
59void* SingleFrameAllocator::allocate(size_t size) {
60
61 // Lock the method with a mutex
62 std::lock_guard<std::mutex> lock(mMutex);
63
64 // Allocate a little bit more memory to make sure we can return an aligned address
65 const size_t totalSize = size + GLOBAL_ALIGNMENT;
66
67 // Check that there is enough remaining memory in the buffer
68 if (mCurrentOffset + totalSize > mTotalSizeBytes) {
69
70 // We need to allocate more memory next time reset() is called
71 mNeedToAllocatedMore = true;
72
73 // Return default memory allocation
74 return mBaseAllocator.allocate(size);
75 }
76
77 // Next available memory location
78 void* nextAvailableMemory = mMemoryBufferStart + mCurrentOffset;
79
80 // Compute the next aligned memory address
81 nextAvailableMemory = alignAddress(nextAvailableMemory, GLOBAL_ALIGNMENT);
82
83 // Increment the offset
84 mCurrentOffset += totalSize;
85
86 // Check that allocated memory is 16-bytes aligned
87 assert(reinterpret_cast<uintptr_t>(nextAvailableMemory) % GLOBAL_ALIGNMENT == 0);
88
89 // Return the next available memory location
90 return nextAvailableMemory;
91}
92
93// Release previously allocated memory.
94void SingleFrameAllocator::release(void* pointer, size_t size) {

Callers 2

SingleFrameAllocatorMethod · 0.45
resetMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected