summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/node_crypto_bio.cc24
-rw-r--r--src/node_crypto_bio.h4
2 files changed, 28 insertions, 0 deletions
diff --git a/src/node_crypto_bio.cc b/src/node_crypto_bio.cc
index 52e5e78274..64d9efaf04 100644
--- a/src/node_crypto_bio.cc
+++ b/src/node_crypto_bio.cc
@@ -210,10 +210,34 @@ size_t NodeBIO::Read(char* out, size_t size) {
assert(expected == bytes_read);
length_ -= bytes_read;
+ // Free all empty buffers, but write_head's child
+ FreeEmpty();
+
return bytes_read;
}
+void NodeBIO::FreeEmpty() {
+ Buffer* child = write_head_->next_;
+ if (child == write_head_ || child == read_head_)
+ return;
+ Buffer* cur = child->next_;
+ if (cur == write_head_ || cur == read_head_)
+ return;
+
+ while (cur != read_head_) {
+ assert(cur != write_head_);
+ assert(cur->write_pos_ == cur->read_pos_);
+
+ Buffer* next = cur->next_;
+ child->next_ = next;
+ delete cur;
+
+ cur = next;
+ }
+}
+
+
size_t NodeBIO::IndexOf(char delim, size_t limit) {
size_t bytes_read = 0;
size_t max = Length() > limit ? limit : Length();
diff --git a/src/node_crypto_bio.h b/src/node_crypto_bio.h
index 9411b3bce7..e565b7fc93 100644
--- a/src/node_crypto_bio.h
+++ b/src/node_crypto_bio.h
@@ -62,6 +62,10 @@ class NodeBIO {
// Read `len` bytes maximum into `out`, return actual number of read bytes
size_t Read(char* out, size_t size);
+ // Memory optimization:
+ // Deallocate children of write head's child if they're empty
+ void FreeEmpty();
+
// Find first appearance of `delim` in buffer or `limit` if `delim`
// wasn't found.
size_t IndexOf(char delim, size_t limit);