Skip to content

Commit 372cb13

Browse files
committed
Use libcoarse in the file provider
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 0870aaf commit 372cb13

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

src/provider/provider_file_memory.c

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ umf_memory_provider_ops_t *umfFileMemoryProviderOps(void) {
2828
#else // !defined(_WIN32) && !defined(UMF_NO_HWLOC)
2929

3030
#include "base_alloc_global.h"
31+
#include "coarse.h"
3132
#include "critnib.h"
3233
#include "utils_common.h"
3334
#include "utils_concurrency.h"
@@ -65,6 +66,8 @@ typedef struct file_memory_provider_t {
6566
// It is needed mainly in the get_ipc_handle and open_ipc_handle hooks
6667
// to mmap a specific part of a file.
6768
critnib *fd_offset_map;
69+
70+
coarse_t *coarse; // coarse handle
6871
} file_memory_provider_t;
6972

7073
typedef struct file_last_native_error_t {
@@ -123,6 +126,14 @@ file_translate_params(umf_file_memory_provider_params_t *in_params,
123126
return UMF_RESULT_SUCCESS;
124127
}
125128

129+
static umf_result_t file_alloc_cb(void *provider, size_t size, size_t alignment,
130+
void **resultPtr);
131+
static umf_result_t file_allocation_split_cb(void *provider, void *ptr,
132+
size_t totalSize,
133+
size_t firstSize);
134+
static umf_result_t file_allocation_merge_cb(void *provider, void *lowPtr,
135+
void *highPtr, size_t totalSize);
136+
126137
static umf_result_t file_initialize(void *params, void **provider) {
127138
umf_result_t ret;
128139

@@ -190,10 +201,27 @@ static umf_result_t file_initialize(void *params, void **provider) {
190201
file_provider->page_size = utils_get_page_size();
191202
}
192203

204+
coarse_params_t coarse_params = {0};
205+
coarse_params.provider = file_provider;
206+
coarse_params.page_size = file_provider->page_size;
207+
coarse_params.cb.alloc = file_alloc_cb;
208+
coarse_params.cb.free = NULL; // not available
209+
coarse_params.cb.split = file_allocation_split_cb;
210+
coarse_params.cb.merge = file_allocation_merge_cb;
211+
212+
coarse_t *coarse = NULL;
213+
ret = coarse_new(&coarse_params, &coarse);
214+
if (ret != UMF_RESULT_SUCCESS) {
215+
LOG_ERR("coarse_new() failed");
216+
goto err_close_fd;
217+
}
218+
219+
file_provider->coarse = coarse;
220+
193221
if (utils_mutex_init(&file_provider->lock) == NULL) {
194222
LOG_ERR("lock init failed");
195223
ret = UMF_RESULT_ERROR_UNKNOWN;
196-
goto err_close_fd;
224+
goto err_coarse_delete;
197225
}
198226

199227
file_provider->fd_offset_map = critnib_new();
@@ -218,6 +246,8 @@ static umf_result_t file_initialize(void *params, void **provider) {
218246
critnib_delete(file_provider->fd_offset_map);
219247
err_mutex_destroy_not_free:
220248
utils_mutex_destroy_not_free(&file_provider->lock);
249+
err_coarse_delete:
250+
coarse_delete(file_provider->coarse);
221251
err_close_fd:
222252
utils_close_fd(file_provider->fd);
223253
err_free_file_provider:
@@ -242,6 +272,7 @@ static void file_finalize(void *provider) {
242272
utils_close_fd(file_provider->fd);
243273
critnib_delete(file_provider->fd_offset_map);
244274
critnib_delete(file_provider->mmaps);
275+
coarse_delete(file_provider->coarse);
245276
umf_ba_global_free(file_provider);
246277
}
247278

@@ -398,8 +429,8 @@ static umf_result_t file_alloc_aligned(file_memory_provider_t *file_provider,
398429
return UMF_RESULT_SUCCESS;
399430
}
400431

401-
static umf_result_t file_alloc(void *provider, size_t size, size_t alignment,
402-
void **resultPtr) {
432+
static umf_result_t file_alloc_cb(void *provider, size_t size, size_t alignment,
433+
void **resultPtr) {
403434
umf_result_t umf_result;
404435
int ret;
405436

@@ -527,8 +558,9 @@ static const char *file_get_name(void *provider) {
527558

528559
// This function is supposed to be thread-safe, so it should NOT be called concurrently
529560
// with file_allocation_merge() with the same pointer.
530-
static umf_result_t file_allocation_split(void *provider, void *ptr,
531-
size_t totalSize, size_t firstSize) {
561+
static umf_result_t file_allocation_split_cb(void *provider, void *ptr,
562+
size_t totalSize,
563+
size_t firstSize) {
532564
(void)totalSize;
533565

534566
file_memory_provider_t *file_provider = (file_memory_provider_t *)provider;
@@ -559,8 +591,8 @@ static umf_result_t file_allocation_split(void *provider, void *ptr,
559591
}
560592

561593
// It should NOT be called concurrently with file_allocation_split() with the same pointer.
562-
static umf_result_t file_allocation_merge(void *provider, void *lowPtr,
563-
void *highPtr, size_t totalSize) {
594+
static umf_result_t file_allocation_merge_cb(void *provider, void *lowPtr,
595+
void *highPtr, size_t totalSize) {
564596
(void)lowPtr;
565597
(void)totalSize;
566598

@@ -700,6 +732,7 @@ static umf_result_t file_open_ipc_handle(void *provider, void *providerIpcData,
700732
static umf_result_t file_close_ipc_handle(void *provider, void *ptr,
701733
size_t size) {
702734
file_memory_provider_t *file_provider = (file_memory_provider_t *)provider;
735+
703736
if (!file_provider->IPC_enabled) {
704737
LOG_ERR("memory visibility mode is not UMF_MEM_MAP_SHARED")
705738
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
@@ -725,6 +758,24 @@ static umf_result_t file_close_ipc_handle(void *provider, void *ptr,
725758
return UMF_RESULT_SUCCESS;
726759
}
727760

761+
static umf_result_t file_alloc(void *provider, size_t size, size_t alignment,
762+
void **resultPtr) {
763+
file_memory_provider_t *file_provider = (file_memory_provider_t *)provider;
764+
return coarse_alloc(file_provider->coarse, size, alignment, resultPtr);
765+
}
766+
767+
static umf_result_t file_allocation_split(void *provider, void *ptr,
768+
size_t totalSize, size_t firstSize) {
769+
file_memory_provider_t *file_provider = (file_memory_provider_t *)provider;
770+
return coarse_split(file_provider->coarse, ptr, totalSize, firstSize);
771+
}
772+
773+
static umf_result_t file_allocation_merge(void *provider, void *lowPtr,
774+
void *highPtr, size_t totalSize) {
775+
file_memory_provider_t *file_provider = (file_memory_provider_t *)provider;
776+
return coarse_merge(file_provider->coarse, lowPtr, highPtr, totalSize);
777+
}
778+
728779
static umf_memory_provider_ops_t UMF_FILE_MEMORY_PROVIDER_OPS = {
729780
.version = UMF_VERSION_CURRENT,
730781
.initialize = file_initialize,

0 commit comments

Comments
 (0)