1 .\" Copyright (c) 2001 by John Levon <moz@compsoc.man.ac.uk>
2 .\" Based in part on GNU libc documentation.
4 .\" %%%LICENSE_START(VERBATIM)
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" 2001-10-11, 2003-08-22, aeb, added some details
27 .\" 2012-03-23, Michael Kerrisk <mtk.manpages@mail.com>
28 .\" Document pvalloc() and aligned_alloc()
29 .TH POSIX_MEMALIGN 3 2020-04-11 "GNU" "Linux Programmer's Manual"
31 posix_memalign, aligned_alloc, memalign, valloc, pvalloc \- allocate aligned memory
34 .B #include <stdlib.h>
36 .BI "int posix_memalign(void **" memptr ", size_t " alignment ", size_t " size );
37 .BI "void *aligned_alloc(size_t " alignment ", size_t " size );
38 .BI "void *valloc(size_t " size );
40 .B #include <malloc.h>
42 .BI "void *memalign(size_t " alignment ", size_t " size );
43 .BI "void *pvalloc(size_t " size );
47 Feature Test Macro Requirements for glibc (see
48 .BR feature_test_macros (7)):
52 .BR posix_memalign ():
53 _POSIX_C_SOURCE\ >=\ 200112L
65 (_XOPEN_SOURCE\ >=\ 500) && !(_POSIX_C_SOURCE\ >=\ 200112L)
66 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
67 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
72 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
73 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
76 (The (nonstandard) header file
78 also exposes the declaration of
80 no feature test macros are required.)
88 bytes and places the address of the allocated memory in
90 The address of the allocated memory will be a multiple of
92 which must be a power of two and a multiple of
93 .IR "sizeof(void\ *)" .
101 or a unique pointer value that can later be successfully passed to
104 The obsolete function
108 bytes and returns a pointer to the allocated memory.
109 The memory address will be a multiple of
111 which must be a power of two.
112 .\" The behavior of memalign() for size==0 is as for posix_memalign()
113 .\" but no standards govern this.
119 except for the added restriction that
121 should be a multiple of
124 The obsolete function
128 bytes and returns a pointer to the allocated memory.
129 The memory address will be a multiple of the page size.
131 .IR "memalign(sysconf(_SC_PAGESIZE),size)" .
133 The obsolete function
137 but rounds the size of the allocation up to
138 the next multiple of the system page size.
140 For all of these functions, the memory is not zeroed.
142 .BR aligned_alloc (),
147 return a pointer to the allocated memory on success.
148 On error, NULL is returned, and \fIerrno\fP is set
149 to indicate the cause of the error.
151 .BR posix_memalign ()
152 returns zero on success, or one of the error values listed in the
153 next section on failure.
157 On Linux (and other systems),
158 .BR posix_memalign ()
162 A requirement standardizing this behavior was added in POSIX.1-2016.
163 .\" http://austingroupbugs.net/view.php?id=520
169 argument was not a power of two, or was not a multiple of
170 .IR "sizeof(void\ *)" .
173 There was insufficient memory to fulfill the allocation request.
180 have been available since at least glibc 2.0.
184 was added to glibc in version 2.16.
187 .BR posix_memalign ()
188 is available since glibc 2.1.91.
190 For an explanation of the terms used in this section, see
196 Interface Attribute Value
198 .BR aligned_alloc (),
202 .BR posix_memalign ()
203 T} Thread safety MT-Safe
208 T} Thread safety MT-Unsafe init
215 It is documented as being obsolete in 4.3BSD,
216 and as legacy in SUSv2.
217 It does not appear in POSIX.1.
225 appears in SunOS 4.1.3 but not in 4.4BSD.
228 .BR posix_memalign ()
229 comes from POSIX.1d and is specified in POSIX.1-2001 and POSIX.1-2008.
233 is specified in the C11 standard.
236 Everybody agrees that
237 .BR posix_memalign ()
238 is declared in \fI<stdlib.h>\fP.
242 is declared in \fI<stdlib.h>\fP instead of \fI<malloc.h>\fP.
246 is declared in \fI<stdlib.h>\fP.
247 Libc4,5 and glibc declare it in \fI<malloc.h>\fP, and also in
249 if suitable feature test macros are defined (see above).
251 On many systems there are alignment restrictions, for example, on buffers
252 used for direct block device I/O.
254 .I "pathconf(path,_PC_REC_XFER_ALIGN)"
255 call that tells what alignment is needed.
257 .BR posix_memalign ()
258 to satisfy this requirement.
260 .BR posix_memalign ()
263 matches the requirements detailed above.
265 may not check that the
269 POSIX requires that memory obtained from
270 .BR posix_memalign ()
273 Some systems provide no way to reclaim memory allocated with
277 (because one can pass to
279 only a pointer obtained from
285 and then align the obtained value).
286 .\" Other systems allow passing the result of
292 The glibc implementation
293 allows memory obtained from any of these functions to be
299 always returns 8-byte aligned memory addresses, so these functions are
300 needed only if you require larger alignment values.