|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (C) 2024 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/memory_pool_ops.h> |
| 11 | +#include <umf/pools/pool_proxy.h> |
| 12 | + |
| 13 | +#include <assert.h> |
| 14 | +#include <stdlib.h> |
| 15 | + |
| 16 | +#include "utils_common.h" |
| 17 | + |
| 18 | +static __TLS umf_result_t TLS_last_allocation_error; |
| 19 | + |
| 20 | +struct proxy_memory_pool { |
| 21 | + umf_memory_provider_handle_t hProvider; |
| 22 | +}; |
| 23 | + |
| 24 | +static umf_result_t |
| 25 | +proxy_pool_initialize(umf_memory_provider_handle_t hProvider, void *params, |
| 26 | + void **ppPool) { |
| 27 | + (void)params; // unused |
| 28 | + |
| 29 | + struct proxy_memory_pool *pool = malloc(sizeof(struct proxy_memory_pool)); |
| 30 | + if (!pool) { |
| 31 | + return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; |
| 32 | + } |
| 33 | + |
| 34 | + pool->hProvider = hProvider; |
| 35 | + *ppPool = (void *)pool; |
| 36 | + |
| 37 | + return UMF_RESULT_SUCCESS; |
| 38 | +} |
| 39 | + |
| 40 | +static void proxy_pool_finalize(void *pool) { free(pool); } |
| 41 | + |
| 42 | +static void *proxy_aligned_malloc(void *pool, size_t size, size_t alignment) { |
| 43 | + assert(pool); |
| 44 | + |
| 45 | + void *ptr; |
| 46 | + struct proxy_memory_pool *hPool = (struct proxy_memory_pool *)pool; |
| 47 | + |
| 48 | + umf_result_t ret = |
| 49 | + umfMemoryProviderAlloc(hPool->hProvider, size, alignment, &ptr); |
| 50 | + if (ret != UMF_RESULT_SUCCESS) { |
| 51 | + TLS_last_allocation_error = ret; |
| 52 | + return NULL; |
| 53 | + } |
| 54 | + |
| 55 | + TLS_last_allocation_error = UMF_RESULT_SUCCESS; |
| 56 | + return ptr; |
| 57 | +} |
| 58 | + |
| 59 | +static void *proxy_malloc(void *pool, size_t size) { |
| 60 | + assert(pool); |
| 61 | + |
| 62 | + return proxy_aligned_malloc(pool, size, 0); |
| 63 | +} |
| 64 | + |
| 65 | +static void *proxy_calloc(void *pool, size_t num, size_t size) { |
| 66 | + assert(pool); |
| 67 | + |
| 68 | + (void)pool; |
| 69 | + (void)num; |
| 70 | + (void)size; |
| 71 | + |
| 72 | + // Currently we cannot implement calloc in a way that would |
| 73 | + // work for memory that is inaccessible on the host |
| 74 | + TLS_last_allocation_error = UMF_RESULT_ERROR_NOT_SUPPORTED; |
| 75 | + return NULL; |
| 76 | +} |
| 77 | + |
| 78 | +static void *proxy_realloc(void *pool, void *ptr, size_t size) { |
| 79 | + assert(pool); |
| 80 | + |
| 81 | + (void)pool; |
| 82 | + (void)ptr; |
| 83 | + (void)size; |
| 84 | + |
| 85 | + // Currently we cannot implement realloc in a way that would |
| 86 | + // work for memory that is inaccessible on the host |
| 87 | + TLS_last_allocation_error = UMF_RESULT_ERROR_NOT_SUPPORTED; |
| 88 | + return NULL; |
| 89 | +} |
| 90 | + |
| 91 | +static umf_result_t proxy_free(void *pool, void *ptr) { |
| 92 | + assert(pool); |
| 93 | + |
| 94 | + struct proxy_memory_pool *hPool = (struct proxy_memory_pool *)pool; |
| 95 | + return umfMemoryProviderFree(hPool->hProvider, ptr, 0); |
| 96 | +} |
| 97 | + |
| 98 | +static size_t proxy_malloc_usable_size(void *pool, void *ptr) { |
| 99 | + assert(pool); |
| 100 | + |
| 101 | + (void)pool; |
| 102 | + (void)ptr; |
| 103 | + |
| 104 | + TLS_last_allocation_error = UMF_RESULT_ERROR_NOT_SUPPORTED; |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | +static umf_result_t proxy_get_last_allocation_error(void *pool) { |
| 109 | + (void)pool; // not used |
| 110 | + return TLS_last_allocation_error; |
| 111 | +} |
| 112 | + |
| 113 | +static umf_memory_pool_ops_t UMF_PROXY_POOL_OPS = { |
| 114 | + .version = UMF_VERSION_CURRENT, |
| 115 | + .initialize = proxy_pool_initialize, |
| 116 | + .finalize = proxy_pool_finalize, |
| 117 | + .malloc = proxy_malloc, |
| 118 | + .calloc = proxy_calloc, |
| 119 | + .realloc = proxy_realloc, |
| 120 | + .aligned_malloc = proxy_aligned_malloc, |
| 121 | + .malloc_usable_size = proxy_malloc_usable_size, |
| 122 | + .free = proxy_free, |
| 123 | + .get_last_allocation_error = proxy_get_last_allocation_error}; |
| 124 | + |
| 125 | +umf_memory_pool_ops_t *umfProxyPoolOps(void) { return &UMF_PROXY_POOL_OPS; } |
0 commit comments