|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (C) 2023 Intel Corporation |
| 4 | + * |
| 5 | + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. |
| 6 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +#include "umf/ipc.h" |
| 11 | + |
| 12 | +#include "ipc_internal.h" |
| 13 | +#include "memory_pool_internal.h" |
| 14 | +#include "provider/provider_tracking.h" |
| 15 | + |
| 16 | +#include <assert.h> |
| 17 | +#include <stdlib.h> |
| 18 | + |
| 19 | +umf_result_t |
| 20 | +umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle, size_t *size) { |
| 21 | + size_t ipcHandleSize = 0; |
| 22 | + umf_alloc_info_t allocInfo; |
| 23 | + umf_result_t ret = umfMemoryTrackerGetAllocInfo(ptr, &allocInfo); |
| 24 | + if (ret != UMF_RESULT_SUCCESS) { |
| 25 | + return ret; |
| 26 | + } |
| 27 | + |
| 28 | + // We cannot use umfPoolGetMemoryProvider function because it returns |
| 29 | + // upstream provider but we need tracking one |
| 30 | + umf_memory_provider_handle_t provider = allocInfo.pool->provider; |
| 31 | + assert(provider); |
| 32 | + |
| 33 | + size_t providerIPCHandleSize; |
| 34 | + ret = umfMemoryProviderGetIPCHandleSize(provider, &providerIPCHandleSize); |
| 35 | + if (ret != UMF_RESULT_SUCCESS) { |
| 36 | + return ret; |
| 37 | + } |
| 38 | + |
| 39 | + ipcHandleSize = sizeof(umf_ipc_data_t) + providerIPCHandleSize; |
| 40 | + umf_ipc_data_t *ipcData = malloc(ipcHandleSize); |
| 41 | + if (!ipcData) { |
| 42 | + return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; |
| 43 | + } |
| 44 | + |
| 45 | + ret = |
| 46 | + umfMemoryProviderGetIPCHandle(provider, allocInfo.base, allocInfo.size, |
| 47 | + (void *)ipcData->providerData); |
| 48 | + if (ret != UMF_RESULT_SUCCESS) { |
| 49 | + free(ipcData); |
| 50 | + return ret; |
| 51 | + } |
| 52 | + |
| 53 | + ipcData->size = allocInfo.size; |
| 54 | + ipcData->offset = (uintptr_t)ptr - (uintptr_t)allocInfo.base; |
| 55 | + |
| 56 | + *umfIPCHandle = ipcData; |
| 57 | + *size = ipcHandleSize; |
| 58 | + |
| 59 | + return ret; |
| 60 | +} |
| 61 | + |
| 62 | +umf_result_t umfPutIPCHandle(umf_ipc_handle_t umfIPCHandle) { |
| 63 | + umf_result_t ret = UMF_RESULT_SUCCESS; |
| 64 | + |
| 65 | + // TODO: Just return SUCCESS because current tracking memory provider |
| 66 | + // implementation does nothing in Put function. Tracking memory |
| 67 | + // provider relies on IPC cache and actually Put IPC handle back |
| 68 | + // to upstream memory provider when umfMemoryProviderFree is called. |
| 69 | + // To support incapsulation we should not take into account |
| 70 | + // implementation details of tracking memory provider and find the |
| 71 | + // approrpiate pool, get memory provider of that pool and call |
| 72 | + // umfMemoryProviderPutIPCHandle(hProvider, |
| 73 | + // umfIPCHandle->providerData); |
| 74 | + free(umfIPCHandle); |
| 75 | + |
| 76 | + return ret; |
| 77 | +} |
| 78 | + |
| 79 | +umf_result_t umfOpenIPCHandle(umf_memory_pool_handle_t hPool, |
| 80 | + umf_ipc_handle_t umfIPCHandle, void **ptr) { |
| 81 | + |
| 82 | + // We cannot use umfPoolGetMemoryProvider function because it returns |
| 83 | + // upstream provider but we need tracking one |
| 84 | + umf_memory_provider_handle_t hProvider = hPool->provider; |
| 85 | + void *base = NULL; |
| 86 | + |
| 87 | + umf_result_t ret = umfMemoryProviderOpenIPCHandle( |
| 88 | + hProvider, (void *)umfIPCHandle->providerData, &base); |
| 89 | + if (ret != UMF_RESULT_SUCCESS) { |
| 90 | + return ret; |
| 91 | + } |
| 92 | + *ptr = (void *)((uintptr_t)base + umfIPCHandle->offset); |
| 93 | + |
| 94 | + return UMF_RESULT_SUCCESS; |
| 95 | +} |
| 96 | + |
| 97 | +umf_result_t umfCloseIPCHandle(void *ptr) { |
| 98 | + umf_alloc_info_t allocInfo; |
| 99 | + umf_result_t ret = |
| 100 | + umfMemoryTrackerGetAllocInfo(ptr, &allocInfo); |
| 101 | + if (ret != UMF_RESULT_SUCCESS) { |
| 102 | + return ret; |
| 103 | + } |
| 104 | + |
| 105 | + // We cannot use umfPoolGetMemoryProvider function because it returns |
| 106 | + // upstream provider but we need tracking one |
| 107 | + umf_memory_provider_handle_t hProvider = allocInfo.pool->provider; |
| 108 | + |
| 109 | + return umfMemoryProviderCloseIPCHandle(hProvider, allocInfo.base); |
| 110 | +} |
0 commit comments