man-pages/man3/posix_memalign.3.html
2021-03-31 01:06:50 +01:00

452 lines
9.0 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of POSIX_MEMALIGN</TITLE>
</HEAD><BODY>
<H1>POSIX_MEMALIGN</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-05-09<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
posix_memalign, aligned_alloc, memalign, valloc, pvalloc - allocate aligned memory
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;</B>
<B>int posix_memalign(void **</B><I>memptr</I><B>, size_t </B><I>alignment</I><B>, size_t </B><I>size</I><B>);</B>
<B>void *aligned_alloc(size_t </B><I>alignment</I><B>, size_t </B><I>size</I><B>);</B>
<B>void *valloc(size_t </B><I>size</I><B>);</B>
<B>#include &lt;<A HREF="file:///usr/include/malloc.h">malloc.h</A>&gt;</B>
<B>void *memalign(size_t </B><I>alignment</I><B>, size_t </B><I>size</I><B>);</B>
<B>void *pvalloc(size_t </B><I>size</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>posix_memalign</B>():
_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200112L
<P>
<B>aligned_alloc</B>():
_ISOC11_SOURCE
<P>
<B>valloc</B>():
<BR>
<DL COMPACT><DT id="1"><DD>
<DL COMPACT>
<DT id="2">Since glibc 2.12:<DD>
<PRE>
(_XOPEN_SOURCE&nbsp;&gt;=&nbsp;500) &amp;&amp; !(_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200112L)
|| /* Glibc since 2.19: */ _DEFAULT_SOURCE
|| /* Glibc versions &lt;= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
<BR>
</PRE>
<DT id="3">Before glibc 2.12:<DD>
_BSD_SOURCE || _XOPEN_SOURCE&nbsp;&gt;=&nbsp;500
<BR>
(The (nonstandard) header file
<I>&lt;<A HREF="file:///usr/include/malloc.h">malloc.h</A>&gt;</I>
also exposes the declaration of
<B>valloc</B>();
no feature test macros are required.)
</DL>
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The function
<B>posix_memalign</B>()
allocates
<I>size</I>
bytes and places the address of the allocated memory in
<I>*memptr</I>.
The address of the allocated memory will be a multiple of
<I>alignment</I>,
which must be a power of two and a multiple of
<I>sizeof(void&nbsp;*)</I>.
If
<I>size</I>
is 0, then
the value placed in
<I>*memptr</I>
is either NULL,
or a unique pointer value that can later be successfully passed to
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(3).
<P>
The obsolete function
<B>memalign</B>()
allocates
<I>size</I>
bytes and returns a pointer to the allocated memory.
The memory address will be a multiple of
<I>alignment</I>,
which must be a power of two.
<P>
The function
<B>aligned_alloc</B>()
is the same as
<B>memalign</B>(),
except for the added restriction that
<I>size</I>
should be a multiple of
<I>alignment</I>.
<P>
The obsolete function
<B>valloc</B>()
allocates
<I>size</I>
bytes and returns a pointer to the allocated memory.
The memory address will be a multiple of the page size.
It is equivalent to
<I>memalign(sysconf(_SC_PAGESIZE),size)</I>.
<P>
The obsolete function
<B>pvalloc</B>()
is similar to
<B>valloc</B>(),
but rounds the size of the allocation up to
the next multiple of the system page size.
<P>
For all of these functions, the memory is not zeroed.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
<B>aligned_alloc</B>(),
<B>memalign</B>(),
<B>valloc</B>(),
and
<B>pvalloc</B>()
return a pointer to the allocated memory on success.
On error, NULL is returned, and <I>errno</I> is set
to indicate the cause of the error.
<P>
<B>posix_memalign</B>()
returns zero on success, or one of the error values listed in the
next section on failure.
The value of
<I>errno</I>
is not set.
On Linux (and other systems),
<B>posix_memalign</B>()
does not modify
<I>memptr</I>
on failure.
A requirement standardizing this behavior was added in POSIX.1-2016.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="4"><B>EINVAL</B>
<DD>
The
<I>alignment</I>
argument was not a power of two, or was not a multiple of
<I>sizeof(void&nbsp;*)</I>.
<DT id="5"><B>ENOMEM</B>
<DD>
There was insufficient memory to fulfill the allocation request.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
The functions
<B>memalign</B>(),
<B>valloc</B>(),
and
<B>pvalloc</B>()
have been available in all Linux libc libraries.
<P>
The function
<B>aligned_alloc</B>()
was added to glibc in version 2.16.
<P>
The function
<B>posix_memalign</B>()
is available since glibc 2.1.91.
<A NAME="lbAH">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>aligned_alloc</B>(),
<BR>
<B>memalign</B>(),
<BR>
<B>posix_memalign</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
<TR VALIGN=top><TD>
<B>valloc</B>(),
<BR>
<B>pvalloc</B>()
</TD><TD>Thread safety</TD><TD>MT-Unsafe init<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
The function
<B>valloc</B>()
appeared in 3.0BSD.
It is documented as being obsolete in 4.3BSD,
and as legacy in SUSv2.
It does not appear in POSIX.1.
<P>
The function
<B>pvalloc</B>()
is a GNU extension.
<P>
The function
<B>memalign</B>()
appears in SunOS 4.1.3 but not in 4.4BSD.
<P>
The function
<B>posix_memalign</B>()
comes from POSIX.1d and is specified in POSIX.1-2001 and POSIX.1-2008.
<P>
The function
<B>aligned_alloc</B>()
is specified in the C11 standard.
<A NAME="lbAJ">&nbsp;</A>
<H3>Headers</H3>
Everybody agrees that
<B>posix_memalign</B>()
is declared in <I>&lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;</I>.
<P>
On some systems
<B>memalign</B>()
is declared in <I>&lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;</I> instead of <I>&lt;<A HREF="file:///usr/include/malloc.h">malloc.h</A>&gt;</I>.
<P>
According to SUSv2,
<B>valloc</B>()
is declared in <I>&lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;</I>.
Libc4,5 and glibc declare it in <I>&lt;<A HREF="file:///usr/include/malloc.h">malloc.h</A>&gt;</I>, and also in
<I>&lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;</I>
if suitable feature test macros are defined (see above).
<A NAME="lbAK">&nbsp;</A>
<H2>NOTES</H2>
On many systems there are alignment restrictions, for example, on buffers
used for direct block device I/O.
POSIX specifies the
<I>pathconf(path,_PC_REC_XFER_ALIGN)</I>
call that tells what alignment is needed.
Now one can use
<B>posix_memalign</B>()
to satisfy this requirement.
<P>
<B>posix_memalign</B>()
verifies that
<I>alignment</I>
matches the requirements detailed above.
<B>memalign</B>()
may not check that the
<I>alignment</I>
argument is correct.
<P>
POSIX requires that memory obtained from
<B>posix_memalign</B>()
can be freed using
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(3).
Some systems provide no way to reclaim memory allocated with
<B>memalign</B>()
or
<B>valloc</B>()
(because one can pass to
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(3)
only a pointer obtained from
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3),
while, for example,
<B>memalign</B>()
would call
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
and then align the obtained value).
The glibc implementation
allows memory obtained from any of these functions to be
reclaimed with
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(3).
<P>
The glibc
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
always returns 8-byte aligned memory addresses, so these functions are
needed only if you require larger alignment values.
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+brk">brk</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+getpagesize">getpagesize</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
<A NAME="lbAM">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="6"><A HREF="#lbAB">NAME</A><DD>
<DT id="7"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="8"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="9"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="10"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="11"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="12"><A HREF="#lbAH">ATTRIBUTES</A><DD>
<DT id="13"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DL>
<DT id="14"><A HREF="#lbAJ">Headers</A><DD>
</DL>
<DT id="15"><A HREF="#lbAK">NOTES</A><DD>
<DT id="16"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="17"><A HREF="#lbAM">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:52 GMT, March 31, 2021
</BODY>
</HTML>