node_crypto_bio: adjust external memory size
authorFedor Indutny <fedor@indutny.com>
Fri, 6 Mar 2015 19:58:24 +0000 (14:58 -0500)
committerFedor Indutny <fedor@indutny.com>
Sun, 8 Mar 2015 23:07:46 +0000 (19:07 -0400)
Adjust V8's external memory size when allocating buffers for TLS data to
ensure that V8 has enough information to trigger the GC at right time.

PR-URL: https://github.com/iojs/io.js/pull/1085
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
src/node_crypto_bio.cc
src/node_crypto_bio.h
src/tls_wrap.cc

index 9597135..c28e118 100644 (file)
@@ -27,6 +27,11 @@ BIO* NodeBIO::New() {
 }
 
 
+void NodeBIO::AssignEnvironment(Environment* env) {
+  env_ = env;
+}
+
+
 int NodeBIO::New(BIO* bio) {
   bio->ptr = new NodeBIO();
 
@@ -399,7 +404,7 @@ void NodeBIO::TryAllocateForWrite(size_t hint) {
                              kThroughputBufferLength;
     if (len < hint)
       len = hint;
-    Buffer* next = new Buffer(len);
+    Buffer* next = new Buffer(env_, len);
 
     if (w == nullptr) {
       next->next_ = next;
index 4d19d46..5c38f1b 100644 (file)
@@ -2,14 +2,18 @@
 #define SRC_NODE_CRYPTO_BIO_H_
 
 #include "openssl/bio.h"
+#include "env.h"
+#include "env-inl.h"
 #include "util.h"
 #include "util-inl.h"
+#include "v8.h"
 
 namespace node {
 
 class NodeBIO {
  public:
-  NodeBIO() : initial_(kInitialBufferLength),
+  NodeBIO() : env_(nullptr),
+              initial_(kInitialBufferLength),
               length_(0),
               read_head_(nullptr),
               write_head_(nullptr) {
@@ -19,6 +23,8 @@ class NodeBIO {
 
   static BIO* New();
 
+  void AssignEnvironment(Environment* env);
+
   // Move read head to next buffer if needed
   void TryMoveReadHead();
 
@@ -89,17 +95,23 @@ class NodeBIO {
 
   class Buffer {
    public:
-    explicit Buffer(size_t len) : read_pos_(0),
-                                  write_pos_(0),
-                                  len_(len),
-                                  next_(nullptr) {
+    Buffer(Environment* env, size_t len) : env_(env),
+                                           read_pos_(0),
+                                           write_pos_(0),
+                                           len_(len),
+                                           next_(nullptr) {
       data_ = new char[len];
+      if (env_ != nullptr)
+        env_->isolate()->AdjustAmountOfExternalAllocatedMemory(len);
     }
 
     ~Buffer() {
       delete[] data_;
+      if (env_ != nullptr)
+        env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-len_);
     }
 
+    Environment* env_;
     size_t read_pos_;
     size_t write_pos_;
     size_t len_;
@@ -107,6 +119,7 @@ class NodeBIO {
     char* data_;
   };
 
+  Environment* env_;
   size_t initial_;
   size_t length_;
   Buffer* read_head_;
index c5d5d55..8ecf33a 100644 (file)
@@ -127,6 +127,8 @@ void TLSWrap::InitSSL() {
   // Initialize SSL
   enc_in_ = NodeBIO::New();
   enc_out_ = NodeBIO::New();
+  NodeBIO::FromBIO(enc_in_)->AssignEnvironment(env());
+  NodeBIO::FromBIO(enc_out_)->AssignEnvironment(env());
 
   SSL_set_bio(ssl_, enc_in_, enc_out_);
 
@@ -162,6 +164,7 @@ void TLSWrap::InitSSL() {
 
   // Initialize ring for queud clear data
   clear_in_ = new NodeBIO();
+  clear_in_->AssignEnvironment(env());
 }