posix_memalignのヘルプ・マニュアル
日本語 英語
posix_memalign --help
man posix_memalign
POSIX_MEMALIGN(3) Linux Programmer’s Manual POSIX_MEMALIGN(3)
名前
posix_memalign, memalign, valloc - アラインメントされたメモリの割り当て
を行う
書式
#include
int posix_memalign(void **memptr, size_t alignment, size_t size);
#include
void *valloc(size_t size);
void *memalign(size_t boundary, size_t size);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
posix_memalign(): _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
説明
関数 posix_memalign() は size バイトのメモリを割り当て、割り当てられ た
メモリのアドレスを *memptr に設定する。割り当てられたメモリのアドレスは
alignment の倍数になっているはずである。 alignment は 2 のべき乗で、 か
つ sizeof(void *) の倍数でなければならない。
廃 止された関数である memalign() は、 size バイトのメモリを割り当て、割
り当てられたメモリへのポインタを返す。メモリのアドレスは boundary の 倍
数になっているはずである。 boundary は 2 のべき乗でなければならない。
廃 止された関数である valloc() は size バイトのメモリを割り当て、割り当
てられたメモリへのポインタを返す。メモリのアドレスはページサイズの倍 数
に なっているはずである。これは memalign(sysconf(_SC_PAGESIZE),size) と
等価である。
3 つの関数はいずれもメモリのゼロクリアを行わない。
返り値
memalign() と valloc() は割り当てられたメモリへのポインタを返す。割り当
てに失敗した場合は NULL を返す。
posix_memalign() は成功した場合は 0 を返し、失敗した場合は次のセクショ
ンに記載されたエラー値のいずれかを返す。 errno はセットされないことに注
意すること。
エラー
EINVAL alignment 引き数が 2 のべき乗でなかったか、 sizeof(void *) の倍
数でなかった。
ENOMEM 割り当て要求を満たすのに十分なメモリがなかった。
バージョン
memalign() と valloc() はすべての Linux libc ライブラリで使用可能である
。 posix_memalign() は glibc 2.1.91 以降で使用可能である。
準拠
関数 valloc() は 3.0BSD で登場した。4.3BSD では廃止されたと記載されてお
り、 SUSv2 では過去のものだと記載されている。 POSIX.1-2001 にはもはや存
在 しない。関数 memalign() は SunOS 4.1.3 で登場したが、4.4BSD にはない
。関数 posix_memalign() は POSIX.1d に由来する。
ヘッダ
posix_memalign() の宣言を で行うことに関しては、皆の意見が一
致している。
い く つかのシステムでは、 memalign() は ではなく
で宣言されている。
SUSv2 によると、 valloc() は で宣言される。 libc4,5 や glibc
で は で宣言されており、さらに でも宣言されること
もある (具体的には以下のいずれかの場合: _GNU_SOURCE が定義されている 、
_BSD_SOURCE が定義されている、 glibc で _XOPEN_SOURCE_EXTENDED が定義さ
れている、 glibc で _XOPEN_SOURCE が 500 より小さくない値に定義されてい
る)。
注意
多 くのシステムでは、アラインメントに関して制限がある。例えば、ブロック
デバイスに対するダイレクト I/O に使用するバッファにはアラインメントに関
す る 制限がある。 POSIX では、どんなアラインメントが必要かを知るために
pathconf(path,_PC_REC_XFER_ALIGN) コールを規 定 し て い る 。 こ こ で
posix_memalign() を使うと、この必要条件を満たすことができる。
posix_memalign() は alignment が上で詳細に述べた必要条件を満たすかどう
かを確かめる。 memalign() は boundary 引き数が正しいかどうかの確認を 行
わないかもしれない。
POSIX では posix_memalign() によって獲得したメモリは free(3) を使って解
放することができる必要がある。いくつかのシステムでは memalign() や val-
loc() で割り当てられたメモリを再利用する手段が提供されていない。 (なぜ
なら free(3) に渡すことができるのは malloc(3) から受け取ったポインタ だ
けだが、例えば memalign() は malloc(3) を呼び出し、得た値をアラインメン
トしてしまうからである) glibc の実装では、ここに述べた 3 つの関数のいず
れで獲得したメモリも free(3) で再利用することができる。
glibc の malloc(3) は常に 8 バイトにアラインメントされたメモリアドレス
を返すので、ここで述べた関数が必要になるのは 8 バイトよりも大きなアライ
ンメントが必要な場合だけである。
関連項目
brk(2), getpagesize(2), free(3), malloc(3)
GNU 2007-07-26 POSIX_MEMALIGN(3)
POSIX_MEMALIGN(3) Linux Programmer’s Manual POSIX_MEMALIGN(3)
NAME
posix_memalign, memalign, valloc - Allocate aligned memory
SYNOPSIS
#include
int posix_memalign(void **memptr, size_t alignment, size_t size);
#include
void *valloc(size_t size);
void *memalign(size_t boundary, size_t size);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
posix_memalign(): _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
DESCRIPTION
The function posix_memalign() allocates size bytes and places the
address of the allocated memory in *memptr. The address of the allo-
cated memory will be a multiple of alignment, which must be a power of
two and a multiple of sizeof(void *). If size is 0, then posix_mema-
lign() returns either NULL, or a unique pointer value that can later be
successfully passed to free().
The obsolete function memalign() allocates size bytes and returns a
pointer to the allocated memory. The memory address will be a multiple
of boundary, which must be a power of two.
The obsolete function valloc() allocates size bytes and returns a
pointer to the allocated memory. The memory address will be a multiple
of the page size. It is equivalent to memalign(sysconf(_SC_PAGE-
SIZE),size).
For all three routines, the memory is not zeroed.
RETURN VALUE
memalign() and valloc() return the pointer to the allocated memory, or
NULL if the request fails.
posix_memalign() returns zero on success, or one of the error values
listed in the next section on failure. Note that errno is not set.
ERRORS
EINVAL The alignment argument was not a power of two, or was not a mul-
tiple of sizeof(void *).
ENOMEM There was insufficient memory to fulfill the allocation request.
VERSIONS
The functions memalign() and valloc() have been available in all Linux
libc libraries. The function posix_memalign() is available since glibc
2.1.91.
CONFORMING TO
The function valloc() appeared in 3.0BSD. It is documented as being
obsolete in 4.3BSD, and as legacy in SUSv2. It does not appear in
POSIX.1-2001. The function memalign() appears in SunOS 4.1.3 but not
in 4.4BSD. The function posix_memalign() comes from POSIX.1d.
Headers
Everybody agrees that posix_memalign() is declared in .
On some systems memalign() is declared in instead of .
According to SUSv2, valloc() is declared in . Libc4,5 and
glibc declare it in and perhaps also in (namely,
if _GNU_SOURCE is defined, or _BSD_SOURCE is defined, or, for glibc, if
_XOPEN_SOURCE_EXTENDED is defined, or, equivalently, _XOPEN_SOURCE is
defined to a value not less than 500).
NOTES
On many systems there are alignment restrictions, for example, on
buffers used for direct block device I/O. POSIX specifies the path-
conf(path,_PC_REC_XFER_ALIGN) call that tells what alignment is needed.
Now one can use posix_memalign() to satisfy this requirement.
posix_memalign() verifies that alignment matches the requirements
detailed above. memalign() may not check that the boundary argument is
correct.
POSIX requires that memory obtained from posix_memalign() can be freed
using free(3). Some systems provide no way to reclaim memory allocated
with memalign() or valloc() (because one can only pass to free(3) a
pointer gotten from malloc(3), while, for example, memalign() would
call malloc(3) and then align the obtained value). The glibc implemen-
tation allows memory obtained from any of these three routines to be
reclaimed with free(3).
The glibc malloc(3) always returns 8-byte aligned memory addresses, so
these routines are only needed if you require larger alignment values.
SEE ALSO
brk(2), getpagesize(2), free(3), malloc(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2009-03-30 POSIX_MEMALIGN(3)