microsoft / microsoft/mimalloc

Is it possible to do an aligned allocation with negative offset?

Open
#374 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
13.4k
Forks
1.2k
Avg merge
4d 45m
Merged PRs (30d)
13

Description

I have a pretty niche use-case where I would like to allocate a region of memory such that `B < n < *`, where `n` is the number of bytes allocated `-B` bytes prior to a specified alignment boundary `A`.

For example, if I want to allocate `n` bytes and align to an `A` byte boundary, negatively offsetted by `B` bytes, then for the returned address `P`, `(P+B) % A == 0` would hold (the returned address `P` plus the offset amount `B` would be an address aligned to `A` bytes, after which is an `n-B` byte region of memory).

The existing alignment API already does this but (conceptually) always with `B=0`.

Is this possible with the existing API? I'm specifically looking to do this with heaps, if that makes any difference. The usecase is to encode some fixed-length metadata preamble to a variable length memory region, the latter of which must be accessed on a specified alignment boundary.

I understand I can simply allocate one or more blocks of `A` bytes in addition to `n` (where `B` would be rounded up to a multiple of `A`) and align to `A` to achieve a similar effect, but as `A` increases for short `B` sizes there becomes an increasing amount of wasted memory in cases of many allocations.

Any information would be appreciated :) Thanks so much.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing mimalloc's existing alignment API and heap allocation entry points. Determine whether a negative offset is supported or requires a new API, then document the expected allocation and alignment behavior and add coverage for the requested case if the project has a relevant test location.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.