xref: /llvm-project/libc/src/__support/GPU/allocator.cpp (revision a6ef0debb1d60966b5bcc69f7d58a2b75c9c621d)
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 #include "src/__support/macros/config.h"
14 
15 namespace LIBC_NAMESPACE_DECL {
16 namespace {
17 
18 void *rpc_allocate(uint64_t size) {
19   void *ptr = nullptr;
20   rpc::Client::Port port = rpc::client.open<LIBC_MALLOC>();
21   port.send_and_recv(
22       [=](rpc::Buffer *buffer, uint32_t) { buffer->data[0] = size; },
23       [&](rpc::Buffer *buffer, uint32_t) {
24         ptr = reinterpret_cast<void *>(buffer->data[0]);
25       });
26   port.close();
27   return ptr;
28 }
29 
30 void rpc_free(void *ptr) {
31   rpc::Client::Port port = rpc::client.open<LIBC_FREE>();
32   port.send([=](rpc::Buffer *buffer, uint32_t) {
33     buffer->data[0] = reinterpret_cast<uintptr_t>(ptr);
34   });
35   port.close();
36 }
37 
38 } // namespace
39 
40 namespace gpu {
41 
42 void *allocate(uint64_t size) { return rpc_allocate(size); }
43 
44 void deallocate(void *ptr) { rpc_free(ptr); }
45 
46 } // namespace gpu
47 } // namespace LIBC_NAMESPACE_DECL
48