Skip to content

Commit

Permalink
memory providers
Browse files Browse the repository at this point in the history
  • Loading branch information
bratpiorka committed Jul 7, 2023
1 parent 2c81a20 commit 383e5a9
Show file tree
Hide file tree
Showing 11 changed files with 593 additions and 14 deletions.
2 changes: 2 additions & 0 deletions source/common/unified_malloc_framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(UMF_SOURCES
src/memory_provider.c
src/memory_tracker.cpp
src/memory_provider_get_last_failed.cpp
src/os_memory_provider.c
)

if(UMF_BUILD_SHARED_LIBRARY)
Expand All @@ -22,5 +23,6 @@ else()
endif()

add_library(${PROJECT_NAME}::unified_malloc_framework ALIAS unified_malloc_framework)
target_link_libraries(unified_malloc_framework numa)

target_include_directories(unified_malloc_framework PUBLIC include)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 All @@ -26,7 +28,7 @@ struct umf_memory_pool_ops_t {
uint32_t version;

///
/// \brief Intializes memory pool.
/// \brief Initializes memory pool.
/// \param providers array of memory providers that will be used for coarse-grain allocations.
/// Should contain at least one memory provider.
/// \param numProvider number of elements in the providers array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ umfMemoryProviderCreate(struct umf_memory_provider_ops_t *ops, void *params,
///
void umfMemoryProviderDestroy(umf_memory_provider_handle_t hProvider);

// TODO comment
enum umf_result_t
umfMemoryProviderRegister(umf_memory_provider_handle_t hProvider,
umf_memory_provider_type_t type);

// TODO comment
enum umf_result_t umfMemoryProvidersGet(umf_memory_provider_handle_t *providers,
size_t *numProviders);

umf_memory_provider_type_t
umfMemoryProviderGetType(umf_memory_provider_handle_t hProvider);

///
/// \brief Allocates size bytes of uninitialized storage from memory provider
/// with specified alignment.
Expand Down Expand Up @@ -110,7 +122,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 All @@ -123,7 +135,7 @@ umfMemoryProviderPurgeLazy(umf_memory_provider_handle_t hProvider, void *ptr,

///
/// \brief Discard physical pages within the virtual memory mapping associated at given addr and size.
/// This call is synchronous and if it suceeds, pages are guaranteed to be zero-filled on the next access.
/// This call is synchronous and if it succeeds, pages are guaranteed to be zero-filled on the next access.
/// \param hProvider handle to the memory provider
/// \param ptr beginning of the virtual memory range
/// \param size size of the virtual memory range
Expand All @@ -135,7 +147,7 @@ umfMemoryProviderPurgeForce(umf_memory_provider_handle_t hProvider, void *ptr,
size_t size);

///
/// \brief Retrive name of a given memory provider.
/// \brief Retrieve name of a given memory provider.
/// \param hProvider handle to the memory provider
/// \param ppName [out] pointer to a string containing name of the provider
const char *umfMemoryProviderGetName(umf_memory_provider_handle_t hProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,38 @@
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 Intializes 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 Down
58 changes: 55 additions & 3 deletions source/common/unified_malloc_framework/src/memory_provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@
*
*/

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

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

#include <umf/memory_provider.h>

#include "memory_provider_internal.h"
#include "os_memory_provider.h"

struct umf_memory_provider_t {
struct umf_memory_provider_ops_t ops;
void *provider_priv;
};

umf_memory_provider_handle_t globalProviders[UMF_MEMORY_PROVIDER_TYPE_NUM] = {
0};

enum umf_result_t
umfMemoryProviderCreate(struct umf_memory_provider_ops_t *ops, void *params,
umf_memory_provider_handle_t *hProvider) {
Expand Down Expand Up @@ -46,6 +52,52 @@ umfMemoryProviderCreate(struct umf_memory_provider_ops_t *ops, void *params,
return UMF_RESULT_SUCCESS;
}

enum umf_result_t
umfMemoryProviderRegister(umf_memory_provider_handle_t hProvider,
umf_memory_provider_type_t type) {

// TODO improve
globalProviders[type] = hProvider;

return UMF_RESULT_SUCCESS;
}

enum umf_result_t umfMemoryProvidersGet(umf_memory_provider_handle_t *providers,
size_t *numProviders) {
// TODO improve
if (globalProviders[UMF_MEMORY_PROVIDER_TYPE_NUMA] == NULL) {
// register built-in providers at first call here
umf_memory_provider_handle_t os = NULL;
// TODO type is passed here as a priv - this should be definitely changed
umfMemoryProviderCreate(&OS_MEMORY_PROVIDER_OPS, NULL, &os);
umfMemoryProviderRegister(os, UMF_MEMORY_PROVIDER_TYPE_NUMA);

// TODO IMPORTANT
// as the NUMA (OS) memory provider is the default provider here in the UMF,
// it should be available (predefined) somehow so a user could use it
// without any umalloc or UR libs etc
}

if (providers == NULL) {
// TODO - count them
*numProviders = UMF_MEMORY_PROVIDER_TYPE_NUM;
} else {
// get min
size_t num = (*numProviders > UMF_MEMORY_PROVIDER_TYPE_NUM)
? UMF_MEMORY_PROVIDER_TYPE_NUM
: *numProviders;
memcpy(providers, globalProviders,
num * sizeof(umf_memory_provider_handle_t));
}

return UMF_RESULT_SUCCESS;
}

umf_memory_provider_type_t
umfMemoryProviderGetType(umf_memory_provider_handle_t hProvider) {
return hProvider->ops.type;
}

void umfMemoryProviderDestroy(umf_memory_provider_handle_t hProvider) {
hProvider->ops.finalize(hProvider->provider_priv);
free(hProvider);
Expand Down
Loading

0 comments on commit 383e5a9

Please sign in to comment.