Don't copy memory to allocation if allocation has no storage

Change-Id: I3238bec02e5c1ec5877537318bb563e3d0f3799d
Signed-off-by: Mateusz Jablonski <mateusz.jablonski@intel.com>
This commit is contained in:
Mateusz Jablonski
2019-03-11 11:13:17 +01:00
committed by sys_ocldev
parent 74f1896c03
commit c0325b5d19
4 changed files with 12 additions and 11 deletions

View File

@@ -708,16 +708,18 @@ void Kernel::substituteKernelHeap(void *newKernelHeap, size_t newKernelHeapSize)
SKernelBinaryHeaderCommon *pHeader = const_cast<SKernelBinaryHeaderCommon *>(pKernelInfo->heapInfo.pKernelHeader);
pHeader->KernelHeapSize = static_cast<uint32_t>(newKernelHeapSize);
pKernelInfo->isKernelHeapSubstituted = true;
auto memoryManager = device.getMemoryManager();
auto currentAllocationSize = pKernelInfo->kernelAllocation->getUnderlyingBufferSize();
bool status = false;
if (currentAllocationSize >= newKernelHeapSize) {
memcpy_s(pKernelInfo->kernelAllocation->getUnderlyingBuffer(), newKernelHeapSize, newKernelHeap, newKernelHeapSize);
status = memoryManager->copyMemoryToAllocation(pKernelInfo->kernelAllocation, newKernelHeap, static_cast<uint32_t>(newKernelHeapSize));
} else {
auto memoryManager = device.getMemoryManager();
memoryManager->checkGpuUsageAndDestroyGraphicsAllocations(pKernelInfo->kernelAllocation);
pKernelInfo->kernelAllocation = nullptr;
pKernelInfo->createKernelAllocation(memoryManager);
status = pKernelInfo->createKernelAllocation(memoryManager);
}
UNRECOVERABLE_IF(!status);
}
bool Kernel::isKernelHeapSubstituted() const {

View File

@@ -406,7 +406,7 @@ HeapIndex MemoryManager::selectHeap(const GraphicsAllocation *allocation, const
return HeapIndex::HEAP_LIMITED;
}
bool MemoryManager::copyMemoryToAllocation(GraphicsAllocation *graphicsAllocation, const void *memoryToCopy, uint32_t sizeToCopy) const {
if (!graphicsAllocation || !memoryToCopy || sizeToCopy == 0u) {
if (!graphicsAllocation->getUnderlyingBuffer()) {
return false;
}
memcpy_s(graphicsAllocation->getUnderlyingBuffer(), graphicsAllocation->getUnderlyingBufferSize(), memoryToCopy, sizeToCopy);

View File

@@ -488,6 +488,9 @@ bool KernelInfo::createKernelAllocation(MemoryManager *memoryManager) {
UNRECOVERABLE_IF(kernelAllocation);
auto kernelIsaSize = heapInfo.pKernelHeader->KernelHeapSize;
kernelAllocation = memoryManager->allocateGraphicsMemoryWithProperties({kernelIsaSize, GraphicsAllocation::AllocationType::KERNEL_ISA});
if (!kernelAllocation) {
return false;
}
return memoryManager->copyMemoryToAllocation(kernelAllocation, heapInfo.pKernelHeap, kernelIsaSize);
}

View File

@@ -1723,16 +1723,12 @@ TEST_F(MemoryAllocatorTest, whenCommandStreamerIsNotRegisteredThenReturnNullEngi
auto engineControl = memoryManager->getRegisteredEngineForCsr(dummyCsr);
EXPECT_EQ(nullptr, engineControl);
}
TEST(MemoryManagerCopyMemoryTest, givenNullPointerOrZeroSizeWhenCopyMemoryToAllocationThenReturnFalse) {
TEST(MemoryManagerCopyMemoryTest, givenAllocationWithNoStorageWhenCopyMemoryToAllocationThenReturnFalse) {
ExecutionEnvironment executionEnvironment;
MockMemoryManager memoryManager(false, false, executionEnvironment);
constexpr uint8_t allocationSize = 10;
uint8_t allocationStorage[allocationSize];
MockGraphicsAllocation allocation{allocationStorage, allocationSize};
uint8_t memory = 1;
EXPECT_FALSE(memoryManager.copyMemoryToAllocation(nullptr, &memory, sizeof(memory)));
EXPECT_FALSE(memoryManager.copyMemoryToAllocation(&allocation, nullptr, sizeof(memory)));
EXPECT_FALSE(memoryManager.copyMemoryToAllocation(&allocation, &memory, 0u));
MockGraphicsAllocation invalidAllocation{nullptr, 0u};
EXPECT_FALSE(memoryManager.copyMemoryToAllocation(&invalidAllocation, &memory, sizeof(memory)));
}
TEST(MemoryManagerCopyMemoryTest, givenValidAllocationAndMemoryWhenCopyMemoryToAllocationThenDataIsCopied) {
ExecutionEnvironment executionEnvironment;