Remove bogus extra alignment from sbrk.
Bug: https://code.google.com/p/android/issues/detail?id=37349
Change-Id: I970c7b6be7bb7fbe6bbbe2c332f05816aeb0e09f
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;
}