am 0b09153c: Merge "Move common macros into bionic_macros.h."

* commit '0b09153c12915c6d17434cf9fed2389f6fe4e713':
  Move common macros into bionic_macros.h.
This commit is contained in:
Christopher Ferris 2014-06-18 22:58:29 +00:00 committed by Android Git Automerger
commit 79cd486d54
8 changed files with 42 additions and 29 deletions

View File

@ -14,13 +14,19 @@
* limitations under the License. * limitations under the License.
*/ */
#include <sys/param.h>
#include <unistd.h> #include <unistd.h>
#include "jemalloc.h" #include "jemalloc.h"
#include "private/bionic_macros.h"
void* je_pvalloc(size_t bytes) { void* je_pvalloc(size_t bytes) {
size_t pagesize = sysconf(_SC_PAGESIZE); 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 #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 // but this is not true. Both glibc and dlmalloc round up to the next power
// of 2, so we'll do the same. // of 2, so we'll do the same.
void* je_memalign_round_up_boundary(size_t boundary, size_t size) { void* je_memalign_round_up_boundary(size_t boundary, size_t size) {
unsigned int power_of_2 = static_cast<unsigned int>(boundary); if (boundary != 0) {
if (power_of_2 != 0) { if (!powerof2(boundary)) {
power_of_2 = 1UL << (sizeof(unsigned int)*8 - 1 - __builtin_clz(power_of_2)); boundary = BIONIC_ROUND_UP_POWER_OF_2(boundary);
if (power_of_2 != boundary) {
boundary = power_of_2 << 1;
} }
} else { } else {
boundary = 1; boundary = 1;

View File

@ -38,6 +38,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/param.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/system_properties.h> #include <sys/system_properties.h>
#include <sys/types.h> #include <sys/types.h>
@ -47,8 +48,9 @@
#include "debug_mapinfo.h" #include "debug_mapinfo.h"
#include "debug_stacktrace.h" #include "debug_stacktrace.h"
#include "private/libc_logging.h"
#include "malloc_debug_common.h" #include "malloc_debug_common.h"
#include "private/bionic_macros.h"
#include "private/libc_logging.h"
#include "private/ScopedPthreadMutexLocker.h" #include "private/ScopedPthreadMutexLocker.h"
#define MAX_BACKTRACE_DEPTH 16 #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. // Make the alignment a power of two.
if (alignment & (alignment-1)) { if (!powerof2(alignment)) {
alignment = 1L << (31 - __builtin_clz(alignment)); alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
} }
// here, alignment is at least MALLOC_ALIGNMENT<<1 bytes // 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) { extern "C" int chk_posix_memalign(void** memptr, size_t alignment, size_t size) {
if ((alignment & (alignment - 1)) != 0) { if (!powerof2(alignment)) {
return EINVAL; return EINVAL;
} }
int saved_errno = errno; 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) { extern "C" void* chk_pvalloc(size_t bytes) {
size_t pagesize = sysconf(_SC_PAGESIZE); 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 if (size < bytes) { // Overflow
return NULL; return NULL;
} }

View File

@ -37,6 +37,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/param.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/system_properties.h> #include <sys/system_properties.h>
@ -48,6 +49,7 @@
#include "debug_stacktrace.h" #include "debug_stacktrace.h"
#include "malloc_debug_common.h" #include "malloc_debug_common.h"
#include "private/bionic_macros.h"
#include "private/libc_logging.h" #include "private/libc_logging.h"
#include "private/ScopedPthreadMutexLocker.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) { extern "C" int fill_posix_memalign(void** memptr, size_t alignment, size_t size) {
if ((alignment & (alignment - 1)) != 0) { if (!powerof2(alignment)) {
return EINVAL; return EINVAL;
} }
int saved_errno = errno; 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) { extern "C" void* fill_pvalloc(size_t bytes) {
size_t pagesize = sysconf(_SC_PAGESIZE); 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 if (size < bytes) { // Overflow
return NULL; 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 // need to make sure it's a power of two
if (alignment & (alignment-1)) { if (!powerof2(alignment)) {
alignment = 1L << (31 - __builtin_clz(alignment)); alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
} }
// here, alignment is at least MALLOC_ALIGNMENT<<1 bytes // 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) { extern "C" int leak_posix_memalign(void** memptr, size_t alignment, size_t size) {
if ((alignment & (alignment - 1)) != 0) { if (!powerof2(alignment)) {
return EINVAL; return EINVAL;
} }
int saved_errno = errno; 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) { extern "C" void* leak_pvalloc(size_t bytes) {
size_t pagesize = sysconf(_SC_PAGESIZE); 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 if (size < bytes) { // Overflow
return NULL; return NULL;
} }

View File

@ -47,11 +47,13 @@
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/param.h>
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include "private/libc_logging.h"
#include "malloc_debug_common.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 /* This file should be included into the build only when
* MALLOC_QEMU_INSTRUMENT macro is defined. */ * MALLOC_QEMU_INSTRUMENT macro is defined. */
@ -970,8 +972,8 @@ extern "C" void* qemu_instrumented_memalign(size_t alignment, size_t bytes) {
// size. // size.
if (alignment < DEFAULT_PREFIX_SIZE) { if (alignment < DEFAULT_PREFIX_SIZE) {
alignment = DEFAULT_PREFIX_SIZE; alignment = DEFAULT_PREFIX_SIZE;
} else if (alignment & (alignment - 1)) { } else if (!powerof2(alignment)) {
alignment = 1L << (31 - __builtin_clz(alignment)); alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
} }
desc.prefix_size = alignment; desc.prefix_size = alignment;
desc.requested_bytes = bytes; 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) { extern "C" void* qemu_instrumented_pvalloc(size_t bytes) {
size_t pagesize = sysconf(_SC_PAGESIZE); 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 if (size < bytes) { // Overflow
qemu_error_log("<libc_pid=%03u, pid=%03u> pvalloc(%zu): overflow (%zu).", qemu_error_log("<libc_pid=%03u, pid=%03u> pvalloc(%zu): overflow (%zu).",
malloc_pid, getpid(), bytes, size); malloc_pid, getpid(), bytes, size);

View File

@ -33,6 +33,7 @@
#include "pthread_internal.h" #include "pthread_internal.h"
#include "private/bionic_macros.h"
#include "private/bionic_ssp.h" #include "private/bionic_ssp.h"
#include "private/bionic_tls.h" #include "private/bionic_tls.h"
#include "private/libc_logging.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. // 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.stack_size = BIONIC_ALIGN(thread->attr.stack_size, PAGE_SIZE);
thread->attr.guard_size = (thread->attr.guard_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1); thread->attr.guard_size = BIONIC_ALIGN(thread->attr.guard_size, PAGE_SIZE);
if (thread->attr.stack_base == NULL) { if (thread->attr.stack_base == NULL) {
// The caller didn't provide a stack, so allocate one. // The caller didn't provide a stack, so allocate one.

View File

@ -55,9 +55,6 @@
#include "private/bionic_futex.h" #include "private/bionic_futex.h"
#include "private/bionic_macros.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; 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) static void *allocate_obj(const size_t size, uint32_t *const off)
{ {
prop_area *pa = __system_property_area__; 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) { if (pa->bytes_used + aligned > pa_data_size) {
return NULL; return NULL;
} }

View File

@ -33,4 +33,10 @@
TypeName(); \ TypeName(); \
DISALLOW_COPY_AND_ASSIGN(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_ #endif // _BIONIC_MACROS_H_

View File

@ -31,6 +31,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#include <sys/limits.h> #include <sys/limits.h>
#include "bionic_macros.h"
#include "__get_tls.h" #include "__get_tls.h"
__BEGIN_DECLS __BEGIN_DECLS
@ -84,8 +85,6 @@ enum {
#define BIONIC_TLS_RESERVED_SLOTS GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT #define BIONIC_TLS_RESERVED_SLOTS GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT
#endif #endif
#define BIONIC_ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1))
/* /*
* Maximum number of elements in the TLS array. * Maximum number of elements in the TLS array.
* This includes space for pthread keys and our own internal slots. * This includes space for pthread keys and our own internal slots.