am d7ff139f: Merge "Remove bogus extra alignment from sbrk."
# Via Elliott Hughes (1) and Gerrit Code Review (1)
* commit 'd7ff139fec5cec08793523aa97503ade2b13c38e':
Remove bogus extra alignment from sbrk.
diff --git a/libc/Android.mk b/libc/Android.mk
index ee7fece..301be37 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -151,7 +151,6 @@
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 @@
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_bionic_src_files := \
bionic/assert.cpp \
+ bionic/brk.cpp \
bionic/debug_format.cpp \
bionic/dirent.cpp \
bionic/eventfd.cpp \
@@ -282,6 +281,7 @@
bionic/__memset_chk.cpp \
bionic/pthread_sigmask.cpp \
bionic/raise.cpp \
+ bionic/sbrk.cpp \
bionic/__set_errno.cpp \
bionic/setlocale.cpp \
bionic/signalfd.cpp \
diff --git a/libc/bionic/brk.c b/libc/bionic/brk.cpp
similarity index 84%
rename from libc/bionic/brk.c
rename to libc/bionic/brk.cpp
index bf2f108..633b914 100644
--- a/libc/bionic/brk.c
+++ b/libc/bionic/brk.cpp
@@ -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)
- return -1;
-
- __bionic_brk = new_brk;
-
+int brk(void* end_data) {
+ void* new_brk = __brk(end_data);
+ if (new_brk != end_data) {
+ return -1;
+ }
+ __bionic_brk = new_brk;
return 0;
}
diff --git a/libc/bionic/sbrk.c b/libc/bionic/sbrk.cpp
similarity index 72%
rename from libc/bionic/sbrk.c
rename to libc/bionic/sbrk.cpp
index a112b6c..6c9b534 100644
--- a/libc/bionic/sbrk.c
+++ b/libc/bionic/sbrk.cpp
@@ -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
-
-/* shared with brk() implementation */
-char* __bionic_brk;
-
-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)
- return new_brk;
- else if (new_brk < end)
- {
- errno = ENOMEM;
- return (void*)-1;
+void* sbrk(ptrdiff_t increment) {
+ if (__bionic_brk == NULL) {
+ __bionic_brk = __brk(NULL);
}
- __bionic_brk = new_brk;
- return start;
+ void* original_brk = __bionic_brk;
+ void* desired_brk = (void*) ((uintptr_t) original_brk + increment);
+
+ void* new_brk = __brk(desired_brk);
+ if (new_brk == (void*) -1) {
+ return new_brk;
+ } else if (new_brk < desired_brk) {
+ errno = ENOMEM;
+ return (void*) -1;
+ }
+
+ __bionic_brk = new_brk;
+ return original_brk;
}
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index a9dd5d2..3ccaf3b 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -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);
+}