| 1 |
2 |
alfik |
/******************************************************************************
|
| 2 |
|
|
* *
|
| 3 |
|
|
* License Agreement *
|
| 4 |
|
|
* *
|
| 5 |
|
|
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
|
| 6 |
|
|
* All rights reserved. *
|
| 7 |
|
|
* *
|
| 8 |
|
|
* Permission is hereby granted, free of charge, to any person obtaining a *
|
| 9 |
|
|
* copy of this software and associated documentation files (the "Software"), *
|
| 10 |
|
|
* to deal in the Software without restriction, including without limitation *
|
| 11 |
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
|
| 12 |
|
|
* and/or sell copies of the Software, and to permit persons to whom the *
|
| 13 |
|
|
* Software is furnished to do so, subject to the following conditions: *
|
| 14 |
|
|
* *
|
| 15 |
|
|
* The above copyright notice and this permission notice shall be included in *
|
| 16 |
|
|
* all copies or substantial portions of the Software. *
|
| 17 |
|
|
* *
|
| 18 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
| 19 |
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
| 20 |
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
| 21 |
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
| 22 |
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
|
| 23 |
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
|
| 24 |
|
|
* DEALINGS IN THE SOFTWARE. *
|
| 25 |
|
|
* *
|
| 26 |
|
|
* This agreement shall be governed in all respects by the laws of the State *
|
| 27 |
|
|
* of California and by the laws of the United States of America. *
|
| 28 |
|
|
* *
|
| 29 |
|
|
* Altera does not recommend, suggest or require that this reference design *
|
| 30 |
|
|
* file be used in conjunction or combination with any other product. *
|
| 31 |
|
|
******************************************************************************/
|
| 32 |
|
|
|
| 33 |
|
|
#include <sys/types.h>
|
| 34 |
|
|
|
| 35 |
|
|
#include "os/alt_syscall.h"
|
| 36 |
|
|
|
| 37 |
|
|
#include "sys/alt_irq.h"
|
| 38 |
|
|
#include "sys/alt_stack.h"
|
| 39 |
|
|
|
| 40 |
|
|
#include "system.h"
|
| 41 |
|
|
|
| 42 |
|
|
/*
|
| 43 |
|
|
* sbrk() is called to dynamically extend the data segment for the application.
|
| 44 |
|
|
* Thie input argument "incr" is the size of the block to allocate.
|
| 45 |
|
|
*
|
| 46 |
|
|
* This simple implementation does not perform any bounds checking. Memory will
|
| 47 |
|
|
* be allocated, even if the request region colides with the stack or overflows
|
| 48 |
|
|
* the available physical memory.
|
| 49 |
|
|
*
|
| 50 |
|
|
* ALT_SBRK is mapped onto the sbrk() system call in alt_syscall.h
|
| 51 |
|
|
*
|
| 52 |
|
|
* This function is called by the profiling code to allocate memory so must be
|
| 53 |
|
|
* safe if called from an interrupt context. It must also not be instrumented
|
| 54 |
|
|
* because that would lead to an infinate loop.
|
| 55 |
|
|
*/
|
| 56 |
|
|
|
| 57 |
|
|
extern char __alt_heap_start[]; /* set by linker */
|
| 58 |
|
|
extern char __alt_heap_limit[]; /* set by linker */
|
| 59 |
|
|
|
| 60 |
|
|
static char *heap_end = __alt_heap_start;
|
| 61 |
|
|
|
| 62 |
|
|
#if defined(ALT_EXCEPTION_STACK) && defined(ALT_STACK_CHECK)
|
| 63 |
|
|
char * alt_exception_old_stack_limit = NULL;
|
| 64 |
|
|
#endif
|
| 65 |
|
|
|
| 66 |
|
|
caddr_t ALT_SBRK (int incr) __attribute__ ((no_instrument_function ));
|
| 67 |
|
|
|
| 68 |
|
|
caddr_t ALT_SBRK (int incr)
|
| 69 |
|
|
{
|
| 70 |
|
|
alt_irq_context context;
|
| 71 |
|
|
char *prev_heap_end;
|
| 72 |
|
|
|
| 73 |
|
|
context = alt_irq_disable_all();
|
| 74 |
|
|
|
| 75 |
|
|
/* Always return data aligned on a word boundary */
|
| 76 |
|
|
heap_end = (char *)(((unsigned int)heap_end + 3) & ~3);
|
| 77 |
|
|
|
| 78 |
|
|
#ifdef ALT_MAX_HEAP_BYTES
|
| 79 |
|
|
/*
|
| 80 |
|
|
* User specified a maximum heap size. Return -1 if it would
|
| 81 |
|
|
* be exceeded by this sbrk call.
|
| 82 |
|
|
*/
|
| 83 |
|
|
if (((heap_end + incr) - __alt_heap_start) > ALT_MAX_HEAP_BYTES) {
|
| 84 |
|
|
alt_irq_enable_all(context);
|
| 85 |
|
|
return (caddr_t)-1;
|
| 86 |
|
|
}
|
| 87 |
|
|
#else
|
| 88 |
|
|
if ((heap_end + incr) > __alt_heap_limit) {
|
| 89 |
|
|
alt_irq_enable_all(context);
|
| 90 |
|
|
return (caddr_t)-1;
|
| 91 |
|
|
}
|
| 92 |
|
|
#endif
|
| 93 |
|
|
|
| 94 |
|
|
prev_heap_end = heap_end;
|
| 95 |
|
|
heap_end += incr;
|
| 96 |
|
|
|
| 97 |
|
|
#ifdef ALT_STACK_CHECK
|
| 98 |
|
|
/*
|
| 99 |
|
|
* If the stack and heap are contiguous then extending the heap reduces the
|
| 100 |
|
|
* space available for the stack. If we are still using the default stack
|
| 101 |
|
|
* then adjust the stack limit to note this, while checking for stack
|
| 102 |
|
|
* pointer overflow.
|
| 103 |
|
|
* If the stack limit isn't pointing at the top of the heap then the code
|
| 104 |
|
|
* is using a different stack so none of this needs to be done.
|
| 105 |
|
|
*/
|
| 106 |
|
|
|
| 107 |
|
|
if (alt_stack_limit() == prev_heap_end)
|
| 108 |
|
|
{
|
| 109 |
|
|
if (alt_stack_pointer() <= heap_end)
|
| 110 |
|
|
alt_report_stack_overflow();
|
| 111 |
|
|
|
| 112 |
|
|
alt_set_stack_limit(heap_end);
|
| 113 |
|
|
}
|
| 114 |
|
|
|
| 115 |
|
|
#ifdef ALT_EXCEPTION_STACK
|
| 116 |
|
|
/*
|
| 117 |
|
|
* If we are executing from the exception stack then compare against the
|
| 118 |
|
|
* stack we switched away from as well. The exception stack is a fixed
|
| 119 |
|
|
* size so doesn't need to be checked.
|
| 120 |
|
|
*/
|
| 121 |
|
|
|
| 122 |
|
|
if (alt_exception_old_stack_limit == prev_heap_end)
|
| 123 |
|
|
{
|
| 124 |
|
|
if (alt_exception_old_stack_limit <= heap_end)
|
| 125 |
|
|
alt_report_stack_overflow();
|
| 126 |
|
|
|
| 127 |
|
|
alt_exception_old_stack_limit = heap_end;
|
| 128 |
|
|
}
|
| 129 |
|
|
#endif
|
| 130 |
|
|
|
| 131 |
|
|
#endif
|
| 132 |
|
|
|
| 133 |
|
|
alt_irq_enable_all(context);
|
| 134 |
|
|
|
| 135 |
|
|
return (caddr_t) prev_heap_end;
|
| 136 |
|
|
}
|