]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/posix_memalign.3
membarrier.2: Remove redundant mention of return value of MEMBARRIER_CMD_SHARED
[thirdparty/man-pages.git] / man3 / posix_memalign.3
1 .\" Copyright (c) 2001 by John Levon <moz@compsoc.man.ac.uk>
2 .\" Based in part on GNU libc documentation.
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" 2001-10-11, 2003-08-22, aeb, added some details
27 .\" 2012-03-23, Michael Kerrisk <mtk.manpages@mail.com>
28 .\" Document pvalloc() and aligned_alloc()
29 .TH POSIX_MEMALIGN 3 2015-08-08 "GNU" "Linux Programmer's Manual"
30 .SH NAME
31 posix_memalign, aligned_alloc, memalign, valloc, pvalloc \- allocate aligned memory
32 .SH SYNOPSIS
33 .nf
34 .B #include <stdlib.h>
35 .sp
36 .BI "int posix_memalign(void **" memptr ", size_t " alignment ", size_t " size );
37 .BI "void *aligned_alloc(size_t " alignment ", size_t " size );
38 .BI "void *valloc(size_t " size );
39 .sp
40 .B #include <malloc.h>
41 .sp
42 .BI "void *memalign(size_t " alignment ", size_t " size );
43 .BI "void *pvalloc(size_t " size );
44 .fi
45 .sp
46 .in -4n
47 Feature Test Macro Requirements for glibc (see
48 .BR feature_test_macros (7)):
49 .in
50 .sp
51 .ad l
52 .BR posix_memalign ():
53 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
54 .sp
55 .BR aligned_alloc ():
56 _ISOC11_SOURCE
57 .sp
58 .BR valloc ():
59 .br
60 .PD 0
61 .RS 4
62 .TP 4
63 Since glibc 2.12:
64 .nf
65 _BSD_SOURCE ||
66 (_XOPEN_SOURCE\ >=\ 500 ||
67 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED) &&
68 !(_POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600)
69 .br
70 .fi
71 .TP
72 Before glibc 2.12:
73 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
74 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
75 .ad b
76 .br
77 (The (nonstandard) header file
78 .I <malloc.h>
79 also exposes the declaration of
80 .BR valloc ();
81 no feature test macros are required.)
82 .RE
83 .PD
84 .SH DESCRIPTION
85 The function
86 .BR posix_memalign ()
87 allocates
88 .I size
89 bytes and places the address of the allocated memory in
90 .IR "*memptr" .
91 The address of the allocated memory will be a multiple of
92 .IR "alignment" ,
93 which must be a power of two and a multiple of
94 .IR "sizeof(void\ *)" .
95 If
96 .I size
97 is 0, then
98 the value placed in
99 .IR "*memptr"
100 is either NULL,
101 .\" glibc does this:
102 or a unique pointer value that can later be successfully passed to
103 .BR free (3).
104
105 The obsolete function
106 .BR memalign ()
107 allocates
108 .I size
109 bytes and returns a pointer to the allocated memory.
110 The memory address will be a multiple of
111 .IR alignment ,
112 which must be a power of two.
113 .\" The behavior of memalign() for size==0 is as for posix_memalign()
114 .\" but no standards govern this.
115
116 The function
117 .BR aligned_alloc ()
118 is the same as
119 .BR memalign (),
120 except for the added restriction that
121 .I size
122 should be a multiple of
123 .IR alignment .
124
125 The obsolete function
126 .BR valloc ()
127 allocates
128 .I size
129 bytes and returns a pointer to the allocated memory.
130 The memory address will be a multiple of the page size.
131 It is equivalent to
132 .IR "memalign(sysconf(_SC_PAGESIZE),size)" .
133
134 The obsolete function
135 .BR pvalloc ()
136 is similar to
137 .BR valloc (),
138 but rounds the size of the allocation up to
139 the next multiple of the system page size.
140
141 For all of these functions, the memory is not zeroed.
142 .SH RETURN VALUE
143 .BR aligned_alloc (),
144 .BR memalign (),
145 .BR valloc (),
146 and
147 .BR pvalloc ()
148 return a pointer to the allocated memory, or NULL if the request fails.
149
150 .BR posix_memalign ()
151 returns zero on success, or one of the error values listed in the
152 next section on failure.
153 The value of
154 .I errno
155 is indeterminate after a call to
156 .BR posix_memalign ().
157 .SH ERRORS
158 .TP
159 .B EINVAL
160 The
161 .I alignment
162 argument was not a power of two, or was not a multiple of
163 .IR "sizeof(void\ *)" .
164 .TP
165 .B ENOMEM
166 There was insufficient memory to fulfill the allocation request.
167 .SH VERSIONS
168 The functions
169 .BR memalign (),
170 .BR valloc (),
171 and
172 .BR pvalloc ()
173 have been available in all Linux libc libraries.
174
175 The function
176 .BR aligned_alloc ()
177 was added to glibc in version 2.16.
178
179 The function
180 .BR posix_memalign ()
181 is available since glibc 2.1.91.
182 .SH ATTRIBUTES
183 For an explanation of the terms used in this section, see
184 .BR attributes (7).
185 .TS
186 allbox;
187 lb lb lb
188 l l l.
189 Interface Attribute Value
190 T{
191 .BR aligned_alloc (),
192 .br
193 .BR memalign (),
194 .br
195 .BR posix_memalign ()
196 T} Thread safety MT-Safe
197 T{
198 .BR valloc (),
199 .br
200 .BR pvalloc ()
201 T} Thread safety MT-Unsafe init
202 .TE
203
204 .SH CONFORMING TO
205 The function
206 .BR valloc ()
207 appeared in 3.0BSD.
208 It is documented as being obsolete in 4.3BSD,
209 and as legacy in SUSv2.
210 It does not appear in POSIX.1.
211
212 The function
213 .BR pvalloc ()
214 is a GNU extension.
215
216 The function
217 .BR memalign ()
218 appears in SunOS 4.1.3 but not in 4.4BSD.
219
220 The function
221 .BR posix_memalign ()
222 comes from POSIX.1d and is specified in POSIX.1-2001 and POSIX.1-2008.
223
224 The function
225 .BR aligned_alloc ()
226 is specified in the C11 standard.
227 .\"
228 .SS Headers
229 Everybody agrees that
230 .BR posix_memalign ()
231 is declared in \fI<stdlib.h>\fP.
232
233 On some systems
234 .BR memalign ()
235 is declared in \fI<stdlib.h>\fP instead of \fI<malloc.h>\fP.
236
237 According to SUSv2,
238 .BR valloc ()
239 is declared in \fI<stdlib.h>\fP.
240 Libc4,5 and glibc declare it in \fI<malloc.h>\fP, and also in
241 \fI<stdlib.h>\fP
242 if suitable feature test macros are defined (see above).
243 .SH NOTES
244 On many systems there are alignment restrictions, for example, on buffers
245 used for direct block device I/O.
246 POSIX specifies the
247 .I "pathconf(path,_PC_REC_XFER_ALIGN)"
248 call that tells what alignment is needed.
249 Now one can use
250 .BR posix_memalign ()
251 to satisfy this requirement.
252
253 .BR posix_memalign ()
254 verifies that
255 .I alignment
256 matches the requirements detailed above.
257 .BR memalign ()
258 may not check that the
259 .I alignment
260 argument is correct.
261
262 POSIX requires that memory obtained from
263 .BR posix_memalign ()
264 can be freed using
265 .BR free (3).
266 Some systems provide no way to reclaim memory allocated with
267 .BR memalign ()
268 or
269 .BR valloc ()
270 (because one can pass to
271 .BR free (3)
272 only a pointer obtained from
273 .BR malloc (3),
274 while, for example,
275 .BR memalign ()
276 would call
277 .BR malloc (3)
278 and then align the obtained value).
279 .\" Other systems allow passing the result of
280 .\" .IR valloc ()
281 .\" to
282 .\" .IR free (3),
283 .\" but not to
284 .\" .IR realloc (3).
285 The glibc implementation
286 allows memory obtained from any of these functions to be
287 reclaimed with
288 .BR free (3).
289
290 The glibc
291 .BR malloc (3)
292 always returns 8-byte aligned memory addresses, so these functions are
293 needed only if you require larger alignment values.
294 .SH SEE ALSO
295 .BR brk (2),
296 .BR getpagesize (2),
297 .BR free (3),
298 .BR malloc (3)