Skip to content

Commit e9c9d9f

Browse files
authored
Merge pull request #185 from igchor/proxy_pool
Add proxy_pool
2 parents 60a6f95 + 389e77b commit e9c9d9f

File tree

7 files changed

+213
-160
lines changed

7 files changed

+213
-160
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ A memory provider that provides memory from an operating system.
3737

3838
## Memory pool managers
3939

40+
### proxy_pool (part of libumf)
41+
42+
This memory pool is distributed as part of libumf. It forwards all requests to the underlying
43+
memory provider. Currently umfPoolRealloc, umfPoolCalloc and umfPoolMallocUsableSize functions
44+
are not supported by the proxy pool.
45+
4046
### libumf_pool_disjoint
4147

4248
TODO: Add a description

include/umf/pools/pool_proxy.h

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

src/CMakeLists.txt

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

3738
set(UMF_SOURCES_LINUX

src/pool/pool_proxy.c

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
// TODO: use ba_alloc here
30+
struct proxy_memory_pool *pool = malloc(sizeof(struct proxy_memory_pool));
31+
if (!pool) {
32+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
33+
}
34+
35+
pool->hProvider = hProvider;
36+
*ppPool = (void *)pool;
37+
38+
return UMF_RESULT_SUCCESS;
39+
}
40+
41+
static void proxy_pool_finalize(void *pool) { free(pool); }
42+
43+
static void *proxy_aligned_malloc(void *pool, size_t size, size_t alignment) {
44+
assert(pool);
45+
46+
void *ptr;
47+
struct proxy_memory_pool *hPool = (struct proxy_memory_pool *)pool;
48+
49+
umf_result_t ret =
50+
umfMemoryProviderAlloc(hPool->hProvider, size, alignment, &ptr);
51+
if (ret != UMF_RESULT_SUCCESS) {
52+
TLS_last_allocation_error = ret;
53+
return NULL;
54+
}
55+
56+
TLS_last_allocation_error = UMF_RESULT_SUCCESS;
57+
return ptr;
58+
}
59+
60+
static void *proxy_malloc(void *pool, size_t size) {
61+
assert(pool);
62+
63+
return proxy_aligned_malloc(pool, size, 0);
64+
}
65+
66+
static void *proxy_calloc(void *pool, size_t num, size_t size) {
67+
assert(pool);
68+
69+
(void)pool;
70+
(void)num;
71+
(void)size;
72+
73+
// Currently we cannot implement calloc in a way that would
74+
// work for memory that is inaccessible on the host
75+
TLS_last_allocation_error = UMF_RESULT_ERROR_NOT_SUPPORTED;
76+
return NULL;
77+
}
78+
79+
static void *proxy_realloc(void *pool, void *ptr, size_t size) {
80+
assert(pool);
81+
82+
(void)pool;
83+
(void)ptr;
84+
(void)size;
85+
86+
// Currently we cannot implement realloc in a way that would
87+
// work for memory that is inaccessible on the host
88+
TLS_last_allocation_error = UMF_RESULT_ERROR_NOT_SUPPORTED;
89+
return NULL;
90+
}
91+
92+
static umf_result_t proxy_free(void *pool, void *ptr) {
93+
assert(pool);
94+
95+
struct proxy_memory_pool *hPool = (struct proxy_memory_pool *)pool;
96+
return umfMemoryProviderFree(hPool->hProvider, ptr, 0);
97+
}
98+
99+
static size_t proxy_malloc_usable_size(void *pool, void *ptr) {
100+
assert(pool);
101+
102+
(void)pool;
103+
(void)ptr;
104+
105+
TLS_last_allocation_error = UMF_RESULT_ERROR_NOT_SUPPORTED;
106+
return 0;
107+
}
108+
109+
static umf_result_t proxy_get_last_allocation_error(void *pool) {
110+
(void)pool; // not used
111+
return TLS_last_allocation_error;
112+
}
113+
114+
static umf_memory_pool_ops_t UMF_PROXY_POOL_OPS = {
115+
.version = UMF_VERSION_CURRENT,
116+
.initialize = proxy_pool_initialize,
117+
.finalize = proxy_pool_finalize,
118+
.malloc = proxy_malloc,
119+
.calloc = proxy_calloc,
120+
.realloc = proxy_realloc,
121+
.aligned_malloc = proxy_aligned_malloc,
122+
.malloc_usable_size = proxy_malloc_usable_size,
123+
.free = proxy_free,
124+
.get_last_allocation_error = proxy_get_last_allocation_error};
125+
126+
umf_memory_pool_ops_t *umfProxyPoolOps(void) { return &UMF_PROXY_POOL_OPS; }

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)