From: Fedor Indutny Date: Tue, 11 Jun 2013 10:49:03 +0000 (+0200) Subject: crypto: fix excessive buffer allocation X-Git-Tag: v0.11.3~40 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=56d9c48573e4bfe98021fc0c678e20b90538c976;p=platform%2Fupstream%2Fnodejs.git crypto: fix excessive buffer allocation Allocate buffer only if the next one isn't free. --- diff --git a/src/node_crypto_bio.cc b/src/node_crypto_bio.cc index 64d9efa..389d281 100644 --- a/src/node_crypto_bio.cc +++ b/src/node_crypto_bio.cc @@ -303,11 +303,7 @@ void NodeBIO::Write(const char* data, size_t size) { // Go to next buffer if there still are some bytes to write if (left != 0) { - if (write_head_->write_pos_ == kBufferLength) { - Buffer* next = new Buffer(); - next->next_ = write_head_->next_; - write_head_->next_ = next; - } + TryAllocateForWrite(); write_head_ = write_head_->next_; } } @@ -315,6 +311,18 @@ void NodeBIO::Write(const char* data, size_t size) { } +void NodeBIO::TryAllocateForWrite() { + // If write head is full, next buffer is either read head or not empty. + if (write_head_->write_pos_ == kBufferLength && + (write_head_->next_ == read_head_ || + write_head_->next_->write_pos_ != 0)) { + Buffer* next = new Buffer(); + next->next_ = write_head_->next_; + write_head_->next_ = next; + } +} + + void NodeBIO::Reset() { while (read_head_->read_pos_ != read_head_->write_pos_) { assert(read_head_->write_pos_ > read_head_->read_pos_); diff --git a/src/node_crypto_bio.h b/src/node_crypto_bio.h index e565b7f..c2fd6a0 100644 --- a/src/node_crypto_bio.h +++ b/src/node_crypto_bio.h @@ -59,6 +59,9 @@ class NodeBIO { ~NodeBIO(); + // Allocate new buffer for write if needed + void TryAllocateForWrite(); + // Read `len` bytes maximum into `out`, return actual number of read bytes size_t Read(char* out, size_t size);