1 .\" (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 2003-08-22 "GNU" "Linux Programmer's Manual"
28 posix_memalign, memalign, valloc \- Allocate aligned memory
31 .B #define _XOPEN_SOURCE 600
32 .B #include <stdlib.h>
34 .BI "int posix_memalign(void **" memptr ", size_t " alignment ", size_t " size );
36 .B #include <malloc.h>
38 .BI "void *valloc(size_t " size );
39 .BI "void *memalign(size_t " boundary ", size_t " size );
47 bytes and places the address of the allocated memory in
49 The address of the allocated memory will be a multiple of
51 which must be a power of two and a multiple of
58 bytes and returns a pointer to the allocated memory.
59 The memory address will be a multiple of
61 which must be a power of two.
67 bytes and returns a pointer to the allocated memory.
68 The memory address will be a multiple of the page size.
70 .IR "memalign(sysconf(_SC_PAGESIZE),size)" .
72 For all three routines, the memory is not zeroed.
78 return the pointer to the allocated memory, or
83 returns zero on success, or one of the error values listed in the
84 next section on failure. Note that
93 parameter was not a power of two, or was not a multiple of
94 .IR "sizeof(void *)" .
97 There was insufficient memory to fulfill the allocation request.
100 On many systems there are alignment restrictions, e.g. on buffers
101 used for direct block device I/O. POSIX specifies the
102 .I "pathconf(path,_PC_REC_XFER_ALIGN)"
103 call that tells what alignment is needed. Now one can use
104 .BR posix_memalign ()
105 to satisfy this requirement.
107 .BR posix_memalign ()
110 matches the requirements detailed above.
112 may not check that the
114 parameter is correct.
116 POSIX requires that memory obtained from
120 Some systems provide no way to reclaim memory allocated with
124 (because one can only pass to
126 a pointer gotten from
132 and then align the obtained value).
133 .\" Other systems allow passing the result of
139 GNU libc allows memory obtained from any of these three routines to be
145 always returns 8-byte aligned memory addresses, so these routines are only
146 needed if you require larger alignment values.
153 have been available in all Linux libc libraries.
155 .IR posix_memalign ()
156 is available since glibc 2.1.91.
161 appeared in 3.0 BSD. It is documented as being obsolete in BSD 4.3,
162 and as legacy in SUSv2. It no longer occurs in SUSv3.
165 appears in SunOS 4.1.3 but not in BSD 4.4.
167 .IR posix_memalign ()
168 comes from POSIX 1003.1d.
171 Everybody agrees that
172 .IR posix_memalign ()
173 is declared in <stdlib.h>. In order to declare it, glibc needs
174 _GNU_SOURCE defined, or _XOPEN_SOURCE defined to a value not less than 600.
176 Everybody agrees that
178 is declared in <malloc.h>.
182 is declared in <stdlib.h>.
183 Libc4,5 and glibc declare it in <malloc.h> and perhaps also in <stdlib.h>
184 (namely, if _GNU_SOURCE is defined, or _BSD_SOURCE is defined, or,
185 for glibc, if _XOPEN_SOURCE_EXTENDED is defined, or, equivalently,
186 _XOPEN_SOURCE is defined to a value not less than 500).