Skip to content

Commit 8a3e86c

Browse files
committed
Add proxy_pool
a pool that forwards all (supported) requests to the memory provider
1 parent 3a21984 commit 8a3e86c

File tree

8 files changed

+209
-167
lines changed

8 files changed

+209
-167
lines changed

include/umf/pools/pool_proxy.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
#ifndef UMF_PROXY_MEMORY_POOL_H
11+
#define UMF_PROXY_MEMORY_POOL_H 1
12+
13+
#include <umf/memory_pool.h>
14+
#include <umf/memory_provider.h>
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
umf_memory_pool_ops_t *umfProxyPoolOps(void);
21+
22+
#ifdef __cplusplus
23+
}
24+
#endif
25+
26+
#endif /* UMF_PROXY_MEMORY_POOL_H */

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ set(UMF_SOURCES
3131
memspace.c
3232
provider/provider_tracking.c
3333
critnib/critnib.c
34+
pool/pool_proxy.c
3435
)
3536

3637
set(UMF_SOURCES_LINUX

src/pool/pool_proxy.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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; }

src/umf.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ EXPORTS
3535
umfPoolMalloc
3636
umfPoolMallocUsableSize
3737
umfPoolRealloc
38+
umfProxyPoolOps
3839
umfOsMemoryProviderOps
3940
umfPoolCreateFromMemspace
4041
umfMemoryProviderCreateFromMemspace

src/umf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ UMF_1.0 {
2929
umfPoolMalloc;
3030
umfPoolMallocUsableSize;
3131
umfPoolRealloc;
32+
umfProxyPoolOps;
3233
umfOsMemoryProviderOps;
3334
umfPoolCreateFromMemspace;
3435
umfMemoryProviderCreateFromMemspace;

test/common/pool.hpp

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ namespace umf_test {
2929

3030
umf_memory_pool_handle_t
3131
createPoolChecked(umf_memory_pool_ops_t *ops,
32-
umf_memory_provider_handle_t hProvider, void *params) {
32+
umf_memory_provider_handle_t hProvider, void *params,
33+
umf_pool_create_flags_t flags = 0) {
3334
umf_memory_pool_handle_t hPool;
34-
auto ret = umfPoolCreate(ops, hProvider, params, 0, &hPool);
35+
auto ret = umfPoolCreate(ops, hProvider, params, flags, &hPool);
3536
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
3637
return hPool;
3738
}
@@ -130,54 +131,6 @@ struct malloc_pool : public pool_base_t {
130131
umf_memory_pool_ops_t MALLOC_POOL_OPS =
131132
umf::poolMakeCOps<umf_test::malloc_pool, void>();
132133

133-
struct proxy_pool : public pool_base_t {
134-
umf_result_t initialize(umf_memory_provider_handle_t provider) noexcept {
135-
this->provider = provider;
136-
return UMF_RESULT_SUCCESS;
137-
}
138-
void *malloc(size_t size) noexcept { return aligned_malloc(size, 0); }
139-
void *calloc(size_t num, size_t size) noexcept {
140-
void *ptr = nullptr;
141-
auto ret = umfMemoryProviderAlloc(provider, num * size, 0, &ptr);
142-
umf::getPoolLastStatusRef<proxy_pool>() = ret;
143-
144-
if (!ptr) {
145-
return ptr;
146-
}
147-
148-
memset(ptr, 0, num * size);
149-
return ptr;
150-
}
151-
void *realloc([[maybe_unused]] void *ptr,
152-
[[maybe_unused]] size_t size) noexcept {
153-
// TODO: not supported
154-
umf::getPoolLastStatusRef<proxy_pool>() =
155-
UMF_RESULT_ERROR_NOT_SUPPORTED;
156-
return nullptr;
157-
}
158-
void *aligned_malloc(size_t size, size_t alignment) noexcept {
159-
void *ptr = nullptr;
160-
auto ret = umfMemoryProviderAlloc(provider, size, alignment, &ptr);
161-
umf::getPoolLastStatusRef<proxy_pool>() = ret;
162-
return ptr;
163-
}
164-
size_t malloc_usable_size([[maybe_unused]] void *ptr) noexcept {
165-
// TODO: not supported
166-
return 0;
167-
}
168-
umf_result_t free(void *ptr) noexcept {
169-
auto ret = umfMemoryProviderFree(provider, ptr, 0);
170-
return ret;
171-
}
172-
umf_result_t get_last_allocation_error() {
173-
return umf::getPoolLastStatusRef<proxy_pool>();
174-
}
175-
umf_memory_provider_handle_t provider;
176-
};
177-
178-
umf_memory_pool_ops_t PROXY_POOL_OPS =
179-
umf::poolMakeCOps<umf_test::proxy_pool, void>();
180-
181134
} // namespace umf_test
182135

183136
#endif /* UMF_TEST_POOL_HPP */

0 commit comments

Comments
 (0)