Skip to content

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

Description

@OscarTHZhang

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions