1 | //===-- GPU memory allocator implementation ---------------------*- C++ -*-===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "allocator.h" |
10 | |
11 | #include "src/__support/GPU/utils.h" |
12 | #include "src/__support/RPC/rpc_client.h" |
13 | |
14 | namespace LIBC_NAMESPACE { |
15 | namespace { |
16 | |
17 | void *rpc_allocate(uint64_t size) { |
18 | void *ptr = nullptr; |
19 | rpc::Client::Port port = rpc::client.open<RPC_MALLOC>(); |
20 | port.send_and_recv(fill: [=](rpc::Buffer *buffer) { buffer->data[0] = size; }, |
21 | use: [&](rpc::Buffer *buffer) { |
22 | ptr = reinterpret_cast<void *>(buffer->data[0]); |
23 | }); |
24 | port.close(); |
25 | return ptr; |
26 | } |
27 | |
28 | void rpc_free(void *ptr) { |
29 | rpc::Client::Port port = rpc::client.open<RPC_FREE>(); |
30 | port.send(fill: [=](rpc::Buffer *buffer) { |
31 | buffer->data[0] = reinterpret_cast<uintptr_t>(ptr); |
32 | }); |
33 | port.close(); |
34 | } |
35 | |
36 | } // namespace |
37 | |
38 | namespace gpu { |
39 | |
40 | void *allocate(uint64_t size) { return rpc_allocate(size); } |
41 | |
42 | void deallocate(void *ptr) { rpc_free(ptr); } |
43 | |
44 | } // namespace gpu |
45 | } // namespace LIBC_NAMESPACE |
46 |