@@ -27,6 +27,7 @@ umf_memory_provider_ops_t *umfDevDaxMemoryProviderOps(void) {
2727#else // !defined(_WIN32) && !defined(UMF_NO_HWLOC)
2828
2929#include "base_alloc_global.h"
30+ #include "coarse.h"
3031#include "utils_common.h"
3132#include "utils_concurrency.h"
3233#include "utils_log.h"
@@ -42,6 +43,7 @@ typedef struct devdax_memory_provider_t {
4243 size_t offset ; // offset in the file used for memory mapping
4344 utils_mutex_t lock ; // lock of ptr and offset
4445 unsigned protection ; // combination of OS-specific protection flags
46+ coarse_t * coarse ; // coarse handle
4547} devdax_memory_provider_t ;
4648
4749typedef struct devdax_last_native_error_t {
@@ -94,6 +96,12 @@ devdax_translate_params(umf_devdax_memory_provider_params_t *in_params,
9496 return UMF_RESULT_SUCCESS ;
9597}
9698
99+ static umf_result_t devdax_allocation_split_cb (void * provider , void * ptr ,
100+ size_t totalSize ,
101+ size_t firstSize );
102+ static umf_result_t devdax_allocation_merge_cb (void * provider , void * lowPtr ,
103+ void * highPtr , size_t totalSize );
104+
97105static umf_result_t devdax_initialize (void * params , void * * provider ) {
98106 umf_result_t ret ;
99107
@@ -122,21 +130,38 @@ static umf_result_t devdax_initialize(void *params, void **provider) {
122130
123131 memset (devdax_provider , 0 , sizeof (* devdax_provider ));
124132
125- ret = devdax_translate_params (in_params , devdax_provider );
133+ coarse_params_t coarse_params = {0 };
134+ coarse_params .provider = devdax_provider ;
135+ coarse_params .page_size = DEVDAX_PAGE_SIZE_2MB ;
136+ coarse_params .cb .alloc = NULL ; // not available
137+ coarse_params .cb .free = NULL ; // not available
138+ coarse_params .cb .split = devdax_allocation_split_cb ;
139+ coarse_params .cb .merge = devdax_allocation_merge_cb ;
140+
141+ coarse_t * coarse = NULL ;
142+ ret = coarse_new (& coarse_params , & coarse );
126143 if (ret != UMF_RESULT_SUCCESS ) {
144+ LOG_ERR ("coarse_new() failed" );
127145 goto err_free_devdax_provider ;
128146 }
129147
148+ devdax_provider -> coarse = coarse ;
149+
150+ ret = devdax_translate_params (in_params , devdax_provider );
151+ if (ret != UMF_RESULT_SUCCESS ) {
152+ goto err_coarse_delete ;
153+ }
154+
130155 devdax_provider -> size = in_params -> size ;
131156 if (utils_copy_path (in_params -> path , devdax_provider -> path , PATH_MAX )) {
132- goto err_free_devdax_provider ;
157+ goto err_coarse_delete ;
133158 }
134159
135160 int fd = utils_devdax_open (in_params -> path );
136161 if (fd == -1 ) {
137162 LOG_ERR ("cannot open the device DAX: %s" , in_params -> path );
138163 ret = UMF_RESULT_ERROR_INVALID_ARGUMENT ;
139- goto err_free_devdax_provider ;
164+ goto err_coarse_delete ;
140165 }
141166
142167 bool is_dax = false;
@@ -150,23 +175,25 @@ static umf_result_t devdax_initialize(void *params, void **provider) {
150175 LOG_PDEBUG ("mapping the devdax failed (path=%s, size=%zu)" ,
151176 in_params -> path , devdax_provider -> size );
152177 ret = UMF_RESULT_ERROR_UNKNOWN ;
153- goto err_free_devdax_provider ;
178+ goto err_coarse_delete ;
154179 }
155180
156181 if (!is_dax ) {
157182 LOG_ERR ("mapping the devdax with MAP_SYNC failed: %s" , in_params -> path );
158183 ret = UMF_RESULT_ERROR_UNKNOWN ;
159-
160- if (devdax_provider -> base ) {
161- utils_munmap (devdax_provider -> base , devdax_provider -> size );
162- }
163-
164- goto err_free_devdax_provider ;
184+ goto err_unmap_devdax ;
165185 }
166186
167187 LOG_DEBUG ("devdax memory mapped (path=%s, size=%zu, addr=%p)" ,
168188 in_params -> path , devdax_provider -> size , devdax_provider -> base );
169189
190+ ret = coarse_add_memory_block (coarse , devdax_provider -> base ,
191+ devdax_provider -> size );
192+ if (ret != UMF_RESULT_SUCCESS ) {
193+ LOG_ERR ("adding memory block failed" );
194+ goto err_unmap_devdax ;
195+ }
196+
170197 if (utils_mutex_init (& devdax_provider -> lock ) == NULL ) {
171198 LOG_ERR ("lock init failed" );
172199 ret = UMF_RESULT_ERROR_UNKNOWN ;
@@ -178,7 +205,11 @@ static umf_result_t devdax_initialize(void *params, void **provider) {
178205 return UMF_RESULT_SUCCESS ;
179206
180207err_unmap_devdax :
181- utils_munmap (devdax_provider -> base , devdax_provider -> size );
208+ if (devdax_provider -> base ) {
209+ utils_munmap (devdax_provider -> base , devdax_provider -> size );
210+ }
211+ err_coarse_delete :
212+ coarse_delete (devdax_provider -> coarse );
182213err_free_devdax_provider :
183214 umf_ba_global_free (devdax_provider );
184215 return ret ;
@@ -188,9 +219,11 @@ static void devdax_finalize(void *provider) {
188219 devdax_memory_provider_t * devdax_provider = provider ;
189220 utils_mutex_destroy_not_free (& devdax_provider -> lock );
190221 utils_munmap (devdax_provider -> base , devdax_provider -> size );
222+ coarse_delete (devdax_provider -> coarse );
191223 umf_ba_global_free (devdax_provider );
192224}
193225
226+ /*
194227static int devdax_alloc_aligned(size_t length, size_t alignment, void *base,
195228 size_t size, utils_mutex_t *lock,
196229 void **out_addr, size_t *offset) {
@@ -225,7 +258,7 @@ static int devdax_alloc_aligned(size_t length, size_t alignment, void *base,
225258 return 0;
226259}
227260
228- static umf_result_t devdax_alloc (void * provider , size_t size , size_t alignment ,
261+ umf_result_t devdax_alloc(void *provider, size_t size, size_t alignment,
229262 void **resultPtr) {
230263 int ret;
231264
@@ -261,6 +294,7 @@ static umf_result_t devdax_alloc(void *provider, size_t size, size_t alignment,
261294
262295 return UMF_RESULT_SUCCESS;
263296}
297+ */
264298
265299static void devdax_get_last_native_error (void * provider , const char * * ppMessage ,
266300 int32_t * pError ) {
@@ -342,18 +376,19 @@ static const char *devdax_get_name(void *provider) {
342376 return "DEVDAX" ;
343377}
344378
345- static umf_result_t devdax_allocation_split (void * provider , void * ptr ,
346- size_t totalSize ,
347- size_t firstSize ) {
379+ static umf_result_t devdax_allocation_split_cb (void * provider , void * ptr ,
380+ size_t totalSize ,
381+ size_t firstSize ) {
348382 (void )provider ;
349383 (void )ptr ;
350384 (void )totalSize ;
351385 (void )firstSize ;
352386 return UMF_RESULT_SUCCESS ;
353387}
354388
355- static umf_result_t devdax_allocation_merge (void * provider , void * lowPtr ,
356- void * highPtr , size_t totalSize ) {
389+ static umf_result_t devdax_allocation_merge_cb (void * provider , void * lowPtr ,
390+ void * highPtr ,
391+ size_t totalSize ) {
357392 (void )provider ;
358393 (void )lowPtr ;
359394 (void )highPtr ;
@@ -488,6 +523,28 @@ static umf_result_t devdax_close_ipc_handle(void *provider, void *ptr,
488523 return UMF_RESULT_SUCCESS ;
489524}
490525
526+ static umf_result_t devdax_alloc (void * provider , size_t size , size_t alignment ,
527+ void * * resultPtr ) {
528+ devdax_memory_provider_t * devdax_provider =
529+ (devdax_memory_provider_t * )provider ;
530+ return coarse_alloc (devdax_provider -> coarse , size , alignment , resultPtr );
531+ }
532+
533+ static umf_result_t devdax_allocation_split (void * provider , void * ptr ,
534+ size_t totalSize ,
535+ size_t firstSize ) {
536+ devdax_memory_provider_t * devdax_provider =
537+ (devdax_memory_provider_t * )provider ;
538+ return coarse_split (devdax_provider -> coarse , ptr , totalSize , firstSize );
539+ }
540+
541+ static umf_result_t devdax_allocation_merge (void * provider , void * lowPtr ,
542+ void * highPtr , size_t totalSize ) {
543+ devdax_memory_provider_t * devdax_provider =
544+ (devdax_memory_provider_t * )provider ;
545+ return coarse_merge (devdax_provider -> coarse , lowPtr , highPtr , totalSize );
546+ }
547+
491548static umf_memory_provider_ops_t UMF_DEVDAX_MEMORY_PROVIDER_OPS = {
492549 .version = UMF_VERSION_CURRENT ,
493550 .initialize = devdax_initialize ,
0 commit comments