diff --git a/apps/nexus/mfile.cpp b/apps/nexus/mfile.cpp index 1c818608..eb76ce08 100644 --- a/apps/nexus/mfile.cpp +++ b/apps/nexus/mfile.cpp @@ -10,7 +10,7 @@ bool MFile::Create(const string &fname, unsigned int mxs) { files.clear(); size = 0; readonly = false; - assert(mxs <= (1<<30)); + assert(mxs <= MFILE_MAX_SIZE); max_size = mxs; return AddFile(); } @@ -19,7 +19,7 @@ bool MFile::Load(const string &fname, bool ronly) { filename = fname; files.clear(); readonly = ronly; - max_size = (1<<30); + max_size = MFILE_MAX_SIZE; size = 0; while(1) { @@ -57,12 +57,14 @@ void MFile::Redim(int64 sz) { if(sz > size) { unsigned int totfile = (unsigned int)(sz/max_size); //TODO test rhis!!!! - while(files.size() < totfile) { + while(files.size() <= totfile) { RedimLast(max_size); + assert(size == max_size * (files.size())); AddFile(); } - assert(size < sz); + assert(size <= sz); assert(sz - size < max_size); + assert(files.back().Length() + (unsigned int)(sz - size) < max_size); RedimLast(files.back().Length() + (unsigned int)(sz - size)); } else { while(size - files.back().Length() > sz) @@ -75,7 +77,8 @@ void MFile::Redim(int64 sz) { void MFile::SetPosition(int64 pos) { assert(pos < size); curr_fp = (unsigned int)(pos/(int64)max_size); - curr_pos = (unsigned int)(pos - (int64)max_size * (int64)curr_fp); + curr_pos = (unsigned int)(pos - (int64)max_size * (int64)curr_fp); + assert(curr_pos < max_size); assert(curr_fp < files.size()); files[curr_fp].SetPosition(curr_pos); } @@ -129,7 +132,8 @@ void MFile::WriteBuffer(void *data, unsigned int sz) { #endif } -void MFile::RedimLast(unsigned int sz) { +void MFile::RedimLast(unsigned int sz) { + assert(sz <= max_size); File &file = files.back(); unsigned int last_size = file.Length(); file.Redim(sz); diff --git a/apps/nexus/mfile.h b/apps/nexus/mfile.h index dd9caf4e..bf167b72 100644 --- a/apps/nexus/mfile.h +++ b/apps/nexus/mfile.h @@ -14,6 +14,8 @@ typedef __int64 int64; typedef unsigned long long int64; #endif +#define MFILE_MAX_SIZE (1<<30) + class MFile { public: @@ -22,7 +24,7 @@ class MFile { //max is so default is 1 G bool Create(const std::string &filename, - unsigned int max_file_size = (1<<30)); + unsigned int max_file_size = MFILE_MAX_SIZE); bool Load(const std::string &filename, bool readonly = false); void Close(); diff --git a/apps/nexus/patchserver.cpp b/apps/nexus/patchserver.cpp index bd853ab8..2709bde6 100644 --- a/apps/nexus/patchserver.cpp +++ b/apps/nexus/patchserver.cpp @@ -219,22 +219,22 @@ void PatchServer::Flush(PTime &ptime) { char *compressed = ptime.patch->Compress(entry.ram_size * chunk_size, compressed_size); if(entry.disk_size == 0xffff) {//allocate space - assert(entry.patch_start == 0xffffffff); - entry.disk_size = (unsigned int)((compressed_size-1)/chunk_size) + 1; - entry.patch_start = (unsigned int)(Length()/chunk_size); - Redim(Length() + entry.disk_size * chunk_size); + assert(entry.patch_start == 0xffffffff); + entry.disk_size = (unsigned int)((compressed_size-1)/chunk_size) + 1; + entry.patch_start = (unsigned int)(Length()/chunk_size); + Redim(Length() + entry.disk_size * chunk_size); } else { - cerr << "OOOOPSPPPS not supported!" << endl; - exit(-1); + cerr << "OOOOPSPPPS not supported!" << endl; + exit(-1); } SetPosition(entry.patch_start * chunk_size); WriteBuffer(compressed, entry.disk_size * chunk_size); delete []compressed; } else { if(entry.disk_size == 0xffff) { - entry.disk_size = entry.ram_size; - entry.patch_start = (unsigned int)(Length()/chunk_size); - Redim(Length() + entry.disk_size * chunk_size); + entry.disk_size = entry.ram_size; + entry.patch_start = (unsigned int)(Length()/chunk_size); + Redim(Length() + entry.disk_size * chunk_size); } SetPosition(entry.patch_start * chunk_size); WriteBuffer(ptime.patch->start, entry.disk_size * chunk_size);