1POSIX_MEMALIGN(3) Linux Programmer's Manual POSIX_MEMALIGN(3)
2
3
4
6 posix_memalign, aligned_alloc, memalign, valloc, pvalloc - allocate
7 aligned memory
8
10 #include <stdlib.h>
11
12 int posix_memalign(void **memptr, size_t alignment, size_t size);
13 void *aligned_alloc(size_t alignment, size_t size);
14 void *valloc(size_t size);
15
16 #include <malloc.h>
17
18 void *memalign(size_t alignment, size_t size);
19 void *pvalloc(size_t size);
20
21 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
22
23 posix_memalign(): _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
24
25 aligned_alloc(): _ISOC11_SOURCE
26
27 valloc():
28 Since glibc 2.12:
29 _BSD_SOURCE ||
30 (_XOPEN_SOURCE >= 500 ||
31 _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) &&
32 !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
33 Before glibc 2.12:
34 _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
35 _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
36 (The (nonstandard) header file <malloc.h> also exposes the dec‐
37 laration of valloc(); no feature test macros are required.)
38
40 The function posix_memalign() allocates size bytes and places the
41 address of the allocated memory in *memptr. The address of the allo‐
42 cated memory will be a multiple of alignment, which must be a power of
43 two and a multiple of sizeof(void *). If size is 0, then posix_mema‐
44 lign() returns either NULL, or a unique pointer value that can later be
45 successfully passed to free(3).
46
47 The obsolete function memalign() allocates size bytes and returns a
48 pointer to the allocated memory. The memory address will be a multiple
49 of alignment, which must be a power of two.
50
51 The function aligned_alloc() is the same as memalign(), except for the
52 added restriction that size should be a multiple of alignment.
53
54 The obsolete function valloc() allocates size bytes and returns a
55 pointer to the allocated memory. The memory address will be a multiple
56 of the page size. It is equivalent to memalign(sysconf(_SC_PAGE‐
57 SIZE),size).
58
59 The obsolete function pvalloc() is similar to valloc(), but rounds the
60 size of the allocation up to the next multiple of the system page size.
61
62 For all of these functions, the memory is not zeroed.
63
65 aligned_alloc(), memalign(), valloc(), and pvalloc() return a pointer
66 to the allocated memory, or NULL if the request fails.
67
68 posix_memalign() returns zero on success, or one of the error values
69 listed in the next section on failure. Note that errno is not set.
70
72 EINVAL The alignment argument was not a power of two, or was not a mul‐
73 tiple of sizeof(void *).
74
75 ENOMEM There was insufficient memory to fulfill the allocation request.
76
78 The functions memalign(), valloc(), and pvalloc() have been available
79 in all Linux libc libraries.
80
81 The function aligned_alloc() was added to glibc in version 2.16.
82
83 The function posix_memalign() is available since glibc 2.1.91.
84
86 The function valloc() appeared in 3.0BSD. It is documented as being
87 obsolete in 4.3BSD, and as legacy in SUSv2. It does not appear in
88 POSIX.1-2001.
89
90 The function pvalloc() is a GNU extension.
91
92 The function memalign() appears in SunOS 4.1.3 but not in 4.4BSD.
93
94 The function posix_memalign() comes from POSIX.1d.
95
96 The function aligned_alloc() is specified in the C11 standard.
97
98 Headers
99 Everybody agrees that posix_memalign() is declared in <stdlib.h>.
100
101 On some systems memalign() is declared in <stdlib.h> instead of <mal‐
102 loc.h>.
103
104 According to SUSv2, valloc() is declared in <stdlib.h>. Libc4,5 and
105 glibc declare it in <malloc.h>, and also in <stdlib.h> if suitable fea‐
106 ture test macros are defined (see above).
107
109 On many systems there are alignment restrictions, for example, on buf‐
110 fers used for direct block device I/O. POSIX specifies the path‐
111 conf(path,_PC_REC_XFER_ALIGN) call that tells what alignment is needed.
112 Now one can use posix_memalign() to satisfy this requirement.
113
114 posix_memalign() verifies that alignment matches the requirements
115 detailed above. memalign() may not check that the alignment argument
116 is correct.
117
118 POSIX requires that memory obtained from posix_memalign() can be freed
119 using free(3). Some systems provide no way to reclaim memory allocated
120 with memalign() or valloc() (because one can pass to free(3) only a
121 pointer obtained from malloc(3), while, for example, memalign() would
122 call malloc(3) and then align the obtained value). The glibc implemen‐
123 tation allows memory obtained from any of these functions to be
124 reclaimed with free(3).
125
126 The glibc malloc(3) always returns 8-byte aligned memory addresses, so
127 these functions are needed only if you require larger alignment values.
128
130 brk(2), getpagesize(2), free(3), malloc(3)
131
133 This page is part of release 3.53 of the Linux man-pages project. A
134 description of the project, and information about reporting bugs, can
135 be found at http://www.kernel.org/doc/man-pages/.
136
137
138
139GNU 2012-03-23 POSIX_MEMALIGN(3)