@@ -206,6 +206,17 @@ void handle_printf(rpc::Server::Port &port) {
206
206
delete[] reinterpret_cast <char *>(ptr);
207
207
}
208
208
209
+ namespace {
210
+ struct TempStorage {
211
+ char *alloc (size_t size) {
212
+ storage.emplace_back (std::make_unique<char []>(size));
213
+ return storage.back ().get ();
214
+ }
215
+
216
+ std::vector<std::unique_ptr<char []>> storage;
217
+ };
218
+ } // namespace
219
+
209
220
template <uint32_t lane_size>
210
221
rpc_status_t handle_server_impl (
211
222
rpc::Server &server,
@@ -216,6 +227,8 @@ rpc_status_t handle_server_impl(
216
227
if (!port)
217
228
return RPC_STATUS_SUCCESS;
218
229
230
+ TempStorage temp_storage;
231
+
219
232
switch (port->get_opcode ()) {
220
233
case RPC_WRITE_TO_STREAM:
221
234
case RPC_WRITE_TO_STDERR:
@@ -234,29 +247,28 @@ rpc_status_t handle_server_impl(
234
247
std::fill (files, files + lane_size, stdout);
235
248
}
236
249
237
- port->recv_n (strs, sizes, [&](uint64_t size) { return new char [size]; });
250
+ port->recv_n (strs, sizes,
251
+ [&](uint64_t size) { return temp_storage.alloc (size); });
238
252
port->send ([&](rpc::Buffer *buffer, uint32_t id) {
239
253
flockfile (files[id]);
240
254
buffer->data [0 ] = fwrite_unlocked (strs[id], 1 , sizes[id], files[id]);
241
255
if (port->get_opcode () == RPC_WRITE_TO_STDOUT_NEWLINE &&
242
256
buffer->data [0 ] == sizes[id])
243
257
buffer->data [0 ] += fwrite_unlocked (" \n " , 1 , 1 , files[id]);
244
258
funlockfile (files[id]);
245
- delete[] reinterpret_cast <uint8_t *>(strs[id]);
246
259
});
247
260
break ;
248
261
}
249
262
case RPC_READ_FROM_STREAM: {
250
263
uint64_t sizes[lane_size] = {0 };
251
264
void *data[lane_size] = {nullptr };
252
265
port->recv ([&](rpc::Buffer *buffer, uint32_t id) {
253
- data[id] = new char [ buffer->data [0 ]] ;
266
+ data[id] = temp_storage. alloc ( buffer->data [0 ]) ;
254
267
sizes[id] =
255
268
fread (data[id], 1 , buffer->data [0 ], file::to_stream (buffer->data [1 ]));
256
269
});
257
270
port->send_n (data, sizes);
258
271
port->send ([&](rpc::Buffer *buffer, uint32_t id) {
259
- delete[] reinterpret_cast <uint8_t *>(data[id]);
260
272
std::memcpy (buffer->data , &sizes[id], sizeof (uint64_t ));
261
273
});
262
274
break ;
@@ -265,27 +277,24 @@ rpc_status_t handle_server_impl(
265
277
uint64_t sizes[lane_size] = {0 };
266
278
void *data[lane_size] = {nullptr };
267
279
port->recv ([&](rpc::Buffer *buffer, uint32_t id) {
268
- data[id] = new char [ buffer->data [0 ]] ;
280
+ data[id] = temp_storage. alloc ( buffer->data [0 ]) ;
269
281
const char *str =
270
282
fgets (reinterpret_cast <char *>(data[id]), buffer->data [0 ],
271
283
file::to_stream (buffer->data [1 ]));
272
284
sizes[id] = !str ? 0 : std::strlen (str) + 1 ;
273
285
});
274
286
port->send_n (data, sizes);
275
- for (uint32_t id = 0 ; id < lane_size; ++id)
276
- if (data[id])
277
- delete[] reinterpret_cast <uint8_t *>(data[id]);
278
287
break ;
279
288
}
280
289
case RPC_OPEN_FILE: {
281
290
uint64_t sizes[lane_size] = {0 };
282
291
void *paths[lane_size] = {nullptr };
283
- port->recv_n (paths, sizes, [&](uint64_t size) { return new char [size]; });
292
+ port->recv_n (paths, sizes,
293
+ [&](uint64_t size) { return temp_storage.alloc (size); });
284
294
port->recv_and_send ([&](rpc::Buffer *buffer, uint32_t id) {
285
295
FILE *file = fopen (reinterpret_cast <char *>(paths[id]),
286
296
reinterpret_cast <char *>(buffer->data ));
287
297
buffer->data [0 ] = reinterpret_cast <uintptr_t >(file);
288
- delete[] reinterpret_cast <uint8_t *>(paths[id]);
289
298
});
290
299
break ;
291
300
}
@@ -316,13 +325,12 @@ rpc_status_t handle_server_impl(
316
325
case RPC_HOST_CALL: {
317
326
uint64_t sizes[lane_size] = {0 };
318
327
void *args[lane_size] = {nullptr };
319
- port->recv_n (args, sizes, [&](uint64_t size) { return new char [size]; });
328
+ port->recv_n (args, sizes,
329
+ [&](uint64_t size) { return temp_storage.alloc (size); });
320
330
port->recv ([&](rpc::Buffer *buffer, uint32_t id) {
321
331
reinterpret_cast <void (*)(void *)>(buffer->data [0 ])(args[id]);
322
332
});
323
- port->send ([&](rpc::Buffer *, uint32_t id) {
324
- delete[] reinterpret_cast <uint8_t *>(args[id]);
325
- });
333
+ port->send ([&](rpc::Buffer *, uint32_t id) {});
326
334
break ;
327
335
}
328
336
case RPC_FEOF: {
@@ -385,11 +393,11 @@ rpc_status_t handle_server_impl(
385
393
case RPC_REMOVE: {
386
394
uint64_t sizes[lane_size] = {0 };
387
395
void *args[lane_size] = {nullptr };
388
- port->recv_n (args, sizes, [&](uint64_t size) { return new char [size]; });
396
+ port->recv_n (args, sizes,
397
+ [&](uint64_t size) { return temp_storage.alloc (size); });
389
398
port->send ([&](rpc::Buffer *buffer, uint32_t id) {
390
399
buffer->data [0 ] = static_cast <uint64_t >(
391
400
remove (reinterpret_cast <const char *>(args[id])));
392
- delete[] reinterpret_cast <uint8_t *>(args[id]);
393
401
});
394
402
break ;
395
403
}
@@ -399,26 +407,24 @@ rpc_status_t handle_server_impl(
399
407
void *oldpath[lane_size] = {nullptr };
400
408
void *newpath[lane_size] = {nullptr };
401
409
port->recv_n (oldpath, oldsizes,
402
- [&](uint64_t size) { return new char [ size] ; });
410
+ [&](uint64_t size) { return temp_storage. alloc ( size) ; });
403
411
port->recv_n (newpath, newsizes,
404
- [&](uint64_t size) { return new char [ size] ; });
412
+ [&](uint64_t size) { return temp_storage. alloc ( size) ; });
405
413
port->send ([&](rpc::Buffer *buffer, uint32_t id) {
406
414
buffer->data [0 ] = static_cast <uint64_t >(
407
415
rename (reinterpret_cast <const char *>(oldpath[id]),
408
416
reinterpret_cast <const char *>(newpath[id])));
409
- delete[] reinterpret_cast <uint8_t *>(oldpath[id]);
410
- delete[] reinterpret_cast <uint8_t *>(newpath[id]);
411
417
});
412
418
break ;
413
419
}
414
420
case RPC_SYSTEM: {
415
421
uint64_t sizes[lane_size] = {0 };
416
422
void *args[lane_size] = {nullptr };
417
- port->recv_n (args, sizes, [&](uint64_t size) { return new char [size]; });
423
+ port->recv_n (args, sizes,
424
+ [&](uint64_t size) { return temp_storage.alloc (size); });
418
425
port->send ([&](rpc::Buffer *buffer, uint32_t id) {
419
426
buffer->data [0 ] = static_cast <uint64_t >(
420
427
system (reinterpret_cast <const char *>(args[id])));
421
- delete[] reinterpret_cast <uint8_t *>(args[id]);
422
428
});
423
429
break ;
424
430
}
0 commit comments