]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/posix_memalign.3
arch_prctl.2, getpeername.2, getrlimit.2, ioctl_list.2, mmap2.2, mremap.2, sched_seta...
[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()
287cdf88 29.TH POSIX_MEMALIGN 3 2012-03-23 "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
MK
34.B #include <stdlib.h>
35.sp
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 );
fea681da
MK
39.sp
40.B #include <malloc.h>
41.sp
01657408 42.BI "void *memalign(size_t " alignment ", size_t " size );
59b02287 43.BI "void *pvalloc(size_t " size );
fea681da 44.fi
cc4615cc
MK
45.sp
46.in -4n
47Feature Test Macro Requirements for glibc (see
48.BR feature_test_macros (7)):
49.in
50.sp
51.ad l
52.BR posix_memalign ():
53_POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
124b387f 54.sp
8f804f14
MK
55.BR aligned_alloc ():
56_ISOC11_SOURCE
57.sp
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
MK
64.nf
65_BSD_SOURCE ||
f6b326eb 66 (_XOPEN_SOURCE\ >=\ 500 ||
7b072c4d 67 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED) &&
98dbe7af 68 !(_POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600)
124b387f
MK
69.br
70.fi
71.TP
72Before glibc 2.12:
98dbe7af 73_BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
124b387f 74_XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
287cdf88
MK
75.ad b
76.br
77(The (nonstandard) header file
78.I <malloc.h>
79also exposes the declaration of
80.BR valloc ();
81no feature test macros are required.)
124b387f 82.RE
e68ab40d 83.PD
fea681da
MK
84.SH DESCRIPTION
85The function
63aa9df0 86.BR posix_memalign ()
fea681da
MK
87allocates
88.I size
89bytes and places the address of the allocated memory in
a5e0a0e4 90.IR "*memptr" .
fea681da 91The address of the allocated memory will be a multiple of
a5e0a0e4 92.IR "alignment" ,
fea681da 93which must be a power of two and a multiple of
5049da5b 94.IR "sizeof(void\ *)" .
b6dd683a
MK
95If
96.I size
97is 0, then
98.BR posix_memalign ()
99returns either NULL,
100.\" glibc does this:
101or a unique pointer value that can later be successfully passed to
0b80cf56 102.BR free (3).
fea681da
MK
103
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.
fea681da 114
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 .
123
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)" .
132
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.
139
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.
fea681da 148
63aa9df0 149.BR posix_memalign ()
fea681da 150returns zero on success, or one of the error values listed in the
c13182ef
MK
151next section on failure.
152Note that
0daa9e92 153.I errno
fea681da 154is not set.
47297adb 155.SH ERRORS
fea681da
MK
156.TP
157.B EINVAL
158The
0daa9e92 159.I alignment
c4bb193f 160argument was not a power of two, or was not a multiple of
5049da5b 161.IR "sizeof(void\ *)" .
fea681da
MK
162.TP
163.B ENOMEM
164There was insufficient memory to fulfill the allocation request.
2b2581ee
MK
165.SH VERSIONS
166The functions
59b02287
MK
167.BR memalign (),
168.BR valloc (),
2b2581ee 169and
59b02287 170.BR pvalloc ()
2b2581ee 171have been available in all Linux libc libraries.
8f804f14
MK
172
173The function
174.BR aligned_alloc ()
175was added to glibc in version 2.16.
176
2b2581ee
MK
177The function
178.BR posix_memalign ()
179is available since glibc 2.1.91.
47297adb 180.SH CONFORMING TO
2b2581ee
MK
181The function
182.BR valloc ()
183appeared in 3.0BSD.
184It is documented as being obsolete in 4.3BSD,
185and as legacy in SUSv2.
186It does not appear in POSIX.1-2001.
c36b9932 187
59b02287
MK
188The function
189.BR pvalloc ()
190is a GNU extension.
191
2b2581ee
MK
192The function
193.BR memalign ()
194appears in SunOS 4.1.3 but not in 4.4BSD.
c36b9932 195
2b2581ee
MK
196The function
197.BR posix_memalign ()
198comes from POSIX.1d.
8f804f14
MK
199
200The function
ebca85d8 201.BR aligned_alloc ()
8f804f14
MK
202is specified in the C11 standard.
203.\"
2b2581ee
MK
204.SS Headers
205Everybody agrees that
206.BR posix_memalign ()
c84371c6 207is declared in \fI<stdlib.h>\fP.
2b2581ee
MK
208
209On some systems
210.BR memalign ()
c84371c6 211is declared in \fI<stdlib.h>\fP instead of \fI<malloc.h>\fP.
2b2581ee
MK
212
213According to SUSv2,
214.BR valloc ()
c84371c6 215is declared in \fI<stdlib.h>\fP.
287cdf88 216Libc4,5 and glibc declare it in \fI<malloc.h>\fP, and also in
d9a10d9d 217\fI<stdlib.h>\fP
287cdf88 218if suitable feature test macros are defined (see above).
fea681da 219.SH NOTES
75b94dc3 220On many systems there are alignment restrictions, for example, on buffers
c13182ef
MK
221used for direct block device I/O.
222POSIX specifies the
fea681da 223.I "pathconf(path,_PC_REC_XFER_ALIGN)"
c13182ef
MK
224call that tells what alignment is needed.
225Now one can use
fea681da
MK
226.BR posix_memalign ()
227to satisfy this requirement.
228
229.BR posix_memalign ()
230verifies that
0daa9e92 231.I alignment
fea681da 232matches the requirements detailed above.
63aa9df0 233.BR memalign ()
fea681da 234may not check that the
01657408 235.I alignment
c4bb193f 236argument is correct.
fea681da
MK
237
238POSIX requires that memory obtained from
63aa9df0 239.BR posix_memalign ()
fea681da 240can be freed using
fb186734 241.BR free (3).
fea681da 242Some systems provide no way to reclaim memory allocated with
31e9a9ec 243.BR memalign ()
fea681da 244or
31e9a9ec 245.BR valloc ()
33a0ccb2 246(because one can pass to
fb186734 247.BR free (3)
33a0ccb2 248only a pointer obtained from
fb186734 249.BR malloc (3),
75b94dc3 250while, for example,
31e9a9ec 251.BR memalign ()
fea681da 252would call
fb186734 253.BR malloc (3)
fea681da
MK
254and then align the obtained value).
255.\" Other systems allow passing the result of
256.\" .IR valloc ()
257.\" to
fb186734 258.\" .IR free (3),
fea681da 259.\" but not to
fb186734 260.\" .IR realloc (3).
5260fe08 261The glibc implementation
b00b80e0 262allows memory obtained from any of these functions to be
fea681da 263reclaimed with
fb186734 264.BR free (3).
fea681da 265
5260fe08 266The glibc
fb186734 267.BR malloc (3)
33a0ccb2
MK
268always returns 8-byte aligned memory addresses, so these functions are
269needed only if you require larger alignment values.
47297adb 270.SH SEE ALSO
fea681da
MK
271.BR brk (2),
272.BR getpagesize (2),
273.BR free (3),
cc4615cc 274.BR malloc (3)