1 .\" Copyright (c) 2001 by John Levon <moz@compsoc.man.ac.uk>
2 .\" Based in part on GNU libc documentation.
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" 2001-10-11, 2003-08-22, aeb, added some details
26 .\" 2012-03-23, Michael Kerrisk <mtk.manpages@mail.com>
27 .\" Document pvalloc() and aligned_alloc()
28 .TH POSIX_MEMALIGN 3 2012-03-23 "GNU" "Linux Programmer's Manual"
30 posix_memalign, aligned_alloc, memalign, valloc, pvalloc \- Allocate aligned memory
33 .B #include <stdlib.h>
35 .BI "int posix_memalign(void **" memptr ", size_t " alignment ", size_t " size );
36 .BI "void *aligned_alloc(size_t " alignment ", size_t " size );
37 .BI "void *valloc(size_t " size );
39 .B #include <malloc.h>
41 .BI "void *memalign(size_t " alignment ", size_t " size );
42 .BI "void *pvalloc(size_t " size );
46 Feature Test Macro Requirements for glibc (see
47 .BR feature_test_macros (7)):
51 .BR posix_memalign ():
52 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
65 (_XOPEN_SOURCE\ >=\ 500 ||
66 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED) &&
67 !(_POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600)
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
100 or a unique pointer value that can later be successfully passed to
103 The obsolete function
107 bytes and returns a pointer to the allocated memory.
108 The memory address will be a multiple of
110 which must be a power of two.
111 .\" The behavior of memalign() for size==0 is as for posix_memalign()
112 .\" but no standards govern this.
118 except for the added restriction that
120 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, or NULL if the request fails.
149 .BR posix_memalign ()
150 returns zero on success, or one of the error values listed in the
151 next section on failure.
160 argument was not a power of two, or was not a multiple of
161 .IR "sizeof(void *)" .
164 There was insufficient memory to fulfill the allocation request.
171 have been available in all Linux libc libraries.
175 was added to glibc in version 2.16.
178 .BR posix_memalign ()
179 is available since glibc 2.1.91.
184 It is documented as being obsolete in 4.3BSD,
185 and as legacy in SUSv2.
186 It does not appear in POSIX.1-2001.
194 appears in SunOS 4.1.3 but not in 4.4BSD.
197 .BR posix_memalign ()
202 is specified in the C11 standard.
205 Everybody agrees that
206 .BR posix_memalign ()
207 is declared in \fI<stdlib.h>\fP.
211 is declared in \fI<stdlib.h>\fP instead of \fI<malloc.h>\fP.
215 is declared in \fI<stdlib.h>\fP.
216 Libc4,5 and glibc declare it in \fI<malloc.h>\fP, and also in
218 if suitable feature test macros are defined (see above).
220 On many systems there are alignment restrictions, for example, on buffers
221 used for direct block device I/O.
223 .I "pathconf(path,_PC_REC_XFER_ALIGN)"
224 call that tells what alignment is needed.
226 .BR posix_memalign ()
227 to satisfy this requirement.
229 .BR posix_memalign ()
232 matches the requirements detailed above.
234 may not check that the
238 POSIX requires that memory obtained from
239 .BR posix_memalign ()
242 Some systems provide no way to reclaim memory allocated with
246 (because one can only pass to
248 a pointer gotten from
254 and then align the obtained value).
255 .\" Other systems allow passing the result of
261 The glibc implementation
262 allows memory obtained from any of these these functions to be
268 always returns 8-byte aligned memory addresses, so these functions are only
269 needed if you require larger alignment values.