-
Notifications
You must be signed in to change notification settings - Fork 35
Add proxy_pool #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add proxy_pool #185
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* | ||
* Copyright (C) 2024 Intel Corporation | ||
* | ||
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. | ||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
* | ||
*/ | ||
|
||
#ifndef UMF_PROXY_MEMORY_POOL_H | ||
#define UMF_PROXY_MEMORY_POOL_H 1 | ||
|
||
#include <umf/base.h> | ||
#include <umf/memory_pool.h> | ||
#include <umf/memory_provider.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
UMF_EXPORT umf_memory_pool_ops_t *umfProxyPoolOps(void); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* UMF_PROXY_MEMORY_POOL_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* | ||
* Copyright (C) 2024 Intel Corporation | ||
* | ||
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. | ||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
* | ||
*/ | ||
|
||
#include <umf/memory_pool_ops.h> | ||
#include <umf/pools/pool_proxy.h> | ||
|
||
#include <assert.h> | ||
#include <stdlib.h> | ||
|
||
#include "utils_common.h" | ||
|
||
static __TLS umf_result_t TLS_last_allocation_error; | ||
|
||
struct proxy_memory_pool { | ||
umf_memory_provider_handle_t hProvider; | ||
}; | ||
|
||
static umf_result_t | ||
proxy_pool_initialize(umf_memory_provider_handle_t hProvider, void *params, | ||
void **ppPool) { | ||
(void)params; // unused | ||
|
||
// TODO: use ba_alloc here | ||
struct proxy_memory_pool *pool = malloc(sizeof(struct proxy_memory_pool)); | ||
if (!pool) { | ||
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; | ||
} | ||
|
||
pool->hProvider = hProvider; | ||
*ppPool = (void *)pool; | ||
|
||
return UMF_RESULT_SUCCESS; | ||
} | ||
|
||
static void proxy_pool_finalize(void *pool) { free(pool); } | ||
|
||
static void *proxy_aligned_malloc(void *pool, size_t size, size_t alignment) { | ||
assert(pool); | ||
|
||
void *ptr; | ||
struct proxy_memory_pool *hPool = (struct proxy_memory_pool *)pool; | ||
|
||
umf_result_t ret = | ||
umfMemoryProviderAlloc(hPool->hProvider, size, alignment, &ptr); | ||
if (ret != UMF_RESULT_SUCCESS) { | ||
TLS_last_allocation_error = ret; | ||
return NULL; | ||
} | ||
|
||
TLS_last_allocation_error = UMF_RESULT_SUCCESS; | ||
return ptr; | ||
} | ||
|
||
static void *proxy_malloc(void *pool, size_t size) { | ||
assert(pool); | ||
|
||
return proxy_aligned_malloc(pool, size, 0); | ||
} | ||
|
||
static void *proxy_calloc(void *pool, size_t num, size_t size) { | ||
assert(pool); | ||
|
||
(void)pool; | ||
(void)num; | ||
(void)size; | ||
|
||
// Currently we cannot implement calloc in a way that would | ||
// work for memory that is inaccessible on the host | ||
bratpiorka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TLS_last_allocation_error = UMF_RESULT_ERROR_NOT_SUPPORTED; | ||
return NULL; | ||
} | ||
|
||
static void *proxy_realloc(void *pool, void *ptr, size_t size) { | ||
assert(pool); | ||
|
||
(void)pool; | ||
(void)ptr; | ||
(void)size; | ||
|
||
// Currently we cannot implement realloc in a way that would | ||
// work for memory that is inaccessible on the host | ||
TLS_last_allocation_error = UMF_RESULT_ERROR_NOT_SUPPORTED; | ||
return NULL; | ||
} | ||
|
||
static umf_result_t proxy_free(void *pool, void *ptr) { | ||
assert(pool); | ||
|
||
struct proxy_memory_pool *hPool = (struct proxy_memory_pool *)pool; | ||
return umfMemoryProviderFree(hPool->hProvider, ptr, 0); | ||
} | ||
|
||
static size_t proxy_malloc_usable_size(void *pool, void *ptr) { | ||
assert(pool); | ||
|
||
(void)pool; | ||
(void)ptr; | ||
|
||
TLS_last_allocation_error = UMF_RESULT_ERROR_NOT_SUPPORTED; | ||
return 0; | ||
} | ||
|
||
static umf_result_t proxy_get_last_allocation_error(void *pool) { | ||
(void)pool; // not used | ||
return TLS_last_allocation_error; | ||
} | ||
|
||
static umf_memory_pool_ops_t UMF_PROXY_POOL_OPS = { | ||
.version = UMF_VERSION_CURRENT, | ||
.initialize = proxy_pool_initialize, | ||
.finalize = proxy_pool_finalize, | ||
.malloc = proxy_malloc, | ||
.calloc = proxy_calloc, | ||
.realloc = proxy_realloc, | ||
.aligned_malloc = proxy_aligned_malloc, | ||
.malloc_usable_size = proxy_malloc_usable_size, | ||
.free = proxy_free, | ||
.get_last_allocation_error = proxy_get_last_allocation_error}; | ||
|
||
umf_memory_pool_ops_t *umfProxyPoolOps(void) { return &UMF_PROXY_POOL_OPS; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.