OSDN Git Service

(split) LDP: Update original to LDP v3.50.
[linuxjm/LDP_man-pages.git] / original / 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  2012-03-23 "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 .BR posix_memalign ()
99 returns either NULL,
100 .\" glibc does this:
101 or a unique pointer value that can later be successfully passed to
102 .BR free (3).
103
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
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
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
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
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
149 .BR posix_memalign ()
150 returns zero on success, or one of the error values listed in the
151 next section on failure.
152 Note that
153 .I errno
154 is not set.
155 .SH ERRORS
156 .TP
157 .B EINVAL
158 The
159 .I alignment
160 argument was not a power of two, or was not a multiple of
161 .IR "sizeof(void *)" .
162 .TP
163 .B ENOMEM
164 There was insufficient memory to fulfill the allocation request.
165 .SH VERSIONS
166 The functions
167 .BR memalign (),
168 .BR valloc (),
169 and
170 .BR pvalloc ()
171 have been available in all Linux libc libraries.
172
173 The function
174 .BR aligned_alloc ()
175 was added to glibc in version 2.16.
176
177 The function
178 .BR posix_memalign ()
179 is available since glibc 2.1.91.
180 .SH CONFORMING TO
181 The function
182 .BR valloc ()
183 appeared in 3.0BSD.
184 It is documented as being obsolete in 4.3BSD,
185 and as legacy in SUSv2.
186 It does not appear in POSIX.1-2001.
187
188 The function
189 .BR pvalloc ()
190 is a GNU extension.
191
192 The function
193 .BR memalign ()
194 appears in SunOS 4.1.3 but not in 4.4BSD.
195
196 The function
197 .BR posix_memalign ()
198 comes from POSIX.1d.
199
200 The function
201 .BR aligned_alloc ()
202 is specified in the C11 standard.
203 .\"
204 .SS Headers
205 Everybody agrees that
206 .BR posix_memalign ()
207 is declared in \fI<stdlib.h>\fP.
208
209 On some systems
210 .BR memalign ()
211 is declared in \fI<stdlib.h>\fP instead of \fI<malloc.h>\fP.
212
213 According to SUSv2,
214 .BR valloc ()
215 is declared in \fI<stdlib.h>\fP.
216 Libc4,5 and glibc declare it in \fI<malloc.h>\fP, and also in
217 \fI<stdlib.h>\fP
218 if suitable feature test macros are defined (see above).
219 .SH NOTES
220 On many systems there are alignment restrictions, for example, on buffers
221 used for direct block device I/O.
222 POSIX specifies the
223 .I "pathconf(path,_PC_REC_XFER_ALIGN)"
224 call that tells what alignment is needed.
225 Now one can use
226 .BR posix_memalign ()
227 to satisfy this requirement.
228
229 .BR posix_memalign ()
230 verifies that
231 .I alignment
232 matches the requirements detailed above.
233 .BR memalign ()
234 may not check that the
235 .I alignment
236 argument is correct.
237
238 POSIX requires that memory obtained from
239 .BR posix_memalign ()
240 can be freed using
241 .BR free (3).
242 Some systems provide no way to reclaim memory allocated with
243 .BR memalign ()
244 or
245 .BR valloc ()
246 (because one can only pass to
247 .BR free (3)
248 a pointer gotten from
249 .BR malloc (3),
250 while, for example,
251 .BR memalign ()
252 would call
253 .BR malloc (3)
254 and then align the obtained value).
255 .\" Other systems allow passing the result of
256 .\" .IR valloc ()
257 .\" to
258 .\" .IR free (3),
259 .\" but not to
260 .\" .IR realloc (3).
261 The glibc implementation
262 allows memory obtained from any of these functions to be
263 reclaimed with
264 .BR free (3).
265
266 The glibc
267 .BR malloc (3)
268 always returns 8-byte aligned memory addresses, so these functions are only
269 needed if you require larger alignment values.
270 .SH SEE ALSO
271 .BR brk (2),
272 .BR getpagesize (2),
273 .BR free (3),
274 .BR malloc (3)