Skip to content

Commit d05b275

Browse files
mulugetamasonje
andauthored
Avoid calling new and delete explicitly. (#44)
Signed-off-by: Mulugeta Mammo <[email protected]> Co-authored-by: Olasoji Denloye <[email protected]>
1 parent 35c4630 commit d05b275

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

iaa.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,38 @@
66
#include "config/config.h"
77
#include "logging.h"
88
#include "utils.h"
9+
910
using namespace config;
11+
1012
#ifdef USE_IAA
11-
#include <iostream>
1213

1314
#include "utils.h"
1415

15-
#define PREPENDED_BLOCK_LENGTH 5
16-
#define MAX_BUFFER_SIZE (2 << 20)
17-
1816
void IAAJob::InitJob(qpl_path_t execution_path) {
1917
uint32_t size;
2018
qpl_status status = qpl_get_job_size(execution_path, &size);
2119
if (status != QPL_STS_OK) {
22-
jobs_[execution_path] = nullptr;
2320
return;
2421
}
22+
23+
QplJobPtr job = nullptr;
2524
try {
26-
jobs_[execution_path] = reinterpret_cast<qpl_job*>(new char[size]);
25+
job = CreateQplJob(size);
2726
} catch (std::bad_alloc& e) {
28-
jobs_[execution_path] = nullptr;
2927
return;
3028
}
31-
status = qpl_init_job(execution_path, jobs_[execution_path]);
29+
status = qpl_init_job(execution_path, job.get());
3230
if (status != QPL_STS_OK) {
33-
delete[] jobs_[execution_path];
34-
jobs_[execution_path] = nullptr;
31+
return;
3532
}
33+
34+
// Transfer ownership to the jobs_ vector
35+
jobs_[execution_path] = std::move(job);
3636
}
3737

3838
void IAAJob::DestroyJob(qpl_path_t execution_path) {
39-
if (jobs_[execution_path] != nullptr) {
40-
qpl_fini_job(jobs_[execution_path]);
41-
delete[] jobs_[execution_path];
42-
jobs_[execution_path] = nullptr;
39+
if (jobs_[execution_path]) {
40+
jobs_[execution_path].reset();
4341
}
4442
}
4543

@@ -68,7 +66,7 @@ int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
6866
}
6967

7068
qpl_job* job = job_.GetJob(execution_path);
71-
if (job == nullptr) {
69+
if (!job) {
7270
Log(LogLevel::LOG_ERROR, "CompressIAA() Line ", __LINE__,
7371
" Error qpl_job is null\n");
7472
return 1;
@@ -197,7 +195,7 @@ int UncompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
197195
}
198196

199197
qpl_job* job = job_.GetJob(execution_path);
200-
if (job == nullptr) {
198+
if (!job) {
201199
Log(LogLevel::LOG_ERROR, "UncompressIAA() Line ", __LINE__,
202200
" Error qpl_job is null\n");
203201
return 1;

iaa.h

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,46 @@
66
#ifdef USE_IAA
77
#define VISIBLE_FOR_TESTING __attribute__((visibility("default")))
88

9+
#include <memory>
910
#include <vector>
1011

1112
#include "qpl/qpl.h"
1213

14+
inline constexpr unsigned int PREPENDED_BLOCK_LENGTH = 5;
15+
inline constexpr unsigned int MAX_BUFFER_SIZE = (2 << 20);
16+
1317
class IAAJob {
1418
public:
15-
IAAJob() : jobs_(3, nullptr) {}
16-
17-
~IAAJob() {
18-
for (qpl_job* job : jobs_) {
19-
if (job != nullptr) {
20-
qpl_fini_job(job);
21-
delete[] job;
22-
}
23-
}
24-
}
19+
IAAJob() : jobs_(3) {}
2520

2621
qpl_job* GetJob(qpl_path_t execution_path) {
27-
if (jobs_[execution_path] == nullptr) {
22+
if (!jobs_[execution_path]) {
2823
InitJob(execution_path);
2924
}
30-
return jobs_[execution_path];
25+
return jobs_[execution_path].get();
3126
}
3227

3328
void DestroyJob(qpl_path_t execution_path);
3429

3530
private:
31+
struct QplJobDeleter {
32+
void operator()(qpl_job* job) const {
33+
if (job) {
34+
qpl_fini_job(job);
35+
delete[] reinterpret_cast<char*>(job);
36+
}
37+
}
38+
};
39+
40+
using QplJobPtr = std::unique_ptr<qpl_job, QplJobDeleter>;
41+
3642
void InitJob(qpl_path_t execution_path);
3743

38-
std::vector<qpl_job*> jobs_;
44+
QplJobPtr CreateQplJob(uint32_t size) {
45+
return QplJobPtr(reinterpret_cast<qpl_job*>(new char[size]));
46+
}
47+
48+
std::vector<QplJobPtr> jobs_;
3949
};
4050

4151
int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,

0 commit comments

Comments
 (0)