[PATCH 2/2] RISC-V: Fix _sbrk, it's failed only when return value is -1.

Kito Cheng <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <CA+yXCZBVBy35qfaZ8rCJ_yfMbHm1JacKXRfph5KOeS9vXUfBUA@mail.gmail.com>
This patch is fix _sbrk for RISC-V libgloss, _sbrk might return an
valid address which MSB is set to 1, but it will treat as fail in
current implementation.
0002-RISC-V-Fix-_sbrk-it-s-failed-only-when-return-value-.patch (text/x-patch, 2 KB)
From d1259a170f228b36be344405bca7a6bf6869c4c1 Mon Sep 17 00:00:00 2001
From: Denis Ivanov <[email protected]>
Date: Fri, 6 Jul 2018 19:03:23 +0300
Subject: [PATCH 2/2] RISC-V: Fix _sbrk, it's failed only when return value is
 -1.

Signed-off-by: Kito Cheng <[email protected]>
---
 libgloss/riscv/internal_syscall.h | 11 ++++++++---
 libgloss/riscv/sys_sbrk.c         |  8 ++++----
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/libgloss/riscv/internal_syscall.h b/libgloss/riscv/internal_syscall.h
index 8f7dcb451..e5d559496 100644
--- a/libgloss/riscv/internal_syscall.h
+++ b/libgloss/riscv/internal_syscall.h
@@ -40,13 +40,18 @@ __internal_syscall(long n, long _a0, long _a1, long _a2, long _a3, long _a4, lon
   asm volatile ("scall"
 		: "+r"(a0) : "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5), "r"(syscall_id));
 
+  return a0;
+}
+
+static inline long
+syscall_errno(long n, long _a0, long _a1, long _a2, long _a3, long _a4, long _a5)
+{
+  long a0 = __internal_syscall (n, _a0, _a1, _a2, _a3, _a4, _a5);
+
   if (a0 < 0)
     return __syscall_error (a0);
   else
     return a0;
 }
 
-#define syscall_errno(n, a, b, c, d, e, f) \
-        __internal_syscall(n, (long)(a), (long)(b), (long)(c), (long)(d), (long)(e), (long)(f))
-
 #endif
diff --git a/libgloss/riscv/sys_sbrk.c b/libgloss/riscv/sys_sbrk.c
index 19802fb7b..f91c2c506 100644
--- a/libgloss/riscv/sys_sbrk.c
+++ b/libgloss/riscv/sys_sbrk.c
@@ -41,14 +41,14 @@ _sbrk(ptrdiff_t incr)
 
   if (heap_end == 0)
     {
-      long brk = syscall_errno (SYS_brk, 0, 0, 0, 0, 0, 0);
+      long brk = __internal_syscall (SYS_brk, 0, 0, 0, 0, 0, 0);
       if (brk == -1)
-	return (void *)-1;
+        return (void *)__syscall_error (-ENOMEM);
       heap_end = brk;
     }
 
-  if (syscall_errno (SYS_brk, heap_end + incr, 0, 0, 0, 0, 0) != heap_end + incr)
-    return (void *)-1;
+  if (__internal_syscall (SYS_brk, heap_end + incr, 0, 0, 0, 0, 0) != heap_end + incr)
+    return (void *)__syscall_error (-ENOMEM);
 
   heap_end += incr;
   return (void *)(heap_end - incr);
-- 
2.17.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.