| 1 |
180 |
jeremybenn |
/* sbrk.c. Implementation of the _sbrk syscall for newlib
|
| 2 |
|
|
|
| 3 |
|
|
Copyright (C) 2004, Jacob Bower
|
| 4 |
|
|
Copyright (C) 2010, Embecosm Limited <info@embecosm.com>
|
| 5 |
|
|
|
| 6 |
|
|
Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
|
| 7 |
|
|
|
| 8 |
|
|
This file is part of Newlib.
|
| 9 |
|
|
|
| 10 |
|
|
The original work by Jacob Bower is provided as-is without any kind of
|
| 11 |
|
|
warranty. Use it at your own risk!
|
| 12 |
|
|
|
| 13 |
|
|
All subsequent work is bound by version 3 of the GPL as follows.
|
| 14 |
|
|
|
| 15 |
|
|
This program is free software; you can redistribute it and/or modify it
|
| 16 |
|
|
under the terms of the GNU General Public License as published by the Free
|
| 17 |
|
|
Software Foundation; either version 3 of the License, or (at your option)
|
| 18 |
|
|
any later version.
|
| 19 |
|
|
|
| 20 |
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
| 21 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
| 22 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
| 23 |
|
|
more details.
|
| 24 |
|
|
|
| 25 |
|
|
You should have received a copy of the GNU General Public License along
|
| 26 |
|
|
with this program. If not, see <http:#www.gnu.org/licenses/>. */
|
| 27 |
|
|
/* -------------------------------------------------------------------------- */
|
| 28 |
|
|
/* This program is commented throughout in a fashion suitable for processing
|
| 29 |
|
|
with Doxygen. */
|
| 30 |
|
|
/* -------------------------------------------------------------------------- */
|
| 31 |
|
|
|
| 32 |
148 |
jeremybenn |
#include <errno.h>
|
| 33 |
|
|
|
| 34 |
180 |
jeremybenn |
/*! Reserved stack space inbytes. */
|
| 35 |
|
|
#define STACK_BUFFER 65536
|
| 36 |
148 |
jeremybenn |
|
| 37 |
180 |
jeremybenn |
/*! Define NULL if not yet defined. */
|
| 38 |
148 |
jeremybenn |
#ifndef NULL
|
| 39 |
180 |
jeremybenn |
#define NULL ((void *) 0)
|
| 40 |
148 |
jeremybenn |
#endif
|
| 41 |
|
|
|
| 42 |
|
|
|
| 43 |
180 |
jeremybenn |
/* -------------------------------------------------------------------------- */
|
| 44 |
|
|
/*!Extend the heap
|
| 45 |
|
|
|
| 46 |
|
|
We just increment a pointer in what's left of memory on the board.
|
| 47 |
|
|
|
| 48 |
|
|
While the heap grows upwards, the stack grows downwards. Eventually these
|
| 49 |
|
|
two things may colide and sbrk() won't even be able to return properly.
|
| 50 |
|
|
|
| 51 |
|
|
To mitigate this we reserve upto STACK_BUFFER _words_ at the top of memory.
|
| 52 |
|
|
Note this doesn't actually solve the problem, it just provides an error
|
| 53 |
|
|
margin. The real solution is to use an OS with a proper virtual memory
|
| 54 |
|
|
manager.
|
| 55 |
|
|
|
| 56 |
|
|
Remember that this function is *not* reentrant, so no static state should
|
| 57 |
|
|
be held.
|
| 58 |
|
|
|
| 59 |
|
|
@todo We break this rule with heap_ptr. This needs to be clean, so that a
|
| 60 |
|
|
re-entrant call to sbrk (e.g. in an ISR) is certain to work.
|
| 61 |
|
|
|
| 62 |
|
|
@param[in] nbytes The number of bytes to be allocated.
|
| 63 |
|
|
|
| 64 |
|
|
@return The previous heap end on success, -1 on failure with an error
|
| 65 |
|
|
code in errno. */
|
| 66 |
|
|
/* -------------------------------------------------------------------------- */
|
| 67 |
|
|
void *
|
| 68 |
|
|
_sbrk (int nbytes)
|
| 69 |
148 |
jeremybenn |
{
|
| 70 |
180 |
jeremybenn |
/* Symbols defined by linker map */
|
| 71 |
|
|
extern int end; /* start of free memory */
|
| 72 |
|
|
extern int stack; /* end of free memory */
|
| 73 |
148 |
jeremybenn |
|
| 74 |
180 |
jeremybenn |
/* The statically held previous end of the stack, with its initialization. */
|
| 75 |
|
|
static void *heap_ptr = (void *)&end; /* Previous end */
|
| 76 |
148 |
jeremybenn |
|
| 77 |
180 |
jeremybenn |
if (((void *) &stack - (heap_ptr + nbytes)) > STACK_BUFFER )
|
| 78 |
|
|
{
|
| 79 |
|
|
void * base = heap_ptr;
|
| 80 |
|
|
heap_ptr += nbytes;
|
| 81 |
148 |
jeremybenn |
|
| 82 |
180 |
jeremybenn |
return base;
|
| 83 |
|
|
}
|
| 84 |
|
|
else
|
| 85 |
|
|
{
|
| 86 |
|
|
errno = ENOMEM;
|
| 87 |
|
|
return (void *) -1;
|
| 88 |
|
|
}
|
| 89 |
|
|
} /* _sbrk () */
|