Skip to content

Commit 2031511

Browse files
committed
Update File provider config API
1 parent 3d0fe19 commit 2031511

File tree

11 files changed

+443
-79
lines changed

11 files changed

+443
-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: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,40 @@ 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+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
32+
}
33+
34+
umf_result_t umfFileMemoryProviderParamsDestroy(
35+
umf_file_memory_provider_params_handle_t hParams) {
36+
(void)hParams;
37+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
38+
}
39+
40+
umf_result_t umfFileMemoryProviderParamsSetPath(
41+
umf_file_memory_provider_params_handle_t hParams, const char *path) {
42+
(void)hParams;
43+
(void)path;
44+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
45+
}
46+
47+
umf_result_t umfFileMemoryProviderParamsSetProtection(
48+
umf_file_memory_provider_params_handle_t hParams, unsigned protection) {
49+
(void)hParams;
50+
(void)protection;
51+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
52+
}
53+
54+
umf_result_t umfFileMemoryProviderParamsSetVisibility(
55+
umf_file_memory_provider_params_handle_t hParams,
56+
umf_memory_visibility_t visibility) {
57+
(void)hParams;
58+
(void)visibility;
59+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
60+
}
61+
2862
#else // !defined(_WIN32) && !defined(UMF_NO_HWLOC)
2963

3064
#include "base_alloc_global.h"
@@ -67,6 +101,13 @@ typedef struct file_memory_provider_t {
67101
critnib *fd_offset_map;
68102
} file_memory_provider_t;
69103

104+
// File Memory Provider settings struct
105+
typedef struct umf_file_memory_provider_params_t {
106+
char *path;
107+
unsigned protection;
108+
umf_memory_visibility_t visibility;
109+
} umf_file_memory_provider_params_t;
110+
70111
typedef struct file_last_native_error_t {
71112
int32_t native_error;
72113
int errno_value;
@@ -748,4 +789,106 @@ umf_memory_provider_ops_t *umfFileMemoryProviderOps(void) {
748789
return &UMF_FILE_MEMORY_PROVIDER_OPS;
749790
}
750791

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