Skip to content

Commit

Permalink
Fix a bug introduced in the last change.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownbrackets committed Nov 2, 2014
1 parent 57d68b0 commit 756da93
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/compress.h
Expand Up @@ -7,7 +7,7 @@

namespace maxcso {

static const char *VERSION = "1.4.1";
static const char *VERSION = "1.4.2";

struct Task;

Expand Down
30 changes: 14 additions & 16 deletions src/output.cpp
Expand Up @@ -116,21 +116,19 @@ void Output::Enqueue(int64_t pos, uint8_t *buffer) {
HandleReadySector(sector);
});

if (blockSize_ != SECTOR_SIZE) {
const bool lastBlock = pos + SECTOR_SIZE >= srcSize_;
const bool needsPad = pos + blockSize_ > srcSize_;
if (lastBlock && needsPad) {
// Our src is not aligned to the blockSize_, so this sector might not ever wake up.
// So let's send in some padding.
for (int64_t padPos = srcSize_; padPos < pos + blockSize_; padPos += SECTOR_SIZE) {
// Sector takes ownership, so we need a new one each time.
uint8_t *padBuffer = pool.Alloc();
memset(padBuffer, 0, SECTOR_SIZE);
sector->Process(padPos, padBuffer, [this, sector, block](bool status, const char *reason) {
partialSectors_.erase(block);
HandleReadySector(sector);
});
}
// Only check for the last block of a larger block size.
if (blockSize_ != SECTOR_SIZE && pos + SECTOR_SIZE >= srcSize_) {
// Our src may not be aligned to the blockSize_, so this sector might never wake up.
// So let's send in some padding if needed.
const int64_t paddedSize = (srcSize_ + blockSize_ - 1) & ~static_cast<int64_t>(blockSize_ - 1);
for (int64_t padPos = srcSize_; padPos < paddedSize; padPos += SECTOR_SIZE) {
// Sector takes ownership, so we need a new one each time.
uint8_t *padBuffer = pool.Alloc();
memset(padBuffer, 0, SECTOR_SIZE);
sector->Process(padPos, padBuffer, [this, sector, block](bool status, const char *reason) {
partialSectors_.erase(block);
HandleReadySector(sector);
});
}
}
}
Expand Down Expand Up @@ -248,7 +246,7 @@ void Output::HandleReadySector(Sector *sector) {

progress_(srcPos_, srcSize_, dstPos_);

if (nextPos == srcSize_) {
if (nextPos >= srcSize_) {
state_ |= STATE_DATA_WRITTEN;
CheckFinish();
} else {
Expand Down
9 changes: 8 additions & 1 deletion src/sector.cpp
Expand Up @@ -23,7 +23,7 @@ static void EndZlib(z_stream *&z) {
}

Sector::Sector(uint32_t flags, uint32_t orig_max_cost, uint32_t lz4_max_cost)
: flags_(flags), origMaxCost_(orig_max_cost), lz4MaxCost_(lz4_max_cost), busy_(false),
: flags_(flags), origMaxCost_(orig_max_cost), lz4MaxCost_(lz4_max_cost), busy_(false), enqueued_(false),
compress_(true), readySize_(0), buffer_(nullptr), best_(nullptr) {
// Set up the zlib streams, which we will reuse each time we hit this sector.
if (!(flags_ & TASKFLAG_NO_ZLIB_DEFAULT)) {
Expand Down Expand Up @@ -90,7 +90,13 @@ void Sector::Process(int64_t pos, uint8_t *buffer, SectorCallback ready) {
return;
}

if (enqueued_) {
ready_(false, "Sector already waiting for queued operation");
return;
}

if (compress_) {
enqueued_ = true;
ready_ = ready;
uv_.queue_work(loop_, &work_, [this](uv_work_t *req) {
Compress();
Expand Down Expand Up @@ -274,6 +280,7 @@ void Sector::Release() {
}

busy_ = false;
enqueued_ = false;
compress_ = true;
readySize_ = 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/sector.h
Expand Up @@ -86,6 +86,7 @@ class Sector {
uint32_t origMaxCost_;
uint32_t lz4MaxCost_;
bool busy_;
bool enqueued_;
bool compress_;

uint32_t blockSize_;
Expand Down

0 comments on commit 756da93

Please sign in to comment.