]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/posix_memalign.3
stdarg.3: SEE ALSO: add vprintf(3), vscanf(3), vsyslog(3)
[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 2020-04-11 "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 on success.
148 On error, NULL is returned, and \fIerrno\fP is set
149 to indicate the cause of the error.
150 .PP
151 .BR posix_memalign ()
152 returns zero on success, or one of the error values listed in the
153 next section on failure.
154 The value of
155 .I errno
156 is not set.
157 On Linux (and other systems),
158 .BR posix_memalign ()
159 does not modify
160 .I memptr
161 on failure.
162 A requirement standardizing this behavior was added in POSIX.1-2016.
163 .\" http://austingroupbugs.net/view.php?id=520
164 .SH ERRORS
165 .TP
166 .B EINVAL
167 The
168 .I alignment
169 argument was not a power of two, or was not a multiple of
170 .IR "sizeof(void\ *)" .
171 .TP
172 .B ENOMEM
173 There was insufficient memory to fulfill the allocation request.
174 .SH VERSIONS
175 The functions
176 .BR memalign (),
177 .BR valloc (),
178 and
179 .BR pvalloc ()
180 have been available since at least glibc 2.0.
181 .PP
182 The function
183 .BR aligned_alloc ()
184 was added to glibc in version 2.16.
185 .PP
186 The function
187 .BR posix_memalign ()
188 is available since glibc 2.1.91.
189 .SH ATTRIBUTES
190 For an explanation of the terms used in this section, see
191 .BR attributes (7).
192 .TS
193 allbox;
194 lb lb lb
195 l l l.
196 Interface Attribute Value
197 T{
198 .BR aligned_alloc (),
199 .br
200 .BR memalign (),
201 .br
202 .BR posix_memalign ()
203 T} Thread safety MT-Safe
204 T{
205 .BR valloc (),
206 .br
207 .BR pvalloc ()
208 T} Thread safety MT-Unsafe init
209 .TE
210 .sp 1
211 .SH CONFORMING TO
212 The function
213 .BR valloc ()
214 appeared in 3.0BSD.
215 It is documented as being obsolete in 4.3BSD,
216 and as legacy in SUSv2.
217 It does not appear in POSIX.1.
218 .PP
219 The function
220 .BR pvalloc ()
221 is a GNU extension.
222 .PP
223 The function
224 .BR memalign ()
225 appears in SunOS 4.1.3 but not in 4.4BSD.
226 .PP
227 The function
228 .BR posix_memalign ()
229 comes from POSIX.1d and is specified in POSIX.1-2001 and POSIX.1-2008.
230 .PP
231 The function
232 .BR aligned_alloc ()
233 is specified in the C11 standard.
234 .\"
235 .SS Headers
236 Everybody agrees that
237 .BR posix_memalign ()
238 is declared in \fI<stdlib.h>\fP.
239 .PP
240 On some systems
241 .BR memalign ()
242 is declared in \fI<stdlib.h>\fP instead of \fI<malloc.h>\fP.
243 .PP
244 According to SUSv2,
245 .BR valloc ()
246 is declared in \fI<stdlib.h>\fP.
247 Libc4,5 and glibc declare it in \fI<malloc.h>\fP, and also in
248 \fI<stdlib.h>\fP
249 if suitable feature test macros are defined (see above).
250 .SH NOTES
251 On many systems there are alignment restrictions, for example, on buffers
252 used for direct block device I/O.
253 POSIX specifies the
254 .I "pathconf(path,_PC_REC_XFER_ALIGN)"
255 call that tells what alignment is needed.
256 Now one can use
257 .BR posix_memalign ()
258 to satisfy this requirement.
259 .PP
260 .BR posix_memalign ()
261 verifies that
262 .I alignment
263 matches the requirements detailed above.
264 .BR memalign ()
265 may not check that the
266 .I alignment
267 argument is correct.
268 .PP
269 POSIX requires that memory obtained from
270 .BR posix_memalign ()
271 can be freed using
272 .BR free (3).
273 Some systems provide no way to reclaim memory allocated with
274 .BR memalign ()
275 or
276 .BR valloc ()
277 (because one can pass to
278 .BR free (3)
279 only a pointer obtained from
280 .BR malloc (3),
281 while, for example,
282 .BR memalign ()
283 would call
284 .BR malloc (3)
285 and then align the obtained value).
286 .\" Other systems allow passing the result of
287 .\" .IR valloc ()
288 .\" to
289 .\" .IR free (3),
290 .\" but not to
291 .\" .IR realloc (3).
292 The glibc implementation
293 allows memory obtained from any of these functions to be
294 reclaimed with
295 .BR free (3).
296 .PP
297 The glibc
298 .BR malloc (3)
299 always returns 8-byte aligned memory addresses, so these functions are
300 needed only if you require larger alignment values.
301 .SH SEE ALSO
302 .BR brk (2),
303 .BR getpagesize (2),
304 .BR free (3),
305 .BR malloc (3)