Skip to content

Commit 33351a1

Browse files
codyhazelwoodjasnell
authored andcommitted
src: use MallocedBuffer abstraction for buffers
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>
1 parent 1f6c86d commit 33351a1

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/stream_base.cc

+11-15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "env-inl.h"
99
#include "js_stream.h"
1010
#include "string_bytes.h"
11+
#include "util.h"
1112
#include "util-inl.h"
1213
#include "v8.h"
1314

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

3940

40-
struct Free {
41-
void operator()(char* ptr) const { free(ptr); }
42-
};
43-
44-
4541
int StreamBase::ReadStartJS(const FunctionCallbackInfo<Value>& args) {
4642
return ReadStart();
4743
}
@@ -127,9 +123,9 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
127123
}
128124
}
129125

130-
std::unique_ptr<char[], Free> storage;
126+
MallocedBuffer<char> storage;
131127
if (storage_size > 0)
132-
storage = std::unique_ptr<char[], Free>(Malloc(storage_size));
128+
storage = MallocedBuffer<char>(storage_size);
133129

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

146142
// Write string
147143
CHECK_LE(offset, storage_size);
148-
char* str_storage = storage.get() + offset;
144+
char* str_storage = storage.data + offset;
149145
size_t str_size = storage_size - offset;
150146

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

165161
StreamWriteResult res = Write(*bufs, count, nullptr, req_wrap_obj);
166162
SetWriteResultPropertiesOnWrapObject(env, req_wrap_obj, res);
167-
if (res.wrap != nullptr && storage) {
163+
if (res.wrap != nullptr && storage_size > 0) {
168164
res.wrap->SetAllocatedStorage(storage.release(), storage_size);
169165
}
170166
return res.err;
@@ -263,26 +259,26 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
263259
CHECK_EQ(count, 1);
264260
}
265261

266-
std::unique_ptr<char[], Free> data;
262+
MallocedBuffer<char> data;
267263

268264
if (try_write) {
269265
// Copy partial data
270-
data = std::unique_ptr<char[], Free>(Malloc(buf.len));
271-
memcpy(data.get(), buf.base, buf.len);
266+
data = MallocedBuffer<char>(buf.len);
267+
memcpy(data.data, buf.base, buf.len);
272268
data_size = buf.len;
273269
} else {
274270
// Write it
275-
data = std::unique_ptr<char[], Free>(Malloc(storage_size));
271+
data = MallocedBuffer<char>(storage_size);
276272
data_size = StringBytes::Write(env->isolate(),
277-
data.get(),
273+
data.data,
278274
storage_size,
279275
string,
280276
enc);
281277
}
282278

283279
CHECK_LE(data_size, storage_size);
284280

285-
buf = uv_buf_init(data.get(), data_size);
281+
buf = uv_buf_init(data.data, data_size);
286282

287283
uv_stream_t* send_handle = nullptr;
288284

0 commit comments

Comments
 (0)