1POSIX_MEMALIGN(3) Linux Programmer's Manual POSIX_MEMALIGN(3)
2
3
4
6 posix_memalign, memalign, valloc - Allocate aligned memory
7
9 #include <stdlib.h>
10
11 int posix_memalign(void **memptr, size_t alignment, size_t size);
12
13 #include <malloc.h>
14
15 void *valloc(size_t size);
16 void *memalign(size_t boundary, size_t size);
17
18 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20 posix_memalign(): _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
21
23 The function posix_memalign() allocates size bytes and places the
24 address of the allocated memory in *memptr. The address of the allo‐
25 cated memory will be a multiple of alignment, which must be a power of
26 two and a multiple of sizeof(void *). If size is 0, then posix_mema‐
27 lign() returns either NULL, or a unique pointer value that can later be
28 successfully passed to free().
29
30 The obsolete function memalign() allocates size bytes and returns a
31 pointer to the allocated memory. The memory address will be a multiple
32 of boundary, which must be a power of two.
33
34 The obsolete function valloc() allocates size bytes and returns a
35 pointer to the allocated memory. The memory address will be a multiple
36 of the page size. It is equivalent to memalign(sysconf(_SC_PAGE‐
37 SIZE),size).
38
39 For all three routines, the memory is not zeroed.
40
42 memalign() and valloc() return the pointer to the allocated memory, or
43 NULL if the request fails.
44
45 posix_memalign() returns zero on success, or one of the error values
46 listed in the next section on failure. Note that errno is not set.
47
49 EINVAL The alignment argument was not a power of two, or was not a mul‐
50 tiple of sizeof(void *).
51
52 ENOMEM There was insufficient memory to fulfill the allocation request.
53
55 The functions memalign() and valloc() have been available in all Linux
56 libc libraries. The function posix_memalign() is available since glibc
57 2.1.91.
58
60 The function valloc() appeared in 3.0BSD. It is documented as being
61 obsolete in 4.3BSD, and as legacy in SUSv2. It does not appear in
62 POSIX.1-2001. The function memalign() appears in SunOS 4.1.3 but not
63 in 4.4BSD. The function posix_memalign() comes from POSIX.1d.
64
65 Headers
66 Everybody agrees that posix_memalign() is declared in <stdlib.h>.
67
68 On some systems memalign() is declared in <stdlib.h> instead of <mal‐
69 loc.h>.
70
71 According to SUSv2, valloc() is declared in <stdlib.h>. Libc4,5 and
72 glibc declare it in <malloc.h> and perhaps also in <stdlib.h> (namely,
73 if _GNU_SOURCE is defined, or _BSD_SOURCE is defined, or, for glibc, if
74 _XOPEN_SOURCE_EXTENDED is defined, or, equivalently, _XOPEN_SOURCE is
75 defined to a value not less than 500).
76
78 On many systems there are alignment restrictions, for example, on buf‐
79 fers used for direct block device I/O. POSIX specifies the path‐
80 conf(path,_PC_REC_XFER_ALIGN) call that tells what alignment is needed.
81 Now one can use posix_memalign() to satisfy this requirement.
82
83 posix_memalign() verifies that alignment matches the requirements
84 detailed above. memalign() may not check that the boundary argument is
85 correct.
86
87 POSIX requires that memory obtained from posix_memalign() can be freed
88 using free(3). Some systems provide no way to reclaim memory allocated
89 with memalign() or valloc() (because one can only pass to free(3) a
90 pointer gotten from malloc(3), while, for example, memalign() would
91 call malloc(3) and then align the obtained value). The glibc implemen‐
92 tation allows memory obtained from any of these three routines to be
93 reclaimed with free(3).
94
95 The glibc malloc(3) always returns 8-byte aligned memory addresses, so
96 these routines are only needed if you require larger alignment values.
97
99 brk(2), getpagesize(2), free(3), malloc(3)
100
102 This page is part of release 3.22 of the Linux man-pages project. A
103 description of the project, and information about reporting bugs, can
104 be found at http://www.kernel.org/doc/man-pages/.
105
106
107
108GNU 2009-03-30 POSIX_MEMALIGN(3)