Browse Source

crypto: fix excessive buffer allocation

Allocate buffer only if the next one isn't free.
v0.11.3-release
Fedor Indutny 12 years ago
parent
commit
56d9c48573
  1. 18
      src/node_crypto_bio.cc
  2. 3
      src/node_crypto_bio.h

18
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_);

3
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);

Loading…
Cancel
Save