1
1
#include " stream_base.h"
2
+ #include " stream_base-inl.h"
2
3
#include " stream_wrap.h"
3
4
4
5
#include " node.h"
@@ -108,6 +109,8 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
108
109
// Determine storage size first
109
110
size_t storage_size = 0 ;
110
111
for (size_t i = 0 ; i < count; i++) {
112
+ storage_size = ROUND_UP (storage_size, WriteWrap::kAlignSize );
113
+
111
114
Handle <Value> chunk = chunks->Get (i * 2 );
112
115
113
116
if (Buffer::HasInstance (chunk))
@@ -124,7 +127,7 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
124
127
else
125
128
chunk_size = StringBytes::StorageSize (env->isolate (), string, encoding);
126
129
127
- storage_size += chunk_size + 15 ;
130
+ storage_size += chunk_size;
128
131
}
129
132
130
133
if (storage_size > INT_MAX)
@@ -133,13 +136,14 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
133
136
if (ARRAY_SIZE (bufs_) < count)
134
137
bufs = new uv_buf_t [count];
135
138
136
- storage_size += sizeof (WriteWrap);
137
- char * storage = new char [storage_size];
138
- WriteWrap* req_wrap =
139
- new (storage) WriteWrap (env, req_wrap_obj, this , AfterWrite);
139
+ WriteWrap* req_wrap = WriteWrap::New (env,
140
+ req_wrap_obj,
141
+ this ,
142
+ AfterWrite,
143
+ storage_size);
140
144
141
145
uint32_t bytes = 0 ;
142
- size_t offset = sizeof (WriteWrap) ;
146
+ size_t offset = 0 ;
143
147
for (size_t i = 0 ; i < count; i++) {
144
148
Handle <Value> chunk = chunks->Get (i * 2 );
145
149
@@ -152,9 +156,9 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
152
156
}
153
157
154
158
// Write string
155
- offset = ROUND_UP (offset, 16 );
159
+ offset = ROUND_UP (offset, WriteWrap:: kAlignSize );
156
160
CHECK_LT (offset, storage_size);
157
- char * str_storage = storage + offset;
161
+ char * str_storage = req_wrap-> Extra ( offset) ;
158
162
size_t str_size = storage_size - offset;
159
163
160
164
Handle <String> string = chunk->ToString (env->isolate ());
@@ -187,10 +191,8 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
187
191
ClearError ();
188
192
}
189
193
190
- if (err) {
191
- req_wrap->~WriteWrap ();
192
- delete[] storage;
193
- }
194
+ if (err)
195
+ req_wrap->Dispose ();
194
196
195
197
return err;
196
198
}
@@ -207,7 +209,6 @@ int StreamBase::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
207
209
const char * data = Buffer::Data (args[1 ]);
208
210
size_t length = Buffer::Length (args[1 ]);
209
211
210
- char * storage;
211
212
WriteWrap* req_wrap;
212
213
uv_buf_t buf;
213
214
buf.base = const_cast <char *>(data);
@@ -224,17 +225,14 @@ int StreamBase::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
224
225
CHECK_EQ (count, 1 );
225
226
226
227
// Allocate, or write rest
227
- storage = new char [sizeof (WriteWrap)];
228
- req_wrap = new (storage) WriteWrap (env, req_wrap_obj, this , AfterWrite);
228
+ req_wrap = WriteWrap::New (env, req_wrap_obj, this , AfterWrite);
229
229
230
230
err = DoWrite (req_wrap, bufs, count, nullptr );
231
231
req_wrap->Dispatched ();
232
232
req_wrap_obj->Set (env->async (), True (env->isolate ()));
233
233
234
- if (err) {
235
- req_wrap->~WriteWrap ();
236
- delete[] storage;
237
- }
234
+ if (err)
235
+ req_wrap->Dispose ();
238
236
239
237
done:
240
238
const char * msg = Error ();
@@ -275,14 +273,13 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
275
273
return UV_ENOBUFS;
276
274
277
275
// Try writing immediately if write size isn't too big
278
- char * storage;
279
276
WriteWrap* req_wrap;
280
277
char * data;
281
278
char stack_storage[16384 ]; // 16kb
282
279
size_t data_size;
283
280
uv_buf_t buf;
284
281
285
- bool try_write = storage_size + 15 <= sizeof (stack_storage) &&
282
+ bool try_write = storage_size <= sizeof (stack_storage) &&
286
283
(!IsIPCPipe () || send_handle_obj.IsEmpty ());
287
284
if (try_write) {
288
285
data_size = StringBytes::Write (env->isolate (),
@@ -308,11 +305,9 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
308
305
CHECK_EQ (count, 1 );
309
306
}
310
307
311
- storage = new char [sizeof (WriteWrap) + storage_size + 15 ];
312
- req_wrap = new (storage) WriteWrap (env, req_wrap_obj, this , AfterWrite);
308
+ req_wrap = WriteWrap::New (env, req_wrap_obj, this , AfterWrite, storage_size);
313
309
314
- data = reinterpret_cast <char *>(ROUND_UP (
315
- reinterpret_cast <uintptr_t >(storage) + sizeof (WriteWrap), 16 ));
310
+ data = req_wrap->Extra ();
316
311
317
312
if (try_write) {
318
313
// Copy partial data
@@ -355,10 +350,8 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
355
350
req_wrap->Dispatched ();
356
351
req_wrap->object ()->Set (env->async (), True (env->isolate ()));
357
352
358
- if (err) {
359
- req_wrap->~WriteWrap ();
360
- delete[] storage;
361
- }
353
+ if (err)
354
+ req_wrap->Dispose ();
362
355
363
356
done:
364
357
const char * msg = Error ();
@@ -404,8 +397,7 @@ void StreamBase::AfterWrite(WriteWrap* req_wrap, int status) {
404
397
if (req_wrap->object ()->Has (env->oncomplete_string ()))
405
398
req_wrap->MakeCallback (env->oncomplete_string (), ARRAY_SIZE (argv), argv);
406
399
407
- req_wrap->~WriteWrap ();
408
- delete[] reinterpret_cast <char *>(req_wrap);
400
+ req_wrap->Dispose ();
409
401
}
410
402
411
403
0 commit comments