Skip to content

Commit ef390b3

Browse files
Hardcode84jhuber6
andauthored
[libc] Use RAII based alloc in gpu rpc_server instead of manual new/delete (#110341)
Co-authored-by: Joseph Huber <[email protected]>
1 parent 969abfe commit ef390b3

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

libc/utils/gpu/server/rpc_server.cpp

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ void handle_printf(rpc::Server::Port &port) {
206206
delete[] reinterpret_cast<char *>(ptr);
207207
}
208208

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+
209220
template <uint32_t lane_size>
210221
rpc_status_t handle_server_impl(
211222
rpc::Server &server,
@@ -216,6 +227,8 @@ rpc_status_t handle_server_impl(
216227
if (!port)
217228
return RPC_STATUS_SUCCESS;
218229

230+
TempStorage temp_storage;
231+
219232
switch (port->get_opcode()) {
220233
case RPC_WRITE_TO_STREAM:
221234
case RPC_WRITE_TO_STDERR:
@@ -234,29 +247,28 @@ rpc_status_t handle_server_impl(
234247
std::fill(files, files + lane_size, stdout);
235248
}
236249

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); });
238252
port->send([&](rpc::Buffer *buffer, uint32_t id) {
239253
flockfile(files[id]);
240254
buffer->data[0] = fwrite_unlocked(strs[id], 1, sizes[id], files[id]);
241255
if (port->get_opcode() == RPC_WRITE_TO_STDOUT_NEWLINE &&
242256
buffer->data[0] == sizes[id])
243257
buffer->data[0] += fwrite_unlocked("\n", 1, 1, files[id]);
244258
funlockfile(files[id]);
245-
delete[] reinterpret_cast<uint8_t *>(strs[id]);
246259
});
247260
break;
248261
}
249262
case RPC_READ_FROM_STREAM: {
250263
uint64_t sizes[lane_size] = {0};
251264
void *data[lane_size] = {nullptr};
252265
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]);
254267
sizes[id] =
255268
fread(data[id], 1, buffer->data[0], file::to_stream(buffer->data[1]));
256269
});
257270
port->send_n(data, sizes);
258271
port->send([&](rpc::Buffer *buffer, uint32_t id) {
259-
delete[] reinterpret_cast<uint8_t *>(data[id]);
260272
std::memcpy(buffer->data, &sizes[id], sizeof(uint64_t));
261273
});
262274
break;
@@ -265,27 +277,24 @@ rpc_status_t handle_server_impl(
265277
uint64_t sizes[lane_size] = {0};
266278
void *data[lane_size] = {nullptr};
267279
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]);
269281
const char *str =
270282
fgets(reinterpret_cast<char *>(data[id]), buffer->data[0],
271283
file::to_stream(buffer->data[1]));
272284
sizes[id] = !str ? 0 : std::strlen(str) + 1;
273285
});
274286
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]);
278287
break;
279288
}
280289
case RPC_OPEN_FILE: {
281290
uint64_t sizes[lane_size] = {0};
282291
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); });
284294
port->recv_and_send([&](rpc::Buffer *buffer, uint32_t id) {
285295
FILE *file = fopen(reinterpret_cast<char *>(paths[id]),
286296
reinterpret_cast<char *>(buffer->data));
287297
buffer->data[0] = reinterpret_cast<uintptr_t>(file);
288-
delete[] reinterpret_cast<uint8_t *>(paths[id]);
289298
});
290299
break;
291300
}
@@ -316,13 +325,12 @@ rpc_status_t handle_server_impl(
316325
case RPC_HOST_CALL: {
317326
uint64_t sizes[lane_size] = {0};
318327
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); });
320330
port->recv([&](rpc::Buffer *buffer, uint32_t id) {
321331
reinterpret_cast<void (*)(void *)>(buffer->data[0])(args[id]);
322332
});
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) {});
326334
break;
327335
}
328336
case RPC_FEOF: {
@@ -385,11 +393,11 @@ rpc_status_t handle_server_impl(
385393
case RPC_REMOVE: {
386394
uint64_t sizes[lane_size] = {0};
387395
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); });
389398
port->send([&](rpc::Buffer *buffer, uint32_t id) {
390399
buffer->data[0] = static_cast<uint64_t>(
391400
remove(reinterpret_cast<const char *>(args[id])));
392-
delete[] reinterpret_cast<uint8_t *>(args[id]);
393401
});
394402
break;
395403
}
@@ -399,26 +407,24 @@ rpc_status_t handle_server_impl(
399407
void *oldpath[lane_size] = {nullptr};
400408
void *newpath[lane_size] = {nullptr};
401409
port->recv_n(oldpath, oldsizes,
402-
[&](uint64_t size) { return new char[size]; });
410+
[&](uint64_t size) { return temp_storage.alloc(size); });
403411
port->recv_n(newpath, newsizes,
404-
[&](uint64_t size) { return new char[size]; });
412+
[&](uint64_t size) { return temp_storage.alloc(size); });
405413
port->send([&](rpc::Buffer *buffer, uint32_t id) {
406414
buffer->data[0] = static_cast<uint64_t>(
407415
rename(reinterpret_cast<const char *>(oldpath[id]),
408416
reinterpret_cast<const char *>(newpath[id])));
409-
delete[] reinterpret_cast<uint8_t *>(oldpath[id]);
410-
delete[] reinterpret_cast<uint8_t *>(newpath[id]);
411417
});
412418
break;
413419
}
414420
case RPC_SYSTEM: {
415421
uint64_t sizes[lane_size] = {0};
416422
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); });
418425
port->send([&](rpc::Buffer *buffer, uint32_t id) {
419426
buffer->data[0] = static_cast<uint64_t>(
420427
system(reinterpret_cast<const char *>(args[id])));
421-
delete[] reinterpret_cast<uint8_t *>(args[id]);
422428
});
423429
break;
424430
}

0 commit comments

Comments
 (0)