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 .TH POSIX_MEMALIGN 3 2010-09-20 "GNU" "Linux Programmer's Manual"
28 posix_memalign, memalign, valloc \- Allocate aligned memory
31 .B #include <stdlib.h>
33 .BI "int posix_memalign(void **" memptr ", size_t " alignment ", size_t " size );
35 .B #include <malloc.h>
37 .BI "void *valloc(size_t " size );
38 .BI "void *memalign(size_t " boundary ", size_t " size );
42 Feature Test Macro Requirements for glibc (see
43 .BR feature_test_macros (7)):
47 .BR posix_memalign ():
48 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
58 (_XOPEN_SOURCE\ >=\ 500 ||
59 _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) &&
60 !(_POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600)
65 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
66 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
75 bytes and places the address of the allocated memory in
77 The address of the allocated memory will be a multiple of
79 which must be a power of two and a multiple of
87 or a unique pointer value that can later be successfully passed to
94 bytes and returns a pointer to the allocated memory.
95 The memory address will be a multiple of
97 which must be a power of two.
98 .\" The behavior of memalign() for size==0 is as for posix_memalign()
99 .\" but no standards govern this.
101 The obsolete function
105 bytes and returns a pointer to the allocated memory.
106 The memory address will be a multiple of the page size.
108 .IR "memalign(sysconf(_SC_PAGESIZE),size)" .
110 For all three routines, the memory is not zeroed.
115 return the pointer to the allocated memory, or NULL if the request fails.
117 .BR posix_memalign ()
118 returns zero on success, or one of the error values listed in the
119 next section on failure.
128 argument was not a power of two, or was not a multiple of
129 .IR "sizeof(void *)" .
132 There was insufficient memory to fulfill the allocation request.
138 have been available in all Linux libc libraries.
140 .BR posix_memalign ()
141 is available since glibc 2.1.91.
146 It is documented as being obsolete in 4.3BSD,
147 and as legacy in SUSv2.
148 It does not appear in POSIX.1-2001.
151 appears in SunOS 4.1.3 but not in 4.4BSD.
153 .BR posix_memalign ()
156 Everybody agrees that
157 .BR posix_memalign ()
158 is declared in \fI<stdlib.h>\fP.
162 is declared in \fI<stdlib.h>\fP instead of \fI<malloc.h>\fP.
166 is declared in \fI<stdlib.h>\fP.
167 Libc4,5 and glibc declare it in \fI<malloc.h>\fP and perhaps also in
175 .B _XOPEN_SOURCE_EXTENDED
176 is defined, or, equivalently,
178 is defined to a value not less than 500).
180 On many systems there are alignment restrictions, for example, on buffers
181 used for direct block device I/O.
183 .I "pathconf(path,_PC_REC_XFER_ALIGN)"
184 call that tells what alignment is needed.
186 .BR posix_memalign ()
187 to satisfy this requirement.
189 .BR posix_memalign ()
192 matches the requirements detailed above.
194 may not check that the
198 POSIX requires that memory obtained from
199 .BR posix_memalign ()
202 Some systems provide no way to reclaim memory allocated with
206 (because one can only pass to
208 a pointer gotten from
214 and then align the obtained value).
215 .\" Other systems allow passing the result of
221 The glibc implementation
222 allows memory obtained from any of these three routines to be
228 always returns 8-byte aligned memory addresses, so these routines are only
229 needed if you require larger alignment values.