microsoft / microsoft/mimalloc
Mimalloc arena `mi_heap_malloc_aligned` has a hidden limit on max alignment of 64 KiB per allocation
- Dominant language
- C
- Stars
- 13.4k
- Forks
- 1.2k
- Avg merge
- 4d 45m
- Merged PRs (30d)
- 13
Description
I understand that there may be an internal limit on the max alignment for `mi_heap_malloc_aligned` of 64 KiB. In the short-term, the documentation should have this limit documented clearly. In the long-term, I wonder if there is way that we could support a larger limit or with no limit?
Repro (version: v3):
```c
#include
#include
#include
#include "mimalloc.h"
int main(void) {
const size_t arena_size = 1ULL * 1024 * 1024 * 1024;
const size_t buffer_size = 1 * 1024 * 1024;
const size_t alignment = 128 * 1024;
void* arena = mmap(NULL, arena_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (arena == MAP_FAILED) {
perror("unable to mmap the arena");
return 1;
}
mi_arena_id_t arena_id = NULL;
if (!mi_manage_os_memory_ex(arena, arena_size, true, false, true, -1, true, &arena_id)) {
fprintf(stderr, "unable to register the mmap region as an exclusive arena\n");
return 1;
}
mi_heap_t* heap = mi_heap_new_in_arena(arena_id);
if (heap == NULL) {
fprintf(stderr, "unable to create a heap in the exclusive arena\n");
return 1;
}
void* buffer = mi_heap_malloc_aligned(heap, buffer_size, alignment);
if (buffer == NULL) {
fprintf(stderr, "unable to allocate a buffer with 128 KiB alignment from the arena\n");
mi_heap_destroy(heap);
return 1;
}
const bool is_aligned = ((uintptr_t)buffer % alignment) == 0;
const bool is_in_arena = mi_arena_contains(arena_id, buffer);
mi_free(buffer);
mi_heap_destroy(heap);
if (!is_aligned || !is_in_arena) {
fprintf(stderr, "allocation is not 128 KiB-aligned inside the registered arena\n");
return 1;
}
return 0;
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the mi_heap_malloc_aligned, mi_manage_os_memory_ex, and mi_heap_new_in_arena API documentation, then run the provided v3 C reproduction using a 128 KiB alignment. Document the observed 64 KiB per-allocation alignment limit clearly; larger or unlimited alignment support is a separate follow-up if pursued.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- operating-systems
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100