From 59d5fe4ce0b063d2674a75dee3d671dfe4780956 Mon Sep 17 00:00:00 2001 From: Rafal Rudnicki Date: Thu, 20 Jul 2023 13:12:52 +0200 Subject: [PATCH] providers register API --- .../unified_malloc_framework/CMakeLists.txt | 2 +- .../include/umf/memory_pool_ops.h | 2 + .../include/umf/memory_provider.h | 11 ++++ .../include/umf/memory_provider_ops.h | 2 + ...{memory_provider.c => memory_provider.cpp} | 52 +++++++++++++++++-- 5 files changed, 64 insertions(+), 5 deletions(-) rename source/common/unified_malloc_framework/src/{memory_provider.c => memory_provider.cpp} (77%) diff --git a/source/common/unified_malloc_framework/CMakeLists.txt b/source/common/unified_malloc_framework/CMakeLists.txt index 15744605ec..2d3003e79f 100644 --- a/source/common/unified_malloc_framework/CMakeLists.txt +++ b/source/common/unified_malloc_framework/CMakeLists.txt @@ -5,7 +5,7 @@ set(UMF_SOURCES src/memory_pool.c - src/memory_provider.c + src/memory_provider.cpp src/memory_tracker.cpp src/memory_provider_get_last_failed.cpp ) diff --git a/source/common/unified_malloc_framework/include/umf/memory_pool_ops.h b/source/common/unified_malloc_framework/include/umf/memory_pool_ops.h index b858e5fc04..9d89c2e1c7 100644 --- a/source/common/unified_malloc_framework/include/umf/memory_pool_ops.h +++ b/source/common/unified_malloc_framework/include/umf/memory_pool_ops.h @@ -18,6 +18,8 @@ extern "C" { #endif +typedef struct umf_memory_provider_t *umf_memory_provider_handle_t; + /// \brief This structure comprises function pointers used by corresponding umfPool* /// calls. Each memory pool implementation should initialize all function /// pointers. diff --git a/source/common/unified_malloc_framework/include/umf/memory_provider.h b/source/common/unified_malloc_framework/include/umf/memory_provider.h index 2e50238238..0718c8efb1 100644 --- a/source/common/unified_malloc_framework/include/umf/memory_provider.h +++ b/source/common/unified_malloc_framework/include/umf/memory_provider.h @@ -37,6 +37,17 @@ umfMemoryProviderCreate(const struct umf_memory_provider_ops_t *ops, /// void umfMemoryProviderDestroy(umf_memory_provider_handle_t hProvider); +// TODO comment +enum umf_result_t +umfMemoryProviderRegister(struct umf_memory_provider_ops_t *ops); + +enum umf_result_t +umfMemoryProvidersRegistryGet(struct umf_memory_provider_ops_t *providers, + size_t *numProviders); + +const struct umf_memory_provider_ops_t * +umfMemoryProvidersRegistryGetOps(char *name); + /// /// \brief Allocates size bytes of uninitialized storage from memory provider /// with specified alignment. diff --git a/source/common/unified_malloc_framework/include/umf/memory_provider_ops.h b/source/common/unified_malloc_framework/include/umf/memory_provider_ops.h index 216be5437b..9a802a2102 100644 --- a/source/common/unified_malloc_framework/include/umf/memory_provider_ops.h +++ b/source/common/unified_malloc_framework/include/umf/memory_provider_ops.h @@ -11,6 +11,7 @@ #ifndef UMF_MEMORY_PROVIDER_OPS_H #define UMF_MEMORY_PROVIDER_OPS_H 1 +#include #include #ifdef __cplusplus @@ -50,6 +51,7 @@ struct umf_memory_provider_ops_t { enum umf_result_t (*purge_lazy)(void *provider, void *ptr, size_t size); enum umf_result_t (*purge_force)(void *provider, void *ptr, size_t size); const char *(*get_name)(void *provider); + bool (*supports_device)(const char *name); }; #ifdef __cplusplus diff --git a/source/common/unified_malloc_framework/src/memory_provider.c b/source/common/unified_malloc_framework/src/memory_provider.cpp similarity index 77% rename from source/common/unified_malloc_framework/src/memory_provider.c rename to source/common/unified_malloc_framework/src/memory_provider.cpp index 8a977e37c5..084c311767 100644 --- a/source/common/unified_malloc_framework/src/memory_provider.c +++ b/source/common/unified_malloc_framework/src/memory_provider.cpp @@ -8,22 +8,30 @@ * */ -#include "memory_provider_internal.h" -#include - #include #include +#include +#include +#include +#include + +#include "umf/memory_provider.h" + +#include "memory_provider_internal.h" + struct umf_memory_provider_t { struct umf_memory_provider_ops_t ops; void *provider_priv; }; +std::vector globalProviders; + enum umf_result_t umfMemoryProviderCreate(const struct umf_memory_provider_ops_t *ops, void *params, umf_memory_provider_handle_t *hProvider) { umf_memory_provider_handle_t provider = - malloc(sizeof(struct umf_memory_provider_t)); + (umf_memory_provider_t *)malloc(sizeof(struct umf_memory_provider_t)); if (!provider) { return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; } @@ -46,6 +54,42 @@ umfMemoryProviderCreate(const struct umf_memory_provider_ops_t *ops, return UMF_RESULT_SUCCESS; } +enum umf_result_t umfMemoryProviderRegister(umf_memory_provider_ops_t *ops) { + + // TODO check if this provider isn't already registered + globalProviders.push_back(*ops); + + return UMF_RESULT_SUCCESS; +} + +enum umf_result_t +umfMemoryProvidersRegistryGet(umf_memory_provider_ops_t *providers, + size_t *numProviders) { + + if (providers == NULL) { + *numProviders = globalProviders.size(); + } else { + memcpy(providers, globalProviders.data(), + sizeof(umf_memory_provider_ops_t) * *numProviders); + } + + return UMF_RESULT_SUCCESS; +} + +// TODO rename ;) +const umf_memory_provider_ops_t *umfMemoryProvidersRegistryGetOps(char *name) { + auto it = std::find_if( + std::begin(globalProviders), std::end(globalProviders), + [&](auto &ops) { return std::strcmp(ops.get_name(NULL), name) == 0; }); + + if (it != globalProviders.end()) { + return &(*it); + } + + // else + return NULL; +} + void umfMemoryProviderDestroy(umf_memory_provider_handle_t hProvider) { hProvider->ops.finalize(hProvider->provider_priv); free(hProvider);