1010#include " runtime/memory_manager/memory_manager.h"
1111
1212namespace OCLRT {
13- InternalAllocationStorage::InternalAllocationStorage (CommandStreamReceiver &commandStreamReceiver) : commandStreamReceiver(commandStreamReceiver){};
13+ InternalAllocationStorage::InternalAllocationStorage (CommandStreamReceiver &commandStreamReceiver) : commandStreamReceiver(commandStreamReceiver), contextId(commandStreamReceiver.getDeviceIndex()) {};
1414void InternalAllocationStorage::storeAllocation (std::unique_ptr<GraphicsAllocation> gfxAllocation, uint32_t allocationUsage) {
15- uint32_t taskCount = gfxAllocation->taskCount ;
15+ uint32_t taskCount = gfxAllocation->getTaskCount (contextId) ;
1616
1717 if (allocationUsage == REUSABLE_ALLOCATION) {
1818 taskCount = commandStreamReceiver.peekTaskCount ();
@@ -28,7 +28,7 @@ void InternalAllocationStorage::storeAllocationWithTaskCount(std::unique_ptr<Gra
2828 }
2929 }
3030 auto &allocationsList = (allocationUsage == TEMPORARY_ALLOCATION) ? temporaryAllocations : allocationsForReuse;
31- gfxAllocation->taskCount = taskCount ;
31+ gfxAllocation->updateTaskCount ( taskCount, contextId) ;
3232 allocationsList.pushTailOne (*gfxAllocation.release ());
3333}
3434
@@ -43,7 +43,7 @@ void InternalAllocationStorage::freeAllocationsList(uint32_t waitTaskCount, Allo
4343 IDList<GraphicsAllocation, false , true > allocationsLeft;
4444 while (curr != nullptr ) {
4545 auto *next = curr->next ;
46- if (curr->taskCount <= waitTaskCount) {
46+ if (curr->getTaskCount (contextId) <= waitTaskCount) {
4747 memoryManager->freeGraphicsMemory (curr);
4848 } else {
4949 allocationsLeft.pushTailOne (*curr);
@@ -57,8 +57,41 @@ void InternalAllocationStorage::freeAllocationsList(uint32_t waitTaskCount, Allo
5757}
5858
5959std::unique_ptr<GraphicsAllocation> InternalAllocationStorage::obtainReusableAllocation (size_t requiredSize, bool internalAllocation) {
60- auto allocation = allocationsForReuse.detachAllocation (requiredSize, commandStreamReceiver. getTagAddress () , internalAllocation);
60+ auto allocation = allocationsForReuse.detachAllocation (requiredSize, commandStreamReceiver, internalAllocation);
6161 return allocation;
6262}
6363
64+ struct ReusableAllocationRequirements {
65+ size_t requiredMinimalSize;
66+ volatile uint32_t *csrTagAddress;
67+ bool internalAllocationRequired;
68+ uint32_t contextId;
69+ };
70+
71+ std::unique_ptr<GraphicsAllocation> AllocationsList::detachAllocation (size_t requiredMinimalSize, CommandStreamReceiver &commandStreamReceiver, bool internalAllocationRequired) {
72+ ReusableAllocationRequirements req;
73+ req.requiredMinimalSize = requiredMinimalSize;
74+ req.csrTagAddress = commandStreamReceiver.getTagAddress ();
75+ req.internalAllocationRequired = internalAllocationRequired;
76+ req.contextId = commandStreamReceiver.getDeviceIndex ();
77+ GraphicsAllocation *a = nullptr ;
78+ GraphicsAllocation *retAlloc = processLocked<AllocationsList, &AllocationsList::detachAllocationImpl>(a, static_cast <void *>(&req));
79+ return std::unique_ptr<GraphicsAllocation>(retAlloc);
80+ }
81+
82+ GraphicsAllocation *AllocationsList::detachAllocationImpl (GraphicsAllocation *, void *data) {
83+ ReusableAllocationRequirements *req = static_cast <ReusableAllocationRequirements *>(data);
84+ auto *curr = head;
85+ while (curr != nullptr ) {
86+ auto currentTagValue = *req->csrTagAddress ;
87+ if ((req->internalAllocationRequired == curr->is32BitAllocation ) &&
88+ (curr->getUnderlyingBufferSize () >= req->requiredMinimalSize ) &&
89+ ((currentTagValue > curr->getTaskCount (req->contextId )) || (curr->getTaskCount (req->contextId ) == 0 ))) {
90+ return removeOneImpl (curr, nullptr );
91+ }
92+ curr = curr->next ;
93+ }
94+ return nullptr ;
95+ }
96+
6497} // namespace OCLRT
0 commit comments