From 4475e2228d14eeb2853014f0a5f3f489e6c7302f Mon Sep 17 00:00:00 2001 From: "Singh, Prasoon" Date: Wed, 29 Mar 2023 07:03:39 +0000 Subject: [PATCH] [Sysman] Replace normal pointers with smart pointers (7/n) Replacing normal pointers by smart pointers in memory module of L0 sysman. Related-To: LOCI-2810 Signed-off-by: Singh, Prasoon --- .../source/sysman/memory/linux/os_memory_imp.cpp | 6 +++--- .../sysman/memory/linux/os_memory_imp_dg1.cpp | 6 +++--- .../sysman/memory/linux/os_memory_imp_prelim.cpp | 6 +++--- level_zero/tools/source/sysman/memory/memory.cpp | 14 ++++---------- level_zero/tools/source/sysman/memory/memory.h | 2 +- .../tools/source/sysman/memory/memory_imp.cpp | 8 ++------ .../tools/source/sysman/memory/memory_imp.h | 4 ++-- .../tools/source/sysman/memory/os_memory.h | 6 ++++-- .../sysman/memory/windows/os_memory_imp.cpp | 6 +++--- .../sysman/memory/linux/test_sysman_memory.cpp | 13 ++----------- .../memory/linux/test_sysman_memory_dg1.cpp | 13 ++----------- .../memory/linux/test_sysman_memory_prelim.cpp | 16 +--------------- .../sysman/memory/windows/test_zes_memory.cpp | 8 -------- 13 files changed, 30 insertions(+), 78 deletions(-) diff --git a/level_zero/tools/source/sysman/memory/linux/os_memory_imp.cpp b/level_zero/tools/source/sysman/memory/linux/os_memory_imp.cpp index eb676c8673..5e63be429b 100644 --- a/level_zero/tools/source/sysman/memory/linux/os_memory_imp.cpp +++ b/level_zero/tools/source/sysman/memory/linux/os_memory_imp.cpp @@ -41,9 +41,9 @@ ze_result_t LinuxMemoryImp::getState(zes_mem_state_t *pState) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } -OsMemory *OsMemory::create(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) { - LinuxMemoryImp *pLinuxMemoryImp = new LinuxMemoryImp(pOsSysman, onSubdevice, subdeviceId); - return static_cast(pLinuxMemoryImp); +std::unique_ptr OsMemory::create(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) { + std::unique_ptr pLinuxMemoryImp = std::make_unique(pOsSysman, onSubdevice, subdeviceId); + return pLinuxMemoryImp; } } // namespace L0 diff --git a/level_zero/tools/source/sysman/memory/linux/os_memory_imp_dg1.cpp b/level_zero/tools/source/sysman/memory/linux/os_memory_imp_dg1.cpp index b74f85fd7f..55da3914d3 100644 --- a/level_zero/tools/source/sysman/memory/linux/os_memory_imp_dg1.cpp +++ b/level_zero/tools/source/sysman/memory/linux/os_memory_imp_dg1.cpp @@ -63,9 +63,9 @@ ze_result_t LinuxMemoryImp::getState(zes_mem_state_t *pState) { return ZE_RESULT_SUCCESS; } -OsMemory *OsMemory::create(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) { - LinuxMemoryImp *pLinuxMemoryImp = new LinuxMemoryImp(pOsSysman, onSubdevice, subdeviceId); - return static_cast(pLinuxMemoryImp); +std::unique_ptr OsMemory::create(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) { + std::unique_ptr pLinuxMemoryImp = std::make_unique(pOsSysman, onSubdevice, subdeviceId); + return pLinuxMemoryImp; } } // namespace L0 diff --git a/level_zero/tools/source/sysman/memory/linux/os_memory_imp_prelim.cpp b/level_zero/tools/source/sysman/memory/linux/os_memory_imp_prelim.cpp index 9f8a4f5a79..f612b0bffa 100644 --- a/level_zero/tools/source/sysman/memory/linux/os_memory_imp_prelim.cpp +++ b/level_zero/tools/source/sysman/memory/linux/os_memory_imp_prelim.cpp @@ -320,9 +320,9 @@ ze_result_t LinuxMemoryImp::getState(zes_mem_state_t *pState) { return ZE_RESULT_SUCCESS; } -OsMemory *OsMemory::create(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) { - LinuxMemoryImp *pLinuxMemoryImp = new LinuxMemoryImp(pOsSysman, onSubdevice, subdeviceId); - return static_cast(pLinuxMemoryImp); +std::unique_ptr OsMemory::create(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) { + std::unique_ptr pLinuxMemoryImp = std::make_unique(pOsSysman, onSubdevice, subdeviceId); + return pLinuxMemoryImp; } } // namespace L0 \ No newline at end of file diff --git a/level_zero/tools/source/sysman/memory/memory.cpp b/level_zero/tools/source/sysman/memory/memory.cpp index 725e1f7dd9..5077d6aadd 100644 --- a/level_zero/tools/source/sysman/memory/memory.cpp +++ b/level_zero/tools/source/sysman/memory/memory.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2022 Intel Corporation + * Copyright (C) 2020-2023 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -14,18 +14,12 @@ namespace L0 { -MemoryHandleContext::~MemoryHandleContext() { - for (Memory *pMemory : handleList) { - delete pMemory; - } -} +MemoryHandleContext::~MemoryHandleContext() = default; void MemoryHandleContext::createHandle(ze_device_handle_t deviceHandle) { - Memory *pMemory = new MemoryImp(pOsSysman, deviceHandle); + std::unique_ptr pMemory = std::make_unique(pOsSysman, deviceHandle); if (pMemory->initSuccess == true) { - handleList.push_back(pMemory); - } else { - delete pMemory; + handleList.push_back(std::move(pMemory)); } } diff --git a/level_zero/tools/source/sysman/memory/memory.h b/level_zero/tools/source/sysman/memory/memory.h index dab78092b1..c0e6017f03 100644 --- a/level_zero/tools/source/sysman/memory/memory.h +++ b/level_zero/tools/source/sysman/memory/memory.h @@ -40,7 +40,7 @@ struct MemoryHandleContext { OsSysman *pOsSysman = nullptr; bool isLmemSupported = false; - std::vector handleList = {}; + std::vector> handleList = {}; private: void createHandle(ze_device_handle_t deviceHandle); diff --git a/level_zero/tools/source/sysman/memory/memory_imp.cpp b/level_zero/tools/source/sysman/memory/memory_imp.cpp index bf96e0f82d..00da5229f1 100644 --- a/level_zero/tools/source/sysman/memory/memory_imp.cpp +++ b/level_zero/tools/source/sysman/memory/memory_imp.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2022 Intel Corporation + * Copyright (C) 2020-2023 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -39,10 +39,6 @@ MemoryImp::MemoryImp(OsSysman *pOsSysman, ze_device_handle_t handle) { init(); } -MemoryImp::~MemoryImp() { - if (nullptr != pOsMemory) { - delete pOsMemory; - } -} +MemoryImp::~MemoryImp() = default; } // namespace L0 diff --git a/level_zero/tools/source/sysman/memory/memory_imp.h b/level_zero/tools/source/sysman/memory/memory_imp.h index 05fd8f8d9a..f529366198 100644 --- a/level_zero/tools/source/sysman/memory/memory_imp.h +++ b/level_zero/tools/source/sysman/memory/memory_imp.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2022 Intel Corporation + * Copyright (C) 2020-2023 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -25,7 +25,7 @@ class MemoryImp : public Memory, NEO::NonCopyableOrMovableClass { MemoryImp() = default; void init(); - OsMemory *pOsMemory = nullptr; + std::unique_ptr pOsMemory; private: zes_mem_properties_t memoryProperties = {}; diff --git a/level_zero/tools/source/sysman/memory/os_memory.h b/level_zero/tools/source/sysman/memory/os_memory.h index de93bf075a..35cb01956f 100644 --- a/level_zero/tools/source/sysman/memory/os_memory.h +++ b/level_zero/tools/source/sysman/memory/os_memory.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Intel Corporation + * Copyright (C) 2020-2023 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -9,6 +9,8 @@ #include +#include + namespace L0 { struct OsSysman; @@ -18,7 +20,7 @@ class OsMemory { virtual ze_result_t getBandwidth(zes_mem_bandwidth_t *pBandwidth) = 0; virtual ze_result_t getState(zes_mem_state_t *pState) = 0; virtual bool isMemoryModuleSupported() = 0; - static OsMemory *create(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId); + static std::unique_ptr create(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId); virtual ~OsMemory() {} }; diff --git a/level_zero/tools/source/sysman/memory/windows/os_memory_imp.cpp b/level_zero/tools/source/sysman/memory/windows/os_memory_imp.cpp index b4a5558fce..d50e1e0307 100644 --- a/level_zero/tools/source/sysman/memory/windows/os_memory_imp.cpp +++ b/level_zero/tools/source/sysman/memory/windows/os_memory_imp.cpp @@ -285,9 +285,9 @@ WddmMemoryImp::~WddmMemoryImp() { } } -OsMemory *OsMemory::create(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) { - WddmMemoryImp *pWddmMemoryImp = new WddmMemoryImp(pOsSysman, onSubdevice, subdeviceId); - return static_cast(pWddmMemoryImp); +std::unique_ptr OsMemory::create(OsSysman *pOsSysman, ze_bool_t onSubdevice, uint32_t subdeviceId) { + std::unique_ptr pWddmMemoryImp = std::make_unique(pOsSysman, onSubdevice, subdeviceId); + return pWddmMemoryImp; } } // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory.cpp b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory.cpp index a2473c6ca0..2235e7f682 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2022 Intel Corporation + * Copyright (C) 2020-2023 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -31,10 +31,6 @@ class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { pMemoryManager->localMemorySupported[0] = false; device->getDriverHandle()->setMemoryManager(pMemoryManager); - for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { - delete handle; - } - pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); uint32_t subDeviceCount = 0; std::vector deviceHandles; @@ -64,10 +60,6 @@ class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { void setLocalSupportedAndReinit(bool supported) { pMemoryManager->localMemorySupported[0] = supported; - for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { - delete handle; - } - pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); uint32_t subDeviceCount = 0; std::vector deviceHandles; @@ -176,11 +168,10 @@ TEST_F(SysmanMultiDeviceFixture, GivenValidDevicePointerWhenGettingMemoryPropert ze_device_properties_t deviceProperties = {}; ze_bool_t isSubDevice = deviceProperties.flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE; Device::fromHandle(device)->getProperties(&deviceProperties); - LinuxMemoryImp *pLinuxMemoryImp = new LinuxMemoryImp(pOsSysman, isSubDevice, deviceProperties.subdeviceId); + std::unique_ptr pLinuxMemoryImp = std::make_unique(pOsSysman, isSubDevice, deviceProperties.subdeviceId); EXPECT_EQ(ZE_RESULT_SUCCESS, pLinuxMemoryImp->getProperties(&properties)); EXPECT_EQ(properties.subdeviceId, deviceProperties.subdeviceId); EXPECT_EQ(properties.onSubdevice, isSubDevice); - delete pLinuxMemoryImp; } } // namespace ult diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_dg1.cpp b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_dg1.cpp index 6a7cedfcee..d4718ef740 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_dg1.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_dg1.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Intel Corporation + * Copyright (C) 2021-2023 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -40,10 +40,6 @@ class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { pLinuxSysmanImp = static_cast(pOsSysman); pLinuxSysmanImp->pDrm = pDrm; - for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { - delete handle; - } - pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); uint32_t subDeviceCount = 0; std::vector deviceHandles; @@ -78,10 +74,6 @@ class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { void setLocalSupportedAndReinit(bool supported) { pMemoryManager->localMemorySupported[0] = supported; - for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { - delete handle; - } - pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); uint32_t subDeviceCount = 0; std::vector deviceHandles; @@ -252,11 +244,10 @@ TEST_F(SysmanMultiDeviceFixture, GivenValidDevicePointerWhenGettingMemoryPropert ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES}; ze_bool_t isSubDevice = deviceProperties.flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE; Device::fromHandle(device)->getProperties(&deviceProperties); - LinuxMemoryImp *pLinuxMemoryImp = new LinuxMemoryImp(pOsSysman, isSubDevice, deviceProperties.subdeviceId); + std::unique_ptr pLinuxMemoryImp = std::make_unique(pOsSysman, isSubDevice, deviceProperties.subdeviceId); EXPECT_EQ(ZE_RESULT_SUCCESS, pLinuxMemoryImp->getProperties(&properties)); EXPECT_EQ(properties.subdeviceId, deviceProperties.subdeviceId); EXPECT_EQ(properties.onSubdevice, isSubDevice); - delete pLinuxMemoryImp; } } // namespace ult diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_prelim.cpp b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_prelim.cpp index a85cc32f79..5200cd6601 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_prelim.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/linux/test_sysman_memory_prelim.cpp @@ -62,9 +62,7 @@ class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { pLinuxSysmanImp->pFsAccess = pFsAccess.get(); pDrm->setMemoryType(INTEL_HWCONFIG_MEMORY_TYPE_HBM2e); pDrm->ioctlHelper = static_cast>(std::make_unique(*pDrm)); - for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { - delete handle; - } + pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); uint32_t subDeviceCount = 0; // We received a device handle. Check for subdevices in this device @@ -115,10 +113,6 @@ class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { void setLocalSupportedAndReinit(bool supported) { pMemoryManager->localMemorySupported[0] = supported; - for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { - delete handle; - } - pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); uint32_t subDeviceCount = 0; std::vector deviceHandles; @@ -812,10 +806,6 @@ class SysmanMultiDeviceMemoryFixture : public SysmanMultiDeviceFixture { pLinuxSysmanImp = static_cast(pOsSysman); pLinuxSysmanImp->pDrm = pDrm; - for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { - delete handle; - } - pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); // We received a device handle. Check for subdevices in this device Device::fromHandle(device->toHandle())->getSubDevices(&subDeviceCount, nullptr); @@ -849,10 +839,6 @@ class SysmanMultiDeviceMemoryFixture : public SysmanMultiDeviceFixture { void setLocalSupportedAndReinit(bool supported) { pMemoryManager->localMemorySupported[0] = supported; - for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { - delete handle; - } - pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); uint32_t subDeviceCount = 0; std::vector deviceHandles; diff --git a/level_zero/tools/test/unit_tests/sources/sysman/memory/windows/test_zes_memory.cpp b/level_zero/tools/test/unit_tests/sources/sysman/memory/windows/test_zes_memory.cpp index 7e4a6b863b..fa7910de42 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/memory/windows/test_zes_memory.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/memory/windows/test_zes_memory.cpp @@ -32,10 +32,6 @@ class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { pOriginalKmdSysManager = pWddmSysmanImp->pKmdSysManager; pWddmSysmanImp->pKmdSysManager = pKmdSysManager.get(); - for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { - delete handle; - } - pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); uint32_t subDeviceCount = 0; std::vector deviceHandles; @@ -61,10 +57,6 @@ class SysmanDeviceMemoryFixture : public SysmanDeviceFixture { void setLocalSupportedAndReinit(bool supported) { pMemoryManager->localMemorySupported[0] = supported; - for (auto handle : pSysmanDeviceImp->pMemoryHandleContext->handleList) { - delete handle; - } - pSysmanDeviceImp->pMemoryHandleContext->handleList.clear(); uint32_t subDeviceCount = 0; std::vector deviceHandles;