Skip to content

Commit c9f4b11

Browse files
committed
Update File provider config API
1 parent 61f02ef commit c9f4b11

File tree

11 files changed

+446
-79
lines changed

11 files changed

+446
-79
lines changed

examples/dram_and_fsdax/dram_and_fsdax.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,25 @@ static umf_memory_pool_handle_t create_fsdax_pool(const char *path) {
4848
umf_memory_pool_handle_t pool_fsdax;
4949
umf_result_t umf_result;
5050

51-
umf_file_memory_provider_params_t params_fsdax =
52-
umfFileMemoryProviderParamsDefault(path);
51+
umf_file_memory_provider_params_handle_t params_fsdax = NULL;
52+
umf_result = umfFileMemoryProviderParamsCreate(&params_fsdax, path);
53+
if (umf_result != UMF_RESULT_SUCCESS) {
54+
fprintf(stderr, "Failed to create the File Memory Provider params");
55+
return NULL;
56+
}
5357
// FSDAX requires mapping the UMF_MEM_MAP_SHARED flag
54-
params_fsdax.visibility = UMF_MEM_MAP_SHARED;
58+
umf_result = umfFileMemoryProviderParamsSetVisibility(params_fsdax,
59+
UMF_MEM_MAP_SHARED);
60+
if (umf_result != UMF_RESULT_SUCCESS) {
61+
fprintf(stderr,
62+
"Failed to set the visibility of the FSDAX file provider");
63+
umfFileMemoryProviderParamsDestroy(params_fsdax);
64+
return NULL;
65+
}
5566

5667
umf_result = umfMemoryProviderCreate(umfFileMemoryProviderOps(),
57-
&params_fsdax, &provider_fsdax);
68+
params_fsdax, &provider_fsdax);
69+
umfFileMemoryProviderParamsDestroy(params_fsdax);
5870
if (umf_result != UMF_RESULT_SUCCESS) {
5971
fprintf(stderr, "Failed to create the FSDAX file provider");
6072
return NULL;

include/umf/providers/provider_file_memory.h

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,45 @@ extern "C" {
1818
#define UMF_FILE_RESULTS_START_FROM 3000
1919
/// @endcond
2020

21-
/// @brief Memory provider settings struct
22-
typedef struct umf_file_memory_provider_params_t {
23-
/// a path to the file (of maximum length PATH_MAX characters)
24-
const char *path;
25-
/// combination of 'umf_mem_protection_flags_t' flags
26-
unsigned protection;
27-
/// memory visibility mode
28-
umf_memory_visibility_t visibility;
29-
} umf_file_memory_provider_params_t;
21+
struct umf_file_memory_provider_params_t;
22+
23+
typedef struct umf_file_memory_provider_params_t
24+
*umf_file_memory_provider_params_handle_t;
25+
26+
/// @brief Create a struct to store parameters of the File Memory Provider.
27+
/// @param hParams [out] handle to the newly created parameters struct.
28+
/// @param path path to the file.
29+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
30+
umf_result_t umfFileMemoryProviderParamsCreate(
31+
umf_file_memory_provider_params_handle_t *hParams, const char *path);
32+
33+
/// @brief Destroy parameters struct.
34+
/// @param hParams handle to the parameters of the File Memory Provider.
35+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
36+
umf_result_t umfFileMemoryProviderParamsDestroy(
37+
umf_file_memory_provider_params_handle_t hParams);
38+
39+
/// @brief Set the path in the parameters struct.
40+
/// @param hParams handle to the parameters of the File Memory Provider.
41+
/// @param path path to the file.
42+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
43+
umf_result_t umfFileMemoryProviderParamsSetPath(
44+
umf_file_memory_provider_params_handle_t hParams, const char *path);
45+
46+
/// @brief Set the protection in the parameters struct.
47+
/// @param hParams handle to the parameters of the File Memory Provider.
48+
/// @param protection protection. Combination of \p umf_mem_protection_flags_t flags
49+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
50+
umf_result_t umfFileMemoryProviderParamsSetProtection(
51+
umf_file_memory_provider_params_handle_t hParams, unsigned protection);
52+
53+
/// @brief Set the visibility in the parameters struct.
54+
/// @param hParams handle to the parameters of the File Memory Provider.
55+
/// @param visibility visibility.
56+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
57+
umf_result_t umfFileMemoryProviderParamsSetVisibility(
58+
umf_file_memory_provider_params_handle_t hParams,
59+
umf_memory_visibility_t visibility);
3060

3161
/// @brief File Memory Provider operation results
3262
typedef enum umf_file_memory_provider_native_error {
@@ -38,18 +68,6 @@ typedef enum umf_file_memory_provider_native_error {
3868

3969
umf_memory_provider_ops_t *umfFileMemoryProviderOps(void);
4070

41-
/// @brief Create default params for the file memory provider
42-
static inline umf_file_memory_provider_params_t
43-
umfFileMemoryProviderParamsDefault(const char *path) {
44-
umf_file_memory_provider_params_t params = {
45-
path, /* a path to the file */
46-
UMF_PROTECTION_READ | UMF_PROTECTION_WRITE, /* protection */
47-
UMF_MEM_MAP_PRIVATE, /* visibility mode */
48-
};
49-
50-
return params;
51-
}
52-
5371
#ifdef __cplusplus
5472
}
5573
#endif

src/libumf.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ EXPORTS
2525
umfDevDaxMemoryProviderOps
2626
umfFree
2727
umfFileMemoryProviderOps
28+
umfFileMemoryProviderParamsCreate
29+
umfFileMemoryProviderParamsDestroy
30+
umfFileMemoryProviderParamsSetPath
31+
umfFileMemoryProviderParamsSetProtection
32+
umfFileMemoryProviderParamsSetVisibility
2833
umfGetIPCHandle
2934
umfGetLastFailedMemoryProvider
3035
umfLevelZeroMemoryProviderOps

src/libumf.map

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ UMF_1.0 {
1919
umfDevDaxMemoryProviderOps;
2020
umfFree;
2121
umfFileMemoryProviderOps;
22+
umfFileMemoryProviderParamsCreate;
23+
umfFileMemoryProviderParamsDestroy;
24+
umfFileMemoryProviderParamsSetPath;
25+
umfFileMemoryProviderParamsSetProtection;
26+
umfFileMemoryProviderParamsSetVisibility;
2227
umfGetIPCHandle;
2328
umfGetLastFailedMemoryProvider;
2429
umfLevelZeroMemoryProviderOps;

src/provider/provider_file_memory.c

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,41 @@ umf_memory_provider_ops_t *umfFileMemoryProviderOps(void) {
2525
return NULL;
2626
}
2727

28+
umf_result_t umfFileMemoryProviderParamsCreate(
29+
umf_file_memory_provider_params_handle_t *hParams, const char *path) {
30+
(void)hParams;
31+
(void)path;
32+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
33+
}
34+
35+
umf_result_t umfFileMemoryProviderParamsDestroy(
36+
umf_file_memory_provider_params_handle_t hParams) {
37+
(void)hParams;
38+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
39+
}
40+
41+
umf_result_t umfFileMemoryProviderParamsSetPath(
42+
umf_file_memory_provider_params_handle_t hParams, const char *path) {
43+
(void)hParams;
44+
(void)path;
45+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
46+
}
47+
48+
umf_result_t umfFileMemoryProviderParamsSetProtection(
49+
umf_file_memory_provider_params_handle_t hParams, unsigned protection) {
50+
(void)hParams;
51+
(void)protection;
52+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
53+
}
54+
55+
umf_result_t umfFileMemoryProviderParamsSetVisibility(
56+
umf_file_memory_provider_params_handle_t hParams,
57+
umf_memory_visibility_t visibility) {
58+
(void)hParams;
59+
(void)visibility;
60+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
61+
}
62+
2863
#else // !defined(_WIN32) && !defined(UMF_NO_HWLOC)
2964

3065
#include "base_alloc_global.h"
@@ -67,6 +102,13 @@ typedef struct file_memory_provider_t {
67102
critnib *fd_offset_map;
68103
} file_memory_provider_t;
69104

105+
// File Memory Provider settings struct
106+
typedef struct umf_file_memory_provider_params_t {
107+
char *path;
108+
unsigned protection;
109+
umf_memory_visibility_t visibility;
110+
} umf_file_memory_provider_params_t;
111+
70112
typedef struct file_last_native_error_t {
71113
int32_t native_error;
72114
int errno_value;
@@ -748,4 +790,107 @@ umf_memory_provider_ops_t *umfFileMemoryProviderOps(void) {
748790
return &UMF_FILE_MEMORY_PROVIDER_OPS;
749791
}
750792

793+
umf_result_t umfFileMemoryProviderParamsCreate(
794+
umf_file_memory_provider_params_handle_t *hParams, const char *path) {
795+
if (hParams == NULL) {
796+
LOG_ERR("File Memory Provider params handle is NULL");
797+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
798+
}
799+
800+
if (path == NULL) {
801+
LOG_ERR("File path is NULL");
802+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
803+
}
804+
805+
umf_file_memory_provider_params_handle_t params =
806+
umf_ba_global_alloc(sizeof(*params));
807+
if (params == NULL) {
808+
LOG_ERR("allocating memory for File Memory Provider params failed");
809+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
810+
}
811+
812+
params->path = NULL;
813+
params->protection = UMF_PROTECTION_READ | UMF_PROTECTION_WRITE;
814+
params->visibility = UMF_MEM_MAP_PRIVATE;
815+
816+
umf_result_t res = umfFileMemoryProviderParamsSetPath(params, path);
817+
if (res != UMF_RESULT_SUCCESS) {
818+
umf_ba_global_free(params);
819+
return res;
820+
}
821+
822+
*hParams = params;
823+
824+
return UMF_RESULT_SUCCESS;
825+
}
826+
827+
umf_result_t umfFileMemoryProviderParamsDestroy(
828+
umf_file_memory_provider_params_handle_t hParams) {
829+
if (hParams != NULL) {
830+
umf_ba_global_free(hParams->path);
831+
umf_ba_global_free(hParams);
832+
}
833+
834+
return UMF_RESULT_SUCCESS;
835+
}
836+
837+
umf_result_t umfFileMemoryProviderParamsSetPath(
838+
umf_file_memory_provider_params_handle_t hParams, const char *path) {
839+
if (hParams == NULL) {
840+
LOG_ERR("File Memory Provider params handle is NULL");
841+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
842+
}
843+
844+
if (path == NULL) {
845+
LOG_ERR("File path is NULL");
846+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
847+
}
848+
849+
size_t len = strlen(path);
850+
if (len == 0) {
851+
LOG_ERR("File path is empty");
852+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
853+
}
854+
855+
len += 1; // for the null terminator
856+
char *new_path = NULL;
857+
new_path = umf_ba_global_alloc(len);
858+
if (new_path == NULL) {
859+
LOG_ERR("allocating memory for the file path failed");
860+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
861+
}
862+
863+
strncpy(new_path, path, len);
864+
865+
umf_ba_global_free(hParams->path);
866+
hParams->path = new_path;
867+
868+
return UMF_RESULT_SUCCESS;
869+
}
870+
871+
umf_result_t umfFileMemoryProviderParamsSetProtection(
872+
umf_file_memory_provider_params_handle_t hParams, unsigned protection) {
873+
if (hParams == NULL) {
874+
LOG_ERR("File Memory Provider params handle is NULL");
875+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
876+
}
877+
878+
hParams->protection = protection;
879+
880+
return UMF_RESULT_SUCCESS;
881+
}
882+
883+
umf_result_t umfFileMemoryProviderParamsSetVisibility(
884+
umf_file_memory_provider_params_handle_t hParams,
885+
umf_memory_visibility_t visibility) {
886+
if (hParams == NULL) {
887+
LOG_ERR("File Memory Provider params handle is NULL");
888+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
889+
}
890+
891+
hParams->visibility = visibility;
892+
893+
return UMF_RESULT_SUCCESS;
894+
}
895+
751896
#endif // !defined(_WIN32) && !defined(UMF_NO_HWLOC)

test/ipc_file_prov_consumer.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,35 @@ int main(int argc, char *argv[]) {
2222
return -1;
2323
}
2424

25+
int ret = 0;
2526
int port = atoi(argv[1]);
2627
char *file_name = argv[2];
2728

28-
umf_file_memory_provider_params_t file_params;
29-
file_params = umfFileMemoryProviderParamsDefault(file_name);
30-
file_params.visibility = UMF_MEM_MAP_SHARED;
29+
umf_file_memory_provider_params_handle_t file_params = NULL;
30+
umf_result_t umf_result =
31+
umfFileMemoryProviderParamsCreate(&file_params, file_name);
32+
if (umf_result != UMF_RESULT_SUCCESS) {
33+
fprintf(
34+
stderr,
35+
"[consumer] ERROR: creating File Memory Provider params failed\n");
36+
return -1;
37+
}
38+
39+
umf_result = umfFileMemoryProviderParamsSetVisibility(file_params,
40+
UMF_MEM_MAP_SHARED);
41+
if (umf_result != UMF_RESULT_SUCCESS) {
42+
fprintf(stderr, "[consumer] ERROR: setting File Memory Provider "
43+
"visibility failed\n");
44+
goto destroy_provider_params;
45+
}
3146

3247
void *pool_params = NULL;
3348

34-
return run_consumer(port, umfScalablePoolOps(), pool_params,
35-
umfFileMemoryProviderOps(), &file_params, memcopy,
36-
NULL);
49+
ret = run_consumer(port, umfScalablePoolOps(), pool_params,
50+
umfFileMemoryProviderOps(), file_params, memcopy, NULL);
51+
52+
destroy_provider_params:
53+
umfFileMemoryProviderParamsDestroy(file_params);
54+
55+
return ret;
3756
}

test/ipc_file_prov_producer.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,35 @@ int main(int argc, char *argv[]) {
2222
return -1;
2323
}
2424

25+
int ret = 0;
2526
int port = atoi(argv[1]);
2627
char *file_name = argv[2];
2728

28-
umf_file_memory_provider_params_t file_params;
29-
file_params = umfFileMemoryProviderParamsDefault(file_name);
30-
file_params.visibility = UMF_MEM_MAP_SHARED;
29+
umf_file_memory_provider_params_handle_t file_params = NULL;
30+
umf_result_t umf_result =
31+
umfFileMemoryProviderParamsCreate(&file_params, file_name);
32+
if (umf_result != UMF_RESULT_SUCCESS) {
33+
fprintf(
34+
stderr,
35+
"[producer] ERROR: creating File Memory Provider params failed\n");
36+
return -1;
37+
}
38+
39+
umf_result = umfFileMemoryProviderParamsSetVisibility(file_params,
40+
UMF_MEM_MAP_SHARED);
41+
if (umf_result != UMF_RESULT_SUCCESS) {
42+
fprintf(stderr, "[producer] ERROR: setting File Memory Provider "
43+
"visibility failed\n");
44+
goto destroy_provider_params;
45+
}
3146

3247
void *pool_params = NULL;
3348

34-
return run_producer(port, umfScalablePoolOps(), pool_params,
35-
umfFileMemoryProviderOps(), &file_params, memcopy,
36-
NULL);
49+
ret = run_producer(port, umfScalablePoolOps(), pool_params,
50+
umfFileMemoryProviderOps(), file_params, memcopy, NULL);
51+
52+
destroy_provider_params:
53+
umfFileMemoryProviderParamsDestroy(file_params);
54+
55+
return ret;
3756
}

0 commit comments

Comments
 (0)