diff options
author | Christopher Ferris <cferris@google.com> | 2014-06-13 13:57:51 -0700 |
---|---|---|
committer | Christopher Ferris <cferris@google.com> | 2014-06-18 14:23:46 -0700 |
commit | 03eebcb6e8762e668a0d3af6bb303cccb88c5b81 (patch) | |
tree | 3c5053d90eb3d1fac8c21fa390367a911e188191 | |
parent | 64dfbd242cddc3ef95576e27e3940d68b89b5fce (diff) | |
download | bionic-03eebcb6e8762e668a0d3af6bb303cccb88c5b81.zip bionic-03eebcb6e8762e668a0d3af6bb303cccb88c5b81.tar.gz bionic-03eebcb6e8762e668a0d3af6bb303cccb88c5b81.tar.bz2 |
Move common macros into bionic_macros.h.
Bug: 15590152
Change-Id: I730636613ef3653f68c5ab1d43b53beaf8e0dc25
-rw-r--r-- | libc/bionic/jemalloc_wrapper.cpp | 16 | ||||
-rw-r--r-- | libc/bionic/malloc_debug_check.cpp | 12 | ||||
-rw-r--r-- | libc/bionic/malloc_debug_leak.cpp | 14 | ||||
-rw-r--r-- | libc/bionic/malloc_debug_qemu.cpp | 10 | ||||
-rw-r--r-- | libc/bionic/pthread_create.cpp | 5 | ||||
-rw-r--r-- | libc/bionic/system_properties.cpp | 5 | ||||
-rw-r--r-- | libc/private/bionic_macros.h | 6 | ||||
-rw-r--r-- | libc/private/bionic_tls.h | 3 |
8 files changed, 42 insertions, 29 deletions
diff --git a/libc/bionic/jemalloc_wrapper.cpp b/libc/bionic/jemalloc_wrapper.cpp index 625d789..d1fe960 100644 --- a/libc/bionic/jemalloc_wrapper.cpp +++ b/libc/bionic/jemalloc_wrapper.cpp @@ -14,13 +14,19 @@ * limitations under the License. */ +#include <sys/param.h> #include <unistd.h> #include "jemalloc.h" +#include "private/bionic_macros.h" void* je_pvalloc(size_t bytes) { size_t pagesize = sysconf(_SC_PAGESIZE); - return je_memalign(pagesize, (bytes + pagesize - 1) & ~(pagesize - 1)); + size_t size = BIONIC_ALIGN(bytes, pagesize); + if (size < bytes) { + return NULL; + } + return je_memalign(pagesize, size); } #ifdef je_memalign @@ -31,11 +37,9 @@ void* je_pvalloc(size_t bytes) { // but this is not true. Both glibc and dlmalloc round up to the next power // of 2, so we'll do the same. void* je_memalign_round_up_boundary(size_t boundary, size_t size) { - unsigned int power_of_2 = static_cast<unsigned int>(boundary); - if (power_of_2 != 0) { - power_of_2 = 1UL << (sizeof(unsigned int)*8 - 1 - __builtin_clz(power_of_2)); - if (power_of_2 != boundary) { - boundary = power_of_2 << 1; + if (boundary != 0) { + if (!powerof2(boundary)) { + boundary = BIONIC_ROUND_UP_POWER_OF_2(boundary); } } else { boundary = 1; diff --git a/libc/bionic/malloc_debug_check.cpp b/libc/bionic/malloc_debug_check.cpp index faf61bf..e4e4c2e 100644 --- a/libc/bionic/malloc_debug_check.cpp +++ b/libc/bionic/malloc_debug_check.cpp @@ -38,6 +38,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/param.h> #include <sys/socket.h> #include <sys/system_properties.h> #include <sys/types.h> @@ -47,8 +48,9 @@ #include "debug_mapinfo.h" #include "debug_stacktrace.h" -#include "private/libc_logging.h" #include "malloc_debug_common.h" +#include "private/bionic_macros.h" +#include "private/libc_logging.h" #include "private/ScopedPthreadMutexLocker.h" #define MAX_BACKTRACE_DEPTH 16 @@ -350,8 +352,8 @@ extern "C" void* chk_memalign(size_t alignment, size_t bytes) { } // Make the alignment a power of two. - if (alignment & (alignment-1)) { - alignment = 1L << (31 - __builtin_clz(alignment)); + if (!powerof2(alignment)) { + alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment); } // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes @@ -526,7 +528,7 @@ extern "C" struct mallinfo chk_mallinfo() { } extern "C" int chk_posix_memalign(void** memptr, size_t alignment, size_t size) { - if ((alignment & (alignment - 1)) != 0) { + if (!powerof2(alignment)) { return EINVAL; } int saved_errno = errno; @@ -537,7 +539,7 @@ extern "C" int chk_posix_memalign(void** memptr, size_t alignment, size_t size) extern "C" void* chk_pvalloc(size_t bytes) { size_t pagesize = sysconf(_SC_PAGESIZE); - size_t size = (bytes + pagesize - 1) & ~(pagesize - 1); + size_t size = BIONIC_ALIGN(bytes, pagesize); if (size < bytes) { // Overflow return NULL; } diff --git a/libc/bionic/malloc_debug_leak.cpp b/libc/bionic/malloc_debug_leak.cpp index 2cc38cc..308d40b 100644 --- a/libc/bionic/malloc_debug_leak.cpp +++ b/libc/bionic/malloc_debug_leak.cpp @@ -37,6 +37,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/param.h> #include <sys/select.h> #include <sys/socket.h> #include <sys/system_properties.h> @@ -48,6 +49,7 @@ #include "debug_stacktrace.h" #include "malloc_debug_common.h" +#include "private/bionic_macros.h" #include "private/libc_logging.h" #include "private/ScopedPthreadMutexLocker.h" @@ -255,7 +257,7 @@ extern "C" struct mallinfo fill_mallinfo() { } extern "C" int fill_posix_memalign(void** memptr, size_t alignment, size_t size) { - if ((alignment & (alignment - 1)) != 0) { + if (!powerof2(alignment)) { return EINVAL; } int saved_errno = errno; @@ -266,7 +268,7 @@ extern "C" int fill_posix_memalign(void** memptr, size_t alignment, size_t size) extern "C" void* fill_pvalloc(size_t bytes) { size_t pagesize = sysconf(_SC_PAGESIZE); - size_t size = (bytes + pagesize - 1) & ~(pagesize - 1); + size_t size = BIONIC_ALIGN(bytes, pagesize); if (size < bytes) { // Overflow return NULL; } @@ -401,8 +403,8 @@ extern "C" void* leak_memalign(size_t alignment, size_t bytes) { } // need to make sure it's a power of two - if (alignment & (alignment-1)) { - alignment = 1L << (31 - __builtin_clz(alignment)); + if (!powerof2(alignment)) { + alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment); } // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes @@ -464,7 +466,7 @@ extern "C" struct mallinfo leak_mallinfo() { } extern "C" int leak_posix_memalign(void** memptr, size_t alignment, size_t size) { - if ((alignment & (alignment - 1)) != 0) { + if (!powerof2(alignment)) { return EINVAL; } int saved_errno = errno; @@ -475,7 +477,7 @@ extern "C" int leak_posix_memalign(void** memptr, size_t alignment, size_t size) extern "C" void* leak_pvalloc(size_t bytes) { size_t pagesize = sysconf(_SC_PAGESIZE); - size_t size = (bytes + pagesize - 1) & ~(pagesize - 1); + size_t size = BIONIC_ALIGN(bytes, pagesize); if (size < bytes) { // Overflow return NULL; } diff --git a/libc/bionic/malloc_debug_qemu.cpp b/libc/bionic/malloc_debug_qemu.cpp index 9b3bb55..fd5161a 100644 --- a/libc/bionic/malloc_debug_qemu.cpp +++ b/libc/bionic/malloc_debug_qemu.cpp @@ -47,11 +47,13 @@ #include <stdio.h> #include <fcntl.h> #include <sys/mman.h> +#include <sys/param.h> #include <pthread.h> #include <unistd.h> #include <errno.h> -#include "private/libc_logging.h" #include "malloc_debug_common.h" +#include "private/bionic_macros.h" +#include "private/libc_logging.h" /* This file should be included into the build only when * MALLOC_QEMU_INSTRUMENT macro is defined. */ @@ -970,8 +972,8 @@ extern "C" void* qemu_instrumented_memalign(size_t alignment, size_t bytes) { // size. if (alignment < DEFAULT_PREFIX_SIZE) { alignment = DEFAULT_PREFIX_SIZE; - } else if (alignment & (alignment - 1)) { - alignment = 1L << (31 - __builtin_clz(alignment)); + } else if (!powerof2(alignment)) { + alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment); } desc.prefix_size = alignment; desc.requested_bytes = bytes; @@ -1047,7 +1049,7 @@ extern "C" int qemu_instrumented_posix_memalign(void** memptr, size_t alignment, extern "C" void* qemu_instrumented_pvalloc(size_t bytes) { size_t pagesize = sysconf(_SC_PAGESIZE); - size_t size = (bytes + pagesize - 1) & ~(pagesize - 1); + size_t size = BIONIC_ALIGN(bytes, pagesize); if (size < bytes) { // Overflow qemu_error_log("<libc_pid=%03u, pid=%03u> pvalloc(%zu): overflow (%zu).", malloc_pid, getpid(), bytes, size); diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp index c4cb262..c83b5a0 100644 --- a/libc/bionic/pthread_create.cpp +++ b/libc/bionic/pthread_create.cpp @@ -33,6 +33,7 @@ #include "pthread_internal.h" +#include "private/bionic_macros.h" #include "private/bionic_ssp.h" #include "private/bionic_tls.h" #include "private/libc_logging.h" @@ -183,8 +184,8 @@ int pthread_create(pthread_t* thread_out, pthread_attr_t const* attr, } // Make sure the stack size and guard size are multiples of PAGE_SIZE. - thread->attr.stack_size = (thread->attr.stack_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1); - thread->attr.guard_size = (thread->attr.guard_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1); + thread->attr.stack_size = BIONIC_ALIGN(thread->attr.stack_size, PAGE_SIZE); + thread->attr.guard_size = BIONIC_ALIGN(thread->attr.guard_size, PAGE_SIZE); if (thread->attr.stack_base == NULL) { // The caller didn't provide a stack, so allocate one. diff --git a/libc/bionic/system_properties.cpp b/libc/bionic/system_properties.cpp index 7618586..a564c39 100644 --- a/libc/bionic/system_properties.cpp +++ b/libc/bionic/system_properties.cpp @@ -55,9 +55,6 @@ #include "private/bionic_futex.h" #include "private/bionic_macros.h" -#define ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1)) - - static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME; @@ -301,7 +298,7 @@ static int map_prop_area() static void *allocate_obj(const size_t size, uint32_t *const off) { prop_area *pa = __system_property_area__; - const size_t aligned = ALIGN(size, sizeof(uint32_t)); + const size_t aligned = BIONIC_ALIGN(size, sizeof(uint32_t)); if (pa->bytes_used + aligned > pa_data_size) { return NULL; } diff --git a/libc/private/bionic_macros.h b/libc/private/bionic_macros.h index 34da501..a3a6985 100644 --- a/libc/private/bionic_macros.h +++ b/libc/private/bionic_macros.h @@ -33,4 +33,10 @@ TypeName(); \ DISALLOW_COPY_AND_ASSIGN(TypeName) +#define BIONIC_ALIGN(value, alignment) \ + (((value) + (alignment) - 1) & ~((alignment) - 1)) + +#define BIONIC_ROUND_UP_POWER_OF_2(value) \ + (1UL << (sizeof(value) * 8 - 1 - __builtin_clz(value))) + #endif // _BIONIC_MACROS_H_ diff --git a/libc/private/bionic_tls.h b/libc/private/bionic_tls.h index b52013f..56a61be 100644 --- a/libc/private/bionic_tls.h +++ b/libc/private/bionic_tls.h @@ -31,6 +31,7 @@ #include <sys/cdefs.h> #include <sys/limits.h> +#include "bionic_macros.h" #include "__get_tls.h" __BEGIN_DECLS @@ -84,8 +85,6 @@ enum { #define BIONIC_TLS_RESERVED_SLOTS GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT #endif -#define BIONIC_ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1)) - /* * Maximum number of elements in the TLS array. * This includes space for pthread keys and our own internal slots. |