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 rpc_allocate(uint64_t size)17void *rpc_allocate(uint64_t size) { 18 void *ptr = nullptr; 19 rpc::Client::Port port = rpc::client.open<RPC_MALLOC>(); 20 port.send_and_recv([=](rpc::Buffer *buffer) { buffer->data[0] = size; }, 21 [&](rpc::Buffer *buffer) { 22 ptr = reinterpret_cast<void *>(buffer->data[0]); 23 }); 24 port.close(); 25 return ptr; 26 } 27 rpc_free(void * ptr)28void rpc_free(void *ptr) { 29 rpc::Client::Port port = rpc::client.open<RPC_FREE>(); 30 port.send([=](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 allocate(uint64_t size)40void *allocate(uint64_t size) { return rpc_allocate(size); } 41 deallocate(void * ptr)42void deallocate(void *ptr) { rpc_free(ptr); } 43 44 } // namespace gpu 45 } // namespace LIBC_NAMESPACE 46