Skip to content

Commit

Permalink
3rd way to free memory
Browse files Browse the repository at this point in the history
By vector().swap(vector).
None of three seem to work as I wanted, so it's commented out (in lines 336 and 347).
  • Loading branch information
VaKonS authored Dec 12, 2017
1 parent f20a91e commit fb9ad6a
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions zipper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ DIR * dir_handle;
dirent * dir_entry;
std::ifstream file_check;
std::ofstream outfile;
std::vector<std::vector<char>> zip_samples, zip_pass1;
std::vector<std::vector<char>> zip_samples;
std::vector<unsigned> cycleN_count;
char path_buf[2048];
MEMORYSTATUS mem_stat;
Expand All @@ -53,7 +53,7 @@ int main(int argc, char** argv) {

// definition of command line arguments abcdefghijklmnopqrstuvwxyz
// abcd.f..i..lmnop.rst......
TCLAP::CmdLine cmd("Zipper: checks different number of compression passes for 7-Zip ZIP archives.", ' ', "Zipper v1.3");
TCLAP::CmdLine cmd("Zipper: checks different number of compression passes for 7-Zip ZIP archives.", ' ', "Zipper v1.32");

TCLAP::ValueArg<std::string> cmdInputDir("i", "input-mask",
"Directory with files to compress.\nRun Zipper from this directory to avoid paths inside archives. [.]", false,
Expand Down Expand Up @@ -270,9 +270,9 @@ int main(int argc, char** argv) {
if (zip_length > 0) {
pass_counter = p + 1;
file_check.seekg(0, file_check.beg);
zip_pass1.resize(1);
zip_pass1[0].resize(zip_length); // allocating memory
file_check.read(zip_pass1[0].data(), zip_length); // reading zip to memory
std::vector<char> zip_pass1;
zip_pass1.resize(zip_length); // allocating memory
file_check.read(zip_pass1.data(), zip_length); // reading zip to memory
file_check.close();
int t, e;
bool line_start = true;
Expand All @@ -286,16 +286,16 @@ int main(int argc, char** argv) {
int add_index = -1;
for (int c = (zip_samples.size() - t); c >= e; c--) {
if (zip_samples[c].size() == static_cast<size_t>(zip_length)) {
if (memcmp(zip_samples[c].data(), zip_pass1[0].data(), zip_length) == 0) {
if (memcmp(zip_samples[c].data(), zip_pass1.data(), zip_length) == 0) {
add_index = c;
if (old_detection) {
match_counter++;
std::cout << "Matched archives: " << match_counter << "/" << detect_threshold << std::endl;
cycle_size = detect_threshold;
if (match_counter == detect_threshold) {
is_full = true;
zip_pass1[0].clear();
zip_pass1.clear();
std::vector<char>().swap(zip_pass1);
goto passes_checked;
} else
break;
Expand All @@ -316,8 +316,8 @@ int main(int argc, char** argv) {
std::cout << (line_start ? "Cycle: " : ", ") << dc << "/" << cycle_size << ".\n"
"Compression cycling detected, " << cycle_size << " archives. More passes should not be necessary." << std::endl;
is_full = true;
zip_pass1[0].clear();
zip_pass1.clear();
std::vector<char>().swap(zip_pass1);
goto passes_checked;
wrong_cycle:
if (line_start) {
Expand All @@ -332,28 +332,27 @@ int main(int argc, char** argv) {
}
}
if (add_index != -1) {
zip_pass1[0].clear();
zip_pass1.clear();
//std::vector<char>().swap(zip_pass1);
zip_samples.push_back(zip_samples[add_index]); // same sample, referencing previous copy
goto sample_added;
} else {
cycleN_count.clear(); // reset all matches counters
cycle_size_max = 0;
for (unsigned c = 0; c < zip_samples.size(); c++) {
if (zip_samples[c].size() == static_cast<size_t>(zip_length)) {
if (memcmp(zip_samples[c].data(), zip_pass1[0].data(), zip_length) == 0) {
if (memcmp(zip_samples[c].data(), zip_pass1.data(), zip_length) == 0) {
//std::cout << "Matched sample, referencing previous copy." << std::endl;
zip_pass1[0].clear();
zip_pass1.clear();
//std::vector<char>().swap(zip_pass1);
zip_samples.push_back(zip_samples[c]);
goto sample_added;
}
}
}
}
//std::cout << "Adding new archive." << std::endl;
zip_samples.push_back(zip_pass1[0]);
zip_pass1.clear();
zip_samples.push_back(zip_pass1);
mem_use += zip_length;
if ((unsigned) zip_length < minimal_zip_length) {
minimal_zip_sample = zip_samples.back(); //zip_samples[zip_samples.size() - 1];
Expand Down

0 comments on commit fb9ad6a

Please sign in to comment.