OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [libgloss/] [or32/] [sbrk.c] - Blame information for rev 148

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
#include <errno.h>
2
 
3
/*
4
 * While the heap grows upwards, the stack grows downwards.
5
 * Evnetually these two things may colide and sbrk()
6
 * won't even be able to return properly. To mitigate this
7
 * we reserve upto STACK_BUFFER _words_ at the top of memory.
8
 * Note this doesn't actually solve the problem, it just
9
 * provides an error margin. The real solution is to use
10
 * an OS with a proper virtual memory manager.
11
 */
12
#define STACK_BUFFER 16384
13
 
14
#ifndef NULL
15
#define NULL ((void *)0)
16
#endif
17
 
18
 
19
/*
20
 * sbrk -- changes heap size size. Get nbytes more
21
 *         RAM. We just increment a pointer in what's
22
 *         left of memory on the board.
23
 */
24
void *sbrk(int nbytes)
25
{
26
        /* symbols defined by linker map */
27
        extern int _end;   // start of free memory
28
        extern int _stack; // end of free memory
29
 
30
        static unsigned long *heap_ptr = NULL;
31
        void *base;
32
        int nwords = (nbytes + 3) / 4;
33
 
34
        if (heap_ptr == NULL)
35
                heap_ptr = (unsigned long *)&_end;
36
 
37
        if ( ((unsigned long *)&_stack - (heap_ptr + nwords)) > STACK_BUFFER ) {
38
                base = heap_ptr;
39
                heap_ptr += nwords;
40
 
41
                return (void *)base;
42
        } else {
43
                errno = ENOMEM;
44
//              write(1, "Failed to extend heap.", 23);
45
 
46
                return (void *)-1;
47
        }
48
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.