From ac5c7955c035a51345ea06f845464f5c5c58fab7 Mon Sep 17 00:00:00 2001 From: Mulugeta Mammo Date: Mon, 27 Oct 2025 16:51:22 +0000 Subject: [PATCH] Avoid calling new and delete explicitly. Signed-off-by: Mulugeta Mammo --- qat.cpp | 109 +++++++++++++++++++++++++------------------------------- qat.h | 23 ++++++------ 2 files changed, 61 insertions(+), 71 deletions(-) diff --git a/qat.cpp b/qat.cpp index bf2c17c..b4e35ce 100644 --- a/qat.cpp +++ b/qat.cpp @@ -6,35 +6,58 @@ #include "config/config.h" #include "logging.h" #include "utils.h" + using namespace config; + #ifdef USE_QAT -#include + +void QATJob::QzSessionDeleter::operator()(QzSession_T *qzSession) const { + if (!qzSession) { + return; + } + + int rc = qzTeardownSession(qzSession); + if (rc != QZ_OK) { + Log(LogLevel::LOG_ERROR, + "qzTeardownSession() Line %d session %p returned %d\n", __LINE__, + qzSession, rc); + } + + // Attempt to close the session + rc = qzClose(qzSession); + if (rc != QZ_OK) { + Log(LogLevel::LOG_ERROR, "qzClose() Line %d session %p returned %d\n", + __LINE__, qzSession, rc); + } + + delete qzSession; +} QzSession_T *QATJob::GetQATSession(int window_bits, bool gzip_ext) { CompressedFormat format = GetCompressedFormat(window_bits); switch (format) { case CompressedFormat::DEFLATE_RAW: if (qzSession_deflate_raw == nullptr) { - Init(&qzSession_deflate_raw, format); + Init(qzSession_deflate_raw, format); } - return qzSession_deflate_raw; + return qzSession_deflate_raw.get(); case CompressedFormat::ZLIB: if (qzSession_zlib == nullptr) { - Init(&qzSession_zlib, + Init(qzSession_zlib, format); // we do not have zlib format in public enum of qatzip } - return qzSession_zlib; + return qzSession_zlib.get(); case CompressedFormat::GZIP: if (gzip_ext) { if (qzSession_gzip_ext == nullptr) { - Init(&qzSession_gzip_ext, format, true); + Init(qzSession_gzip_ext, format, true); } - return qzSession_gzip_ext; + return qzSession_gzip_ext.get(); } else { if (qzSession_gzip == nullptr) { - Init(&qzSession_gzip, format); + Init(qzSession_gzip, format); } - return qzSession_gzip; + return qzSession_gzip.get(); } case CompressedFormat::INVALID: return nullptr; @@ -46,20 +69,16 @@ void QATJob::CloseQATSession(int window_bits, bool gzip_ext) { CompressedFormat format = GetCompressedFormat(window_bits); switch (format) { case CompressedFormat::DEFLATE_RAW: - Close(qzSession_deflate_raw); - qzSession_deflate_raw = nullptr; + qzSession_deflate_raw.reset(); break; case CompressedFormat::ZLIB: - Close(qzSession_zlib); - qzSession_zlib = nullptr; + qzSession_zlib.reset(); break; case CompressedFormat::GZIP: if (gzip_ext) { - Close(qzSession_gzip_ext); - qzSession_gzip_ext = nullptr; + qzSession_gzip_ext.reset(); } else { - Close(qzSession_gzip); - qzSession_gzip = nullptr; + qzSession_gzip.reset(); } break; default: @@ -67,28 +86,27 @@ void QATJob::CloseQATSession(int window_bits, bool gzip_ext) { } } -void QATJob::Init(QzSession_T **qzSession, CompressedFormat format, +void QATJob::Init(QzSessionPtr &qzSession, CompressedFormat format, bool gzip_ext) { + QzSessionPtr session = nullptr; try { - *qzSession = new QzSession_T; - memset(*qzSession, 0, sizeof(QzSession_T)); + session = QzSessionPtr(new QzSession_T()); + memset(session.get(), 0, sizeof(QzSession_T)); } catch (std::bad_alloc &e) { - *qzSession = nullptr; return; } + // Initialize QAT hardware - int status = qzInit(*qzSession, 0); + int status = qzInit(session.get(), 0); if (status != QZ_OK && status != QZ_DUPLICATE) { Log(LogLevel::LOG_ERROR, "qzInit() failure Line %d session %p returned %d\n", __LINE__, - *qzSession, status); - delete *qzSession; - *qzSession = nullptr; + session.get(), status); return; } else { Log(LogLevel::LOG_INFO, "qzInit() success Line %d session %p returned %d\n", __LINE__, - *qzSession, status); + session.get(), status); } QzSessionParamsDeflateExt_T deflateExt = {{}, 0, 0}; @@ -134,47 +152,16 @@ void QATJob::Init(QzSession_T **qzSession, CompressedFormat format, deflateExt.deflate_params.data_fmt = QZ_FMT_NUM; break; } - status = qzSetupSessionDeflateExt(*qzSession, &deflateExt); + status = qzSetupSessionDeflateExt(session.get(), &deflateExt); if (status != QZ_OK) { Log(LogLevel::LOG_ERROR, "qzSetupSessionDeflateExt() Line %d session %p returned %d\n", __LINE__, - *qzSession, status); - Close(); - return; - } -} - -void QATJob::Close() { - Close(qzSession_deflate_raw); - qzSession_deflate_raw = nullptr; - Close(qzSession_zlib); - qzSession_zlib = nullptr; - Close(qzSession_gzip); - qzSession_gzip = nullptr; - Close(qzSession_gzip_ext); - qzSession_gzip_ext = nullptr; -} - -void QATJob::Close(QzSession_T *qzSession) { - if (!qzSession) { + session.get(), status); return; } - int rc = qzTeardownSession(qzSession); - if (rc != QZ_OK) { - Log(LogLevel::LOG_ERROR, - "qzTeardownSession() Line %d session %p returned %d\n", __LINE__, - qzSession, rc); - } - - // Attempt to close the session - rc = qzClose(qzSession); - if (rc != QZ_OK) { - Log(LogLevel::LOG_ERROR, "qzClose() Line %d session %p returned %d\n", - __LINE__, qzSession, rc); - } - - delete qzSession; + // Transfer ownership to qzSession + qzSession = std::move(session); } static thread_local QATJob qat_job_; diff --git a/qat.h b/qat.h index 66c8406..6709aba 100644 --- a/qat.h +++ b/qat.h @@ -8,29 +8,32 @@ #include -#include +#include #include "utils.h" -#define QAT_HW_BUFF_SZ QZ_HW_BUFF_MAX_SZ +inline constexpr unsigned int QAT_HW_BUFF_SZ = QZ_HW_BUFF_MAX_SZ; class QATJob { public: QATJob() {} - ~QATJob() { Close(); } QzSession_T* GetQATSession(int window_bits, bool gzip_ext); void CloseQATSession(int window_bits, bool gzip_ext); private: - void Init(QzSession_T** qzSession, CompressedFormat format, + struct QzSessionDeleter { + void operator()(QzSession_T* session) const; + }; + + using QzSessionPtr = std::unique_ptr; + + void Init(QzSessionPtr& qzSession, CompressedFormat format, bool gzip_ext = false); - void Close(); - void Close(QzSession_T* qzSession); - QzSession_T* qzSession_deflate_raw = nullptr; - QzSession_T* qzSession_gzip = nullptr; - QzSession_T* qzSession_gzip_ext = nullptr; - QzSession_T* qzSession_zlib = nullptr; + QzSessionPtr qzSession_deflate_raw; + QzSessionPtr qzSession_gzip; + QzSessionPtr qzSession_gzip_ext; + QzSessionPtr qzSession_zlib; }; int CompressQAT(uint8_t* input, uint32_t* input_length, uint8_t* output,