]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/posix_memalign.3
Import of man-pages 1.70
[thirdparty/man-pages.git] / man3 / posix_memalign.3
1 .\" (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 .TH POSIX_MEMALIGN 3 2003-08-22 "GNU" "Linux Programmer's Manual"
27 .SH NAME
28 posix_memalign, memalign, valloc \- Allocate aligned memory
29 .SH SYNOPSIS
30 .nf
31 .B #define _XOPEN_SOURCE 600
32 .B #include <stdlib.h>
33 .sp
34 .BI "int posix_memalign(void **" memptr ", size_t " alignment ", size_t " size );
35 .sp
36 .B #include <malloc.h>
37 .sp
38 .BI "void *valloc(size_t " size );
39 .BI "void *memalign(size_t " boundary ", size_t " size );
40 .nl
41 .fi
42 .SH DESCRIPTION
43 The function
44 .B posix_memalign()
45 allocates
46 .I size
47 bytes and places the address of the allocated memory in
48 .IR "*memptr".
49 The address of the allocated memory will be a multiple of
50 .IR "alignment",
51 which must be a power of two and a multiple of
52 .IR "sizeof(void *)".
53
54 The obsolete function
55 .B memalign()
56 allocates
57 .I size
58 bytes and returns a pointer to the allocated memory.
59 The memory address will be a multiple of
60 .IR "boundary",
61 which must be a power of two.
62
63 The obsolete function
64 .B valloc()
65 allocates
66 .I size
67 bytes and returns a pointer to the allocated memory.
68 The memory address will be a multiple of the page size.
69 It is equivalent to
70 .IR "memalign(sysconf(_SC_PAGESIZE),size)" .
71
72 For all three routines, the memory is not zeroed.
73
74 .SH "RETURN VALUE"
75 .BR memalign()
76 and
77 .BR valloc()
78 return the pointer to the allocated memory, or
79 .B NULL
80 if the request fails.
81
82 .BR posix_memalign()
83 returns zero on success, or one of the error values listed in the
84 next section on failure. Note that
85 .IR errno
86 is not set.
87
88 .SH "ERRORS"
89 .TP
90 .B EINVAL
91 The
92 .IR alignment
93 parameter was not a power of two, or was not a multiple of
94 .IR "sizeof(void *)" .
95 .TP
96 .B ENOMEM
97 There was insufficient memory to fulfill the allocation request.
98
99 .SH NOTES
100 On many systems there are alignment restrictions, e.g. on buffers
101 used for direct block device I/O. POSIX specifies the
102 .I "pathconf(path,_PC_REC_XFER_ALIGN)"
103 call that tells what alignment is needed. Now one can use
104 .BR posix_memalign ()
105 to satisfy this requirement.
106
107 .BR posix_memalign ()
108 verifies that
109 .IR alignment
110 matches the requirements detailed above.
111 .BR memalign()
112 may not check that the
113 .IR boundary
114 parameter is correct.
115
116 POSIX requires that memory obtained from
117 .BR posix_memalign()
118 can be freed using
119 .IR free ().
120 Some systems provide no way to reclaim memory allocated with
121 .IR memalign ()
122 or
123 .IR valloc ()
124 (because one can only pass to
125 .IR free ()
126 a pointer gotten from
127 .IR malloc (),
128 while e.g.
129 .IR memalign ()
130 would call
131 .IR malloc ()
132 and then align the obtained value).
133 .\" Other systems allow passing the result of
134 .\" .IR valloc ()
135 .\" to
136 .\" .IR free (),
137 .\" but not to
138 .\" .IR realloc ().
139 GNU libc allows memory obtained from any of these three routines to be
140 reclaimed with
141 .IR free ().
142
143 GNU libc
144 .BR "malloc()"
145 always returns 8-byte aligned memory addresses, so these routines are only
146 needed if you require larger alignment values.
147
148 .SH AVAILABILITY
149 The functions
150 .IR memalign ()
151 and
152 .IR valloc ()
153 have been available in all Linux libc libraries.
154 The function
155 .IR posix_memalign ()
156 is available since glibc 2.1.91.
157
158 .SH "CONFORMING TO"
159 The function
160 .IR valloc ()
161 appeared in 3.0 BSD. It is documented as being obsolete in BSD 4.3,
162 and as legacy in SUSv2. It no longer occurs in SUSv3.
163 The function
164 .IR memalign ()
165 appears in SunOS 4.1.3 but not in BSD 4.4.
166 The function
167 .IR posix_memalign ()
168 comes from POSIX 1003.1d.
169
170 .SH HEADERS
171 Everybody agrees that
172 .IR posix_memalign ()
173 is declared in <stdlib.h>. In order to declare it, glibc needs
174 _GNU_SOURCE defined, or _XOPEN_SOURCE defined to a value not less than 600.
175
176 Everybody agrees that
177 .IR memalign ()
178 is declared in <malloc.h>.
179
180 According to SUSv2,
181 .IR valloc ()
182 is declared in <stdlib.h>.
183 Libc4,5 and glibc declare it in <malloc.h> and perhaps also in <stdlib.h>
184 (namely, if _GNU_SOURCE is defined, or _BSD_SOURCE is defined, or,
185 for glibc, if _XOPEN_SOURCE_EXTENDED is defined, or, equivalently,
186 _XOPEN_SOURCE is defined to a value not less than 500).
187
188 .SH "SEE ALSO"
189 .BR brk (2),
190 .BR getpagesize (2),
191 .BR free (3),
192 .BR malloc (3)