Allocate memory of a given size (in bytes) and return a pointer to the allocated memory. Allocated memory must be 16-bytes aligned.
| 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. |
| 59 | void* 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. |
| 94 | void SingleFrameAllocator::release(void* pointer, size_t size) { |
no outgoing calls
no test coverage detected