source: trunk/libs/newlib/src/libgloss/riscv/sys_sbrk.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 1.2 KB
Line 
1#ifdef USING_SIM_SPECS
2
3// Gdb simulator requires that sbrk be implemented without a syscall.
4extern char _end[];                /* _end is set in the linker command file */
5char *heap_ptr;
6
7/*
8 * sbrk -- changes heap size size. Get nbytes more
9 *         RAM. We just increment a pointer in what's
10 *         left of memory on the board.
11 */
12char *
13_sbrk (nbytes)
14     int nbytes;
15{
16  char        *base;
17
18  if (!heap_ptr)
19    heap_ptr = (char *)&_end;
20  base = heap_ptr;
21  heap_ptr += nbytes;
22
23  return base;
24}
25
26#else
27
28// QEMU uses a syscall.
29#include <machine/syscall.h>
30#include <sys/types.h>
31#include "internal_syscall.h"
32
33/* Increase program data space. As malloc and related functions depend
34   on this, it is useful to have a working implementation. The following
35   is suggested by the newlib docs and suffices for a standalone
36   system.  */
37void *
38_sbrk(ptrdiff_t incr)
39{
40  static unsigned long heap_end;
41
42  if (heap_end == 0)
43    {
44      long brk = syscall_errno (SYS_brk, 0, 0, 0, 0, 0, 0);
45      if (brk == -1)
46        return (void *)-1;
47      heap_end = brk;
48    }
49
50  if (syscall_errno (SYS_brk, heap_end + incr, 0, 0, 0, 0, 0) != heap_end + incr)
51    return (void *)-1;
52
53  heap_end += incr;
54  return (void *)(heap_end - incr);
55}
56#endif
Note: See TracBrowser for help on using the repository browser.