@@ -48,8 +48,6 @@ static __TLS os_last_native_error_t TLS_last_native_error;
48
48
49
49
// helper values used only in the Native_error_str array
50
50
#define _UMF_OS_RESULT_SUCCESS (UMF_OS_RESULT_SUCCESS - UMF_OS_RESULT_SUCCESS)
51
- #define _UMF_OS_RESULT_ERROR_WRONG_ALIGNMENT \
52
- (UMF_OS_RESULT_ERROR_WRONG_ALIGNMENT - UMF_OS_RESULT_SUCCESS)
53
51
#define _UMF_OS_RESULT_ERROR_ALLOC_FAILED \
54
52
(UMF_OS_RESULT_ERROR_ALLOC_FAILED - UMF_OS_RESULT_SUCCESS)
55
53
#define _UMF_OS_RESULT_ERROR_ADDRESS_NOT_ALIGNED \
@@ -65,8 +63,6 @@ static __TLS os_last_native_error_t TLS_last_native_error;
65
63
66
64
static const char * Native_error_str [] = {
67
65
[_UMF_OS_RESULT_SUCCESS ] = "success" ,
68
- [_UMF_OS_RESULT_ERROR_WRONG_ALIGNMENT ] =
69
- "wrong alignment (not a power of 2)" ,
70
66
[_UMF_OS_RESULT_ERROR_ALLOC_FAILED ] = "memory allocation failed" ,
71
67
[_UMF_OS_RESULT_ERROR_ADDRESS_NOT_ALIGNED ] =
72
68
"allocated address is not aligned" ,
@@ -237,6 +233,9 @@ static void os_finalize(void *provider) {
237
233
free (os_provider );
238
234
}
239
235
236
+ static umf_result_t os_get_min_page_size (void * provider , void * ptr ,
237
+ size_t * page_size );
238
+
240
239
static umf_result_t os_alloc (void * provider , size_t size , size_t alignment ,
241
240
void * * resultPtr ) {
242
241
int ret ;
@@ -248,13 +247,20 @@ static umf_result_t os_alloc(void *provider, size_t size, size_t alignment,
248
247
os_memory_provider_t * os_provider = (os_memory_provider_t * )provider ;
249
248
umf_os_memory_provider_config_t * os_config = & os_provider -> config ;
250
249
251
- if (alignment && (alignment & (alignment - 1 ))) {
252
- os_store_last_native_error (UMF_OS_RESULT_ERROR_WRONG_ALIGNMENT , 0 );
250
+ size_t page_size ;
251
+ umf_result_t result = os_get_min_page_size (provider , NULL , & page_size );
252
+ if (result != UMF_RESULT_SUCCESS ) {
253
+ return result ;
254
+ }
255
+
256
+ if (alignment && (alignment % page_size ) && (page_size % alignment )) {
253
257
if (os_config -> traces ) {
254
- fprintf (stderr , "wrong alignment (not a power of 2): %zu\n" ,
255
- alignment );
258
+ fprintf (stderr ,
259
+ "wrong alignment: %zu (not a multiple or a divider of the "
260
+ "minimum page size (%zu))\n" ,
261
+ alignment , page_size );
256
262
}
257
- return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC ;
263
+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
258
264
}
259
265
260
266
int flags = os_config -> visibility ;
@@ -272,8 +278,8 @@ static umf_result_t os_alloc(void *provider, size_t size, size_t alignment,
272
278
return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC ;
273
279
}
274
280
275
- // sanity check - the address should be aligned here
276
- if ((alignment > 0 ) && ((uintptr_t )addr & ( alignment - 1 ) )) {
281
+ // verify the alignment
282
+ if ((alignment > 0 ) && ((uintptr_t )addr % alignment )) {
277
283
if (os_config -> traces ) {
278
284
os_store_last_native_error (UMF_OS_RESULT_ERROR_ADDRESS_NOT_ALIGNED ,
279
285
0 );
0 commit comments