@@ -205,14 +205,24 @@ static int unix_madvise(void* addr, size_t size, int advice) {
205
205
return (res == 0 ? 0 : errno );
206
206
}
207
207
208
- static void * unix_mmap_prim (void * addr , size_t size , size_t try_alignment , int protect_flags , int flags , int fd ) {
208
+ static void * unix_mmap_prim (void * addr , size_t size , int protect_flags , int flags , int fd ) {
209
+ void * p = mmap (addr , size , protect_flags , flags , fd , 0 /* offset */ );
210
+ #if (defined(__linux__ ) || defined(__ANDROID__ ))
211
+ if (p != MAP_FAILED && p != NULL ) {
212
+ prctl (PR_SET_VMA , PR_SET_VMA_ANON_NAME , p , size , "mimalloc" );
213
+ }
214
+ #endif
215
+ return p ;
216
+ }
217
+
218
+ static void * unix_mmap_prim_aligned (void * addr , size_t size , size_t try_alignment , int protect_flags , int flags , int fd ) {
209
219
MI_UNUSED (try_alignment );
210
220
void * p = NULL ;
211
221
#if defined(MAP_ALIGNED ) // BSD
212
222
if (addr == NULL && try_alignment > 1 && (try_alignment % _mi_os_page_size ()) == 0 ) {
213
223
size_t n = mi_bsr (try_alignment );
214
224
if (((size_t )1 << n ) == try_alignment && n >= 12 && n <= 30 ) { // alignment is a power of 2 and 4096 <= alignment <= 1GiB
215
- p = mmap (addr , size , protect_flags , flags | MAP_ALIGNED (n ), fd , 0 );
225
+ p = unix_mmap_prim (addr , size , protect_flags , flags | MAP_ALIGNED (n ), fd );
216
226
if (p == MAP_FAILED || !_mi_is_aligned (p ,try_alignment )) {
217
227
int err = errno ;
218
228
_mi_trace_message ("unable to directly request aligned OS memory (error: %d (0x%x), size: 0x%zx bytes, alignment: 0x%zx, hint address: %p)\n" , err , err , size , try_alignment , addr );
@@ -223,7 +233,7 @@ static void* unix_mmap_prim(void* addr, size_t size, size_t try_alignment, int p
223
233
}
224
234
#elif defined(MAP_ALIGN ) // Solaris
225
235
if (addr == NULL && try_alignment > 1 && (try_alignment % _mi_os_page_size ()) == 0 ) {
226
- p = mmap ((void * )try_alignment , size , protect_flags , flags | MAP_ALIGN , fd , 0 ); // addr parameter is the required alignment
236
+ p = unix_mmap_prim ((void * )try_alignment , size , protect_flags , flags | MAP_ALIGN , fd ); // addr parameter is the required alignment
227
237
if (p != MAP_FAILED ) return p ;
228
238
// fall back to regular mmap
229
239
}
@@ -233,7 +243,7 @@ static void* unix_mmap_prim(void* addr, size_t size, size_t try_alignment, int p
233
243
if (addr == NULL ) {
234
244
void * hint = _mi_os_get_aligned_hint (try_alignment , size );
235
245
if (hint != NULL ) {
236
- p = mmap (hint , size , protect_flags , flags , fd , 0 );
246
+ p = unix_mmap_prim (hint , size , protect_flags , flags , fd );
237
247
if (p == MAP_FAILED || !_mi_is_aligned (p ,try_alignment )) {
238
248
#if MI_TRACK_ENABLED // asan sometimes does not instrument errno correctly?
239
249
int err = 0 ;
@@ -248,7 +258,7 @@ static void* unix_mmap_prim(void* addr, size_t size, size_t try_alignment, int p
248
258
}
249
259
#endif
250
260
// regular mmap
251
- p = mmap (addr , size , protect_flags , flags , fd , 0 );
261
+ p = unix_mmap_prim (addr , size , protect_flags , flags , fd );
252
262
if (p != MAP_FAILED ) return p ;
253
263
// failed to allocate
254
264
return NULL ;
@@ -319,15 +329,15 @@ static void* unix_mmap(void* addr, size_t size, size_t try_alignment, int protec
319
329
if (large_only || lflags != flags ) {
320
330
// try large OS page allocation
321
331
* is_large = true;
322
- p = unix_mmap_prim (addr , size , try_alignment , protect_flags , lflags , lfd );
332
+ p = unix_mmap_prim_aligned (addr , size , try_alignment , protect_flags , lflags , lfd );
323
333
#ifdef MAP_HUGE_1GB
324
334
if (p == NULL && (lflags & MAP_HUGE_1GB ) == MAP_HUGE_1GB ) {
325
335
mi_huge_pages_available = false; // don't try huge 1GiB pages again
326
336
if (large_only ) {
327
337
_mi_warning_message ("unable to allocate huge (1GiB) page, trying large (2MiB) pages instead (errno: %i)\n" , errno );
328
338
}
329
339
lflags = ((lflags & ~MAP_HUGE_1GB ) | MAP_HUGE_2MB );
330
- p = unix_mmap_prim (addr , size , try_alignment , protect_flags , lflags , lfd );
340
+ p = unix_mmap_prim_aligned (addr , size , try_alignment , protect_flags , lflags , lfd );
331
341
}
332
342
#endif
333
343
if (large_only ) return p ;
@@ -340,7 +350,7 @@ static void* unix_mmap(void* addr, size_t size, size_t try_alignment, int protec
340
350
// regular allocation
341
351
if (p == NULL ) {
342
352
* is_large = false;
343
- p = unix_mmap_prim (addr , size , try_alignment , protect_flags , flags , fd );
353
+ p = unix_mmap_prim_aligned (addr , size , try_alignment , protect_flags , flags , fd );
344
354
if (p != NULL ) {
345
355
#if defined(MADV_HUGEPAGE )
346
356
// Many Linux systems don't allow MAP_HUGETLB but they support instead
0 commit comments