| 23 | } |
| 24 | |
| 25 | void* ArenaAllocator::Allocate(uint64 size, uint64 alignment) |
| 26 | { |
| 27 | if (size == 0) |
| 28 | return nullptr; |
| 29 | if (alignment < PLATFORM_MEMORY_ALIGNMENT) |
| 30 | alignment = PLATFORM_MEMORY_ALIGNMENT; |
| 31 | |
| 32 | // Find the first page that has some space left |
| 33 | Page* page = _first; |
| 34 | while (page && page->Offset + size + alignment > page->Size) |
| 35 | page = page->Next; |
| 36 | |
| 37 | // Create a new page if need to |
| 38 | if (!page) |
| 39 | { |
| 40 | uint64 pageSize = Math::Max<uint64>(_pageSize, size + alignment + sizeof(Page)); |
| 41 | #if COMPILE_WITH_PROFILER |
| 42 | ProfilerMemory::OnGroupUpdate(ProfilerMemory::Groups::MallocArena, (int64)pageSize, 1); |
| 43 | #endif |
| 44 | page = (Page*)Allocator::Allocate(pageSize); |
| 45 | page->Next = _first; |
| 46 | page->Offset = sizeof(Page); |
| 47 | page->Size = (uint32)pageSize; |
| 48 | _first = page; |
| 49 | } |
| 50 | |
| 51 | // Allocate within a page |
| 52 | page->Offset = Math::AlignUp(page->Offset, (uint32)alignment); |
| 53 | void* mem = (byte*)page + page->Offset; |
| 54 | page->Offset += (uint32)size; |
| 55 | |
| 56 | return mem; |
| 57 | } |
| 58 | |
| 59 | void ConcurrentArenaAllocator::Free() |
| 60 | { |
no test coverage detected