Re: [PATCH v2] rust: alloc: satisfy `aligned_alloc` requirements

Tamir Duberstein
2025-02-06T19:05:32Z

On Thu, Feb 6, 2025 at 1:58 PM Danilo Krummrich <dakr@kernel.org> wrote:
>
> On Thu, Feb 06, 2025 at 07:20:20PM +0100, Miguel Ojeda wrote:
> > On Thu, Feb 6, 2025 at 7:04 PM Danilo Krummrich <dakr@kernel.org> wrote:
> > >
> > > I previously checked man posix_memalign(3) and it says:
> > >
> > > ERRORS
> > > EINVAL The alignment argument was not a power of two, or was not a
> > > multiple of sizeof(void *).
> >
> > Yeah, but that may not apply to `alligned_alloc` as far as I can see.
>
> What makes you think so?
>
> AFAICS, the man page applies for posix_memalign, aligned_alloc, memalign,
> valloc and pvalloc.
>
> In case behavior differs between the functions, this is stated explicitly, e.g.
> in the "RETURN VALUE" section.
>
> The "ERRORS" setion does not differentiate, hence it should apply to all the
> functions above, including aligned_alloc.
>
> Do I miss anything?

The writing is definitely obtuse.

> Return Value
>. aligned_alloc(), memalign(), valloc(), and pvalloc() return a pointer to the allocated memory, or NULL if the request fails.
>
> posix_memalign() returns zero on success, or one of the error values listed in the next section on failure. Note that errno is not set.
>
> Errors
> EINVAL
> The alignment argument was not a power of two, or was not a multiple of sizeof(void *).
> ENOMEM
>. There was insufficient memory to fulfill the allocation request.

It seems that the errors section applies to posix_memalign only. The
description section also contains:

> The obsolete function memalign() allocates size bytes and returns a pointer to the allocated memory. The memory address will be a multiple of alignment, which must be a power of two.
>
> The function aligned_alloc() is the same as memalign(), except for the added restriction that size should be a multiple of alignment.

So aligned_alloc has the same alignment requirement as memalign, which
is only that it's a power of two.