Reapply bugfix to sbrk() which got dropped during the move from linx386/libsys
authorDavid Given <dg@cowlark.com>
Sat, 18 May 2013 12:00:37 +0000 (13:00 +0100)
committerDavid Given <dg@cowlark.com>
Sat, 18 May 2013 12:00:37 +0000 (13:00 +0100)
to liblinux. Set errno correctly.

plat/linux/liblinux/brk.c
plat/linux/liblinux/sbrk.c

index 8bb53d3..7ce5c5f 100644 (file)
@@ -5,9 +5,13 @@
 
 #include <stdlib.h>
 #include <unistd.h>
+#include <errno.h>
 #include "libsys.h"
 
 int brk(void* end)
 {
-       return _syscall(__NR_brk, (quad) end, 0, 0);
+       int e = _syscall(__NR_brk, (quad) end, 0, 0);
+       if (e == -1)
+               errno = ENOMEM;
+       return e;
 }
index 17b273f..35810ef 100644 (file)
@@ -1,32 +1,39 @@
-/* $Source: /cvsroot/tack/Ack/plat/linux386/libsys/sbrk.c,v $
- * $State: Exp $
- * $Revision: 1.1 $
+/* $Source$
+ * $State$
+ * $Revision$
  */
 
 #include <stdlib.h>
-#include <stdio.h>
 #include <unistd.h>
+#include <errno.h>
 #include "libsys.h"
 
 #define        OUT_OF_MEMORY (void*)(-1)       /* sbrk returns this on failure */
 
-extern char _end[1];
-
-static char* current = _end;
+static char* current = NULL;
  
 void* sbrk(intptr_t increment)
 {
        char* old;
        char* new;
+       char* actual;
+       
+       if (!current)
+               current = (char*) _syscall(__NR_brk, 0, 0, 0);
 
        if (increment == 0)
                return current;
                
        old = current;
        new = old + increment;
-       if (brk(new) < 0)
+
+       actual = (char*) _syscall(__NR_brk, (quad) new, 0, 0);
+       if (actual < new)
+       {
+               errno = ENOMEM;
                return OUT_OF_MEMORY;
+       }
                
-       current = new;
+       current = actual;
        return old;
 }