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