Document Number:N2072
Submitter: Martin Sebor
Submission Date: August 24, 2016
Subject: Using aligned_alloc to allocate smaller objects than alignment

Summary

Prior to DR 460, the aligned_alloc(size_t alignment, size_t size) function specified the following constraint:

The value of alignment shall be a valid alignment supported by the implementation and the value of size shall be an integral multiple of alignment.

Thus, conforming implementations could, and in fact some do, accept as an extension sizes that did not meet the constraint. Such an extension turns out to be useful for allocating small objects at restrictive alignment boundaries on the heap, and complements the ability to allocate such objects on the stack or in the data segment by means of the _Alignas keyword.

The resolution of DR 460 changed the constraint to the following:

If the value of alignment is not a valid alignment supported by the implementation or the value of size is not an integral multiple of alignment the function shall fail by returning a null pointer.

The (unforeseen and unintended) effect of this change is that conforming implementations may not provide the useful extension described above. Removing the extension would with no apparent benefit break programs that have come to rely on it.

As an aside, it's worth noting that the specification leaves open the question of the function's effects with a zero alignment argument (and a valid size). Should portable programs expect the function to ignore it (just like _Alignas is specified to ignore an alignment of zero), or is it meant to be implemementation-defined (and if so, is that helpful for portability)?

Suggested Technical Corrigendum

The proposed corrigendum below removes the unintended constraint introduced in DR 460 and relaxes the requirement on implementations to allow (though not require) them to accept sizes that aren't multiples of the alignment.

In §7.22.3.1, modify paragraph 2 as indicated below:

The aligned_alloc function allocates space for an object whose alignment is specified by alignment, whose size is specified by size, and whose value is indeterminate. TIf the value of alignment shall be is not a valid alignment supported by the implementation andor the value of size shall be an integral multiple of alignment is not positive the function shall fail by returning a null pointer.