]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/posix_memalign.3
tsearch.3: SYNOPSIS: clarify that twalk_r() requires _GNU_SOURCE
[thirdparty/man-pages.git] / man3 / posix_memalign.3
CommitLineData
bf5a7247 1.\" Copyright (c) 2001 by John Levon <moz@compsoc.man.ac.uk>
fea681da
MK
2.\" Based in part on GNU libc documentation.
3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
5.\" Permission is granted to make and distribute verbatim copies of this
6.\" manual provided the copyright notice and this permission notice are
7.\" preserved on all copies.
8.\"
9.\" Permission is granted to copy and distribute modified versions of this
10.\" manual under the conditions for verbatim copying, provided that the
11.\" entire resulting derived work is distributed under the terms of a
12.\" permission notice identical to this one.
c13182ef 13.\"
fea681da
MK
14.\" Since the Linux kernel and libraries are constantly changing, this
15.\" manual page may be incorrect or out-of-date. The author(s) assume no
16.\" responsibility for errors or omissions, or for damages resulting from
17.\" the use of the information contained herein. The author(s) may not
18.\" have taken the same level of care in the production of this manual,
19.\" which is licensed free of charge, as they might when working
20.\" professionally.
c13182ef 21.\"
fea681da
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
fea681da
MK
25.\"
26.\" 2001-10-11, 2003-08-22, aeb, added some details
8f804f14
MK
27.\" 2012-03-23, Michael Kerrisk <mtk.manpages@mail.com>
28.\" Document pvalloc() and aligned_alloc()
4b8c67d9 29.TH POSIX_MEMALIGN 3 2017-09-15 "GNU" "Linux Programmer's Manual"
fea681da 30.SH NAME
f68512e9 31posix_memalign, aligned_alloc, memalign, valloc, pvalloc \- allocate aligned memory
fea681da
MK
32.SH SYNOPSIS
33.nf
fea681da 34.B #include <stdlib.h>
68e4db0a 35.PP
fea681da 36.BI "int posix_memalign(void **" memptr ", size_t " alignment ", size_t " size );
8f804f14 37.BI "void *aligned_alloc(size_t " alignment ", size_t " size );
287cdf88 38.BI "void *valloc(size_t " size );
f90f031e 39
fea681da 40.B #include <malloc.h>
68e4db0a 41.PP
01657408 42.BI "void *memalign(size_t " alignment ", size_t " size );
59b02287 43.BI "void *pvalloc(size_t " size );
fea681da 44.fi
68e4db0a 45.PP
cc4615cc
MK
46.in -4n
47Feature Test Macro Requirements for glibc (see
48.BR feature_test_macros (7)):
49.in
68e4db0a 50.PP
cc4615cc
MK
51.ad l
52.BR posix_memalign ():
a446ac0c 53_POSIX_C_SOURCE\ >=\ 200112L
68e4db0a 54.PP
8f804f14
MK
55.BR aligned_alloc ():
56_ISOC11_SOURCE
68e4db0a 57.PP
124b387f
MK
58.BR valloc ():
59.br
e68ab40d 60.PD 0
124b387f
MK
61.RS 4
62.TP 4
f6b326eb 63Since glibc 2.12:
124b387f 64.nf
2b1b0424
MK
65(_XOPEN_SOURCE\ >=\ 500) && !(_POSIX_C_SOURCE\ >=\ 200112L)
66 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
67 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
124b387f
MK
68.br
69.fi
70.TP
71Before glibc 2.12:
cf7fa0a1
MK
72_BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
73.\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
287cdf88
MK
74.ad b
75.br
76(The (nonstandard) header file
77.I <malloc.h>
78also exposes the declaration of
79.BR valloc ();
80no feature test macros are required.)
124b387f 81.RE
e68ab40d 82.PD
fea681da
MK
83.SH DESCRIPTION
84The function
63aa9df0 85.BR posix_memalign ()
fea681da
MK
86allocates
87.I size
88bytes and places the address of the allocated memory in
a5e0a0e4 89.IR "*memptr" .
fea681da 90The address of the allocated memory will be a multiple of
a5e0a0e4 91.IR "alignment" ,
fea681da 92which must be a power of two and a multiple of
5049da5b 93.IR "sizeof(void\ *)" .
b6dd683a
MK
94If
95.I size
96is 0, then
558f02df
MK
97the value placed in
98.IR "*memptr"
99is either NULL,
b6dd683a
MK
100.\" glibc does this:
101or a unique pointer value that can later be successfully passed to
0b80cf56 102.BR free (3).
847e0d88 103.PP
fea681da 104The obsolete function
63aa9df0 105.BR memalign ()
fea681da
MK
106allocates
107.I size
108bytes and returns a pointer to the allocated memory.
109The memory address will be a multiple of
01657408 110.IR alignment ,
fea681da 111which must be a power of two.
0d244df1
MK
112.\" The behavior of memalign() for size==0 is as for posix_memalign()
113.\" but no standards govern this.
847e0d88 114.PP
8f804f14
MK
115The function
116.BR aligned_alloc ()
117is the same as
118.BR memalign (),
119except for the added restriction that
120.I size
121should be a multiple of
122.IR alignment .
847e0d88 123.PP
fea681da 124The obsolete function
63aa9df0 125.BR valloc ()
fea681da
MK
126allocates
127.I size
128bytes and returns a pointer to the allocated memory.
129The memory address will be a multiple of the page size.
130It is equivalent to
131.IR "memalign(sysconf(_SC_PAGESIZE),size)" .
847e0d88 132.PP
59b02287
MK
133The obsolete function
134.BR pvalloc ()
135is similar to
136.BR valloc (),
137but rounds the size of the allocation up to
138the next multiple of the system page size.
847e0d88 139.PP
59b02287 140For all of these functions, the memory is not zeroed.
47297adb 141.SH RETURN VALUE
8f804f14 142.BR aligned_alloc (),
59b02287
MK
143.BR memalign (),
144.BR valloc (),
fea681da 145and
59b02287 146.BR pvalloc ()
2cfdf8f8
EH
147return a pointer to the allocated memory on success.
148On error, NULL is returned, and \fIerrno\fP is set
149to indicate the cause of the error.
847e0d88 150.PP
63aa9df0 151.BR posix_memalign ()
fea681da 152returns zero on success, or one of the error values listed in the
c13182ef 153next section on failure.
21a1f5bd 154The value of
0daa9e92 155.I errno
df383cdf
MK
156is not set.
157On Linux (and other systems),
158.BR posix_memalign ()
159does not modify
160.I memptr
161on failure.
162A requirement standardizing this behavior was added in POSIX.1-2016.
163.\" http://austingroupbugs.net/view.php?id=520
d8026103 164.SH ERRORS
fea681da
MK
165.TP
166.B EINVAL
167The
0daa9e92 168.I alignment
c4bb193f 169argument was not a power of two, or was not a multiple of
5049da5b 170.IR "sizeof(void\ *)" .
fea681da
MK
171.TP
172.B ENOMEM
173There was insufficient memory to fulfill the allocation request.
2b2581ee
MK
174.SH VERSIONS
175The functions
59b02287
MK
176.BR memalign (),
177.BR valloc (),
2b2581ee 178and
59b02287 179.BR pvalloc ()
2b2581ee 180have been available in all Linux libc libraries.
847e0d88 181.PP
8f804f14
MK
182The function
183.BR aligned_alloc ()
184was added to glibc in version 2.16.
847e0d88 185.PP
2b2581ee
MK
186The function
187.BR posix_memalign ()
188is available since glibc 2.1.91.
b63bb31a
ZL
189.SH ATTRIBUTES
190For an explanation of the terms used in this section, see
191.BR attributes (7).
192.TS
193allbox;
194lb lb lb
195l l l.
196Interface Attribute Value
197T{
198.BR aligned_alloc (),
199.br
200.BR memalign (),
201.br
202.BR posix_memalign ()
203T} Thread safety MT-Safe
204T{
205.BR valloc (),
206.br
207.BR pvalloc ()
208T} Thread safety MT-Unsafe init
209.TE
847e0d88 210.sp 1
47297adb 211.SH CONFORMING TO
2b2581ee
MK
212The function
213.BR valloc ()
214appeared in 3.0BSD.
215It is documented as being obsolete in 4.3BSD,
216and as legacy in SUSv2.
f3924c78 217It does not appear in POSIX.1.
847e0d88 218.PP
59b02287
MK
219The function
220.BR pvalloc ()
221is a GNU extension.
847e0d88 222.PP
2b2581ee
MK
223The function
224.BR memalign ()
225appears in SunOS 4.1.3 but not in 4.4BSD.
847e0d88 226.PP
2b2581ee
MK
227The function
228.BR posix_memalign ()
dc90f4d3 229comes from POSIX.1d and is specified in POSIX.1-2001 and POSIX.1-2008.
847e0d88 230.PP
8f804f14 231The function
ebca85d8 232.BR aligned_alloc ()
8f804f14
MK
233is specified in the C11 standard.
234.\"
2b2581ee
MK
235.SS Headers
236Everybody agrees that
237.BR posix_memalign ()
c84371c6 238is declared in \fI<stdlib.h>\fP.
847e0d88 239.PP
2b2581ee
MK
240On some systems
241.BR memalign ()
c84371c6 242is declared in \fI<stdlib.h>\fP instead of \fI<malloc.h>\fP.
847e0d88 243.PP
2b2581ee
MK
244According to SUSv2,
245.BR valloc ()
c84371c6 246is declared in \fI<stdlib.h>\fP.
287cdf88 247Libc4,5 and glibc declare it in \fI<malloc.h>\fP, and also in
d9a10d9d 248\fI<stdlib.h>\fP
287cdf88 249if suitable feature test macros are defined (see above).
fea681da 250.SH NOTES
75b94dc3 251On many systems there are alignment restrictions, for example, on buffers
c13182ef
MK
252used for direct block device I/O.
253POSIX specifies the
fea681da 254.I "pathconf(path,_PC_REC_XFER_ALIGN)"
c13182ef
MK
255call that tells what alignment is needed.
256Now one can use
fea681da
MK
257.BR posix_memalign ()
258to satisfy this requirement.
847e0d88 259.PP
fea681da
MK
260.BR posix_memalign ()
261verifies that
0daa9e92 262.I alignment
fea681da 263matches the requirements detailed above.
63aa9df0 264.BR memalign ()
fea681da 265may not check that the
01657408 266.I alignment
c4bb193f 267argument is correct.
847e0d88 268.PP
fea681da 269POSIX requires that memory obtained from
63aa9df0 270.BR posix_memalign ()
fea681da 271can be freed using
fb186734 272.BR free (3).
fea681da 273Some systems provide no way to reclaim memory allocated with
31e9a9ec 274.BR memalign ()
fea681da 275or
31e9a9ec 276.BR valloc ()
33a0ccb2 277(because one can pass to
fb186734 278.BR free (3)
33a0ccb2 279only a pointer obtained from
fb186734 280.BR malloc (3),
75b94dc3 281while, for example,
31e9a9ec 282.BR memalign ()
fea681da 283would call
fb186734 284.BR malloc (3)
fea681da
MK
285and then align the obtained value).
286.\" Other systems allow passing the result of
287.\" .IR valloc ()
288.\" to
fb186734 289.\" .IR free (3),
fea681da 290.\" but not to
fb186734 291.\" .IR realloc (3).
5260fe08 292The glibc implementation
b00b80e0 293allows memory obtained from any of these functions to be
fea681da 294reclaimed with
fb186734 295.BR free (3).
847e0d88 296.PP
5260fe08 297The glibc
fb186734 298.BR malloc (3)
33a0ccb2
MK
299always returns 8-byte aligned memory addresses, so these functions are
300needed only if you require larger alignment values.
47297adb 301.SH SEE ALSO
fea681da
MK
302.BR brk (2),
303.BR getpagesize (2),
304.BR free (3),
cc4615cc 305.BR malloc (3)