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