diff --git a/iaa.cpp b/iaa.cpp index 0d1ac00..2580a24 100644 --- a/iaa.cpp +++ b/iaa.cpp @@ -6,40 +6,38 @@ #include "config/config.h" #include "logging.h" #include "utils.h" + using namespace config; + #ifdef USE_IAA -#include #include "utils.h" -#define PREPENDED_BLOCK_LENGTH 5 -#define MAX_BUFFER_SIZE (2 << 20) - void IAAJob::InitJob(qpl_path_t execution_path) { uint32_t size; qpl_status status = qpl_get_job_size(execution_path, &size); if (status != QPL_STS_OK) { - jobs_[execution_path] = nullptr; return; } + + QplJobPtr job = nullptr; try { - jobs_[execution_path] = reinterpret_cast(new char[size]); + job = CreateQplJob(size); } catch (std::bad_alloc& e) { - jobs_[execution_path] = nullptr; return; } - status = qpl_init_job(execution_path, jobs_[execution_path]); + status = qpl_init_job(execution_path, job.get()); if (status != QPL_STS_OK) { - delete[] jobs_[execution_path]; - jobs_[execution_path] = nullptr; + return; } + + // Transfer ownership to the jobs_ vector + jobs_[execution_path] = std::move(job); } void IAAJob::DestroyJob(qpl_path_t execution_path) { - if (jobs_[execution_path] != nullptr) { - qpl_fini_job(jobs_[execution_path]); - delete[] jobs_[execution_path]; - jobs_[execution_path] = nullptr; + if (jobs_[execution_path]) { + jobs_[execution_path].reset(); } } @@ -68,7 +66,7 @@ int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output, } qpl_job* job = job_.GetJob(execution_path); - if (job == nullptr) { + if (!job) { Log(LogLevel::LOG_ERROR, "CompressIAA() Line ", __LINE__, " Error qpl_job is null\n"); return 1; @@ -197,7 +195,7 @@ int UncompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output, } qpl_job* job = job_.GetJob(execution_path); - if (job == nullptr) { + if (!job) { Log(LogLevel::LOG_ERROR, "UncompressIAA() Line ", __LINE__, " Error qpl_job is null\n"); return 1; diff --git a/iaa.h b/iaa.h index ecc3a87..214ed13 100644 --- a/iaa.h +++ b/iaa.h @@ -6,36 +6,46 @@ #ifdef USE_IAA #define VISIBLE_FOR_TESTING __attribute__((visibility("default"))) +#include #include #include "qpl/qpl.h" +inline constexpr unsigned int PREPENDED_BLOCK_LENGTH = 5; +inline constexpr unsigned int MAX_BUFFER_SIZE = (2 << 20); + class IAAJob { public: - IAAJob() : jobs_(3, nullptr) {} - - ~IAAJob() { - for (qpl_job* job : jobs_) { - if (job != nullptr) { - qpl_fini_job(job); - delete[] job; - } - } - } + IAAJob() : jobs_(3) {} qpl_job* GetJob(qpl_path_t execution_path) { - if (jobs_[execution_path] == nullptr) { + if (!jobs_[execution_path]) { InitJob(execution_path); } - return jobs_[execution_path]; + return jobs_[execution_path].get(); } void DestroyJob(qpl_path_t execution_path); private: + struct QplJobDeleter { + void operator()(qpl_job* job) const { + if (job) { + qpl_fini_job(job); + delete[] reinterpret_cast(job); + } + } + }; + + using QplJobPtr = std::unique_ptr; + void InitJob(qpl_path_t execution_path); - std::vector jobs_; + QplJobPtr CreateQplJob(uint32_t size) { + return QplJobPtr(reinterpret_cast(new char[size])); + } + + std::vector jobs_; }; int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,