1POSIX_MEMALIGN(3)          Linux Programmer's Manual         POSIX_MEMALIGN(3)
2
3
4

NAME

6       posix_memalign,  aligned_alloc,  memalign,  valloc,  pvalloc - allocate
7       aligned memory
8

SYNOPSIS

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
24
25       aligned_alloc(): _ISOC11_SOURCE
26
27       valloc():
28           Since glibc 2.12:
29               (_XOPEN_SOURCE >= 500) && !(_POSIX_C_SOURCE >= 200112L)
30                   || /* Glibc since 2.19: */ _DEFAULT_SOURCE
31                   || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
32           Before glibc 2.12:
33               _BSD_SOURCE || _XOPEN_SOURCE >= 500
34               (The (nonstandard) header file <malloc.h> also exposes the dec‐
35               laration of valloc(); no feature test macros are required.)
36

DESCRIPTION

38       The  function  posix_memalign()  allocates  size  bytes  and places the
39       address of the allocated memory in *memptr.  The address of  the  allo‐
40       cated  memory will be a multiple of alignment, which must be a power of
41       two and a multiple of sizeof(void *).  If size is  0,  then  the  value
42       placed  in  *memptr  is either NULL, or a unique pointer value that can
43       later be successfully passed to free(3).
44
45       The obsolete function memalign() allocates size  bytes  and  returns  a
46       pointer to the allocated memory.  The memory address will be a multiple
47       of alignment, which must be a power of two.
48
49       The function aligned_alloc() is the same as memalign(), except for  the
50       added restriction that size should be a multiple of alignment.
51
52       The  obsolete  function  valloc()  allocates  size  bytes and returns a
53       pointer to the allocated memory.  The memory address will be a multiple
54       of  the  page  size.   It  is  equivalent to memalign(sysconf(_SC_PAGE‐
55       SIZE),size).
56
57       The obsolete function pvalloc() is similar to valloc(), but rounds  the
58       size of the allocation up to the next multiple of the system page size.
59
60       For all of these functions, the memory is not zeroed.
61

RETURN VALUE

63       aligned_alloc(),  memalign(),  valloc(), and pvalloc() return a pointer
64       to the allocated memory, or NULL if the request fails.
65
66       posix_memalign() returns zero on success, or one of  the  error  values
67       listed  in the next section on failure.  The value of errno is not set.
68       On Linux (and other systems), posix_memalign() does not  modify  memptr
69       on  failure.   A  requirement  standardizing this behavior was added in
70       POSIX.1-2016.
71

ERRORS

73       EINVAL The alignment argument was not a power of two, or was not a mul‐
74              tiple of sizeof(void *).
75
76       ENOMEM There was insufficient memory to fulfill the allocation request.
77

VERSIONS

79       The  functions  memalign(), valloc(), and pvalloc() have been available
80       in all Linux libc libraries.
81
82       The function aligned_alloc() was added to glibc in version 2.16.
83
84       The function posix_memalign() is available since glibc 2.1.91.
85

ATTRIBUTES

87       For  an  explanation  of  the  terms  used   in   this   section,   see
88       attributes(7).
89
90       ┌─────────────────┬───────────────┬────────────────┐
91Interface        Attribute     Value          
92       ├─────────────────┼───────────────┼────────────────┤
93aligned_alloc(), │ Thread safety │ MT-Safe        │
94memalign(),      │               │                │
95posix_memalign() │               │                │
96       ├─────────────────┼───────────────┼────────────────┤
97valloc(),        │ Thread safety │ MT-Unsafe init │
98pvalloc()        │               │                │
99       └─────────────────┴───────────────┴────────────────┘
100

CONFORMING TO

102       The  function  valloc()  appeared in 3.0BSD.  It is documented as being
103       obsolete in 4.3BSD, and as legacy in SUSv2.   It  does  not  appear  in
104       POSIX.1.
105
106       The function pvalloc() is a GNU extension.
107
108       The function memalign() appears in SunOS 4.1.3 but not in 4.4BSD.
109
110       The  function  posix_memalign() comes from POSIX.1d and is specified in
111       POSIX.1-2001 and POSIX.1-2008.
112
113       The function aligned_alloc() is specified in the C11 standard.
114
115   Headers
116       Everybody agrees that posix_memalign() is declared in <stdlib.h>.
117
118       On some systems memalign() is declared in <stdlib.h> instead  of  <mal‐
119       loc.h>.
120
121       According  to  SUSv2,  valloc() is declared in <stdlib.h>.  Libc4,5 and
122       glibc declare it in <malloc.h>, and also in <stdlib.h> if suitable fea‐
123       ture test macros are defined (see above).
124

NOTES

126       On  many systems there are alignment restrictions, for example, on buf‐
127       fers used for direct block  device  I/O.   POSIX  specifies  the  path‐
128       conf(path,_PC_REC_XFER_ALIGN) call that tells what alignment is needed.
129       Now one can use posix_memalign() to satisfy this requirement.
130
131       posix_memalign()  verifies  that  alignment  matches  the  requirements
132       detailed  above.   memalign() may not check that the alignment argument
133       is correct.
134
135       POSIX requires that memory obtained from posix_memalign() can be  freed
136       using free(3).  Some systems provide no way to reclaim memory allocated
137       with memalign() or valloc() (because one can pass  to  free(3)  only  a
138       pointer  obtained  from malloc(3), while, for example, memalign() would
139       call malloc(3) and then align the obtained value).  The glibc implemen‐
140       tation  allows  memory  obtained  from  any  of  these  functions to be
141       reclaimed with free(3).
142
143       The glibc malloc(3) always returns 8-byte aligned memory addresses,  so
144       these functions are needed only if you require larger alignment values.
145

SEE ALSO

147       brk(2), getpagesize(2), free(3), malloc(3)
148

COLOPHON

150       This  page  is  part of release 4.16 of the Linux man-pages project.  A
151       description of the project, information about reporting bugs,  and  the
152       latest     version     of     this    page,    can    be    found    at
153       https://www.kernel.org/doc/man-pages/.
154
155
156
157GNU                               2017-09-15                 POSIX_MEMALIGN(3)
Impressum