Merge "Remove bogus extra alignment from sbrk."

This commit is contained in:
Elliott Hughes 2013-02-06 00:57:43 +00:00 committed by Gerrit Code Review
commit d7ff139fec
4 changed files with 43 additions and 41 deletions

View File

@ -151,7 +151,6 @@ libc_common_src_files := \
bionic/atoll.c \
bionic/bindresvport.c \
bionic/bionic_clone.c \
bionic/brk.c \
bionic/clearenv.c \
bionic/cpuacct.c \
bionic/daemon.c \
@ -201,7 +200,6 @@ libc_common_src_files := \
bionic/realpath.c \
bionic/reboot.c \
bionic/recv.c \
bionic/sbrk.c \
bionic/sched_cpualloc.c \
bionic/sched_cpucount.c \
bionic/sched_getaffinity.c \
@ -270,6 +268,7 @@ libc_common_src_files := \
libc_bionic_src_files := \
bionic/assert.cpp \
bionic/brk.cpp \
bionic/debug_format.cpp \
bionic/dirent.cpp \
bionic/eventfd.cpp \
@ -282,6 +281,7 @@ libc_bionic_src_files := \
bionic/__memset_chk.cpp \
bionic/pthread_sigmask.cpp \
bionic/raise.cpp \
bionic/sbrk.cpp \
bionic/__set_errno.cpp \
bionic/setlocale.cpp \
bionic/signalfd.cpp \

View File

@ -25,21 +25,17 @@
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stddef.h>
#include <unistd.h>
#include <sys/types.h>
/* shared with sbrk.c */
char *__bionic_brk;
/* Shared with sbrk.c. */
extern "C" void* __bionic_brk; // TODO: should be __LIBC_HIDDEN__ but accidentally exported by NDK :-(
int brk(void* end_data)
{
char* new_brk = __brk( end_data );
if (new_brk != end_data)
int brk(void* end_data) {
void* new_brk = __brk(end_data);
if (new_brk != end_data) {
return -1;
}
__bionic_brk = new_brk;
return 0;
}

View File

@ -25,36 +25,31 @@
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
#include <errno.h>
/* Shared with brk.c. */
extern "C" {
void* __bionic_brk; // TODO: should be __LIBC_HIDDEN__ but accidentally exported by NDK :-(
}
#define SBRK_ALIGN 32
void* sbrk(ptrdiff_t increment) {
if (__bionic_brk == NULL) {
__bionic_brk = __brk(NULL);
}
/* shared with brk() implementation */
char* __bionic_brk;
void* original_brk = __bionic_brk;
void* desired_brk = (void*) ((uintptr_t) original_brk + increment);
void *sbrk(ptrdiff_t increment)
{
char* start;
char* end;
char* new_brk;
if ( !__bionic_brk)
__bionic_brk = __brk((void*)0);
start = (char*)(((long)__bionic_brk + SBRK_ALIGN-1) & ~(SBRK_ALIGN-1));
end = start + increment;
new_brk = __brk(end);
if (new_brk == (void*)-1)
void* new_brk = __brk(desired_brk);
if (new_brk == (void*) -1) {
return new_brk;
else if (new_brk < end)
{
} else if (new_brk < desired_brk) {
errno = ENOMEM;
return (void*) -1;
}
__bionic_brk = new_brk;
return start;
return original_brk;
}

View File

@ -16,8 +16,19 @@
#include <gtest/gtest.h>
#include <stdint.h>
#include <unistd.h>
TEST(unistd, sysconf_SC_MONOTONIC_CLOCK) {
ASSERT_GT(sysconf(_SC_MONOTONIC_CLOCK), 0);
}
TEST(unistd, sbrk) {
void* initial_break = sbrk(0);
void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 2000);
ASSERT_EQ(0, brk(new_break));
void* final_break = sbrk(0);
ASSERT_EQ(final_break, new_break);
}