On 10/03/2023 05:05, Xi Ruoyao wrote:
There is a missing EINVAL description. If the alignment is so large that
the allocation will not be possible to satisfy then the call will fail
and set errno to EINVAL.
POSIX says it should be ENOMEM:
[ENOMEM]
There is insufficient memory available with the requested alignment.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.html
And it seems also true with Glibc (at least Glibc-2.37):
$ cat t.c
#include <stdlib.h>
#include <stdio.h>
int main()
{
void *p;
if (posix_memalign(&p, sizeof(void *) << 55, 1) != 0)
perror("posix_memalign");
}
$ cc t.c
$ ./a.out
posix_memalign: Cannot allocate memory
I was referring to memalign / aligned_alloc. The ERRORS section of the
manpage doesn't specify which errors apply to which functions.
Here is an example
#include <stdlib.h>
#include <malloc.h>
int main()
{
void *p;
if ((p == memalign(0xabcdef0123456789, 1)) == 0)
perror("memalign");
}
This does satisfy
EINVAL The alignmentargument was not a power of two, or was not a
multiple of sizeof(void*).
but that is purely a coincidence. The code in gblic that triggers the
error is
│ 3537 /* If the alignment is greater than SIZE_MAX / 2 + 1 it cannot
be a
│ 3538 power of 2 and will cause overflow in the check below. */
│ 3539 if(alignment >SIZE_MAX /2+1)
│ 3540 {
│ > 3541 __set_errno (EINVAL);
│ 3542 return0;
│ 3543 }
If the power-of-two constraint existed this condition would be
redundant. The next power of two greater than SIZE_MAX / 2 + 1 is
SIZE_MAX + 1 which cannot be represented in a word.
Regards
Paul