Skip to content

Commit

Permalink
src: use MallocedBuffer abstraction for buffers
Browse files Browse the repository at this point in the history
Drop `Free` and `std::unique_ptr` in favor of Node's `MallocedBuffer`
for `char[]` buffer memory mangement.

PR-URL: #23543
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
  • Loading branch information
codyhazelwood authored and MylesBorins committed Oct 30, 2018
1 parent 6e5a5ff commit b55fed0
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/stream_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "env-inl.h"
#include "js_stream.h"
#include "string_bytes.h"
#include "util.h"
#include "util-inl.h"
#include "v8.h"

Expand Down Expand Up @@ -37,11 +38,6 @@ template int StreamBase::WriteString<LATIN1>(
const FunctionCallbackInfo<Value>& args);


struct Free {
void operator()(char* ptr) const { free(ptr); }
};


int StreamBase::ReadStartJS(const FunctionCallbackInfo<Value>& args) {
return ReadStart();
}
Expand Down Expand Up @@ -127,9 +123,9 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
}
}

std::unique_ptr<char[], Free> storage;
MallocedBuffer<char> storage;
if (storage_size > 0)
storage = std::unique_ptr<char[], Free>(Malloc(storage_size));
storage = MallocedBuffer<char>(storage_size);

offset = 0;
if (!all_buffers) {
Expand All @@ -145,7 +141,7 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {

// Write string
CHECK_LE(offset, storage_size);
char* str_storage = storage.get() + offset;
char* str_storage = storage.data + offset;
size_t str_size = storage_size - offset;

Local<String> string = chunk->ToString(env->context()).ToLocalChecked();
Expand All @@ -164,7 +160,7 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {

StreamWriteResult res = Write(*bufs, count, nullptr, req_wrap_obj);
SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res);
if (res.wrap != nullptr && storage) {
if (res.wrap != nullptr && storage_size > 0) {
res.wrap->SetAllocatedStorage(storage.release(), storage_size);
}
return res.err;
Expand Down Expand Up @@ -263,26 +259,26 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(count, 1);
}

std::unique_ptr<char[], Free> data;
MallocedBuffer<char> data;

if (try_write) {
// Copy partial data
data = std::unique_ptr<char[], Free>(Malloc(buf.len));
memcpy(data.get(), buf.base, buf.len);
data = MallocedBuffer<char>(buf.len);
memcpy(data.data, buf.base, buf.len);
data_size = buf.len;
} else {
// Write it
data = std::unique_ptr<char[], Free>(Malloc(storage_size));
data = MallocedBuffer<char>(storage_size);
data_size = StringBytes::Write(env->isolate(),
data.get(),
data.data,
storage_size,
string,
enc);
}

CHECK_LE(data_size, storage_size);

buf = uv_buf_init(data.get(), data_size);
buf = uv_buf_init(data.data, data_size);

uv_stream_t* send_handle = nullptr;

Expand Down

0 comments on commit b55fed0

Please sign in to comment.