Skip to content

Commit

Permalink
src: refacor MallocedBuffer to it's usage scope
Browse files Browse the repository at this point in the history
  • Loading branch information
refack committed Oct 13, 2018
1 parent 8691d8f commit d16f098
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 50 deletions.
7 changes: 0 additions & 7 deletions src/memory_tracker-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,6 @@ void MemoryTracker::TrackField(const char* edge_name,
graph_->AddEdge(CurrentNode(), graph_->V8Node(value), edge_name);
}

template <typename T>
void MemoryTracker::TrackField(const char* edge_name,
const MallocedBuffer<T>& value,
const char* node_name) {
TrackFieldWithSize(edge_name, value.size, "MallocedBuffer");
}

void MemoryTracker::TrackField(const char* name,
const uv_buf_t& value,
const char* node_name) {
Expand Down
4 changes: 0 additions & 4 deletions src/memory_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,6 @@ class MemoryTracker {
inline void TrackField(const char* edge_name,
const v8::Local<T>& value,
const char* node_name = nullptr);
template <typename T>
inline void TrackField(const char* edge_name,
const MallocedBuffer<T>& value,
const char* node_name = nullptr);
inline void TrackField(const char* edge_name,
const uv_buf_t& value,
const char* node_name = nullptr);
Expand Down
18 changes: 11 additions & 7 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4197,9 +4197,13 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
Buffer::Length(args[0]),
0));

MallocedBuffer<char> data(DH_size(diffieHellman->dh_.get()));
size_t dh_size = DH_size(diffieHellman->dh_.get());
struct Free {
void operator()(char* ptr) const { free(ptr); }
};
std::unique_ptr<char, Free> data(Malloc(dh_size));

int size = DH_compute_key(reinterpret_cast<unsigned char*>(data.data),
int size = DH_compute_key(reinterpret_cast<unsigned char*>(data.get()),
key.get(),
diffieHellman->dh_.get());

Expand Down Expand Up @@ -4234,14 +4238,14 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
// DH_compute_key returns number of bytes in a remainder of exponent, which
// may have less bytes than a prime number. Therefore add 0-padding to the
// allocated buffer.
if (static_cast<size_t>(size) != data.size) {
CHECK_GT(data.size, static_cast<size_t>(size));
memmove(data.data + data.size - size, data.data, size);
memset(data.data, 0, data.size - size);
if (static_cast<size_t>(size) != dh_size) {
CHECK_GT(dh_size, static_cast<size_t>(size));
memmove(data.get() + dh_size - size, data.get(), size);
memset(data.get(), 0, dh_size - size);
}

args.GetReturnValue().Set(
Buffer::New(env->isolate(), data.release(), data.size).ToLocalChecked());
Buffer::New(env->isolate(), data.release(), dh_size).ToLocalChecked());
}

void DiffieHellman::SetKey(const v8::FunctionCallbackInfo<Value>& args,
Expand Down
2 changes: 1 addition & 1 deletion src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ Maybe<bool> Message::Serialize(Environment* env,
}

void Message::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("array_buffer_contents", array_buffer_contents_);
tracker->TrackFieldWithSize("array_buffer_contents", array_buffer_contents_.size(), "MallocedBuffer");
tracker->TrackFieldWithSize("shared_array_buffers",
shared_array_buffers_.size() * sizeof(shared_array_buffers_[0]));
tracker->TrackField("message_ports", message_ports_);
Expand Down
31 changes: 31 additions & 0 deletions src/node_messaging.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@
namespace node {
namespace worker {

// Simple RAII wrapper for contiguous data that uses malloc()/free().
template <typename T>
struct MallocedBuffer {
T* data;
size_t size;

T* release() {
T* ret = data;
data = nullptr;
return ret;
}

inline bool is_empty() const { return data == nullptr; }

MallocedBuffer() : data(nullptr) {}
explicit MallocedBuffer(size_t size) : data(Malloc<T>(size)), size(size) {}
MallocedBuffer(T* data, size_t size) : data(data), size(size) {}
MallocedBuffer(MallocedBuffer&& other) : data(other.data), size(other.size) {
other.data = nullptr;
}
MallocedBuffer& operator=(MallocedBuffer&& other) {
this->~MallocedBuffer();
return *new(this) MallocedBuffer(std::move(other));
}
~MallocedBuffer() {
free(data);
}
MallocedBuffer(const MallocedBuffer&) = delete;
MallocedBuffer& operator=(const MallocedBuffer&) = delete;
};

class MessagePortData;
class MessagePort;

Expand Down
31 changes: 0 additions & 31 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,37 +427,6 @@ struct OnScopeLeave {
~OnScopeLeave() { fn_(); }
};

// Simple RAII wrapper for contiguous data that uses malloc()/free().
template <typename T>
struct MallocedBuffer {
T* data;
size_t size;

T* release() {
T* ret = data;
data = nullptr;
return ret;
}

inline bool is_empty() const { return data == nullptr; }

MallocedBuffer() : data(nullptr) {}
explicit MallocedBuffer(size_t size) : data(Malloc<T>(size)), size(size) {}
MallocedBuffer(T* data, size_t size) : data(data), size(size) {}
MallocedBuffer(MallocedBuffer&& other) : data(other.data), size(other.size) {
other.data = nullptr;
}
MallocedBuffer& operator=(MallocedBuffer&& other) {
this->~MallocedBuffer();
return *new(this) MallocedBuffer(std::move(other));
}
~MallocedBuffer() {
free(data);
}
MallocedBuffer(const MallocedBuffer&) = delete;
MallocedBuffer& operator=(const MallocedBuffer&) = delete;
};

// Test whether some value can be called with ().
template <typename T, typename = void>
struct is_callable : std::is_function<T> { };
Expand Down

0 comments on commit d16f098

Please sign in to comment.