Skip to content

Commit 540ff83

Browse files
committed
[Misc] Cleaned up AllocData() function for maintainability in VirtualCommandBuffer class.
Updated commit as previous one calculated the allocation size wrong.
1 parent f7675d5 commit 540ff83

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

sources/Renderer/VirtualCommandBuffer.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -397,38 +397,41 @@ class VirtualCommandBuffer
397397
// @param headerSize: Specifies extra data to be allocated in front of the returned data block, excluded from the memory alignment.
398398
char* AllocData(std::size_t size, std::size_t alignment = 0, std::size_t headerSize = 0)
399399
{
400-
/* Allocate one extra byte to store offset for alignment */
401-
size += sizeof(AlignOffsetType) + headerSize;
400+
LLGL_ASSERT((alignment & (alignment - 1)) == 0, "alignment must be a power-of-two, but %uz was specified", alignment);
402401

403-
/* Check if new data fits into current chunk */
404-
const std::size_t estimatedPadding = (alignment > 0 ? alignment - 1 : 0);
405-
const std::size_t sizeWithEstimatedPadding = size + estimatedPadding;
402+
/* Reserve space to store alignment offset and optional header */
403+
const std::size_t sizeWithOffsetAndHeader = size + sizeof(AlignOffsetType) + headerSize;
404+
405+
/* Check if new data fits into current chunk (Only alignment > 1 offsets the allocated pointer) */
406+
const std::size_t estimatedPadding = (alignment > 1 ? alignment - 1 : 0);
407+
const std::size_t sizeWithEstimatedPadding = sizeWithOffsetAndHeader + estimatedPadding;
406408

407409
if (!FitsIntoCurrentChunk(sizeWithEstimatedPadding))
408410
AllocNextChunk(std::max(NextCapacity(), sizeWithEstimatedPadding));
409411

410412
/* Allocate new data block within current chunk and apply memory alignment */
411-
char* data = VirtualCommandBuffer::GetChunkData(current_) + current_->size;
412-
data += sizeof(AlignOffsetType);
413+
char* basePtr = VirtualCommandBuffer::GetChunkData(current_) + current_->size;
414+
char* payloadPtr = basePtr + sizeof(AlignOffsetType);
413415

414-
const std::uintptr_t basePtr = reinterpret_cast<std::uintptr_t>(data);
415-
const std::uintptr_t offset = GetAlignedSize(basePtr + headerSize, static_cast<std::uintptr_t>(alignment)) - basePtr;
416+
const std::uintptr_t baseAddr = reinterpret_cast<std::uintptr_t>(payloadPtr);
417+
const std::uintptr_t alignOffset = GetAlignedSize(baseAddr + headerSize, static_cast<std::uintptr_t>(alignment)) - baseAddr;
416418

417-
/* Write offset for alignment at first byte in new data block */
419+
/* Write alignment offset at first byte in new data block */
418420
constexpr std::uintptr_t maxAlignmentOffset = std::numeric_limits<AlignOffsetType>::max();
419-
LLGL_ASSERT(offset <= maxAlignmentOffset);
421+
LLGL_ASSERT(alignOffset <= maxAlignmentOffset);
420422

421-
*reinterpret_cast<AlignOffsetType*>(data - sizeof(AlignOffsetType)) = static_cast<AlignOffsetType>(offset);
423+
*reinterpret_cast<AlignOffsetType*>(basePtr) = static_cast<AlignOffsetType>(alignOffset);
422424

423-
/* Now shift return pointer to aligned offset minus the input alignment shift (for opcode) */
424-
data += offset;
425-
size += offset - headerSize;
425+
/* Now shift return pointer to aligned offset and determine final allocation size */
426+
char* alignedPtr = payloadPtr + alignOffset;
427+
const std::size_t finalAllocSize = sizeWithOffsetAndHeader + alignOffset - headerSize;
426428

427429
/* Keep track of chunk size */
428-
current_->size += size;
430+
current_->size += finalAllocSize;
429431
LLGL_ASSERT(current_->size <= current_->capacity);
430-
size_ += size;
431-
return data;
432+
size_ += finalAllocSize;
433+
434+
return alignedPtr;
432435
}
433436

434437
// Allocates a data block with alignment and adds the opcode *before* the aligned payload,

0 commit comments

Comments
 (0)