Skip to content

Commit

Permalink
[fix] memory leak in streamConvertor
Browse files Browse the repository at this point in the history
Signed-off-by: Yifan Yuan <[email protected]>
  • Loading branch information
BigVan committed Dec 7, 2023
1 parent 5fd1cfa commit b0abbd9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/overlaybd/gzindex/gzfile_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ struct IndexEntry {

typedef std::vector<struct IndexEntry *> INDEX;

struct IndexFilterRecorder;
class IndexFilterRecorder;
IndexFilterRecorder *new_index_filter(IndexFileHeader *h, INDEX *index, photon::fs::IFile *save_as);
void delete_index_filter(IndexFilterRecorder *&);

int init_index_header(photon::fs::IFile* src, IndexFileHeader &h, off_t span, int dict_compress_algo, int dict_compress_level);

Expand Down
10 changes: 10 additions & 0 deletions src/overlaybd/gzindex/gzip_index_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ IndexFilterRecorder* new_index_filter(IndexFileHeader *h, INDEX *index, photon::
return new IndexFilterRecorder(h, index, save_as);
}

void delete_index_filter(IndexFilterRecorder *&idx_filter) {
delete idx_filter;
idx_filter = nullptr;
}

static int build_index(IndexFileHeader& h,photon::fs::IFile *gzfile, INDEX &index, photon::fs::IFile* index_file) {
// IndexFilterRecorder filter(&h, &index, index_file);
auto filter = new IndexFilterRecorder(&h, &index, index_file);
Expand Down Expand Up @@ -364,6 +369,11 @@ int create_gz_index(photon::fs::IFile* gzip_file, const char *index_file_path, o
LOG_ERRNO_RETURN(0, -1, "init index header failed.");
}
INDEX index;
DEFER({
for (auto it : index) {
delete it;
}
});
int ret = build_index(h, gzip_file, index, index_file);
if (ret != 0) {
LOG_ERRNO_RETURN(0, -1, "Faild to build_index");
Expand Down
19 changes: 10 additions & 9 deletions src/overlaybd/gzip/gz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "../../tools/sha256file.h"
#include "../gzindex/gzfile_index.h"
#include "photon/common/alog.h"
#include "photon/fs/localfs.h"
class GzAdaptorFile : public photon::fs::VirtualReadOnlyFile {
public:
GzAdaptorFile() {
Expand Down Expand Up @@ -69,7 +70,7 @@ class GzStreamFile : public IGzFile {
public:
GzStreamFile(IStream *sock, ssize_t st_size, bool index_save,
const char* uid, const char *_workdir)
: fstream(sock), st_size(st_size), workdir(_workdir){
: st_size(st_size), fstream(sock), workdir(_workdir){

if (uid == nullptr) {
timeval now;
Expand All @@ -93,30 +94,30 @@ class GzStreamFile : public IGzFile {
ttin = ttout = strm.avail_out = 0;
init_index_header(this, m_idx_header, GZ_CHUNK_SIZE, GZ_DICT_COMPERSS_ALGO,
GZ_COMPRESS_LEVEL);
lfs = photon::fs::new_localfs_adaptor(workdir.c_str());
m_indexes.clear();

m_lfs = photon::fs::new_localfs_adaptor(workdir.c_str());
LOG_INFO("create buffer file(`) and indexfile(`)", fn_buff, fn_idx);
if (index_save) {
m_idx_file = lfs->open(fn_idx.c_str(), O_TRUNC | O_CREAT | O_RDWR, 0644);
m_idx_file = m_lfs->open(fn_idx.c_str(), O_TRUNC | O_CREAT | O_RDWR, 0644);
m_idx_filter = new_index_filter(&m_idx_header, &m_indexes, m_idx_file);
fstream = new_sha256_file((IFile*)fstream, false);
}

buffer_file = lfs->open(fn_buff.c_str(), O_TRUNC | O_CREAT | O_RDWR, 0644);
buffer_file = photon::fs::open_localfile_adaptor(fn_buff.c_str(), O_TRUNC | O_CREAT | O_RDWR, 0644);
LOG_INFO("create a GzStreamFile. workdir: `", workdir);
};

~GzStreamFile() {
(void)inflateEnd(&strm);
delete m_idx_file;
delete buffer_file;
delete m_idx_filter;
delete_index_filter(m_idx_filter);
delete fstream;
for (auto it:m_indexes) {
delete it;
}
lfs->unlink(fn_buff.c_str());
m_lfs->unlink(fn_buff.c_str());
delete m_lfs;
}

UNIMPLEMENTED_POINTER(photon::fs::IFileSystem* filesystem() override);
Expand Down Expand Up @@ -246,7 +247,7 @@ class GzStreamFile : public IGzFile {
}
auto dst_file = this->sha256_checksum() + ".gz_idx";
LOG_INFO("save index as: `", dst_file);
lfs->rename(fn_idx.c_str(), dst_file.c_str());
m_lfs->rename(fn_idx.c_str(), dst_file.c_str());
return std::string(workdir) + "/" + dst_file;
}
ssize_t st_size = 0;
Expand All @@ -264,7 +265,7 @@ class GzStreamFile : public IGzFile {
off_t bf_start = 0, bf_len = 0;

off_t cur_offset = 0;
photon::fs::IFileSystem *lfs = nullptr;
photon::fs::IFileSystem *m_lfs = nullptr;
IFile *buffer_file = nullptr;
IFile *m_idx_file = nullptr;
// const char *FN_BUFF_PREFIX = "/tmp/decompbuffer";
Expand Down
4 changes: 3 additions & 1 deletion src/overlaybd/stream_convertor/stream_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ class StreamConvertor {
int gen_meta(http::Request &req, http::Response &resp, std::string_view){

Task t;
auto uuid = req.headers.get_value("UUID").to_string();
resp.set_result(200);
if (!valid_request(req, resp)) {
return 0;
}
string msg = "{\"code\": 0, \"message\": \"\"}\n";
string msg = string("{\"code\": 0, \"message\":") + uuid + "\"\"}\n";
if (do_task(&req, t) != 0){
msg = "failed";
}
Expand Down Expand Up @@ -183,6 +184,7 @@ class StreamConvertor {
}

int stop() {
delete m_fs;
if (m_uds_serv) {
m_uds_serv->terminate();
delete m_uds_serv;
Expand Down

0 comments on commit b0abbd9

Please sign in to comment.