]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/posix_memalign.3
share/mk/: distcheck: Run 'check' after 'build'
[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 2017-09-15 "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 .PP
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
40 .B #include <malloc.h>
41 .PP
42 .BI "void *memalign(size_t " alignment ", size_t " size );
43 .BI "void *pvalloc(size_t " size );
44 .fi
45 .PP
46 .in -4n
47 Feature Test Macro Requirements for glibc (see
48 .BR feature_test_macros (7)):
49 .in
50 .PP
51 .ad l
52 .BR posix_memalign ():
53 _POSIX_C_SOURCE\ >=\ 200112L
54 .PP
55 .BR aligned_alloc ():
56 _ISOC11_SOURCE
57 .PP
58 .BR valloc ():
59 .br
60 .PD 0
61 .RS 4
62 .TP 4
63 Since glibc 2.12:
64 .nf
65 (_XOPEN_SOURCE\ >=\ 500) && !(_POSIX_C_SOURCE\ >=\ 200112L)
66 || /* Glibc since 2.19: */ _DEFAULT_SOURCE
67 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
68 .br
69 .fi
70 .TP
71 Before glibc 2.12:
72 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
73 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
74 .ad b
75 .br
76 (The (nonstandard) header file
77 .I <malloc.h>
78 also exposes the declaration of
79 .BR valloc ();
80 no feature test macros are required.)
81 .RE
82 .PD
83 .SH DESCRIPTION
84 The function
85 .BR posix_memalign ()
86 allocates
87 .I size
88 bytes and places the address of the allocated memory in
89 .IR "*memptr" .
90 The address of the allocated memory will be a multiple of
91 .IR "alignment" ,
92 which must be a power of two and a multiple of
93 .IR "sizeof(void\ *)" .
94 If
95 .I size
96 is 0, then
97 the value placed in
98 .IR "*memptr"
99 is either NULL,
100 .\" glibc does this:
101 or a unique pointer value that can later be successfully passed to
102 .BR free (3).
103 .PP
104 The obsolete function
105 .BR memalign ()
106 allocates
107 .I size
108 bytes and returns a pointer to the allocated memory.
109 The memory address will be a multiple of
110 .IR alignment ,
111 which must be a power of two.
112 .\" The behavior of memalign() for size==0 is as for posix_memalign()
113 .\" but no standards govern this.
114 .PP
115 The function
116 .BR aligned_alloc ()
117 is the same as
118 .BR memalign (),
119 except for the added restriction that
120 .I size
121 should be a multiple of
122 .IR alignment .
123 .PP
124 The obsolete function
125 .BR valloc ()
126 allocates
127 .I size
128 bytes and returns a pointer to the allocated memory.
129 The memory address will be a multiple of the page size.
130 It is equivalent to
131 .IR "memalign(sysconf(_SC_PAGESIZE),size)" .
132 .PP
133 The obsolete function
134 .BR pvalloc ()
135 is similar to
136 .BR valloc (),
137 but rounds the size of the allocation up to
138 the next multiple of the system page size.
139 .PP
140 For all of these functions, the memory is not zeroed.
141 .SH RETURN VALUE
142 .BR aligned_alloc (),
143 .BR memalign (),
144 .BR valloc (),
145 and
146 .BR pvalloc ()
147 return a pointer to the allocated memory, or NULL if the request fails.
148 .PP
149 .BR posix_memalign ()
150 returns zero on success, or one of the error values listed in the
151 next section on failure.
152 The value of
153 .I errno
154 is not set.
155 On Linux (and other systems),
156 .BR posix_memalign ()
157 does not modify
158 .I memptr
159 on failure.
160 A requirement standardizing this behavior was added in POSIX.1-2016.
161 .\" http://austingroupbugs.net/view.php?id=520
162 .SH ERRORS
163 .TP
164 .B EINVAL
165 The
166 .I alignment
167 argument was not a power of two, or was not a multiple of
168 .IR "sizeof(void\ *)" .
169 .TP
170 .B ENOMEM
171 There was insufficient memory to fulfill the allocation request.
172 .SH VERSIONS
173 The functions
174 .BR memalign (),
175 .BR valloc (),
176 and
177 .BR pvalloc ()
178 have been available in all Linux libc libraries.
179 .PP
180 The function
181 .BR aligned_alloc ()
182 was added to glibc in version 2.16.
183 .PP
184 The function
185 .BR posix_memalign ()
186 is available since glibc 2.1.91.
187 .SH ATTRIBUTES
188 For an explanation of the terms used in this section, see
189 .BR attributes (7).
190 .TS
191 allbox;
192 lb lb lb
193 l l l.
194 Interface Attribute Value
195 T{
196 .BR aligned_alloc (),
197 .br
198 .BR memalign (),
199 .br
200 .BR posix_memalign ()
201 T} Thread safety MT-Safe
202 T{
203 .BR valloc (),
204 .br
205 .BR pvalloc ()
206 T} Thread safety MT-Unsafe init
207 .TE
208 .sp 1
209 .SH CONFORMING TO
210 The function
211 .BR valloc ()
212 appeared in 3.0BSD.
213 It is documented as being obsolete in 4.3BSD,
214 and as legacy in SUSv2.
215 It does not appear in POSIX.1.
216 .PP
217 The function
218 .BR pvalloc ()
219 is a GNU extension.
220 .PP
221 The function
222 .BR memalign ()
223 appears in SunOS 4.1.3 but not in 4.4BSD.
224 .PP
225 The function
226 .BR posix_memalign ()
227 comes from POSIX.1d and is specified in POSIX.1-2001 and POSIX.1-2008.
228 .PP
229 The function
230 .BR aligned_alloc ()
231 is specified in the C11 standard.
232 .\"
233 .SS Headers
234 Everybody agrees that
235 .BR posix_memalign ()
236 is declared in \fI<stdlib.h>\fP.
237 .PP
238 On some systems
239 .BR memalign ()
240 is declared in \fI<stdlib.h>\fP instead of \fI<malloc.h>\fP.
241 .PP
242 According to SUSv2,
243 .BR valloc ()
244 is declared in \fI<stdlib.h>\fP.
245 Libc4,5 and glibc declare it in \fI<malloc.h>\fP, and also in
246 \fI<stdlib.h>\fP
247 if suitable feature test macros are defined (see above).
248 .SH NOTES
249 On many systems there are alignment restrictions, for example, on buffers
250 used for direct block device I/O.
251 POSIX specifies the
252 .I "pathconf(path,_PC_REC_XFER_ALIGN)"
253 call that tells what alignment is needed.
254 Now one can use
255 .BR posix_memalign ()
256 to satisfy this requirement.
257 .PP
258 .BR posix_memalign ()
259 verifies that
260 .I alignment
261 matches the requirements detailed above.
262 .BR memalign ()
263 may not check that the
264 .I alignment
265 argument is correct.
266 .PP
267 POSIX requires that memory obtained from
268 .BR posix_memalign ()
269 can be freed using
270 .BR free (3).
271 Some systems provide no way to reclaim memory allocated with
272 .BR memalign ()
273 or
274 .BR valloc ()
275 (because one can pass to
276 .BR free (3)
277 only a pointer obtained from
278 .BR malloc (3),
279 while, for example,
280 .BR memalign ()
281 would call
282 .BR malloc (3)
283 and then align the obtained value).
284 .\" Other systems allow passing the result of
285 .\" .IR valloc ()
286 .\" to
287 .\" .IR free (3),
288 .\" but not to
289 .\" .IR realloc (3).
290 The glibc implementation
291 allows memory obtained from any of these functions to be
292 reclaimed with
293 .BR free (3).
294 .PP
295 The glibc
296 .BR malloc (3)
297 always returns 8-byte aligned memory addresses, so these functions are
298 needed only if you require larger alignment values.
299 .SH SEE ALSO
300 .BR brk (2),
301 .BR getpagesize (2),
302 .BR free (3),
303 .BR malloc (3)