Skip to content

Move common functions to utils common #728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/umf/memory_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@
extern "C" {
#endif

/// @brief Memory visibility mode
typedef enum umf_memory_visibility_t {
UMF_MEM_MAP_PRIVATE = 1, ///< private memory mapping
UMF_MEM_MAP_SHARED, ///< shared memory mapping (supported on Linux only)
} umf_memory_visibility_t;

/// @brief Protection of the memory allocations
typedef enum umf_mem_protection_flags_t {
UMF_PROTECTION_NONE = (1 << 0), ///< Memory allocations can not be accessed
UMF_PROTECTION_READ = (1 << 1), ///< Memory allocations can be read.
UMF_PROTECTION_WRITE = (1 << 2), ///< Memory allocations can be written.
UMF_PROTECTION_EXEC = (1 << 3), ///< Memory allocations can be executed.
/// @cond
UMF_PROTECTION_MAX // must be the last one
/// @endcond
} umf_mem_protection_flags_t;

/// @brief A struct containing memory provider specific set of functions
typedef struct umf_memory_provider_t *umf_memory_provider_handle_t;

Expand Down
17 changes: 0 additions & 17 deletions include/umf/providers/provider_os_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,6 @@ extern "C" {
#define UMF_OS_RESULTS_START_FROM 1000
/// @endcond

/// @brief Protection of the memory allocations
typedef enum umf_mem_protection_flags_t {
UMF_PROTECTION_NONE = (1 << 0), ///< Memory allocations can not be accessed
UMF_PROTECTION_READ = (1 << 1), ///< Memory allocations can be read.
UMF_PROTECTION_WRITE = (1 << 2), ///< Memory allocations can be written.
UMF_PROTECTION_EXEC = (1 << 3), ///< Memory allocations can be executed.
/// @cond
UMF_PROTECTION_MAX // must be the last one
/// @endcond
} umf_mem_protection_flags_t;

/// @brief Memory visibility mode
typedef enum umf_memory_visibility_t {
UMF_MEM_MAP_PRIVATE = 1, ///< private memory mapping
UMF_MEM_MAP_SHARED, ///< shared memory mapping (supported on Linux only)
} umf_memory_visibility_t;

/// @brief Memory binding mode
/// Specifies how memory is bound to NUMA nodes on systems that support NUMA.
/// Not every mode is supported on every system.
Expand Down
18 changes: 7 additions & 11 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ set(UMF_SOURCES_COMMON_LINUX_MACOSX
provider/provider_devdax_memory.c
provider/provider_file_memory.c
provider/provider_os_memory.c
provider/provider_os_memory_posix.c
memtargets/memtarget_numa.c
memspaces/memspace_numa.c
memspaces/memspace_host_all.c
Expand All @@ -139,17 +138,14 @@ set(UMF_SOURCES_COMMON_LINUX_MACOSX
memspaces/memspace_lowest_latency.c)

if(NOT UMF_DISABLE_HWLOC)
set(UMF_SOURCES_LINUX
${UMF_SOURCES_LINUX} ${UMF_SOURCES_COMMON_LINUX_MACOSX}
provider/provider_os_memory_linux.c)
set(UMF_SOURCES_LINUX ${UMF_SOURCES_LINUX}
${UMF_SOURCES_COMMON_LINUX_MACOSX})

set(UMF_SOURCES_MACOSX
${UMF_SOURCES_MACOSX} ${UMF_SOURCES_COMMON_LINUX_MACOSX}
provider/provider_os_memory_macosx.c)
set(UMF_SOURCES_MACOSX ${UMF_SOURCES_MACOSX}
${UMF_SOURCES_COMMON_LINUX_MACOSX})

set(UMF_SOURCES_WINDOWS
${UMF_SOURCES_WINDOWS} provider/provider_os_memory.c
provider/provider_os_memory_windows.c)
set(UMF_SOURCES_WINDOWS ${UMF_SOURCES_WINDOWS}
provider/provider_os_memory.c)

set(UMF_LIBS ${UMF_LIBS} ${LIBHWLOC_LIBRARIES})

Expand Down Expand Up @@ -180,7 +176,7 @@ set(UMF_PRIVATE_LIBRARY_DIRS ${UMF_PRIVATE_LIBRARY_DIRS}

if(LINUX)
set(UMF_SOURCES ${UMF_SOURCES} ${UMF_SOURCES_LINUX})
set(UMF_LIBS ${UMF_LIBS} dl rt) # librt for shm_open()
set(UMF_LIBS ${UMF_LIBS} dl)
elseif(WINDOWS)
set(UMF_SOURCES ${UMF_SOURCES} ${UMF_SOURCES_WINDOWS})

Expand Down
31 changes: 15 additions & 16 deletions src/provider/provider_devdax_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <string.h>

#include "base_alloc_global.h"
#include "provider_os_memory_internal.h"
#include "utils_common.h"
#include "utils_concurrency.h"
#include "utils_log.h"
Expand Down Expand Up @@ -76,8 +75,8 @@ devdax_translate_params(umf_devdax_memory_provider_params_t *in_params,
devdax_memory_provider_t *provider) {
umf_result_t result;

result = os_translate_mem_protection_flags(in_params->protection,
&provider->protection);
result = utils_translate_mem_protection_flags(in_params->protection,
&provider->protection);
if (result != UMF_RESULT_SUCCESS) {
LOG_ERR("incorrect memory protection flags: %u", in_params->protection);
return result;
Expand Down Expand Up @@ -124,15 +123,15 @@ static umf_result_t devdax_initialize(void *params, void **provider) {
goto err_free_devdax_provider;
}

int fd = os_devdax_open(in_params->path);
int fd = utils_devdax_open(in_params->path);
if (fd == -1) {
LOG_ERR("cannot open the device DAX: %s", in_params->path);
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
goto err_free_devdax_provider;
}

devdax_provider->base = os_devdax_mmap(NULL, devdax_provider->size,
devdax_provider->protection, fd);
devdax_provider->base = utils_devdax_mmap(NULL, devdax_provider->size,
devdax_provider->protection, fd);
utils_close_fd(fd);
if (devdax_provider->base == NULL) {
LOG_PDEBUG("devdax memory mapping failed (path=%s, size=%zu)",
Expand All @@ -155,7 +154,7 @@ static umf_result_t devdax_initialize(void *params, void **provider) {
return UMF_RESULT_SUCCESS;

err_unmap_devdax:
os_munmap(devdax_provider->base, devdax_provider->size);
utils_munmap(devdax_provider->base, devdax_provider->size);
err_free_devdax_provider:
umf_ba_global_free(devdax_provider);
return ret;
Expand All @@ -169,7 +168,7 @@ static void devdax_finalize(void *provider) {

devdax_memory_provider_t *devdax_provider = provider;
util_mutex_destroy_not_free(&devdax_provider->lock);
os_munmap(devdax_provider->base, devdax_provider->size);
utils_munmap(devdax_provider->base, devdax_provider->size);
umf_ba_global_free(devdax_provider);
}

Expand Down Expand Up @@ -281,8 +280,8 @@ static void devdax_get_last_native_error(void *provider, const char **ppMessage,
memcpy(TLS_last_native_error.msg_buff + pos, msg, len + 1);
pos += len;

os_strerror(TLS_last_native_error.errno_value,
TLS_last_native_error.msg_buff + pos, TLS_MSG_BUF_LEN - pos);
utils_strerror(TLS_last_native_error.errno_value,
TLS_last_native_error.msg_buff + pos, TLS_MSG_BUF_LEN - pos);

*ppMessage = TLS_last_native_error.msg_buff;
}
Expand All @@ -296,7 +295,7 @@ static umf_result_t devdax_get_recommended_page_size(void *provider,
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

*page_size = os_get_page_size();
*page_size = util_get_page_size();

return UMF_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -324,7 +323,7 @@ static umf_result_t devdax_purge_force(void *provider, void *ptr, size_t size) {
}

errno = 0;
if (os_purge(ptr, size, UMF_PURGE_FORCE)) {
if (utils_purge(ptr, size, UMF_PURGE_FORCE)) {
devdax_store_last_native_error(
UMF_DEVDAX_RESULT_ERROR_PURGE_FORCE_FAILED, errno);
LOG_PERR("force purging failed");
Expand Down Expand Up @@ -453,14 +452,14 @@ static umf_result_t devdax_open_ipc_handle(void *provider,
}

umf_result_t ret = UMF_RESULT_SUCCESS;
int fd = os_devdax_open(devdax_provider->path);
int fd = utils_devdax_open(devdax_provider->path);
if (fd == -1) {
LOG_PERR("opening a devdax (%s) failed", devdax_provider->path);
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

char *base = os_devdax_mmap(NULL, devdax_provider->size,
devdax_provider->protection, fd);
char *base = utils_devdax_mmap(NULL, devdax_provider->size,
devdax_provider->protection, fd);
if (base == NULL) {
devdax_store_last_native_error(UMF_DEVDAX_RESULT_ERROR_ALLOC_FAILED,
errno);
Expand Down Expand Up @@ -493,7 +492,7 @@ static umf_result_t devdax_close_ipc_handle(void *provider, void *ptr,
(devdax_memory_provider_t *)provider;

errno = 0;
int ret = os_munmap(devdax_provider->base, devdax_provider->size);
int ret = utils_munmap(devdax_provider->base, devdax_provider->size);
// ignore error when size == 0
if (ret && (size > 0)) {
devdax_store_last_native_error(UMF_DEVDAX_RESULT_ERROR_FREE_FAILED,
Expand Down
37 changes: 18 additions & 19 deletions src/provider/provider_file_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include "base_alloc_global.h"
#include "critnib.h"
#include "provider_os_memory_internal.h"
#include "utils_common.h"
#include "utils_concurrency.h"
#include "utils_log.h"
Expand Down Expand Up @@ -88,15 +87,15 @@ file_translate_params(umf_file_memory_provider_params_t *in_params,
file_memory_provider_t *provider) {
umf_result_t result;

result = os_translate_mem_protection_flags(in_params->protection,
&provider->protection);
result = utils_translate_mem_protection_flags(in_params->protection,
&provider->protection);
if (result != UMF_RESULT_SUCCESS) {
LOG_ERR("incorrect memory protection flags: %u", in_params->protection);
return result;
}

result = os_translate_mem_visibility_flag(in_params->visibility,
&provider->visibility);
result = utils_translate_mem_visibility_flag(in_params->visibility,
&provider->visibility);
if (result != UMF_RESULT_SUCCESS) {
LOG_ERR("incorrect memory visibility flag: %u", in_params->visibility);
return result;
Expand All @@ -115,7 +114,7 @@ static umf_result_t file_initialize(void *params, void **provider) {
umf_file_memory_provider_params_t *in_params =
(umf_file_memory_provider_params_t *)params;

size_t page_size = os_get_page_size();
size_t page_size = util_get_page_size();

if (in_params->path == NULL) {
LOG_ERR("file path is missing");
Expand All @@ -141,14 +140,14 @@ static umf_result_t file_initialize(void *params, void **provider) {
goto err_free_file_provider;
}

file_provider->fd = os_file_open_or_create(in_params->path);
file_provider->fd = utils_file_open_or_create(in_params->path);
if (file_provider->fd == -1) {
LOG_ERR("cannot open the file: %s", in_params->path);
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
goto err_free_file_provider;
}

if (os_set_file_size(file_provider->fd, page_size)) {
if (utils_set_file_size(file_provider->fd, page_size)) {
LOG_ERR("cannot set size of the file: %s", in_params->path);
ret = UMF_RESULT_ERROR_UNKNOWN;
goto err_close_fd;
Expand Down Expand Up @@ -207,7 +206,7 @@ static void file_finalize(void *provider) {
void *rvalue = NULL;
while (1 ==
critnib_find(file_provider->mmaps, key, FIND_G, &rkey, &rvalue)) {
os_munmap((void *)rkey, (size_t)rvalue);
utils_munmap((void *)rkey, (size_t)rvalue);
critnib_remove(file_provider->mmaps, rkey);
key = rkey;
}
Expand Down Expand Up @@ -248,7 +247,7 @@ static umf_result_t file_mmap_aligned(file_memory_provider_t *file_provider,
}

if (offset_fd + extended_size > size_fd) {
if (os_fallocate(fd, offset_fd, extended_size)) {
if (utils_fallocate(fd, offset_fd, extended_size)) {
LOG_ERR("cannot grow the file size from %zu to %zu", size_fd,
offset_fd + extended_size);
return UMF_RESULT_ERROR_UNKNOWN;
Expand All @@ -262,7 +261,7 @@ static umf_result_t file_mmap_aligned(file_memory_provider_t *file_provider,
ASSERT_IS_ALIGNED(extended_size, page_size);
ASSERT_IS_ALIGNED(offset_fd, page_size);

void *ptr = os_mmap(NULL, extended_size, prot, flag, fd, offset_fd);
void *ptr = utils_mmap(NULL, extended_size, prot, flag, fd, offset_fd);
if (ptr == NULL) {
LOG_PERR("memory mapping failed");
return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
Expand Down Expand Up @@ -423,8 +422,8 @@ static void file_get_last_native_error(void *provider, const char **ppMessage,
memcpy(TLS_last_native_error.msg_buff + pos, msg, len + 1);
pos += len;

os_strerror(TLS_last_native_error.errno_value,
TLS_last_native_error.msg_buff + pos, TLS_MSG_BUF_LEN - pos);
utils_strerror(TLS_last_native_error.errno_value,
TLS_last_native_error.msg_buff + pos, TLS_MSG_BUF_LEN - pos);

*ppMessage = TLS_last_native_error.msg_buff;
}
Expand All @@ -437,7 +436,7 @@ static umf_result_t file_get_recommended_page_size(void *provider, size_t size,
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

*page_size = os_get_page_size();
*page_size = util_get_page_size();

return UMF_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -465,7 +464,7 @@ static umf_result_t file_purge_force(void *provider, void *ptr, size_t size) {
}

errno = 0;
if (os_purge(ptr, size, UMF_PURGE_FORCE)) {
if (utils_purge(ptr, size, UMF_PURGE_FORCE)) {
file_store_last_native_error(UMF_FILE_RESULT_ERROR_PURGE_FORCE_FAILED,
errno);
LOG_PERR("force purging failed");
Expand Down Expand Up @@ -606,15 +605,15 @@ static umf_result_t file_open_ipc_handle(void *provider, void *providerIpcData,
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

fd = os_file_open(file_ipc_data->path);
fd = utils_file_open(file_ipc_data->path);
if (fd == -1) {
LOG_PERR("opening the file to be mapped (%s) failed",
file_ipc_data->path);
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

*ptr = os_mmap(NULL, file_ipc_data->size, file_provider->protection,
file_provider->visibility, fd, file_ipc_data->offset_fd);
*ptr = utils_mmap(NULL, file_ipc_data->size, file_provider->protection,
file_provider->visibility, fd, file_ipc_data->offset_fd);
(void)utils_close_fd(fd);
if (*ptr == NULL) {
file_store_last_native_error(UMF_FILE_RESULT_ERROR_ALLOC_FAILED, errno);
Expand All @@ -632,7 +631,7 @@ static umf_result_t file_close_ipc_handle(void *provider, void *ptr,
}

errno = 0;
int ret = os_munmap(ptr, size);
int ret = utils_munmap(ptr, size);
// ignore error when size == 0
if (ret && (size > 0)) {
file_store_last_native_error(UMF_FILE_RESULT_ERROR_FREE_FAILED, errno);
Expand Down
Loading
Loading