2 .\" Copyright (c) 2001 by John Levon <moz@compsoc.man.ac.uk>
3 .\" Based in part on GNU libc documentation.
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" 2001-10-11, 2003-08-22, aeb, added some details
8 .\" 2012-03-23, Michael Kerrisk <mtk.manpages@mail.com>
9 .\" Document pvalloc() and aligned_alloc()
10 .TH posix_memalign 3 (date) "Linux man-pages (unreleased)"
12 posix_memalign, aligned_alloc, memalign, valloc, pvalloc \-
13 allocate aligned memory
16 .RI ( libc ", " \-lc )
19 .B #include <stdlib.h>
21 .BI "int posix_memalign(void **" memptr ", size_t " alignment ", size_t " size );
22 .BI "void *aligned_alloc(size_t " alignment ", size_t " size );
23 .BI "[[deprecated]] void *valloc(size_t " size );
25 .B #include <malloc.h>
27 .BI "[[deprecated]] void *memalign(size_t " alignment ", size_t " size );
28 .BI "[[deprecated]] void *pvalloc(size_t " size );
32 Feature Test Macro Requirements for glibc (see
33 .BR feature_test_macros (7)):
36 .BR posix_memalign ():
38 _POSIX_C_SOURCE >= 200112L
49 (_XOPEN_SOURCE >= 500) && !(_POSIX_C_SOURCE >= 200112L)
50 || /* glibc >= 2.19: */ _DEFAULT_SOURCE
51 || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
53 _BSD_SOURCE || _XOPEN_SOURCE >= 500
54 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
61 bytes and places the address of the allocated memory in
63 The address of the allocated memory will be a multiple of
65 which must be a power of two and a multiple of
66 .IR "sizeof(void\ *)" .
67 This address can later be successfully passed to
76 or a unique pointer value.
82 bytes and returns a pointer to the allocated memory.
83 The memory address will be a multiple of
85 which must be a power of two.
86 .\" The behavior of memalign() for size==0 is as for posix_memalign()
87 .\" but no standards govern this.
93 except for the added restriction that
95 must be a power of two.
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.
148 For an explanation of the terms used in this section, see
154 Interface Attribute Value
158 .BR aligned_alloc (),
160 .BR posix_memalign ()
161 T} Thread safety MT-Safe
167 T} Thread safety MT-Unsafe init
174 .BR posix_memalign ()
190 .BR posix_memalign ()
192 POSIX.1d, POSIX.1-2001.
201 Documented as obsolete in 4.3BSD,
202 and as legacy in SUSv2.
208 Everybody agrees that
209 .BR posix_memalign ()
210 is declared in \fI<stdlib.h>\fP.
214 is declared in \fI<stdlib.h>\fP instead of \fI<malloc.h>\fP.
218 is declared in \fI<stdlib.h>\fP.
220 glibc declares it in \fI<malloc.h>\fP, and also in
222 if suitable feature test macros are defined (see above).
224 On many systems there are alignment restrictions, for example, on buffers
225 used for direct block device I/O.
227 .I "pathconf(path,_PC_REC_XFER_ALIGN)"
228 call that tells what alignment is needed.
230 .BR posix_memalign ()
231 to satisfy this requirement.
233 .BR posix_memalign ()
236 matches the requirements detailed above.
238 may not check that the
242 POSIX requires that memory obtained from
243 .BR posix_memalign ()
246 Some systems provide no way to reclaim memory allocated with
250 (because one can pass to
252 only a pointer obtained from
258 and then align the obtained value).
259 .\" Other systems allow passing the result of
265 The glibc implementation
266 allows memory obtained from any of these functions to be
272 always returns 8-byte aligned memory addresses, so these functions are
273 needed only if you require larger alignment values.