Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node_crypto_bio: adjust external memory size #1085

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/node_crypto_bio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ BIO* NodeBIO::New() {
}


void NodeBIO::AssignEnvironment(Environment* env) {
env_ = env;
}


int NodeBIO::New(BIO* bio) {
bio->ptr = new NodeBIO();

Expand Down Expand Up @@ -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;
Expand Down
23 changes: 18 additions & 5 deletions src/node_crypto_bio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -19,6 +23,8 @@ class NodeBIO {

static BIO* New();

void AssignEnvironment(Environment* env);

// Move read head to next buffer if needed
void TryMoveReadHead();

Expand Down Expand Up @@ -89,24 +95,31 @@ class NodeBIO {

class Buffer {
public:
explicit Buffer(size_t len) : read_pos_(0),
write_pos_(0),
len_(len),
next_(nullptr) {
explicit Buffer(Environment* env, size_t len) : env_(env),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can drop the explicit keyword here.

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_;
Buffer* next_;
char* data_;
};

Environment* env_;
size_t initial_;
size_t length_;
Buffer* read_head_;
Expand Down
3 changes: 3 additions & 0 deletions src/tls_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);

Expand Down Expand Up @@ -162,6 +164,7 @@ void TLSWrap::InitSSL() {

// Initialize ring for queud clear data
clear_in_ = new NodeBIO();
clear_in_->AssignEnvironment(env());
}


Expand Down