Mimalloc arena `mi_heap_malloc_aligned` has a hidden limit on max alignment of 64 KiB per allocation

Open Beginner friendly
#1,406 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Active
Tech stack
c

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.

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):

#include <stdint.h>
#include <stdio.h>
#include <sys/mman.h>

#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;
}

Dominant language
C
Stars
13.4k
Forks
1.2k
Avg merge
4d 45m
Merged PRs (30d)
13

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/mimalloc

All issues in microsoft/mimalloc

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.