Skip to content

Commit

Permalink
providers register API
Browse files Browse the repository at this point in the history
  • Loading branch information
bratpiorka committed Jul 20, 2023
1 parent 0d992a0 commit ec3b1c7
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 17 deletions.
2 changes: 1 addition & 1 deletion source/common/unified_malloc_framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,23 @@ typedef struct umf_memory_provider_t *umf_memory_provider_handle_t;
/// \return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
///
enum umf_result_t
umfMemoryProviderCreate(struct umf_memory_provider_ops_t *ops, void *params,
umf_memory_provider_handle_t *hProvider);
umfMemoryProviderCreate(const struct umf_memory_provider_ops_t *ops,
void *params, umf_memory_provider_handle_t *hProvider);

///
/// \brief Destroys memory provider.
/// \param hPool handle to the memory provider
///
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 umfMemoryProvidersRegisterGetNames(char *providers,
size_t *numProviders);
const struct umf_memory_provider_ops_t *
umfMemoryProvidersRegisterGetOps(char *name);

///
/// \brief Allocates size bytes of uninitialized storage from memory provider
/// with specified alignment.
Expand Down Expand Up @@ -110,7 +118,7 @@ umfMemoryProviderGetMinPageSize(umf_memory_provider_handle_t hProvider,

///
/// \brief Discard physical pages within the virtual memory mapping associated at given addr and size.
/// This call is asynchronous and may delay puring the pages indefinitely.
/// This call is asynchronous and may delay purging the pages indefinitely.
/// \param hProvider handle to the memory provider
/// \param ptr beginning of the virtual memory range
/// \param size size of the virtual memory range
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,45 @@
#ifndef UMF_MEMORY_PROVIDER_OPS_H
#define UMF_MEMORY_PROVIDER_OPS_H 1

#include <stdbool.h>
#include <umf/base.h>

#ifdef __cplusplus
extern "C" {
#endif

// TODO this is a fixed list - maybe this could be changed to a map?
// or other dynamic struct?
typedef enum umf_memory_provider_type_t {
UMF_MEMORY_PROVIDER_TYPE_INVALID = -1,
UMF_MEMORY_PROVIDER_TYPE_NUMA = 0,
UMF_MEMORY_PROVIDER_TYPE_USM,
UMF_MEMORY_PROVIDER_TYPE_FIXED,
UMF_MEMORY_PROVIDER_TYPE_FILE,
UMF_MEMORY_PROVIDER_TYPE_USER,
UMF_MEMORY_PROVIDER_TYPE_NUM = UMF_MEMORY_PROVIDER_TYPE_USER + 1
} umf_memory_provider_type_t;

/// This structure comprises function pointers used by corresponding
/// umfMemoryProvider* calls. Each memory provider implementation should
/// initialize all function pointers.
struct umf_memory_provider_ops_t {
/// Version of the ops structure.
/// Should be initialized using UMF_VERSION_CURRENT
uint32_t version;
umf_memory_provider_type_t type; // TODO change to caps?

///
/// \brief Initializes memory pool.
/// \param params pool-specific params
/// \param pool returns pointer to the pool
/// \brief Initializes memory provider.
/// \param params provider-specific params
/// \param provider returns pointer to the provider
/// \return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
enum umf_result_t (*initialize)(void *params, void **pool);
enum umf_result_t (*initialize)(void *params, void **provider);

///
/// \brief Finalizes memory pool.
/// \param pool pool to finalize
void (*finalize)(void *pool);
/// \brief Finalizes memory provider.
/// \param provider provider to finalize
void (*finalize)(void *provider);

/// Refer to memory_provider.h for description of those functions
enum umf_result_t (*alloc)(void *provider, size_t size, size_t alignment,
Expand All @@ -50,6 +64,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,30 @@
*
*/

#include "memory_provider_internal.h"
#include <umf/memory_provider.h>

#include <assert.h>
#include <stdlib.h>

#include <algorithm>
#include <cstring>
#include <list>
#include <string>

#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::list<struct umf_memory_provider_ops_t> globalProviders;

enum umf_result_t
umfMemoryProviderCreate(struct umf_memory_provider_ops_t *ops, void *params,
umf_memory_provider_handle_t *hProvider) {
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;
}
Expand All @@ -46,6 +54,51 @@ umfMemoryProviderCreate(struct umf_memory_provider_ops_t *ops, void *params,
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 umfMemoryProvidersRegisterGetNames(char *providers,
size_t *numProviders) {


if (providers == NULL) {
*numProviders = globalProviders.size();
} else {
// get min
//size_t num = (*numProviders > UMF_MEMORY_PROVIDER_TYPE_NUM)
// ? UMF_MEMORY_PROVIDER_TYPE_NUM
// : *numProviders;

for (auto p : globalProviders) {
std::strcat(providers, p.get_name(NULL));
std::strcat(providers, ";");
}
// remove last ';'
providers[std::strlen(providers) - 1] = '\0';
}

return UMF_RESULT_SUCCESS;
}

// TODO rename ;)
const umf_memory_provider_ops_t *umfMemoryProvidersRegisterGetOps(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);
Expand Down

0 comments on commit ec3b1c7

Please sign in to comment.