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

stream_base: use MallocedBuffer abstraction for buffers #23543

Closed
wants to merge 1 commit into from
Closed
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
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