]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/posix_memalign.3
isatty.3: Most non-tty files nowadays result in the error ENOTTY
[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
MK
146.BR pvalloc ()
147return a pointer to the allocated memory, or NULL if the request fails.
847e0d88 148.PP
63aa9df0 149.BR posix_memalign ()
fea681da 150returns zero on success, or one of the error values listed in the
c13182ef 151next section on failure.
21a1f5bd 152The value of
0daa9e92 153.I errno
df383cdf
MK
154is not set.
155On Linux (and other systems),
156.BR posix_memalign ()
157does not modify
158.I memptr
159on failure.
160A requirement standardizing this behavior was added in POSIX.1-2016.
161.\" http://austingroupbugs.net/view.php?id=520
d8026103 162.SH ERRORS
fea681da
MK
163.TP
164.B EINVAL
165The
0daa9e92 166.I alignment
c4bb193f 167argument was not a power of two, or was not a multiple of
5049da5b 168.IR "sizeof(void\ *)" .
fea681da
MK
169.TP
170.B ENOMEM
171There was insufficient memory to fulfill the allocation request.
2b2581ee
MK
172.SH VERSIONS
173The functions
59b02287
MK
174.BR memalign (),
175.BR valloc (),
2b2581ee 176and
59b02287 177.BR pvalloc ()
2b2581ee 178have been available in all Linux libc libraries.
847e0d88 179.PP
8f804f14
MK
180The function
181.BR aligned_alloc ()
182was added to glibc in version 2.16.
847e0d88 183.PP
2b2581ee
MK
184The function
185.BR posix_memalign ()
186is available since glibc 2.1.91.
b63bb31a
ZL
187.SH ATTRIBUTES
188For an explanation of the terms used in this section, see
189.BR attributes (7).
190.TS
191allbox;
192lb lb lb
193l l l.
194Interface Attribute Value
195T{
196.BR aligned_alloc (),
197.br
198.BR memalign (),
199.br
200.BR posix_memalign ()
201T} Thread safety MT-Safe
202T{
203.BR valloc (),
204.br
205.BR pvalloc ()
206T} Thread safety MT-Unsafe init
207.TE
847e0d88 208.sp 1
47297adb 209.SH CONFORMING TO
2b2581ee
MK
210The function
211.BR valloc ()
212appeared in 3.0BSD.
213It is documented as being obsolete in 4.3BSD,
214and as legacy in SUSv2.
f3924c78 215It does not appear in POSIX.1.
847e0d88 216.PP
59b02287
MK
217The function
218.BR pvalloc ()
219is a GNU extension.
847e0d88 220.PP
2b2581ee
MK
221The function
222.BR memalign ()
223appears in SunOS 4.1.3 but not in 4.4BSD.
847e0d88 224.PP
2b2581ee
MK
225The function
226.BR posix_memalign ()
dc90f4d3 227comes from POSIX.1d and is specified in POSIX.1-2001 and POSIX.1-2008.
847e0d88 228.PP
8f804f14 229The function
ebca85d8 230.BR aligned_alloc ()
8f804f14
MK
231is specified in the C11 standard.
232.\"
2b2581ee
MK
233.SS Headers
234Everybody agrees that
235.BR posix_memalign ()
c84371c6 236is declared in \fI<stdlib.h>\fP.
847e0d88 237.PP
2b2581ee
MK
238On some systems
239.BR memalign ()
c84371c6 240is declared in \fI<stdlib.h>\fP instead of \fI<malloc.h>\fP.
847e0d88 241.PP
2b2581ee
MK
242According to SUSv2,
243.BR valloc ()
c84371c6 244is declared in \fI<stdlib.h>\fP.
287cdf88 245Libc4,5 and glibc declare it in \fI<malloc.h>\fP, and also in
d9a10d9d 246\fI<stdlib.h>\fP
287cdf88 247if suitable feature test macros are defined (see above).
fea681da 248.SH NOTES
75b94dc3 249On many systems there are alignment restrictions, for example, on buffers
c13182ef
MK
250used for direct block device I/O.
251POSIX specifies the
fea681da 252.I "pathconf(path,_PC_REC_XFER_ALIGN)"
c13182ef
MK
253call that tells what alignment is needed.
254Now one can use
fea681da
MK
255.BR posix_memalign ()
256to satisfy this requirement.
847e0d88 257.PP
fea681da
MK
258.BR posix_memalign ()
259verifies that
0daa9e92 260.I alignment
fea681da 261matches the requirements detailed above.
63aa9df0 262.BR memalign ()
fea681da 263may not check that the
01657408 264.I alignment
c4bb193f 265argument is correct.
847e0d88 266.PP
fea681da 267POSIX requires that memory obtained from
63aa9df0 268.BR posix_memalign ()
fea681da 269can be freed using
fb186734 270.BR free (3).
fea681da 271Some systems provide no way to reclaim memory allocated with
31e9a9ec 272.BR memalign ()
fea681da 273or
31e9a9ec 274.BR valloc ()
33a0ccb2 275(because one can pass to
fb186734 276.BR free (3)
33a0ccb2 277only a pointer obtained from
fb186734 278.BR malloc (3),
75b94dc3 279while, for example,
31e9a9ec 280.BR memalign ()
fea681da 281would call
fb186734 282.BR malloc (3)
fea681da
MK
283and then align the obtained value).
284.\" Other systems allow passing the result of
285.\" .IR valloc ()
286.\" to
fb186734 287.\" .IR free (3),
fea681da 288.\" but not to
fb186734 289.\" .IR realloc (3).
5260fe08 290The glibc implementation
b00b80e0 291allows memory obtained from any of these functions to be
fea681da 292reclaimed with
fb186734 293.BR free (3).
847e0d88 294.PP
5260fe08 295The glibc
fb186734 296.BR malloc (3)
33a0ccb2
MK
297always returns 8-byte aligned memory addresses, so these functions are
298needed only if you require larger alignment values.
47297adb 299.SH SEE ALSO
fea681da
MK
300.BR brk (2),
301.BR getpagesize (2),
302.BR free (3),
cc4615cc 303.BR malloc (3)