Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool AudioPool::MemoryPool::Allocate(MemoryChunk & chunk, size_t size)
- {
- size_t alignMask = (AUDREN_BUFFER_ALIGNMENT - 1);
- if (size && alignMask)
- {
- if (size > UINTPTR_MAX - alignMask)
- return false;
- size = (size + alignMask) &~ alignMask;
- }
- for (auto block = first; block; block = block->next)
- {
- auto address = block->base;
- size_t waste = (size_t)address & alignMask;
- LOG("waste: %zu", waste);
- if (waste > 0)
- waste = alignMask + 1 - waste;
- LOG("block size: %zu", block->size)
- if (waste > block->size)
- continue;
- address += waste;
- size_t blockSize = block->size - waste;
- LOG("blockSize: %zu < size: %zu", blockSize, size);
- if (blockSize < size)
- continue;
- // found space
- chunk.address = address;
- chunk.size = size;
- if (!waste)
- {
- block->base += size;
- block->size -= size;
- LOG("No wasted space")
- if (!block->size)
- AudioPool::MemoryPool::DeleteBlock(block);
- LOG("Done!")
- }
- else
- {
- LOG("Wasted space!")
- auto nextAddress = address + size;
- auto nextSize = blockSize - size;
- block->size = waste;
- if (nextSize)
- {
- LOG("Next size! Creating..")
- auto next = MemoryBlock::Create(nextAddress, nextSize);
- LOG("Done.")
- if (next)
- AudioPool::MemoryPool::InsertAfter(block, next);
- else
- chunk.size += nextSize;
- }
- }
- LOG("allocated!")
- return true;
- }
- LOG("failed to allocate")
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement