OSDN Git Service

(split) LDP: Update original to LDP v3.40.
[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 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" License.
24 .\"
25 .\" 2001-10-11, 2003-08-22, aeb, added some details
26 .\" 2012-03-23, Michael Kerrisk <mtk.manpages@mail.com>
27 .\"     Document pvalloc() and aligned_alloc()
28 .TH POSIX_MEMALIGN 3  2012-03-23 "GNU" "Linux Programmer's Manual"
29 .SH NAME
30 posix_memalign, aligned_alloc, memalign, valloc, pvalloc \- Allocate aligned memory
31 .SH SYNOPSIS
32 .nf
33 .B #include <stdlib.h>
34 .sp
35 .BI "int posix_memalign(void **" memptr ", size_t " alignment ", size_t " size );
36 .BI "void *aligned_alloc(size_t " alignment ", size_t " size );
37 .BI "void *valloc(size_t " size );
38 .sp
39 .B #include <malloc.h>
40 .sp
41 .BI "void *memalign(size_t " alignment ", size_t " size );
42 .BI "void *pvalloc(size_t " size );
43 .fi
44 .sp
45 .in -4n
46 Feature Test Macro Requirements for glibc (see
47 .BR feature_test_macros (7)):
48 .in
49 .sp
50 .ad l
51 .BR posix_memalign ():
52 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
53 .sp
54 .BR aligned_alloc ():
55 _ISOC11_SOURCE
56 .sp
57 .BR valloc ():
58 .br
59 .PD 0
60 .RS 4
61 .TP 4
62 Since glibc 2.12:
63 .nf
64 _BSD_SOURCE ||
65     (_XOPEN_SOURCE\ >=\ 500 ||
66         _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED) &&
67     !(_POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600)
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 .BR posix_memalign ()
98 returns either NULL,
99 .\" glibc does this:
100 or a unique pointer value that can later be successfully passed to
101 .BR free (3).
102
103 The obsolete function
104 .BR memalign ()
105 allocates
106 .I size
107 bytes and returns a pointer to the allocated memory.
108 The memory address will be a multiple of
109 .IR alignment ,
110 which must be a power of two.
111 .\" The behavior of memalign() for size==0 is as for posix_memalign()
112 .\" but no standards govern this.
113
114 The function
115 .BR aligned_alloc ()
116 is the same as
117 .BR memalign (),
118 except for the added restriction that
119 .I size
120 should be a multiple of
121 .IR alignment .
122
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 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)