mm/umm_heap: Ensure all user allocation try sbrk before fail

This commit is contained in:
Xiang Xiao 2019-01-26 10:42:36 -06:00 committed by Gregory Nutt
parent 3047ef80dc
commit 2671709e62
3 changed files with 93 additions and 0 deletions

View File

@ -59,5 +59,32 @@
FAR void *calloc(size_t n, size_t elem_size)
{
#ifdef CONFIG_ARCH_ADDRENV
/* Use zalloc() because it implements the sbrk() logic */
FAR void *ret = NULL;
/* Verify input parameters */
if (n > 0 && elem_size > 0)
{
/* Assure that the following multiplication cannot overflow the size_t
* type, i.e., that: SIZE_MAX >= n * elem_size
*
* Refer to SEI CERT C Coding Standard.
*/
if (n <= (SIZE_MAX / elem_size))
{
ret = zalloc(n * elem_size);
}
}
return ret;
#else
/* Use mm_calloc() becuase it implements the clear */
return mm_calloc(USR_HEAP, n, elem_size);
#endif
}

View File

@ -40,6 +40,7 @@
#include <nuttx/config.h>
#include <stdlib.h>
#include <unistd.h>
#include <nuttx/mm/mm.h>
@ -64,5 +65,37 @@
FAR void *memalign(size_t alignment, size_t size)
{
#ifdef CONFIG_ARCH_ADDRENV
FAR void *brkaddr;
FAR void *mem;
/* Loop until we successfully allocate the memory or until an error
* occurs. If we fail to allocate memory on the first pass, then call
* sbrk to extend the heap by one page. This may require several
* passes if more the size of the allocation is more than one page.
*
* An alternative would be to increase the size of the heap by the
* full requested allocation in sbrk(). Then the loop should never
* execute more than twice (but more memory than we need may be
* allocated).
*/
do
{
mem = mm_memalign(USR_HEAP, alignment, size);
if (!mem)
{
brkaddr = sbrk(size);
if (brkaddr == (FAR void *)-1)
{
return NULL;
}
}
}
while (mem == NULL);
return mem;
#else
return mm_memalign(USR_HEAP, alignment, size);
#endif
}

View File

@ -40,6 +40,7 @@
#include <nuttx/config.h>
#include <stdlib.h>
#include <unistd.h>
#include <nuttx/mm/mm.h>
@ -66,5 +67,37 @@
FAR void *realloc(FAR void *oldmem, size_t size)
{
#ifdef CONFIG_ARCH_ADDRENV
FAR void *brkaddr;
FAR void *mem;
/* Loop until we successfully allocate the memory or until an error
* occurs. If we fail to allocate memory on the first pass, then call
* sbrk to extend the heap by one page. This may require several
* passes if more the size of the allocation is more than one page.
*
* An alternative would be to increase the size of the heap by the
* full requested allocation in sbrk(). Then the loop should never
* execute more than twice (but more memory than we need may be
* allocated).
*/
do
{
mem = mm_realloc(USR_HEAP, oldmem, size);
if (!mem)
{
brkaddr = sbrk(size);
if (brkaddr == (FAR void *)-1)
{
return NULL;
}
}
}
while (mem == NULL);
return mem;
#else
return mm_realloc(USR_HEAP, oldmem, size);
#endif
}