1616#include " shared/source/helpers/heap_helper.h"
1717#include " shared/source/helpers/hw_helper.h"
1818#include " shared/source/indirect_heap/indirect_heap.h"
19+ #include " shared/source/memory_manager/allocations_list.h"
1920#include " shared/source/memory_manager/memory_manager.h"
2021
2122namespace NEO {
@@ -26,11 +27,7 @@ CommandContainer::~CommandContainer() {
2627 return ;
2728 }
2829
29- auto memoryManager = device->getMemoryManager ();
30-
31- for (auto *alloc : cmdBufferAllocations) {
32- memoryManager->freeGraphicsMemory (alloc);
33- }
30+ this ->handleCmdBufferAllocations (0u );
3431
3532 for (auto allocationIndirectHeap : allocationIndirectHeaps) {
3633 if (heapHelper) {
@@ -44,19 +41,14 @@ CommandContainer::~CommandContainer() {
4441 }
4542}
4643
47- ErrorCode CommandContainer::initialize (Device *device) {
44+ ErrorCode CommandContainer::initialize (Device *device, AllocationsList *reusableAllocationList ) {
4845 this ->device = device;
46+ this ->reusableAllocationList = reusableAllocationList;
4947
5048 size_t alignedSize = alignUp<size_t >(totalCmdBufferSize, MemoryConstants::pageSize64k);
51- AllocationProperties properties{device->getRootDeviceIndex (),
52- true /* allocateMemory*/ ,
53- alignedSize,
54- GraphicsAllocation::AllocationType::COMMAND_BUFFER,
55- (device->getNumGenericSubDevices () > 1u ) /* multiOsContextCapable */ ,
56- false ,
57- device->getDeviceBitfield ()};
58-
59- auto cmdBufferAllocation = device->getMemoryManager ()->allocateGraphicsMemoryWithProperties (properties);
49+
50+ auto cmdBufferAllocation = this ->obtainNextCommandBufferAllocation ();
51+
6052 if (!cmdBufferAllocation) {
6153 return ErrorCode::OUT_OF_DEVICE_MEMORY;
6254 }
@@ -124,9 +116,7 @@ void CommandContainer::reset() {
124116 getDeallocationContainer ().clear ();
125117 sshAllocations.clear ();
126118
127- for (size_t i = 1 ; i < cmdBufferAllocations.size (); i++) {
128- device->getMemoryManager ()->freeGraphicsMemory (cmdBufferAllocations[i]);
129- }
119+ this ->handleCmdBufferAllocations (1u );
130120 cmdBufferAllocations.erase (cmdBufferAllocations.begin () + 1 , cmdBufferAllocations.end ());
131121
132122 commandStream->replaceBuffer (cmdBufferAllocations[0 ]->getUnderlyingBuffer (),
@@ -220,17 +210,40 @@ IndirectHeap *CommandContainer::getHeapWithRequiredSizeAndAlignment(HeapType hea
220210 return indirectHeap;
221211}
222212
223- void CommandContainer::allocateNextCommandBuffer () {
213+ void CommandContainer::handleCmdBufferAllocations (size_t startIndex) {
214+ for (size_t i = startIndex; i < cmdBufferAllocations.size (); i++) {
215+ if (this ->reusableAllocationList ) {
216+ reusableAllocationList->pushFrontOne (*cmdBufferAllocations[i]);
217+ } else {
218+ this ->device ->getMemoryManager ()->freeGraphicsMemory (cmdBufferAllocations[i]);
219+ }
220+ }
221+ }
222+
223+ GraphicsAllocation *CommandContainer::obtainNextCommandBufferAllocation () {
224224 size_t alignedSize = alignUp<size_t >(totalCmdBufferSize, MemoryConstants::pageSize64k);
225- AllocationProperties properties{device->getRootDeviceIndex (),
226- true /* allocateMemory*/ ,
227- alignedSize,
228- GraphicsAllocation::AllocationType::COMMAND_BUFFER,
229- (device->getNumGenericSubDevices () > 1u ) /* multiOsContextCapable */ ,
230- false ,
231- device->getDeviceBitfield ()};
232-
233- auto cmdBufferAllocation = device->getMemoryManager ()->allocateGraphicsMemoryWithProperties (properties);
225+
226+ GraphicsAllocation *cmdBufferAllocation = nullptr ;
227+ if (this ->reusableAllocationList ) {
228+ cmdBufferAllocation = this ->reusableAllocationList ->detachAllocation (alignedSize, nullptr , nullptr , GraphicsAllocation::AllocationType::COMMAND_BUFFER).release ();
229+ }
230+ if (!cmdBufferAllocation) {
231+ AllocationProperties properties{device->getRootDeviceIndex (),
232+ true /* allocateMemory*/ ,
233+ alignedSize,
234+ GraphicsAllocation::AllocationType::COMMAND_BUFFER,
235+ (device->getNumGenericSubDevices () > 1u ) /* multiOsContextCapable */ ,
236+ false ,
237+ device->getDeviceBitfield ()};
238+
239+ cmdBufferAllocation = device->getMemoryManager ()->allocateGraphicsMemoryWithProperties (properties);
240+ }
241+
242+ return cmdBufferAllocation;
243+ }
244+
245+ void CommandContainer::allocateNextCommandBuffer () {
246+ auto cmdBufferAllocation = this ->obtainNextCommandBufferAllocation ();
234247 UNRECOVERABLE_IF (!cmdBufferAllocation);
235248
236249 cmdBufferAllocations.push_back (cmdBufferAllocation);
0 commit comments