malloc: check if the requested memory is really available

dev/timer
Benjamin Valentin 9 years ago
parent 73f6ac6bce
commit 879768397e

@ -130,8 +130,10 @@ SECTIONS
_estack = .;
} > ram
/* heap section */
. = ALIGN(4);
_end = . ;
_sheap = . ;
_eheap = ORIGIN(ram) + LENGTH(ram);
.flashcca :
{

@ -42,8 +42,9 @@
/**
* manage the heap
*/
extern uint32_t _end; /* address of last used memory cell */
caddr_t heap_top = (caddr_t) &_end + 4;
extern char _sheap; /* start of the heap */
extern char _eheap; /* end of the heap */
caddr_t heap_top = (caddr_t)&_sheap + 4;
#ifndef MODULE_UART0
/**
@ -114,18 +115,23 @@ void _exit(int n)
*
* The current heap implementation is very rudimentary, it is only able to allocate
* memory. But it does not
* - check if the returned address is valid (no check if the memory very exists)
* - have any means to free memory again
*
* TODO: check if the requested memory is really available
*
* @return [description]
*/
caddr_t _sbrk_r(struct _reent *r, size_t incr)
caddr_t _sbrk_r(struct _reent *r, ptrdiff_t incr)
{
unsigned int state = disableIRQ();
caddr_t res = heap_top;
heap_top += incr;
if (((incr > 0) && ((heap_top + incr > &_eheap) || (heap_top + incr < res))) ||
((incr < 0) && ((heap_top + incr < &_sheap) || (heap_top + incr > res)))) {
r->_errno = ENOMEM;
res = (void *) -1;
} else {
heap_top += incr;
}
restoreIRQ(state);
return res;
}

@ -138,6 +138,8 @@ SECTIONS
_estack = .;
} > ram
/* heap section */
. = ALIGN(4);
_end = . ;
_sheap = . ;
_eheap = ORIGIN(ram) + LENGTH(ram);
}

@ -41,8 +41,9 @@
/**
* manage the heap
*/
extern uint32_t _end; /* address of last used memory cell */
caddr_t heap_top = (caddr_t)&_end + 4;
extern char _sheap; /* start of the heap */
extern char _eheap; /* end of the heap */
caddr_t heap_top = (caddr_t)&_sheap + 4;
/**
* @brief use mutex for waiting on incoming UART chars
@ -111,18 +112,24 @@ void _exit(int n)
*
* The current heap implementation is very rudimentary, it is only able to allocate
* memory. But it does not
* - check if the returned address is valid (no check if the memory very exists)
* - have any means to free memory again
*
* TODO: check if the requested memory is really available
*
* @return [description]
*/
caddr_t _sbrk_r(struct _reent *r, ptrdiff_t incr)
{
unsigned int state = disableIRQ();
caddr_t res = heap_top;
heap_top += incr;
if (((incr > 0) && ((heap_top + incr > &_eheap) || (heap_top + incr < res))) ||
((incr < 0) && ((heap_top + incr < &_sheap) || (heap_top + incr > res)))) {
r->_errno = ENOMEM;
res = (void *) -1;
}
else {
heap_top += incr;
}
restoreIRQ(state);
return res;
}

@ -139,6 +139,8 @@ SECTIONS
_estack = .;
} > ram
/* heap section */
. = ALIGN(4);
_end = . ;
}
_sheap = . ;
_eheap = ORIGIN(ram) + LENGTH(ram);
}

@ -37,10 +37,9 @@
/**
* manage the heap
*/
extern uint32_t _end; /* address of last used memory cell */
caddr_t heap_top = (caddr_t)&_end + 4;
extern char _sheap; /* start of the heap */
extern char _eheap; /* end of the heap */
caddr_t heap_top = (caddr_t)&_sheap + 4;
/**
* @brief Initialize NewLib, called by __libc_init_array() from the startup script
*/
@ -77,18 +76,24 @@ void _exit(int n)
*
* The current heap implementation is very rudimentary, it is only able to allocate
* memory. But it does not
* - check if the returned address is valid (no check if the memory very exists)
* - have any means to free memory again
*
* TODO: check if the requested memory is really available
*
* @return [description]
*/
caddr_t _sbrk_r(struct _reent *r, ptrdiff_t incr)
{
unsigned int state = disableIRQ();
caddr_t res = heap_top;
heap_top += incr;
if (((incr > 0) && ((heap_top + incr > &_eheap) || (heap_top + incr < res))) ||
((incr < 0) && ((heap_top + incr < &_sheap) || (heap_top + incr > res)))) {
r->_errno = ENOMEM;
res = (void *) -1;
}
else {
heap_top += incr;
}
restoreIRQ(state);
return res;
}

@ -137,6 +137,8 @@ SECTIONS
_estack = .;
} > ram
/* heap section */
. = ALIGN(4);
_end = . ;
_sheap = . ;
_eheap = ORIGIN(ram) + LENGTH(ram);
}

@ -43,8 +43,9 @@
/**
* manage the heap
*/
extern uint32_t _end; /* address of last used memory cell */
caddr_t heap_top = (caddr_t)&_end + 4;
extern char _sheap; /* start of the heap */
extern char _eheap; /* end of the heap */
caddr_t heap_top = (caddr_t)&_sheap + 4;
#ifndef MODULE_UART0
/**
@ -114,18 +115,24 @@ void _exit(int n)
*
* The current heap implementation is very rudimentary, it is only able to allocate
* memory. But it does not
* - check if the returned address is valid (no check if the memory very exists)
* - have any means to free memory again
*
* TODO: check if the requested memory is really available
*
* @return [description]
*/
caddr_t _sbrk_r(struct _reent *r, ptrdiff_t incr)
{
unsigned int state = disableIRQ();
caddr_t res = heap_top;
heap_top += incr;
if (((incr > 0) && ((heap_top + incr > &_eheap) || (heap_top + incr < res))) ||
((incr < 0) && ((heap_top + incr < &_sheap) || (heap_top + incr > res)))) {
r->_errno = ENOMEM;
res = (void *) -1;
}
else {
heap_top += incr;
}
restoreIRQ(state);
return res;
}

@ -137,6 +137,8 @@ SECTIONS
_estack = .;
} > ram
/* heap section */
. = ALIGN(4);
_end = . ;
_sheap = . ;
_eheap = ORIGIN(ram) + LENGTH(ram);
}

@ -137,6 +137,8 @@ SECTIONS
_estack = .;
} > ram
/* heap section */
. = ALIGN(4);
_end = . ;
_sheap = . ;
_eheap = ORIGIN(ram) + LENGTH(ram);
}

@ -43,8 +43,9 @@
/**
* manage the heap
*/
extern uint32_t _end; /* address of last used memory cell */
caddr_t heap_top = (caddr_t)&_end + 4;
extern char _sheap; /* start of the heap */
extern char _eheap; /* end of the heap */
caddr_t heap_top = (caddr_t)&_sheap + 4;
#ifndef MODULE_UART0
/**
@ -114,18 +115,24 @@ void _exit(int n)
*
* The current heap implementation is very rudimentary, it is only able to allocate
* memory. But it does not
* - check if the returned address is valid (no check if the memory very exists)
* - have any means to free memory again
*
* TODO: check if the requested memory is really available
*
* @return [description]
*/
caddr_t _sbrk_r(struct _reent *r, ptrdiff_t incr)
{
unsigned int state = disableIRQ();
caddr_t res = heap_top;
heap_top += incr;
if (((incr > 0) && ((heap_top + incr > &_eheap) || (heap_top + incr < res))) ||
((incr < 0) && ((heap_top + incr < &_sheap) || (heap_top + incr > res)))) {
r->_errno = ENOMEM;
res = (void *) -1;
}
else {
heap_top += incr;
}
restoreIRQ(state);
return res;
}

@ -138,6 +138,8 @@ SECTIONS
_estack = .;
} > ram
/* heap section */
. = ALIGN(4);
_end = . ;
_sheap = . ;
_eheap = ORIGIN(ram) + LENGTH(ram);
}

@ -44,8 +44,9 @@
/**
* @brief manage the heap
*/
extern uint32_t _end; /* address of last used memory cell */
caddr_t heap_top = (caddr_t)&_end + 4;
extern char _sheap; /* start of the heap */
extern char _eheap; /* end of the heap */
caddr_t heap_top = (caddr_t)&_sheap + 4;
#ifndef MODULE_UART0
/**
@ -114,18 +115,24 @@ void _exit(int n)
*
* The current heap implementation is very rudimentary, it is only able to allocate
* memory. But it does not
* - check if the returned address is valid (no check if the memory very exists)
* - have any means to free memory again
*
* TODO: check if the requested memory is really available
*
* @return [description]
*/
caddr_t _sbrk_r(struct _reent *r, ptrdiff_t incr)
{
unsigned int state = disableIRQ();
caddr_t res = heap_top;
heap_top += incr;
if (((incr > 0) && ((heap_top + incr > &_eheap) || (heap_top + incr < res))) ||
((incr < 0) && ((heap_top + incr < &_sheap) || (heap_top + incr > res)))) {
r->_errno = ENOMEM;
res = (void *) -1;
}
else {
heap_top += incr;
}
restoreIRQ(state);
return res;
}

@ -139,6 +139,8 @@ SECTIONS
_estack = .;
} > ram
/* heap section */
. = ALIGN(4);
_end = . ;
_sheap = . ;
_eheap = ORIGIN(ram) + LENGTH(ram);
}

@ -139,6 +139,8 @@ SECTIONS
_estack = .;
} > ram
/* heap section */
. = ALIGN(4);
_end = . ;
_sheap = . ;
_eheap = ORIGIN(ram) + LENGTH(ram);
}

@ -43,8 +43,9 @@
/**
* manage the heap
*/
extern uint32_t _end; /* address of last used memory cell */
caddr_t heap_top = (caddr_t)&_end + 4;
extern char _sheap; /* start of the heap */
extern char _eheap; /* end of the heap */
caddr_t heap_top = (caddr_t)&_sheap + 4;
#ifndef MODULE_UART0
/**
@ -114,18 +115,24 @@ void _exit(int n)
*
* The current heap implementation is very rudimentary, it is only able to allocate
* memory. But it does not
* - check if the returned address is valid (no check if the memory very exists)
* - have any means to free memory again
*
* TODO: check if the requested memory is really available
*
* @return [description]
*/
caddr_t _sbrk_r(struct _reent *r, ptrdiff_t incr)
{
unsigned int state = disableIRQ();
caddr_t res = heap_top;
heap_top += incr;
if (((incr > 0) && ((heap_top + incr > &_eheap) || (heap_top + incr < res))) ||
((incr < 0) && ((heap_top + incr < &_sheap) || (heap_top + incr > res)))) {
r->_errno = ENOMEM;
res = (void *) -1;
}
else {
heap_top += incr;
}
restoreIRQ(state);
return res;
}

Loading…
Cancel
Save