diff options
author | Ken Sumrall <ksumrall@android.com> | 2011-12-14 20:50:01 -0800 |
---|---|---|
committer | Ken Sumrall <ksumrall@android.com> | 2011-12-14 20:55:43 -0800 |
commit | 85aad909560508410101c18c6ecc6633df39c596 (patch) | |
tree | 1da341473c2090fa35afc5d22351603bd49ac79c /libc/bionic | |
parent | a5cb76bca00b1ce44a04687fb179809c12ea9cd3 (diff) | |
download | bionic-85aad909560508410101c18c6ecc6633df39c596.zip bionic-85aad909560508410101c18c6ecc6633df39c596.tar.gz bionic-85aad909560508410101c18c6ecc6633df39c596.tar.bz2 |
Add the posix_memalign(3) function to bionic
The posix_memalign(3) function is very similar to the traditional
memalign(3) function, but with better error reporting and a guarantee
that the memory it allocates can be freed. In bionic, memalign(3)
allocated memory can be freed, so posix_memalign(3) is just a wrapper
around memalign(3).
Change-Id: I62ee908aa5ba6b887d8446a00d8298d080a6a299
Diffstat (limited to 'libc/bionic')
-rw-r--r-- | libc/bionic/dlmalloc.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/libc/bionic/dlmalloc.c b/libc/bionic/dlmalloc.c index 8c75e9c..496cd1c 100644 --- a/libc/bionic/dlmalloc.c +++ b/libc/bionic/dlmalloc.c @@ -774,6 +774,22 @@ void* dlrealloc(void*, size_t); void* dlmemalign(size_t, size_t); /* + int posix_memalign(void **memptr, size_t alignment, size_t size); + Places a pointer to a newly allocated chunk of size bytes, aligned + in accord with the alignment argument, in *memptr. + + The return value is 0 on success, and ENOMEM on failure. + + The alignment argument should be a power of two. If the argument is + not a power of two, the nearest greater power is used. + 8-byte alignment is guaranteed by normal malloc calls, so don't + bother calling memalign with an argument of 8 or less. + + Overreliance on posix_memalign is a sure way to fragment space. +*/ +int posix_memalign(void **memptr, size_t alignment, size_t size); + +/* valloc(size_t n); Equivalent to memalign(pagesize, n), where pagesize is the page size of the system. If the pagesize is unknown, 4096 is used. @@ -4507,6 +4523,18 @@ void* dlmemalign(size_t alignment, size_t bytes) { return internal_memalign(gm, alignment, bytes); } +int posix_memalign(void **memptr, size_t alignment, size_t size) { + int ret = 0; + + *memptr = dlmemalign(alignment, size); + + if (*memptr == 0) { + ret = ENOMEM; + } + + return ret; +} + void** dlindependent_calloc(size_t n_elements, size_t elem_size, void* chunks[]) { size_t sz = elem_size; /* serves as 1-element array */ |