1 .\" Copyright (c) 2001 by John Levon <moz@compsoc.man.ac.uk>
2 .\" Based in part on GNU libc documentation.
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" 2001-10-11, 2003-08-22, aeb, added some details
7 .\" 2012-03-23, Michael Kerrisk <mtk.manpages@mail.com>
8 .\" Document pvalloc() and aligned_alloc()
9 .TH POSIX_MEMALIGN 3 (date) "Linux man-pages (unreleased)"
11 posix_memalign, aligned_alloc, memalign, valloc, pvalloc \-
12 allocate aligned memory
15 .RI ( libc ", " \-lc )
18 .B #include <stdlib.h>
20 .BI "int posix_memalign(void **" memptr ", size_t " alignment ", size_t " size );
21 .BI "void *aligned_alloc(size_t " alignment ", size_t " size );
22 .BI "void *valloc(size_t " size );
24 .B #include <malloc.h>
26 .BI "void *memalign(size_t " alignment ", size_t " size );
27 .BI "void *pvalloc(size_t " size );
31 Feature Test Macro Requirements for glibc (see
32 .BR feature_test_macros (7)):
35 .BR posix_memalign ():
37 _POSIX_C_SOURCE >= 200112L
48 (_XOPEN_SOURCE >= 500) && !(_POSIX_C_SOURCE >= 200112L)
49 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
50 || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
52 _BSD_SOURCE || _XOPEN_SOURCE >= 500
53 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
60 bytes and places the address of the allocated memory in
62 The address of the allocated memory will be a multiple of
64 which must be a power of two and a multiple of
65 .IR "sizeof(void\ *)" .
66 This address can later be successfully passed to
75 or a unique pointer value.
81 bytes and returns a pointer to the allocated memory.
82 The memory address will be a multiple of
84 which must be a power of two.
85 .\" The behavior of memalign() for size==0 is as for posix_memalign()
86 .\" but no standards govern this.
92 except for the added restriction that
94 should be a multiple of
101 bytes and returns a pointer to the allocated memory.
102 The memory address will be a multiple of the page size.
104 .IR "memalign(sysconf(_SC_PAGESIZE),size)" .
106 The obsolete function
110 but rounds the size of the allocation up to
111 the next multiple of the system page size.
113 For all of these functions, the memory is not zeroed.
115 .BR aligned_alloc (),
120 return a pointer to the allocated memory on success.
121 On error, NULL is returned, and \fIerrno\fP is set
122 to indicate the error.
124 .BR posix_memalign ()
125 returns zero on success, or one of the error values listed in the
126 next section on failure.
130 On Linux (and other systems),
131 .BR posix_memalign ()
135 A requirement standardizing this behavior was added in POSIX.1-2008 TC2.
136 .\" http://austingroupbugs.net/view.php?id=520
142 argument was not a power of two, or was not a multiple of
143 .IR "sizeof(void\ *)" .
146 There was insufficient memory to fulfill the allocation request.
153 have been available since at least glibc 2.0.
157 was added to glibc in version 2.16.
160 .BR posix_memalign ()
161 is available since glibc 2.1.91.
163 For an explanation of the terms used in this section, see
171 Interface Attribute Value
173 .BR aligned_alloc (),
175 .BR posix_memalign ()
176 T} Thread safety MT-Safe
180 T} Thread safety MT-Unsafe init
189 It is documented as being obsolete in 4.3BSD,
190 and as legacy in SUSv2.
191 It does not appear in POSIX.1.
199 appears in SunOS 4.1.3 but not in 4.4BSD.
202 .BR posix_memalign ()
203 comes from POSIX.1d and is specified in POSIX.1-2001 and POSIX.1-2008.
207 is specified in the C11 standard.
210 Everybody agrees that
211 .BR posix_memalign ()
212 is declared in \fI<stdlib.h>\fP.
216 is declared in \fI<stdlib.h>\fP instead of \fI<malloc.h>\fP.
220 is declared in \fI<stdlib.h>\fP.
222 Glibc declares it in \fI<malloc.h>\fP, and also in
224 if suitable feature test macros are defined (see above).
226 On many systems there are alignment restrictions, for example, on buffers
227 used for direct block device I/O.
229 .I "pathconf(path,_PC_REC_XFER_ALIGN)"
230 call that tells what alignment is needed.
232 .BR posix_memalign ()
233 to satisfy this requirement.
235 .BR posix_memalign ()
238 matches the requirements detailed above.
240 may not check that the
244 POSIX requires that memory obtained from
245 .BR posix_memalign ()
248 Some systems provide no way to reclaim memory allocated with
252 (because one can pass to
254 only a pointer obtained from
260 and then align the obtained value).
261 .\" Other systems allow passing the result of
267 The glibc implementation
268 allows memory obtained from any of these functions to be
274 always returns 8-byte aligned memory addresses, so these functions are
275 needed only if you require larger alignment values.