@@ -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,8 @@ void os_finalize(void *provider) {
237
233
free (os_provider );
238
234
}
239
235
236
+ umf_result_t os_get_min_page_size (void * provider , void * ptr , size_t * page_size );
237
+
240
238
static umf_result_t os_alloc (void * provider , size_t size , size_t alignment ,
241
239
void * * resultPtr ) {
242
240
int ret ;
@@ -248,13 +246,20 @@ static umf_result_t os_alloc(void *provider, size_t size, size_t alignment,
248
246
os_memory_provider_t * os_provider = (os_memory_provider_t * )provider ;
249
247
umf_os_memory_provider_config_t * os_config = & os_provider -> config ;
250
248
251
- if (alignment && (alignment & (alignment - 1 ))) {
252
- os_store_last_native_error (UMF_OS_RESULT_ERROR_WRONG_ALIGNMENT , 0 );
249
+ size_t page_size ;
250
+ umf_result_t result = os_get_min_page_size (provider , NULL , & page_size );
251
+ if (result != UMF_RESULT_SUCCESS ) {
252
+ return result ;
253
+ }
254
+
255
+ if (alignment && (alignment % page_size ) && (page_size % alignment )) {
253
256
if (os_config -> traces ) {
254
- fprintf (stderr , "wrong alignment (not a power of 2): %zu\n" ,
255
- alignment );
257
+ fprintf (stderr ,
258
+ "wrong alignment: %zu (not a multiple or a divider of the "
259
+ "minimum page size (%zu))\n" ,
260
+ alignment , page_size );
256
261
}
257
- return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC ;
262
+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
258
263
}
259
264
260
265
int flags = os_config -> visibility ;
@@ -272,8 +277,8 @@ static umf_result_t os_alloc(void *provider, size_t size, size_t alignment,
272
277
return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC ;
273
278
}
274
279
275
- // sanity check - the address should be aligned here
276
- if ((alignment > 0 ) && ((uintptr_t )addr & ( alignment - 1 ) )) {
280
+ // verify the alignment
281
+ if ((alignment > 0 ) && ((uintptr_t )addr % alignment )) {
277
282
if (os_config -> traces ) {
278
283
os_store_last_native_error (UMF_OS_RESULT_ERROR_ADDRESS_NOT_ALIGNED ,
279
284
0 );
0 commit comments