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 2009-03-30 "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
55 bytes and places the address of the allocated memory in
57 The address of the allocated memory will be a multiple of
59 which must be a power of two and a multiple of
67 or a unique pointer value that can later be successfully passed to
74 bytes and returns a pointer to the allocated memory.
75 The memory address will be a multiple of
77 which must be a power of two.
78 .\" The behavior of memalign() for size==0 is as for posix_memalign()
79 .\" but no standards govern this.
85 bytes and returns a pointer to the allocated memory.
86 The memory address will be a multiple of the page size.
88 .IR "memalign(sysconf(_SC_PAGESIZE),size)" .
90 For all three routines, the memory is not zeroed.
95 return the pointer to the allocated memory, or NULL if the request fails.
98 returns zero on success, or one of the error values listed in the
99 next section on failure.
108 argument was not a power of two, or was not a multiple of
109 .IR "sizeof(void *)" .
112 There was insufficient memory to fulfill the allocation request.
118 have been available in all Linux libc libraries.
120 .BR posix_memalign ()
121 is available since glibc 2.1.91.
126 It is documented as being obsolete in 4.3BSD,
127 and as legacy in SUSv2.
128 It does not appear in POSIX.1-2001.
131 appears in SunOS 4.1.3 but not in 4.4BSD.
133 .BR posix_memalign ()
136 Everybody agrees that
137 .BR posix_memalign ()
138 is declared in \fI<stdlib.h>\fP.
142 is declared in \fI<stdlib.h>\fP instead of \fI<malloc.h>\fP.
146 is declared in \fI<stdlib.h>\fP.
147 Libc4,5 and glibc declare it in \fI<malloc.h>\fP and perhaps also in
155 .B _XOPEN_SOURCE_EXTENDED
156 is defined, or, equivalently,
158 is defined to a value not less than 500).
160 On many systems there are alignment restrictions, for example, on buffers
161 used for direct block device I/O.
163 .I "pathconf(path,_PC_REC_XFER_ALIGN)"
164 call that tells what alignment is needed.
166 .BR posix_memalign ()
167 to satisfy this requirement.
169 .BR posix_memalign ()
172 matches the requirements detailed above.
174 may not check that the
178 POSIX requires that memory obtained from
179 .BR posix_memalign ()
182 Some systems provide no way to reclaim memory allocated with
186 (because one can only pass to
188 a pointer gotten from
194 and then align the obtained value).
195 .\" Other systems allow passing the result of
201 The glibc implementation
202 allows memory obtained from any of these three routines to be
208 always returns 8-byte aligned memory addresses, so these routines are only
209 needed if you require larger alignment values.